10
我想通過在正交投影矩陣上創建一個四邊形並向其中添加紋理來設置我的openGL窗口的背景圖像。我也在我的應用程序中使用GLUT工具包。設置openGL窗口的背景圖像
但是我有幾個問題。下面是說明問題的屏幕截圖:儘管紋理大小設置爲與四邊形相同,但圖像是灰度,並且它在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紋理轉換爲原始格式我使用photoshop,所以我認爲灰度圖像可能是由於轉換。
注意:我剛開始學習開放,所以這可能不是最好的方法。請讓我知道是否有更好的方法
編輯:圖像上有留在一個地方,而不是rescalse
附註:我認爲你的意思glTexCoord2f(以下簡稱「d」代表「雙」,而不是「尺寸」)。 –
@MarceloCantos對,但它不應該有所作爲,不是嗎? – Vitalij
不,我不期望這會影響結果。 –