2
votes

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.

2
Any specific reason to give -ve values in specifying lower left corner of viewport ? why don't you give 0,0 there ? - Raki
because giving 0,0 or comment it our will give me this image. I want something like this: image2 - user3335341
The reason commenting glviewport give you that behavior is that the viewport (and scissorbox) is set to the framebuffer dimensions (0,0,1024,768) first time the context is made current. - Andreas
And why is texture generation on when you clearly provide your own coordinates? I smell the need of a mentor... - Andreas

2 Answers

1
votes

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.

0
votes

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.