Main Search
Forum  Strona Główna
 
 
FAQ Members Groups Profile Private Messages

zalicznie

 
Napisz nowy temat   Odpowiedz do tematu    Forum Strona Główna -> Geometria i grafika komp.
Zobacz poprzedni temat :: Zobacz następny temat  
Autor Wiadomość
zirina




Dołączył: 24 Lis 2006
Posty: 8
Przeczytał: 0 tematów

Ostrzeżeń: 0/5

PostWysłany: Pon 10:22, 11 Gru 2006    Temat postu: zalicznie

dla tych ktorych nie bylo:

na ostatnie zajecia zeby miec zaliczenie nalezy zrobic projekt w c mozna w
parach:

tematy:
sala 374 - 2 swiatla, krzesla, lawki, tablica ktora ma sie poruszac
zoo - 5 zwierzakow w klatkach, drzewka, krzaczki, biegnace dziecko
port - woda, 10 statkow, 1 ruchomy, molo

no chyba ze macie min 3 plusy to 3.5 gwarantowane bez projektu:)

temat zapewne bedzie interesujacy ze wzgledu na ilosc kodow ktore zapewne tu wrzucicie Razz pozdrawiam


Post został pochwalony 0 razy
Powrót do góry
Zobacz profil autora
Grzesiek




Dołączył: 09 Cze 2006
Posty: 49
Przeczytał: 0 tematów

Ostrzeżeń: 0/5

PostWysłany: Nie 11:50, 17 Gru 2006    Temat postu:

czy komus sie udalo zaladowac model 3ds ktory uzywa tekstur?

Post został pochwalony 0 razy
Powrót do góry
Zobacz profil autora
Jajo
Administrator



Dołączył: 08 Cze 2006
Posty: 30
Przeczytał: 0 tematów

Ostrzeżeń: 0/5

PostWysłany: Czw 14:09, 04 Sty 2007    Temat postu:

a to to trzeba zrobić na najbliższą sobote ?Very Happy

Post został pochwalony 0 razy
Powrót do góry
Zobacz profil autora
noname
Gość






PostWysłany: Czw 16:58, 04 Sty 2007    Temat postu:

Jajo napisał:
a to to trzeba zrobić na najbliższą sobote ?Very Happy

Z tego co wiem to na ostatnie zajęcia. Z resztą czytając uważnie powyższe posty można samemu wywnioskować...
Powrót do góry
Gabryś




Dołączył: 10 Cze 2006
Posty: 3
Przeczytał: 0 tematów

Ostrzeżeń: 0/5

PostWysłany: Nie 20:22, 14 Sty 2007    Temat postu: 3DS

No dobra... nie zgrałem sobie ostatnich projektów z ćwiczeń, ma ktoś może je w domu na kompie ??

Braliśmy dodawanie zewnętrznych obiektów, a właśnie teraz tego potrzebuję.
Wie ktoś poprzez jakie polecenia dodaje się obiekty ?? W moim przypadku 3DS. Widzę że już jesteście na zaawansowanych etapach, bo widziałem już posty dotyczące nakładania tekstur na obiekty 3DS...


Post został pochwalony 0 razy
Powrót do góry
Zobacz profil autora
Grzesiek




Dołączył: 09 Cze 2006
Posty: 49
Przeczytał: 0 tematów

Ostrzeżeń: 0/5

PostWysłany: Wto 19:50, 16 Sty 2007    Temat postu:

tu masz kod obslugujacy objekty (3 pliki):
model3DS.cpp
Cytat:

#include "model3DS.h"

model3DS::model3DS(const char* filename, float scale) : m_filename(filename), m_scale(scale){

std::ifstream *modelFile = new std::ifstream(filename,std::ios::in | std::ios::binary | std::ios::ate);

if(!modelFile->is_open()){
std::cout<<"[3DS] ERROR: Could not open '"<<filename<<"'"<<std::endl;
return;
}
if(int(modelFile->tellg()) == 0){
std::cout<<"[3DS] ERROR: Model '"<<filename<<"' is empty"<<std::endl;
modelFile->close();
return;
}

// Extract path from filename
int lastSlashPosition=-1, lastForwardSlash=-1, lastBackslash=-1;
lastForwardSlash = (int)m_filename.find_last_of('/');
lastBackslash = (int)m_filename.find_last_of('\\');
if(lastForwardSlash > lastSlashPosition) lastSlashPosition = lastForwardSlash;
if(lastBackslash > lastSlashPosition) lastSlashPosition = lastBackslash;
m_filepath = m_filename.substr(0,lastSlashPosition+1);
m_filename = m_filename.substr(lastSlashPosition+1);

// Check to make sure file is valid 3DS format (begins with 0x4D4D)
ushort chunkHeader;
unsigned int chunkLength;

modelFile->seekg(0, std::ios::beg);
modelFile->read((char*)&chunkHeader,2);
modelFile->read((char*)&chunkLength,4);

if(chunkHeader != CHUNK_MAIN){
std::cout<<"[3DS] ERROR: Model '"<<filename<<"' is not a valid 3DS file"<<std::endl;
modelFile->close();
return;
}

// Detect VBO support
std::stringstream extStream((const char*)glGetString(GL_EXTENSIONS));
std::string nextToken;
bool isVBOSupported=false;
while(!extStream.eof()){
extStream >> nextToken;
if(nextToken == "GL_ARB_vertex_buffer_object"){
isVBOSupported=true;
break;
}
}
m_drawMode = DRAW_VERTEX_ARRAY;

// Initialise bounding box to min & max 4-byte float values
m_boundingBox.minX = m_boundingBox.minY = m_boundingBox.minZ = 3.4e+38f;
m_boundingBox.maxX = m_boundingBox.maxY = m_boundingBox.maxZ = 3.4e-38f;

// Read all 3DS chunks recursively
while(!modelFile->eof()){
readChunk(modelFile, modelFile->tellg(), chunkLength);
}

m_centerX = (m_boundingBox.minX + m_boundingBox.maxX) / 2.f;
m_centerY = (m_boundingBox.minY + m_boundingBox.maxY) / 2.f;
m_centerZ = (m_boundingBox.minZ + m_boundingBox.maxZ) / 2.f;

// Model loaded, clean up
modelFile->close();
delete modelFile;
std::cout<<"[3DS] Model '"<<filename<<"' loaded"<<std::endl;

}

void model3DS::readChunk(std::ifstream *modelFile, const int objectStart, const int objectLength){
//std::cout<<std::hex<<"readChunk("<<objectStart<<"-"<<(objectStart+objectLength)<<")"<<std::dec<<std::endl;

ushort chunkHeader;
unsigned int chunkLength;

unsigned long offset;
ushort numVertices;
ushort usTemp;
unsigned int uiTemp;
float vertexX,vertexY,vertexZ;
int v;
std::string name;
char currentLetter;
unsigned char rgbByte;

while((modelFile->tellg() < (objectStart + objectLength)) && !modelFile->eof()){

offset = modelFile->tellg();

modelFile->read((char*)&chunkHeader, 2);
modelFile->read((char*)&chunkLength, 4);

if(DEBUG_OUTPUT) std::cout<<std::hex<<"["<<offset<<"] chunk: 0x"<<chunkHeader<<" ("<<offset<<"-"<<(offset+chunkLength)<<")"<<std::dec<<std::endl;
switch(chunkHeader){

//////////////////
// Main chunks
/////////////////

case CHUNK_MAIN: continue;

case CHUNK_3D_EDITOR: continue;

case CHUNK_OBJECT_BLOCK:
if(DEBUG_OUTPUT) std::cout<<std::endl<<"[Object block]"<<std::endl;

m_currentMesh = new mesh3DS(this);
m_currentMesh->setDrawMode(m_drawMode);

// Read object name
do{
modelFile->read(&currentLetter,1);
name += currentLetter;
}while(currentLetter!='\0' && name.length()<20);
m_currentMesh->setName(name);
if(DEBUG_OUTPUT) std::cout<<" Object: "<<name<<std::endl;
name.erase();

// Read object sub-chunks
readChunk(modelFile, modelFile->tellg(), chunkLength - (long(modelFile->tellg()) - offset));

if(m_currentMesh->getNumFaces() != 0){
m_currentMesh->buildMesh();
m_meshes.push_back(*m_currentMesh);
}
delete m_currentMesh;
break;

/////////////////////
// Geometry chunks
////////////////////

case CHUNK_MESH:continue;

case CHUNK_VERTICES:
modelFile->read((char*)&numVertices,2);
for(v=0; v < numVertices*3; v+=3){
modelFile->read((char*)&vertexX,4);
modelFile->read((char*)&vertexY,4);
modelFile->read((char*)&vertexZ,4);
// 3DS Max has different axes to OpenGL
vertexX *= m_scale;
vertexY *= m_scale;
vertexZ *= m_scale;
m_currentMesh->addVertex(vertexX);// x
m_currentMesh->addVertex(vertexZ);// y
m_currentMesh->addVertex(-vertexY);// z
// Update bounding box
if(vertexX < m_boundingBox.minX)m_boundingBox.minX = vertexX;
if(vertexZ < m_boundingBox.minY)m_boundingBox.minY = vertexZ;
if(-vertexY < m_boundingBox.minZ)m_boundingBox.minZ = -vertexY;
if(vertexX > m_boundingBox.maxX)m_boundingBox.maxX = vertexX;
if(vertexZ > m_boundingBox.maxY)m_boundingBox.maxY = vertexZ;
if(-vertexY > m_boundingBox.maxZ)m_boundingBox.maxZ = -vertexY;
}
break;

case CHUNK_TEXCOORDS: // texcoords list
modelFile->read((char*)&numVertices,2);
for(v=0; v < numVertices*2; v+=2){
modelFile->read((char*)&vertexX,4);
modelFile->read((char*)&vertexY,4);
m_currentMesh->addTexcoord(vertexX);
m_currentMesh->addTexcoord(vertexY);
}
break;

case CHUNK_FACES:
modelFile->read((char*)&m_tempUshort,2);

for(v=0; v < m_tempUshort*3; v+=3){
modelFile->read((char*)&usTemp,2);m_currentMesh->addFaceIndex(usTemp);
modelFile->read((char*)&usTemp,2);m_currentMesh->addFaceIndex(usTemp);
modelFile->read((char*)&usTemp,2);m_currentMesh->addFaceIndex(usTemp);
modelFile->read((char*)&usTemp,2); //face flags
}

// Read face sub-chunks
readChunk(modelFile, modelFile->tellg(), chunkLength - (long(modelFile->tellg()) - offset));
break;

case CHUNK_SMOOTHING_GROUP:
for(v=0; v < m_tempUshort; v++){
modelFile->read((char*)&uiTemp,4);
m_currentMesh->addFaceSmoothing(uiTemp);
//if(DEBUG_OUTPUT) std::cout<<"Smoothing: "<<uiTemp<<std::endl;
}
break;

/////////////////////
// Material chunks
////////////////////

case CHUNK_FACE_MATERIAL:
// Read material name
do{
modelFile->read(&currentLetter,1);
name += currentLetter;
}while(currentLetter!='\0' && name.length()<20);

modelFile->read((char*)&m_tempUshort,2);

for(v=0; v < m_tempUshort; v++){
modelFile->read((char*)&usTemp,2);
m_currentMesh->addMaterialFace(name, usTemp);
}

name.erase();

break;

case CHUNK_MATERIAL_BLOCK:
if(DEBUG_OUTPUT) std::cout<<std::endl<<"[Material block]"<<std::endl;

m_currentMaterial = new material3DS();

// Read material sub-chunks
readChunk(modelFile, modelFile->tellg(), chunkLength - (long(modelFile->tellg()) - offset));

m_materials[m_currentMaterial->getName()] = *m_currentMaterial;
delete m_currentMaterial;
break;

case CHUNK_MATERIAL_NAME:
// Read material name and add to current material
do{
modelFile->read(&currentLetter,1);
name += currentLetter;
}while(currentLetter!='\0' && name.length()<20);
m_currentMaterial->setName(name);
if(DEBUG_OUTPUT) std::cout<<" Material: "<<m_currentMaterial->getName()<<"("<<m_currentMaterial->getName().size()<<")"<<std::endl;
name.erase();
break;

case CHUNK_TEXTURE_MAP:
case CHUNK_BUMP_MAP:
//Read texture name and add to current material
readChunk(modelFile, modelFile->tellg(), chunkLength - (long(modelFile->tellg()) - offset));
m_currentMaterial->loadTexture(m_filepath + m_tempString, chunkHeader);

break;

case CHUNK_MAP_FILENAME:
// Read texture map filename
m_tempString.erase();
do{
modelFile->read(&currentLetter,1);
m_tempString += currentLetter;
}while(currentLetter!='\0' && m_tempString.length()<20);
break;

case CHUNK_MATERIAL_TWO_SIDED:
m_currentMaterial->setTwoSided(true);
break;

case CHUNK_DIFFUSE_COLOR:
// Read color sub-chunks
readChunk(modelFile, modelFile->tellg(), chunkLength - (long(modelFile->tellg()) - offset));
m_currentMaterial->setDiffuseColor(m_currentColor);

break;

case CHUNK_AMBIENT_COLOR:
// Read color sub-chunks
readChunk(modelFile, modelFile->tellg(), chunkLength - (long(modelFile->tellg()) - offset));
m_currentMaterial->setAmbientColor(m_currentColor);

break;

case CHUNK_SPECULAR_COLOR:
// Read color sub-chunks
readChunk(modelFile, modelFile->tellg(), chunkLength - (long(modelFile->tellg()) - offset));
m_currentMaterial->setSpecularColor(m_currentColor);

break;

case CHUNK_SPECULAR_EXPONENT:
// Read percent sub-chunk
readChunk(modelFile, modelFile->tellg(), chunkLength - (long(modelFile->tellg()) - offset));
m_currentMaterial->setSpecularExponent(m_tempFloat);
break;

case CHUNK_SHININESS:
// Read percent sub-chunk
readChunk(modelFile, modelFile->tellg(), chunkLength - (long(modelFile->tellg()) - offset));
m_currentMaterial->setShininess(m_tempFloat);
break;

case CHUNK_TRANSPARENCY:
// Read percent sub-chunk
readChunk(modelFile, modelFile->tellg(), chunkLength - (long(modelFile->tellg()) - offset));
m_currentMaterial->setOpacity(1.0f - m_tempFloat);
break;

/////////////////////
// Global chunks
////////////////////

case CHUNK_RGB_FLOAT:
case CHUNK_RGB_FLOAT_GAMMA:
modelFile->read((char*)&m_currentColor[0],4);
modelFile->read((char*)&m_currentColor[1],4);
modelFile->read((char*)&m_currentColor[2],4);
break;

case CHUNK_RGB_BYTE:
case CHUNK_RGB_BYTE_GAMMA:
modelFile->read((char*)&rgbByte,1); m_currentColor[0]=float(rgbByte)/255.f;
modelFile->read((char*)&rgbByte,1); m_currentColor[1]=float(rgbByte)/255.f;
modelFile->read((char*)&rgbByte,1); m_currentColor[2]=float(rgbByte)/255.f;
break;

case CHUNK_PERCENT_INT:
modelFile->read((char*)&usTemp,2);
m_tempFloat = usTemp / 100.f;
break;

case CHUNK_PERCENT_FLOAT:
modelFile->read((char*)&m_tempFloat,4);
m_tempFloat /= 100.f;
break;

default:break; // any other chunk

}

// Go to the next chunk's header (if any left in object)
modelFile->seekg(offset + chunkLength, std::ios::beg);
}

}

void material3DS::loadTexture(std::string filename, int chunkType){

std::transform(filename.begin(),filename.end(),filename.begin(),tolower);

if((filename.find(".tga") == std::string::npos) && (filename.find(".bmp") == std::string::npos)){
std::cout<<"[3DS] WARNING: Could not load map '"<<filename<<"'\n[3DS] WARNING: (texture must be TGA or BMP)"<<std::endl;
return;
}

GLuint newTextureId;
glGenTextures(1, &newTextureId);

if(filename.find(".tga") != std::string::npos){
textureTGA newTexture(filename, newTextureId);
}
else if(filename.find(".bmp") != std::string::npos){
textureBMP newTexture(filename, newTextureId);
}
else return;

switch(chunkType){
case CHUNK_TEXTURE_MAP:
m_textureMapId = newTextureId;
m_hasTextureMap = true;
break;
case CHUNK_BUMP_MAP:
m_bumpMapId = newTextureId;
m_hasBumpMap = true;
break;
}

}

void mesh3DS::buildMesh(){
calculateNormals();
sortFacesByMaterial();

}

void mesh3DS::calculateNormals(){

// Doesn't take smoothing groups into account yet

if(DEBUG_OUTPUT) std::cout<<"Calculating normals... ";
m_normals.assign(m_vertices.size(), 0.0f);

Vertex vtx1, vtx2, vtx3;
Vector v1, v2, faceNormal;

for(int face=0; face < int(m_faces.size()); face+=3){
// Calculate face normal
vtx1.set(m_vertices[m_faces[face]*3], m_vertices[(m_faces[face]*3)+1], m_vertices[(m_faces[face]*3)+2]);
vtx2.set(m_vertices[m_faces[face+1]*3], m_vertices[(m_faces[face+1]*3)+1], m_vertices[(m_faces[face+1]*3)+2]);
vtx3.set(m_vertices[m_faces[face+2]*3], m_vertices[(m_faces[face+2]*3)+1], m_vertices[(m_faces[face+2]*3)+2]);

v1 = vtx2 - vtx1;
v2 = vtx3 - vtx1;

faceNormal = v1.crossProduct(v2);

// Add normal to all three vertex normals
m_normals[m_faces[face]*3] += faceNormal.x;
m_normals[(m_faces[face]*3)+1] += faceNormal.y;
m_normals[(m_faces[face]*3)+2] += faceNormal.z;

m_normals[m_faces[face+1]*3] += faceNormal.x;
m_normals[(m_faces[face+1]*3)+1] += faceNormal.y;
m_normals[(m_faces[face+1]*3)+2] += faceNormal.z;

m_normals[m_faces[face+2]*3] += faceNormal.x;
m_normals[(m_faces[face+2]*3)+1] += faceNormal.y;
m_normals[(m_faces[face+2]*3)+2] += faceNormal.z;

}

//normalize all normals
for(int n=0; n < int(m_normals.size()); n+=3){
faceNormal.set(m_normals[n], m_normals[n+1], m_normals[n+2]);
faceNormal.normalize();
m_normals[n] = faceNormal.x;
m_normals[n+1] = faceNormal.y;
m_normals[n+2] = faceNormal.z;
}

if(DEBUG_OUTPUT) std::cout<<"done"<<std::endl;
}

void mesh3DS::sortFacesByMaterial(){

assert(getNumFaces()!=0);
assert(m_parentModel!=NULL);

std::vector<ushort> newMatFaces;

// mark each face off as assigned to a material so
// we can figure out which faces have no material
std::vector<bool> assignedFaces;
std::vector<bool>::iterator assignedFacesIter;
assignedFaces.assign(m_faces.size() / 3, false);

// loop over each material
std::map<std::string, std::vector<ushort> >::iterator matFacesIter;
for(matFacesIter=m_materialFaces.begin(); matFacesIter!=m_materialFaces.end(); ++matFacesIter){
//std::cout<<" Faces in material '"<<matFacesIter->first<<"': "<<matFacesIter->second.size()<<std::endl;

// loop over all the faces with that material
std::vector<ushort>::iterator facesIter;
for(facesIter=matFacesIter->second.begin(); facesIter!=matFacesIter->second.end(); ++facesIter){
newMatFaces.push_back(m_faces[((*facesIter)*3)]);
newMatFaces.push_back(m_faces[((*facesIter)*3)+1]);
newMatFaces.push_back(m_faces[((*facesIter)*3)+2]);
assignedFaces[*facesIter]=true;
}

//replace the material's face indices with the actual face vertex indices
m_materialFaces[matFacesIter->first].assign(newMatFaces.begin(),newMatFaces.end());
newMatFaces.clear();
}

// Make a default material and assign any unused faces to it
int numUnassignedFaces=0;
for(assignedFacesIter=assignedFaces.begin(); assignedFacesIter!=assignedFaces.end(); ++assignedFacesIter){
if(*assignedFacesIter == false){
numUnassignedFaces++;
//assign face to default material
}
}
//std::cout<<"numUnassignedFaces: "<<numUnassignedFaces<<std::endl;
}

void mesh3DS::draw(int textureID){

assert(getNumFaces()!=0);

int face, numFaces, vertexIndex, texcoordIndex;
GLuint materialFaces; //GL_FRONT or GL_FRONT_AND_BACK

std::map<std::string, std::vector<ushort> >::iterator materialsIter;
for(materialsIter=m_materialFaces.begin(); materialsIter!=m_materialFaces.end(); ++materialsIter){
const material3DS& currentMaterial = m_parentModel->getMaterial(materialsIter->first);

// Bind texture map (if any)
bool hasTextureMap = currentMaterial.hasTextureMap();
if (textureID) glBindTexture(GL_TEXTURE_2D, textureID);
else if(hasTextureMap) glBindTexture(GL_TEXTURE_2D, currentMaterial.getTextureMapId());
else glBindTexture(GL_TEXTURE_2D, 0);

const GLfloat *specular = currentMaterial.getSpecularColor();
float shininess = currentMaterial.getShininess();
float adjustedSpecular[4] = {specular[0]*shininess, specular[1]*shininess, specular[2]*shininess, 1};

glPushAttrib(GL_LIGHTING_BIT);
if(currentMaterial.isTwoSided()){
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,1);
materialFaces = GL_FRONT_AND_BACK;
}
else{
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,0);
materialFaces = GL_FRONT;
}

// Apply material colors
if(glIsEnabled(GL_LIGHTING)){
const GLfloat matZero[4]={0,0,0,0};
const GLfloat matOne[4]={1,1,1,1};
if(hasTextureMap){ //replace color with texture, but keep lighting contribution
glMaterialfv(materialFaces, GL_DIFFUSE, matOne);
}
else glMaterialfv(materialFaces, GL_DIFFUSE, currentMaterial.getDiffuseColor());
glMaterialfv(materialFaces, GL_AMBIENT, currentMaterial.getAmbientColor());
glMaterialfv(materialFaces, GL_SPECULAR, adjustedSpecular);
glMaterialf(materialFaces, GL_SHININESS, 128.f * currentMaterial.getSpecularExponent());
}
else glColor3fv(currentMaterial.getDiffuseColor());

const std::vector<ushort> *currentMatFaces = &(materialsIter->second);
numFaces = (int)currentMatFaces->size(); //number of faces in this material

switch(m_drawMode){
case DRAW_IMMEDIATE_MODE:

glBegin(GL_TRIANGLES);
for(face=0; face<numFaces; face+=3){
if(hasTextureMap){
texcoordIndex = (*currentMatFaces)[face]*2;
glTexCoord2f(m_texcoords[texcoordIndex], m_texcoords[texcoordIndex+1]);
}
vertexIndex = (*currentMatFaces)[face]*3;
glNormal3f(m_normals[vertexIndex], m_normals[vertexIndex+1], m_normals[vertexIndex+2]);
glVertex3f(m_vertices[vertexIndex], m_vertices[vertexIndex+1], m_vertices[vertexIndex+2]);

if(hasTextureMap){
texcoordIndex = (*currentMatFaces)[face+1]*2;
glTexCoord2f(m_texcoords[texcoordIndex], m_texcoords[texcoordIndex+1]);
}
vertexIndex = (*currentMatFaces)[face+1]*3;
glNormal3f(m_normals[vertexIndex], m_normals[vertexIndex+1], m_normals[vertexIndex+2]);
glVertex3f(m_vertices[vertexIndex], m_vertices[vertexIndex+1], m_vertices[vertexIndex+2]);

if(hasTextureMap){
texcoordIndex = (*currentMatFaces)[face+2]*2;
glTexCoord2f(m_texcoords[texcoordIndex], m_texcoords[texcoordIndex+1]);
}
vertexIndex = (*currentMatFaces)[face+2]*3;
glNormal3f(m_normals[vertexIndex], m_normals[vertexIndex+1], m_normals[vertexIndex+2]);
glVertex3f(m_vertices[vertexIndex], m_vertices[vertexIndex+1], m_vertices[vertexIndex+2]);
}
glEnd();

break;

case DRAW_VERTEX_ARRAY:

glEnableClientState( GL_VERTEX_ARRAY );
glEnableClientState( GL_NORMAL_ARRAY );

if(hasTextureMap){
glTexCoordPointer( 2, GL_FLOAT, 0, &m_texcoords[0] );
glEnableClientState( GL_TEXTURE_COORD_ARRAY );
}

glVertexPointer( 3, GL_FLOAT, 0, &m_vertices[0] );
glNormalPointer(GL_FLOAT, 0, &m_normals[0] );
glDrawElements(GL_TRIANGLES, numFaces, GL_UNSIGNED_SHORT, &(materialsIter->second[0]));

glDisableClientState( GL_VERTEX_ARRAY );
glDisableClientState( GL_NORMAL_ARRAY );
if(hasTextureMap){
glDisableClientState( GL_TEXTURE_COORD_ARRAY );
}

break;

case DRAW_VBO:

break;

default:
std::cout<<"[3DS] ERROR: Invalid mesh draw mode specified"<<std::endl;
break;
}

glPopAttrib(); // GL_LIGHTING_BIT
}
}

// rysuje wszystkie siatki, pobiera jako parametr:
// - int: id tekstury: jeśli większe od 0 to wymusa automatyczne teksturowanie tą teksturą, jeśli 0 lub brak - przyjmuje teksturowanie z pliku
// - bool: centrowanie obiektu: jeśli true lub brak: za pozycję obiektu przymuje jego geometryczny środek, jeśli false - przyjmje współrzędne z pliku
void model3DS::draw(int TextureID, bool centruj){

GLboolean texGenS;
glGetBooleanv(GL_TEXTURE_GEN_S,&texGenS);
GLboolean texGenT;
glGetBooleanv(GL_TEXTURE_GEN_T,&texGenT);
glShadeModel(GL_SMOOTH); // cieniowanie

if (TextureID){ //gdy podano niezerową teksturę należy właczyć automatyczne generowanie tekstur, w przeciwnym wypadku - koordynaty tekstury bazowej z modelu
glEnable(GL_TEXTURE_GEN_S); // automatyczne generowanie koordynat
glEnable(GL_TEXTURE_GEN_T);
} else
{
glDisable(GL_TEXTURE_GEN_S); // automatyczne generowanie koordynat
glDisable(GL_TEXTURE_GEN_T);
}


std::vector<mesh3DS>::iterator meshIter;

glPushMatrix();
if (centruj) glTranslatef(-m_centerX,-m_centerY,-m_centerZ);

for(meshIter = m_meshes.begin(); meshIter != m_meshes.end(); meshIter++){
meshIter->draw(TextureID);
}
glPopMatrix();


if (texGenS == GL_TRUE) glEnable(GL_TEXTURE_GEN_S); else glDisable(GL_TEXTURE_GEN_S); // ustawienie poprzednich wartości
if (texGenS == GL_TRUE) glEnable(GL_TEXTURE_GEN_T); else glDisable(GL_TEXTURE_GEN_T);

}


model3DS.h
Cytat:


#ifndef MODEL3DS_H
#define MODEL3DS_H

#pragma warning(disable: 4786) //ignore truncated STL debug identifiers warnings

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <list>
#include <map>
#include <string>
#include <sstream>
#include <cctype>
#include <algorithm>
#include <assert.h>
#include <gl\gl.h>
#include "textureTGA.h"
#include "textureBMP.h"
#include "Vector.h"

#define CHUNK_MAIN 0x4D4D
#define CHUNK_3D_EDITOR 0x3D3D
#define CHUNK_OBJECT_BLOCK 0x4000
#define CHUNK_MESH 0x4100
#define CHUNK_VERTICES 0x4110
#define CHUNK_FACES 0x4120
#define CHUNK_FACE_MATERIAL 0x4130
#define CHUNK_SMOOTHING_GROUP 0x4150
#define CHUNK_TEXCOORDS 0x4140
#define CHUNK_MATERIAL_BLOCK 0xAFFF
#define CHUNK_MATERIAL_NAME 0xA000
#define CHUNK_AMBIENT_COLOR 0xA010
#define CHUNK_DIFFUSE_COLOR 0xA020
#define CHUNK_SPECULAR_COLOR 0xA030
#define CHUNK_SPECULAR_EXPONENT 0xA040
#define CHUNK_SHININESS 0xA041
#define CHUNK_TRANSPARENCY 0xA050
#define CHUNK_MATERIAL_TWO_SIDED 0xA081
#define CHUNK_TEXTURE_MAP 0xA200
#define CHUNK_BUMP_MAP 0xA230
#define CHUNK_REFLECTION_MAP 0xA220
#define CHUNK_MAP_FILENAME 0xA300
#define CHUNK_RGB_FLOAT 0x0010
#define CHUNK_RGB_BYTE 0x0011
#define CHUNK_RGB_BYTE_GAMMA 0x0012
#define CHUNK_RGB_FLOAT_GAMMA 0x0013
#define CHUNK_PERCENT_INT 0x0030
#define CHUNK_PERCENT_FLOAT 0x0031

#define DRAW_IMMEDIATE_MODE 0x01
#define DRAW_VERTEX_ARRAY 0x02
#define DRAW_VBO 0x03

#define DEBUG_OUTPUT false

class model3DS;
class mesh3DS;
class material3DS;

typedef unsigned short ushort;

///////////////////////////////////////////////////////
// 3DS Material
// Describes a material to be used by a mesh3DS
/////////////////////////////////////////////////////

class material3DS{
private:
std::string m_name;
float m_diffuseColor[3];
float m_ambientColor[3];
float m_specularColor[3];
float m_specularExponent;
float m_shininess;
float m_opacity;
GLuint m_textureMapId;
GLuint m_bumpMapId;
bool m_hasTextureMap;
bool m_hasBumpMap;
bool m_twoSided;

public:
material3DS():m_hasTextureMap(false),m_hasBumpMap(false),m_twoSided(false){}
void loadTexture(std::string filename, int mapType);

std::string getName()const{return m_name;}
const float* getDiffuseColor()const{return m_diffuseColor;}
const float* getAmbientColor()const{return m_ambientColor;}
const float* getSpecularColor()const{return m_specularColor;}
float getSpecularExponent()const{return m_specularExponent;}
float getShininess()const{return m_shininess;}
float getOpacity()const{return m_opacity;}
GLuint getTextureMapId()const{return m_textureMapId;}
GLuint getBumpMapId()const{return m_bumpMapId;}
bool hasTextureMap()const{return m_hasTextureMap;}
bool hasBumpMap()const{return m_hasBumpMap;}
bool isTwoSided()const{return m_twoSided;}

void setName(const std::string newName){m_name = newName;}
void setDiffuseColor(const float* newColor){memcpy(m_diffuseColor,newColor,sizeof(float)*3);}
void setAmbientColor(const float* newColor){memcpy(m_ambientColor,newColor,sizeof(float)*3);}
void setSpecularColor(const float* newColor){memcpy(m_specularColor,newColor,sizeof(float)*3);}
void setShininess(const float newShininess){m_shininess = newShininess;}
void setSpecularExponent(const float newExponent){m_specularExponent = newExponent;}
void setOpacity(const float newOpacity){m_opacity = newOpacity;}
void setTwoSided(const bool isTwoSided){m_twoSided = isTwoSided;}

};

///////////////////////////////////////////////////////
// 3DS Mesh
// Contains all vertex, face, texcoord & normal data
///////////////////////////////////////////////////////

class mesh3DS{
private:
std::string m_name;
std::string m_materialName;
model3DS *m_parentModel;

std::vector<float> m_vertices;
std::vector<float> m_normals;
std::vector<ushort> m_faces;
std::vector<unsigned int> m_faceSmoothing;
std::vector<float> m_texcoords;
std::map<std::string, std::vector<ushort> > m_materialFaces;

//VBO IDs
GLuint m_verticesArrayId;
GLuint m_normalsArrayId;
GLuint m_texcoordsArrayId;

unsigned int m_drawMode;

public:

mesh3DS(model3DS *parentModel):m_parentModel(parentModel){}

void buildMesh();
void calculateNormals();
void sortFacesByMaterial();
void draw(int textureID = 0);

void addVertex(const float vertex){m_vertices.push_back(vertex);}
void addFaceIndex(const ushort faceIndex){m_faces.push_back(faceIndex);}
void addMaterialFace(const std::string &name, const ushort faceIndex){m_materialFaces[name].push_back(faceIndex);}
void addFaceSmoothing(const unsigned int smoothing){m_faceSmoothing.push_back(smoothing);}
void addTexcoord(const float texcoord){m_texcoords.push_back(texcoord);}

std::string getMaterial()const{return m_materialName;}
std::string getName()const{return m_name;}
int getNumFaces()const{return (int)m_faces.size();}

void setMaterial(const std::string &newMat){m_materialName = newMat;}
void setName(const std::string newName){m_name = newName;}
void setDrawMode(const unsigned int newDrawMode){m_drawMode = newDrawMode;}
};

//////////////////////////////////////////////////////////////////
// 3DS Model
// Contains all the meshes & materials that make up a 3DS model
//////////////////////////////////////////////////////////////////

typedef struct{
float minX,maxX;
float minY,maxY;
float minZ,maxZ;
} boundingBox3DS;

class model3DS{
private:
std::string m_filename;
std::string m_filepath;
std::vector<mesh3DS> m_meshes;
std::map<std::string, material3DS> m_materials;
unsigned int m_drawMode;
boundingBox3DS m_boundingBox;
float m_scale;
float m_centerX, m_centerY, m_centerZ;

// temporaries used while reading chunks
mesh3DS *m_currentMesh;
material3DS *m_currentMaterial;
std::vector<material3DS> *m_currentMaterialFaces;
float m_currentColor[3];
ushort m_tempUshort;
float m_tempFloat;
std::string m_tempString;

void readChunk(std::ifstream *modelFile, const int objectStart, const int objectLength);

// Private copy and assignment constructors
// to prevent object being copied
model3DS(const model3DS &model);
model3DS &operator=(const model3DS &model);

public:
model3DS(const char* filename, float scale = 1);

void draw(int TextureID = 0, bool centruj = true);

std::string getFilename(){return m_filename;}
const material3DS& getMaterial(const std::string &matName){return m_materials[matName];}
};

#endif //MODEL3DS_H


Vector.h
Cytat:


#ifndef VECTOR_H
#define VECTOR_H

#include <math.h>
#include <iostream>

class Vector{
public:
float x;
float y;
float z;

/*
Vector();
Vector(float a, float b, float c);
Vector(Vector& copy);

Vector& operator=(const Vector &other);
int operator== (const Vector &other);
int operator!= (const Vector &other);
Vector operator+(const Vector &other);
Vector operator-(const Vector &other);
Vector operator*(const float &value);
Vector operator/(const float &value);
Vector& operator+=(const Vector &other);
Vector& operator-=(const Vector &other);
Vector& operator*=(const float& other);
Vector& operator/=(const float& other);
float operator[](unsigned i);

float length();
float lengthSq();
float dotProduct(const Vector &other);
Vector crossProduct(const Vector &other);
void normalize();
float distance(const Vector &other);
float distanceSq(const Vector &other);
void set(float newX, float newY, float newZ);
void zero();
*/

/////////////////
// Constructors
/////////////////

inline Vector()Mad(0),y(0),z(0){}
inline Vector(const float a, const float b, const float c)Mad(a),y(b),z(c){}
inline Vector(const Vector& copy)Mad(copy.x),y(copy.y),z(copy.z){}

//////////////
// Operators
//////////////

inline Vector& operator= (const Vector &other){
x=other.x;y=other.y;z=other.z;
return *this;
}

inline int operator== (const Vector &other) const{
return (x==other.x && y==other.y && z==other.z);
}
inline int operator!= (const Vector &other) const{
return (x!=other.x || y!=other.y || z!=other.z);
}

inline Vector operator+ (const Vector &other) const{
return Vector(x+other.x, y+other.y, z+other.z);
}

inline Vector operator- (const Vector &other) const{
return Vector(x-other.x, y-other.y, z-other.z);
}

inline Vector operator* (const float &value) const{
return Vector(x*value, y*value, z*value);
}

inline Vector operator/ (const float &value) const{
return Vector(x/value, y/value, z/value);
}

inline Vector& operator+= (const Vector &other){
x+=other.x;
y+=other.y;
z+=other.z;
return *this;
}
inline Vector& operator-= (const Vector &other){
x-=other.x;
y-=other.y;
z-=other.z;
return *this;
}

inline Vector& operator*= (const float &value){
x*=value;
y*=value;
z*=value;
return *this;
}

inline Vector& operator/= (const float &value){
x/=value;
y/=value;
z/=value;
return *this;
}

inline float operator[] (unsigned i) const{
switch(i){
case 0:return x;
case 1:return y;
case 2:return z;
}
}

/////////////////////
// Other operations
/////////////////////

inline float length() const{
float len=(x*x)+(y*y)+(z*z);
return (float)sqrt(len);
}

inline float lengthSq() const{
return (x*x)+(y*y)+(z*z);
}

inline float dotProduct(const Vector &other) const{
//this[dot]other
return (x*other.x) + (y*other.y) + (z*other.z);
}

inline Vector crossProduct(const Vector &other) const{
//(x1,y1,z1)×(x2,y2,z2) = (y1z2-y2z1, x2z1-x1z2, x1y2-x2y1).
return Vector(
(y*other.z) - (z*other.y),
(z*other.x) - (x*other.z),
(x*other.y) - (y*other.x)
);
}

inline void normalize(){
float len=length();
if(len==0)return;
len=1.0f/len;
x*=len;
y*=len;
z*=len;
}

inline float distance(const Vector &other) const{
return (Vector(other.x-x,other.y-y,other.z-z)).length();
}

inline float distanceSq(const Vector &other) const{
return (Vector(other.x-x,other.y-y,other.z-z)).lengthSq();
}

inline void set(float newX, float newY, float newZ){
x=newX;y=newY;z=newZ;
}

inline void zero(){
x=y=z=0;
}

};

typedef Vector Vertex;

const Vector vZero=Vector(0,0,0);

/////////////////////////////
// Global stream operators
//////////////////////////////
inline std::ostream& operator<<(std::ostream &str, const Vector &v){
str<<v.x<<", "<<v.y<<", "<<v.z;
return str;
}

#endif //VECTOR_H


a co do 'polecen'.......... ADDOBIEKT();
a na powaznie:

Cytat:

model3DS *model_n; // model_n - nazwa kolejnego model, definiujesz to na poczatku kodu
.
.
model_n->draw(); //wywolanie modelu
.
.
//w mainie

model_n = new model3DS ("nazwa_modelu.3DS",1) ; //1 to skala


Post został pochwalony 0 razy
Powrót do góry
Zobacz profil autora
Wyświetl posty z ostatnich:   
Napisz nowy temat   Odpowiedz do tematu    Forum Strona Główna -> Geometria i grafika komp. Wszystkie czasy w strefie CET (Europa)
Strona 1 z 1

 
Skocz do:  
Nie możesz pisać nowych tematów
Nie możesz odpowiadać w tematach
Nie możesz zmieniać swoich postów
Nie możesz usuwać swoich postów
Nie możesz głosować w ankietach
Back to Top              
fora.pl - załóż własne forum dyskusyjne za darmo
Jenova Template © digital-delusion.com
Powered by phpBB © 2001, 2002 phpBB Group

Regulamin