Reputation: 133
I am trying to render a group of textured quads.
I can get colored quads to render, but not textured ones (screen comes up empty.)
I am using LWJGL and PNGDecoder.
Code for initializing my OGL:
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, 800, 0, 600, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glEnable(GL11.GL_TEXTURE_2D);
Code for decoding my image:
ByteBuffer buffer = null;
InputStream in = ClassLoader.getSystemResourceAsStream(filename);
try {
buffer = decodeStreamToBuffer(in);
} finally {
in.close();
}
return buffer;
My decodeStreamToBuffer(InputStream in)
:
PNGDecoder decoder;
ByteBuffer buf = null;
try {
decoder = new PNGDecoder(in);
buf = ByteBuffer.allocateDirect(4*decoder.getWidth()*decoder.getHeight());
decoder.decode(buf, decoder.getWidth()*4, Format.RGBA);
buf.flip();
} catch (Exception e) {
e.printStackTrace();
}
return buf;
My rendering code:
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
GL11.glColor3f(0.5f, 0.5f, 1.0f); //Still there to test color quads.
// draw quad
GL11.glPushMatrix();
GL11.glTranslatef(screencenter.getX(), screencenter.getY(), 0);
GL11.glScalef(1f, 0.5f, 1f);
GL11.glRotatef(camRotation, 0f, 0f, 1f);
GL11.glTranslatef(-screencenter.getX(), -screencenter.getY(), 0);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture);
GL11.glBegin(GL11.GL_TEXTURE_2D);
GL11.glTexCoord2f(0.0f, 0.0f);
GL11.glVertex2f(0, 0);
GL11.glTexCoord2f(1.0f, 0.0f);
GL11.glVertex2f(32, 0);
GL11.glTexCoord2f(1.0f, 1.0f);
GL11.glVertex2f(32, 32);
GL11.glTexCoord2f(0.0f, 1.0f);
GL11.glVertex2f(0, 32);
GL11.glEnd();
GL11.glPopMatrix();
When I leave the texture binding out and change the GL_TEXTURE_2D
to GL_MODELVIEW
it all works... but with color instead of a texture. Am I making a noob mistake here?
Upvotes: 1
Views: 1572
Reputation: 26245
This is wrong GL11.glBegin(GL11.GL_TEXTURE_2D);
The glBegin
method an primitive mode
GLenum
.
Thereby you either use:
GL_POINTS
GL_LINES
GL_LINE_STRIP
GL_LINE_LOOP
GL_TRIANGLES
GL_TRIANGLE_STRIP
GL_TRIANGLE_FAN
GL_QUADS
GL_QUAD_STRIP
GL_POLYGON
The mode you use specify what you are rendering. In your case you would code GL_QUADS
So to fix your code you need to replace GL11.glBegin(GL11.GL_TEXTURE_2D);
with GL11.glBegin(GL11.GL_QUADS);
.
Also take in mind that the glVertex
, glNormal
, glTexCoord
, etc. Methods are deprecated and should not be used. You are suppose to use VBOs and Shaders. Though if you are learning OpenGL then keep using the deprecated methods since they are good and easy to use when learning!
Upvotes: 2