设置 openGL 窗口的背景图像
我试图通过在正交投影矩阵上创建四边形并向其添加纹理来设置 openGL 窗口的背景图像。我还在我的应用程序中使用 GLUT 工具包。
但是我有几个问题。下面的屏幕截图说明了该问题:图像是灰度的,并且在 x 和 y 上重复,即使纹理大小设置为与四边形相同。
这就是我想要实现的目标:
这就是我得到的:
渲染和加载纹理的代码如下所示:
void orthogonalStart (void) {
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluOrtho2D(0, w1, 0, h1);
glScalef(1, -1, 1);
glTranslatef(0, -h1, 0);
glMatrixMode(GL_MODELVIEW);
}
void orthogonalEnd (void) {
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
}
GLuint LoadTexture( const char * filename, int width, int height )
{
GLuint texture;
unsigned char * data;
FILE * file;
//The following code will read in our RAW file
file = fopen( filename, "rb" );
if ( file == NULL ) return 0;
data = (unsigned char *)malloc( width * height * 3 );
fread( data, width * height * 3, 1, file );
fclose( file );
glGenTextures( 1, &texture );
glBindTexture( GL_TEXTURE_2D, texture );
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
//even better quality, but this will do for now.
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR );
//to the edge of our shape.
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
//Generate the texture
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0,GL_RGB, GL_UNSIGNED_BYTE, data);
free( data ); //free the texture
return texture; //return whether it was successful
}
void FreeTexture( GLuint texture )
{
glDeleteTextures( 1, &texture );
}
void background (void) {
glBindTexture( GL_TEXTURE_2D, texture );
orthogonalStart();
glBegin(GL_QUADS);
glTexCoord2d(0.0,0.0); glVertex2f(0, 0);
glTexCoord2d(1.0,0.0); glVertex2f(1024, 0);
glTexCoord2d(1.0,1.0); glVertex2f(1024, 1024);
glTexCoord2d(0.0,1.0); glVertex2f(0, 1024);
glEnd();
orthogonalEnd();
}
void display (void) {
glClearColor (1.0,0.0,0.0,1.0);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glEnable( GL_TEXTURE_2D );
background();
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glutSwapBuffers();
}
要将纹理从 png 转换为 Raw 格式,我使用 Photoshop,所以我认为灰度图像可能是由于转换造成的。
注意:我刚刚开始学习开放,所以这可能不是最好的方法。如果有更好的方法,请告诉我
编辑:图像必须保留在一个位置并且不能重新缩放
I am trying to set a background image of my openGL window by creating a quad on an orthogonal projection matrix and adding texture to it. I am also making use of GLUT toolkit in my application.
However I am having several problems. Below are screenshot illustrating the problem: image is grayscale and it repeats on x and y, even though texture size is set to be the same as the quad.
That is what I am trying to achieve:
And that is what I am getting:
Code for rendering and loading texture looks like this:
void orthogonalStart (void) {
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluOrtho2D(0, w1, 0, h1);
glScalef(1, -1, 1);
glTranslatef(0, -h1, 0);
glMatrixMode(GL_MODELVIEW);
}
void orthogonalEnd (void) {
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
}
GLuint LoadTexture( const char * filename, int width, int height )
{
GLuint texture;
unsigned char * data;
FILE * file;
//The following code will read in our RAW file
file = fopen( filename, "rb" );
if ( file == NULL ) return 0;
data = (unsigned char *)malloc( width * height * 3 );
fread( data, width * height * 3, 1, file );
fclose( file );
glGenTextures( 1, &texture );
glBindTexture( GL_TEXTURE_2D, texture );
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
//even better quality, but this will do for now.
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR );
//to the edge of our shape.
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
//Generate the texture
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0,GL_RGB, GL_UNSIGNED_BYTE, data);
free( data ); //free the texture
return texture; //return whether it was successful
}
void FreeTexture( GLuint texture )
{
glDeleteTextures( 1, &texture );
}
void background (void) {
glBindTexture( GL_TEXTURE_2D, texture );
orthogonalStart();
glBegin(GL_QUADS);
glTexCoord2d(0.0,0.0); glVertex2f(0, 0);
glTexCoord2d(1.0,0.0); glVertex2f(1024, 0);
glTexCoord2d(1.0,1.0); glVertex2f(1024, 1024);
glTexCoord2d(0.0,1.0); glVertex2f(0, 1024);
glEnd();
orthogonalEnd();
}
void display (void) {
glClearColor (1.0,0.0,0.0,1.0);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glEnable( GL_TEXTURE_2D );
background();
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glutSwapBuffers();
}
To convert texture from png into Raw format I use photoshop, so I thought greyscale image might be due to conversion.
Note: I have just started learning open, so this might be not the best approach. Please let me know if there is a better way
Edit: Image has to stay on one place and not rescalse
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我避免做
glFrustum()
或glOrtho() 以外的任何事情
调用 来获取投影矩阵。另外,如果您正在执行 3 分量像素,请将纹理加载的
GL_UNPACK_ALIGNMENT
设置为 1:I'd avoid doing anything other than a
glFrustum()
orglOrtho()
call for your projection matrix.Also, set
GL_UNPACK_ALIGNMENT
to 1 for your texture loading if you're doing 3-component pixels: