gpt4 book ai didi

c++ - 使用 UV 贴图 OpenGL 纹理化 3d 对象

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:53:20 26 4
gpt4 key购买 nike

我在使用 UV 贴图渲染 3D 对象时遇到问题。

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

问题是最终结果出现的不是贴图而是没有贴图的物体。

这是初始化函数:

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);

我知道也许我错过了什么,但我无法解决这个问题,尽管我阅读了很多教程。

谁能帮我解决这个问题?

最佳答案

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

顺序错误。

glVertex commands are used within glBegin/glEnd pairs to specify point, line, and polygon vertices. The current color, normal, texture coordinates, and fog coordinate are associated with the vertex when glVertex is called.

您需要在 glVertex() 之前调用 glTexCoord() 您希望它属于:

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

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

关于c++ - 使用 UV 贴图 OpenGL 纹理化 3d 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24416529/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com