我错过了什么纹理不工作 OpenGL
What am i missing that texture isn't working OpenGL
我现在正在尝试将纹理应用到面部,直到我让它正常工作,但每次应用程序运行时面部只是默认的白色,但我不知道是什么出问题了。
LoadTexture function:
GLuint LoadTexture( const char* texture )
{
GLuint textureID = SOIL_load_OGL_texture( texture, SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID, SOIL_FLAG_MIPMAPS );
glBindTexture( GL_TEXTURE_2D, textureID );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
glBindTexture( GL_TEXTURE_2D, 0 );
return textureID;
}
Main code:
GLuint tex = LoadTexture("grass.jpg");
int crotate = 0;
void Reshape(int w, int h)
{
if (h == 0) h = 1;
float ratio = w * 1.0 / h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, w, h);
gluPerspective(45.0f, ratio, 0.1f, 1000);
glMatrixMode(GL_MODELVIEW);
}
void Cube()
{
glTranslatef(0, 0, -5);
crotate++;
glRotatef(crotate, 1,1,0);
glActiveTexture(tex);
glEnable(GL_TEXTURE_2D);
glBindTexture( GL_TEXTURE_2D, tex);
glBegin(GL_POLYGON);
glTexCoord2f(0.0, 1.0); glVertex3f(-0.5,-0.5,0);
glTexCoord2f(0.0, 0.0); glVertex3f(-0.5,0.5,0);
glTexCoord2f(1.0, 0.0); glVertex3f(0.5,0.5,0);
glTexCoord2f(1.0, 0.0); glVertex3f(0.5,0.5,0);
glTexCoord2f(1.0, 1.0); glVertex3f(0.5,-0.5,0);
glTexCoord2f(0.0, 1.0); glVertex3f(-0.5,-0.5,0);
glEnd();
glDisable(GL_TEXTURE_2D);
}
void Display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
Cube();
glutPostRedisplay();
glutSwapBuffers();
}
void Init()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//Lighting
/*
glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
GLfloat qaDiffuseLight[] = {0.8, 0.8, 0.8, 1};
glLightfv(GL_LIGHT0, GL_DIFFUSE, qaDiffuseLight);
GLfloat qaLightPos[] = {0.5, 0.5, 0, 1.0};
glLightfv(GL_LIGHT0, GL_POSITION, qaLightPos); */
gluPerspective(45.5, 1.0f, 0.1f, 1000);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_DEPTH_TEST);
glClearColor(0, 0.6, 1, 1);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE);
glutInitWindowSize(1280,720);
glutCreateWindow("OpenGL Program");
Init();
glutReshapeFunc(Reshape);
glutDisplayFunc(Display);
glutMainLoop();
return 0;
}
我搜索了许多纹理贴图教程,但我尝试过的所有教程仍然不适合我,我正在使用 GLUT
我遵循的最新教程或博客是尝试添加纹理的
https://www.3dgep.com/texturing-and-lighting-in-opengl/
glActiveTexture
的参数为纹理单元:
`glActiveTexture(tex);
glActiveTexture(GL_TEXTURE0);
纹理对象绑定到纹理单元。当前纹理单元可以用 glActiveTexture
设置。默认纹理单元为 0 (GL_TEXTURE0
).
在拥有有效的当前 OpenGL 上下文之前,您无法执行 OpenGL 语句。因此,您必须在创建 OpenGL window 和上下文之后创建纹理对象。在创建 window 并且上下文成为当前上下文后调用 GLuint tex = LoadTexture("grass.jpg");
。
GLuint tex = 0;
void Init()
{
tex = LoadTexture("grass.jpg");
// [...]
}
int main(int argc, char** argv)
{
// [...]
glutCreateWindow("OpenGL Program");
Init();
// [...]
}
我现在正在尝试将纹理应用到面部,直到我让它正常工作,但每次应用程序运行时面部只是默认的白色,但我不知道是什么出问题了。
LoadTexture function:
GLuint LoadTexture( const char* texture )
{
GLuint textureID = SOIL_load_OGL_texture( texture, SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID, SOIL_FLAG_MIPMAPS );
glBindTexture( GL_TEXTURE_2D, textureID );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
glBindTexture( GL_TEXTURE_2D, 0 );
return textureID;
}
Main code:
GLuint tex = LoadTexture("grass.jpg");
int crotate = 0;
void Reshape(int w, int h)
{
if (h == 0) h = 1;
float ratio = w * 1.0 / h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, w, h);
gluPerspective(45.0f, ratio, 0.1f, 1000);
glMatrixMode(GL_MODELVIEW);
}
void Cube()
{
glTranslatef(0, 0, -5);
crotate++;
glRotatef(crotate, 1,1,0);
glActiveTexture(tex);
glEnable(GL_TEXTURE_2D);
glBindTexture( GL_TEXTURE_2D, tex);
glBegin(GL_POLYGON);
glTexCoord2f(0.0, 1.0); glVertex3f(-0.5,-0.5,0);
glTexCoord2f(0.0, 0.0); glVertex3f(-0.5,0.5,0);
glTexCoord2f(1.0, 0.0); glVertex3f(0.5,0.5,0);
glTexCoord2f(1.0, 0.0); glVertex3f(0.5,0.5,0);
glTexCoord2f(1.0, 1.0); glVertex3f(0.5,-0.5,0);
glTexCoord2f(0.0, 1.0); glVertex3f(-0.5,-0.5,0);
glEnd();
glDisable(GL_TEXTURE_2D);
}
void Display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
Cube();
glutPostRedisplay();
glutSwapBuffers();
}
void Init()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//Lighting
/*
glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
GLfloat qaDiffuseLight[] = {0.8, 0.8, 0.8, 1};
glLightfv(GL_LIGHT0, GL_DIFFUSE, qaDiffuseLight);
GLfloat qaLightPos[] = {0.5, 0.5, 0, 1.0};
glLightfv(GL_LIGHT0, GL_POSITION, qaLightPos); */
gluPerspective(45.5, 1.0f, 0.1f, 1000);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_DEPTH_TEST);
glClearColor(0, 0.6, 1, 1);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE);
glutInitWindowSize(1280,720);
glutCreateWindow("OpenGL Program");
Init();
glutReshapeFunc(Reshape);
glutDisplayFunc(Display);
glutMainLoop();
return 0;
}
我搜索了许多纹理贴图教程,但我尝试过的所有教程仍然不适合我,我正在使用 GLUT 我遵循的最新教程或博客是尝试添加纹理的 https://www.3dgep.com/texturing-and-lighting-in-opengl/
glActiveTexture
的参数为纹理单元:
`glActiveTexture(tex);
glActiveTexture(GL_TEXTURE0);
纹理对象绑定到纹理单元。当前纹理单元可以用 glActiveTexture
设置。默认纹理单元为 0 (GL_TEXTURE0
).
在拥有有效的当前 OpenGL 上下文之前,您无法执行 OpenGL 语句。因此,您必须在创建 OpenGL window 和上下文之后创建纹理对象。在创建 window 并且上下文成为当前上下文后调用 GLuint tex = LoadTexture("grass.jpg");
。
GLuint tex = 0;
void Init()
{
tex = LoadTexture("grass.jpg");
// [...]
}
int main(int argc, char** argv)
{
// [...]
glutCreateWindow("OpenGL Program");
Init();
// [...]
}