我们无法在open gl中添加纹理。
这里我们的代码用于制作简单的黑/白案例纹理:
GLuint tex;//before the main function
然后在主要:
glGenTextures(1, &tex);
float pixels[] = {
0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f
};
glBindTexture(GL_TEXTURE_2D, tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0, GL_RGB, GL_FLOAT, pixels);
glEnable(GL_TEXTURE_2D);
然后在draw方法中我们以下列方式应用它
glBegin(GL_QUADS);
glTexCoord2d(0, 1); glVertex3d(0, -0.1, 0);
glTexCoord2d(0, 0); glVertex3d(50, -0.1, 0);
glTexCoord2d(1, 1); glVertex3d(50, -0.1, 50);
glTexCoord2d(1, 1); glVertex3d(0, -0.1, 50);
glEnd();
我们希望在场景的基础上简单地应用这种纹理。我们错过了什么吗?
答案 0 :(得分:1)
GL_TEXTURE_MIN_FILTER
to GL_NEAREST
or GL_LINEAR
GL_TEXTURE_ENV_MODE
改为GL_DECAL
而非默认GL_MODULATE
或glColor3ub(255, 255, 255)
。所有在一起:
// http://glew.sourceforge.net/
#include <GL/glew.h>
#include <GL/glut.h>
GLuint tex = 0;
void init()
{
// upload texture
glGenTextures( 1, &tex );
glBindTexture( GL_TEXTURE_2D, tex );
float pixels[] =
{
1, 0, 0, 0, 1, 0,
0, 0, 1, 1, 1, 1,
};
glTexImage2D
(
GL_TEXTURE_2D, 0,
GL_RGB, 2, 2, 0,
GL_RGB, GL_FLOAT, pixels
);
// configure texture unit
glActiveTexture( GL_TEXTURE0 + 0 );
glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
}
void display()
{
glClearColor( 0, 0, 0, 1 );
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( -2, 2, -2, 2, -1, 1 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
// select texture unit & enable it
glActiveTexture( GL_TEXTURE0 + 0 );
glEnable( GL_TEXTURE_2D );
glBindTexture( GL_TEXTURE_2D, tex );
glBegin(GL_QUADS);
glTexCoord2i( 0, 0 ); glVertex2i( -1, -1 );
glTexCoord2i( 1, 0 ); glVertex2i( 1, -1 );
glTexCoord2i( 1, 1 ); glVertex2i( 1, 1 );
glTexCoord2i( 0, 1 ); glVertex2i( -1, 1 );
glEnd();
glutSwapBuffers();
}
int main( int argc, char** argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
glutCreateWindow( "GLUT" );
glewInit();
glutDisplayFunc( display );
init();
glutMainLoop();
return 0;
}