2011-10-30 15 views
7

का उपयोग कर 3 डी मॉडल लोडिंग में गलत बनावट मैं डब्लूजीएल के बजाय जीएलयूटी का उपयोग करके एएसएसआईएमपी sample code में शामिल 3 डी मॉडल लोड करने के लिए नमूना कोड को संशोधित करने का प्रयास कर रहा हूं।एएसएसआईएमपी और ओपनजीएल

loaded 3d model

जबकि यह माना जाता है जैसा कि नीचे बताया: हालांकि, मैं बनावट के साथ एक समस्या है, जैसा कि नीचे बताया मिला

original 3d model

और 3 डी मॉडल की बैठक के लिए कोड

void recursive_render (const struct aiScene *sc, const struct aiNode* nd, float scale){ 
unsigned int i; 
unsigned int n=0, t; 
struct aiMatrix4x4 m = nd->mTransformation; 
m.Scaling(aiVector3D(scale, scale, scale), m); 
// update transform 
m.Transpose(); 
glPushMatrix(); 
glMultMatrixf((float*)&m); 
// draw all meshes assigned to this node 
for (; n < nd->mNumMeshes; ++n){ 
    const struct aiMesh* mesh = scene->mMeshes[nd->mMeshes[n]]; 
    apply_material(sc->mMaterials[mesh->mMaterialIndex]); 
    if(mesh->mNormals == NULL){ 
     glDisable(GL_LIGHTING); 
    } 
    else { 
     glEnable(GL_LIGHTING); 
    } 
    if(mesh->mColors[0] != NULL) { 
     glEnable(GL_COLOR_MATERIAL); 
    } 
    else { 
     glDisable(GL_COLOR_MATERIAL); 
    } 

    for (t = 0; t < mesh->mNumFaces; ++t) { 
     const struct aiFace* face = &mesh->mFaces[t]; 
     GLenum face_mode; 
     switch(face->mNumIndices) { 
      case 1: face_mode = GL_POINTS; break; 
      case 2: face_mode = GL_LINES; break; 
      case 3: face_mode = GL_TRIANGLES; break; 
      default: face_mode = GL_POLYGON; break; 
     } 
     glBegin(face_mode); 
     for(i = 0; i < face->mNumIndices; i++){ 
      int vertexIndex = face->mIndices[i]; // get group index for current index 
      if(mesh->mColors[0] != NULL) 
       Color4f(&mesh->mColors[0][vertexIndex]); 
      if(mesh->mNormals != NULL) 
       if(mesh->HasTextureCoords(0)){ 
        glTexCoord2f(mesh->mTextureCoords[0][vertexIndex].x, 1- mesh->mTextureCoords[0][vertexIndex].y);      
       } 
       glNormal3fv(&mesh->mNormals[vertexIndex].x); 
       glVertex3fv(&mesh->mVertices[vertexIndex].x); 
     } 
     glEnd(); 
    } 
} 
// draw all children 
for (n = 0; n < nd->mNumChildren; ++n) { 
    recursive_render(sc, nd->mChildren[n], scale); 
} 
glPopMatrix(); 

}

+०१२३५१६४१०६१: नीचे सूचीबद्ध

apply_material समारोह, लगभग ठीक ASSIMP रूप में एक ही नमूना

void apply_material(const struct aiMaterial *mtl) 
{ 
float c[4]; 
GLenum fill_mode; 
int ret1, ret2; 
struct aiColor4D diffuse; 
struct aiColor4D specular; 
struct aiColor4D ambient; 
struct aiColor4D emission; 
float shininess, strength; 
int two_sided; 
int wireframe; 
unsigned int max; // changed: to unsigned 
int texIndex = 0; 
aiString texPath; //contains filename of texture 
if(AI_SUCCESS == mtl->GetTexture(aiTextureType_DIFFUSE, texIndex, &texPath)) { 
    unsigned int texId = textureIdMap[texPath.data]; 
    glBindTexture(GL_TEXTURE_2D, texId); 
} 

set_float4(c, 0.8f, 0.8f, 0.8f, 1.0f); 
if(AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_DIFFUSE, &diffuse)) 
    color4_to_float4(&diffuse, c); 
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, c); 
set_float4(c, 0.2f, 0.2f, 0.2f, 1.0f); 
if(AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_AMBIENT, &ambient)) 
    color4_to_float4(&ambient, c); 
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, c); 
set_float4(c, 0.0f, 0.0f, 0.0f, 1.0f); 
if(AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_SPECULAR, &specular)) 
    color4_to_float4(&specular, c); 
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, c); 
set_float4(c, 0.0f, 0.0f, 0.0f, 1.0f); 
if(AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_EMISSIVE, &emission)) 
    color4_to_float4(&emission, c); 
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, c); 

max = 1; 
ret1 = aiGetMaterialFloatArray(mtl, AI_MATKEY_SHININESS, &shininess, &max); 
max = 1; 
ret2 = aiGetMaterialFloatArray(mtl, AI_MATKEY_SHININESS_STRENGTH, &strength, &max); 
if((ret1 == AI_SUCCESS) && (ret2 == AI_SUCCESS)) 
    glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess * strength); 
else { 
    glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 0.0f); 
    set_float4(c, 0.0f, 0.0f, 0.0f, 0.0f); 
    glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, c); 
} 

max = 1; 
if(AI_SUCCESS == aiGetMaterialIntegerArray(mtl, AI_MATKEY_ENABLE_WIREFRAME, &wireframe, &max)) 
    fill_mode = wireframe ? GL_LINE : GL_FILL; 
else 
    fill_mode = GL_FILL; 
glPolygonMode(GL_FRONT_AND_BACK, fill_mode); 

max = 1; 
if((AI_SUCCESS == aiGetMaterialIntegerArray(mtl, AI_MATKEY_TWOSIDED, &two_sided, &max)) && two_sided) 
    glEnable(GL_CULL_FACE); 
else 
    glDisable(GL_CULL_FACE); 
} 

और भी loadGLtextures समारोह प्रदान की है, मुझे नहीं लगता कि यह कठिन को मारने से संबंधित है है।

int LoadGLTextures(const aiScene* scene) { 
ILboolean success; 
/* initialization of DevIL */ 
ilInit(); 
/* scan scene's materials for textures */ 
for (unsigned int m=0; m<scene->mNumMaterials; ++m) { 
    int texIndex = 0; 
    aiString path; // filename 
    aiReturn texFound = scene->mMaterials[m]->GetTexture(aiTextureType_DIFFUSE, texIndex, &path); 
    while (texFound == AI_SUCCESS) { 
     //fill map with textures, OpenGL image ids set to 0 
     textureIdMap[path.data] = 0; 
     // more textures? 
     texIndex++; 
     texFound = scene->mMaterials[m]->GetTexture(aiTextureType_DIFFUSE, texIndex, &path); 
    } 
} 

int numTextures = textureIdMap.size(); 
/* create and fill array with DevIL texture ids */ 
ILuint* imageIds = new ILuint[numTextures]; 
ilGenImages(numTextures, imageIds); 
/* create and fill array with GL texture ids */ 
GLuint* textureIds = new GLuint[numTextures]; 
glGenTextures(numTextures, textureIds); /* Texture name generation */ 

/* get iterator */ 
std::map<std::string, GLuint>::iterator itr = textureIdMap.begin(); 
printf("TextureIDMap Begin %i\n", textureIdMap.begin()); 
int i=0; 
for (; itr != textureIdMap.end(); ++i, ++itr) { 
    //save IL image ID 
    std::string filename = (*itr).first; // get filename 
    (*itr).second = textureIds[i]; // save texture id for filename in map 
    printf("Texture loaded: %s\n",filename.c_str()); 
    printf("Texture ID Map End: %i\n",textureIdMap.end()); 
    ilBindImage(imageIds[i]); /* Binding of DevIL image name */ 
    ilEnable(IL_ORIGIN_SET); 
    ilOriginFunc(IL_ORIGIN_LOWER_LEFT); 
    success = ilLoadImage((ILstring)filename.c_str()); 

    if (success) { 
     /* Convert image to RGBA */ 
     ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE); 

     /* Create and load textures to OpenGL */ 
     glBindTexture(GL_TEXTURE_2D, textureIds[i]); 
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 
     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, ilGetInteger(IL_IMAGE_WIDTH), 
      ilGetInteger(IL_IMAGE_HEIGHT), 0, GL_RGBA, GL_UNSIGNED_BYTE, 
      ilGetData()); 
    } 
    else 
     printf("Couldn't load Image: %s\n", filename.c_str()); 
} 
/* Because we have already copied image data into texture data we can release memory used by image. */ 
ilDeleteImages(numTextures, imageIds); 
//Cleanup 
delete [] imageIds; 
delete [] textureIds; 
//return success; 
return true; 
} 

प्रकाशस्तंभ 3 डी यह कर, हालांकि, इस समय मैं GLSL और VAO मेरे कार्यक्रम के लिए लागू नहीं कर सकते के लिए an example दिया है। कोई भी समाधान? अग्रिम में धन्यवाद।

+0

मैं गलत हो सकता है लेकिन शायद समस्या बनावट के साथ, लेकिन सामग्री के साथ नहीं है। –

+0

क्या आपको कोई विचार है कि सामग्री समस्याग्रस्त हो सकती है? धन्यवाद .. – snowball147

+0

मैंने लाइटहाउस 3 डी के ट्यूटोरियल की कोशिश की है, लेकिन मैं इसे आपके मॉडल के लिए मॉडल – SpicyWeenie

उत्तर

8

मैं वैकल्पिक हल मिल गया है।

glTexCoord2f(mesh->mTextureCoords[0][vertexIndex].x, mesh->mTextureCoords[0][vertexIndex].y); 

के बजाय:

glTexCoord2f(mesh->mTextureCoords[0][vertexIndex].x, 1-mesh->mTextureCoords[0][vertexIndex].y); 
+0

मैं GLSL साथ Assimp उपयोग कर रहा हूँ, मॉडल मैं Assimp की आपूर्ति से यूवी बनावट ही काम करता है जब मैं 'का उपयोग glTexCoord2f (mesh-> mTextureCoords [0] [vertexIndex] .x, 1- (mesh-> mTextureCoords [0] [ vertexIndex] .y)); ' अन्यथा मुझे बनावट के साथ अजीब व्यवहार मिलता है।मुझे लगता है कि केवल कुछ मॉडलों के लिए 1-वाई की आवश्यकता है – 2am

2

इस बनावट के साथ एक समस्या नहीं है। आपकी समस्या बैकफेस कूलिंग से आ रही है (कम से कम ऐसा लगता है क्योंकि आप बतख के अंदर देख सकते हैं)। या तो आपके बहुभुज गलत क्रम में घायल हैं, या आपके बैकफ़ेस कूलिंग को गलत तरीके से सेट किया गया है। यदि आप कोड पोस्ट करते हैं जहां आप बैकफ़ेस कूलिंग सेट करते हैं, तो हम वास्तव में गलत क्या देख सकते हैं।

वहाँ भी संभावना है कि आपके normals के कुछ अंदर की ओर का सामना कर रहे हैं (जो भी बहुभुज घुमावदार से परिणाम कर सकते) हो सकता है। यह समझाएगा कि आपके बतख की चोंच क्यों पिच काला है।

+0

लोड करने के लिए नहीं मिल सकता। सक्षम/अक्षम कूलिंग फ़ंक्शन 'apply_material' के अंत में सेट की गई है। मुझे यकीन नहीं है कि समस्या कूलिंग के बारे में है, क्योंकि अगर मैंने कूलिंग को अक्षम कर दिया है, फिर भी, बनावट सही ढंग से लोड नहीं होगी। (CMIIW)। मैंने कोड संपादित किया है। – snowball147

+0

क्या आपने पूरी तरह से कूलिंग को अक्षम करने का प्रयास किया है? अर्थात। अगर कथन को हटा रहा है और सिर्फ glDisable (GL_CULL_FACE) डाला जा रहा है? – NickLH

+0

मेरे पास है, लेकिन यह काम नहीं किया। – snowball147

1

मैं बहुत यकीन है कि समस्या बनावट की जा रही वाई अक्ष के साथ 'फ़्लिप' है कर रहा हूँ मैं कैसे निम्नलिखित कोड का उपयोग कर recursive_render समारोह में बनावट का उपयोग करने के लिए बदल दिया। यही कारण है कि आपका '1-वाई' काम करता है। इसे लोड करते समय वाई के साथ बनावट को फिसलने से तय किया जा सकता है। हालांकि मुझे अभी तक यकीन नहीं है कि क्यों 'आज इस समस्या पर ठोकर खाई गई है।

संबंधित मुद्दे