Reputation: 69
I'm trying to display the texture on the window using openGL. However, the texture is only mapping to the bottom left of my window and it cuts off! output
Here is my code:
Texture:
GLuint textureID[1];
GLubyte Image[1024*768*4];
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(1,textureID);
glBindTexture(GL_TEXTURE_2D, textureID[0]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, 3, 1024, 768, 0, GL_RGBA, GL_UNSIGNED_BYTE, Image);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_GEN_T);
Quad:
glPushMatrix ();
glTranslatef(0, 0.0, -1.1);
glMaterialf(GL_FRONT, GL_SHININESS, 30.0);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textureID[0]);
glViewport(-511,-383,1025,768);
glBegin(GL_QUADS);
glTexCoord2d(0.0, 0.0); glVertex2f(0.0, 0.0);
glTexCoord2d(1.0, 0.0); glVertex2f(1024.0, 0.0);
glTexCoord2d(1.0, 1.0); glVertex2f(1024.0, 768.0);
glTexCoord2d(0.0, 1.0); glVertex2f(0.0, 768.0);
glEnd();
glDisable(GL_TEXTURE_2D);
glPopMatrix ();
glFlush ();
I'm trying to map the texture into my window. Both of my window and texture have the size of 1024x768. What did I do wrong? If I comment out glViewport
, the texture will be mapped to top right.
Upvotes: 2
Views: 111
Reputation: 162367
Your viewport parameters are invalid for your particular desires (negative values for x,y). Also likely you didn't specify a projection / modelview matrix pair that maps local coordinates to pixels (at least not in the code shown), yet the coordinates you pass to glVertex look like you want to address pixels.
Upvotes: 0
Reputation: 22170
The reason why your image gets cut off are the values supplied to glViewport
. This function specifies to which area of the screen the rendering should go. So if you set the x-value to -511 and the width to 1025, then the drawing will happen from pixel -511 to 514, which is exactly what you see.
What you actually want to get the image to your desired position is a projection (most probably an orthographic one), that maps you input coordinates to the appropriate normalized device coordinates (NDC). When not using projections OpenGL works in this NDC coordinates ranging from -1 to 1 on each axis and not, as you assumed, in pixel coordinates.
Upvotes: 1