用UV Map OpenGL纹理化3d对象

时间:2014-06-25 19:02:18

标签: c++ opengl texture-mapping 3d-rendering

使用UV Map渲染3D对象时遇到问题。

首先,我的对象是Wavefront模型。我使用一个解析器将整个文件拆分为顶点,法线,面和texCoords。在解析file.obj之后,我拥有了所有这些。

问题在于纹理不会出现在最终结果中,而是没有纹理化的对象。

这是init函数:

int init()
{
if(SDL_Init(SDL_INIT_VIDEO) != 0)
{
    cout << "SDL error: " << SDL_GetError() << endl;
    return false;
}

// SDL Window crap
createWindow(screenWidth, screenHeight, 32, false, "WaveFront Object Loader");
reshape(screenWidth, screenHeight);

// OpenGL init
// Stuff
glShadeModel(GL_SMOOTH);
glClearColor(0.2f, 0.2f, 0.2f, 0.0f);
glClearDepth(1.0f);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);

// Face culling (for textures)
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK); 
glEnable(GL_DEPTH_TEST);

// Lighting
GLfloat light_ambient[] = { 0.5, 0.5, 0.5, 1.0 };
GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };

glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glShadeModel(GL_SMOOTH);

// Object loader
if(!model.load("BallForRenderl.obj"))
{
    cout << "Could not load model" << endl;
    return false;
}


return true;
 }

这是绘制场景的函数:

void draw(){

    glPushMatrix();

// clear the screen & depth buffer
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);

// set the perspective projection
glCallList(g_persp);

// set the camera position
gluLookAt(  0, 1, -20,  //  eye pos
            0, 0, 0,    //  aim point
            0, 1, 0);   //  up direction

glRotatef(angle, 0.0, 0.5, 1.0);
glColor4f(1.0, 1.0, 1.0, 1.0);


glScalef(0.2,0.2,0.2);

model.draw();

// set the orthographic projection
glCallList(g_ortho);

// 2D/text  *****************************************

glPopMatrix();

// Commented out because we call it in our idle() function - maintains framerate independance
// SDL_GL_SwapBuffers();
}

这是绘制模型的函数:

void WFObject::draw(){

glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); 
glEnable(GL_TEXTURE_2D); 
glColor3f(1.0,1.0,1.0); 
glBegin(GL_TRIANGLES);

for(int f = 0; f < faces.size(); f++)
{
    glNormal3f(normals[faces[f].vn1 - 1].x, normals[faces[f].vn1 - 1].y, normals[faces[f].vn1 - 1].z);
    glVertex3f(vertices[faces[f].v1 - 1].x, vertices[faces[f].v1 - 1].y, vertices[faces[f].v1 - 1].z);
    glTexCoord2f(texCoords[faces[f].vt1 - 1].u,texCoords[faces[f].vt1 - 1].v);
    //printf("%f %f \n",texCoords[faces[f].vt1 - 1].u,texCoords[faces[f].vt1 - 1].v);
    glNormal3f(normals[faces[f].vn2 - 1].x, normals[faces[f].vn2 - 1].y, normals[faces[f].vn2 - 1].z);
    glVertex3f(vertices[faces[f].v2 - 1].x, vertices[faces[f].v2 - 1].y, vertices[faces[f].v2 - 1].z);
    glTexCoord2f(texCoords[faces[f].vt2 - 1].u,texCoords[faces[f].vt2 - 1].v);
    //printf("%f %f \n",texCoords[faces[f].vt2 - 1].u,texCoords[faces[f].vt2 - 1].v);
    glNormal3f(normals[faces[f].vn3 - 1].x, normals[faces[f].vn3 - 1].y, normals[faces[f].vn3 - 1].z);
    glVertex3f(vertices[faces[f].v3 - 1].x, vertices[faces[f].v3 - 1].y, vertices[faces[f].v3 - 1].z);
    glTexCoord2f(texCoords[faces[f].vt3 - 1].u,texCoords[faces[f].vt3 - 1].v);
    //printf("%f %f \n",texCoords[faces[f].vt3 - 1].u,texCoords[faces[f].vt3 - 1].v);
    //printf("\n");
}

glEnd();
glDisable(GL_TEXTURE_2D);

}

我知道也许我会想念一些东西,但我无法解决这个问题,尽管我阅读了很多教程。

任何人都可以帮我吗?

1 个答案:

答案 0 :(得分:0)

glNormal3f(...);
glVertex3f(...);
glTexCoord2f(...);  // this will affect the *next* vertex

错误的订单。

  

glVertex命令在glBegin / glEnd对中用于指定   点,线和多边形顶点。 当前颜色,正常,纹理   当glVertex时,坐标和雾坐标与顶点相关联   被称为。

您必须先致电glTexCoord() 您希望它所属的glVertex()

glNormal3f(...);
glTexCoord2f(...);
glVertex3f(...);

你似乎也从未在任何地方绑定纹理对象(通过glBindTexture())。您需要绑定纹理对象,以便OpenGL知道要采样的纹理。