I adapted the "Quad textured" example for the fixed pipeline but when I run it I don't see anything and it is not clear to me what I am doing wrong. This is a pretty slavish adaptation, so it should not take long to look at it: it is only the rendering part in the loop (the render method below) that differs; the rest is as in the original. Please don't give me grief over the use of the fixed pipeline: I am working on an existing code base and I don't have a mandate to perform radical surgery. Thanks!
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL12;
import org.lwjgl.opengl.GL13;
import org.lwjgl.opengl.GL15;
import org.lwjgl.opengl.GL30;
import org.lwjgl.util.glu.GLU;
import de.matthiasmann.twl.utils.PNGDecoder;
import de.matthiasmann.twl.utils.PNGDecoder.Format;
public class LwjglSquare
{
private static final int WIDTH = 250;
private static final int HEIGHT = 250;
private static final int TEXTURE_UNIT = GL13.GL_TEXTURE0;
private static int vboVId, vboIId, texId;
public static void main( String[] argv ) throws Exception
{
Display.setDisplayMode( new DisplayMode( WIDTH, HEIGHT ) );
Display.setTitle( LwglSquareScene.class.getSimpleName() );
Display.create();
GL11.glClearColor( 0.4f, 0.6f, 0.9f, 0f );
GL11.glViewport( 0, 0, WIDTH, HEIGHT );
GL11.glEnable( GL11.GL_TEXTURE_2D );
setup();
while ( !Display.isCloseRequested() )
{
render();
Display.update();
}
destroy();
}
private static void setup() throws IOException
{
FloatBuffer vertexBuf = BufferUtils.createFloatBuffer( 16 );
vertexBuf.put( new float[] { -0.5f, 0.5f, 0, 0, -0.5f, -0.5f, 0, 1, 0.5f, -0.5f, 1, 1, 0.5f, 0.5f, 1, 0 } );
vertexBuf.flip();
ByteBuffer indexBuf = BufferUtils.createByteBuffer( 6 );
indexBuf.put( new byte[] { 0, 1, 2, 2, 3, 0 } );
indexBuf.flip();
vboVId = GL15.glGenBuffers();
GL15.glBindBuffer( GL15.GL_ARRAY_BUFFER, vboVId );
GL15.glBufferData( GL15.GL_ARRAY_BUFFER, vertexBuf, GL15.GL_STATIC_DRAW );
GL15.glBindBuffer( GL15.GL_ARRAY_BUFFER, 0 );
vboIId = GL15.glGenBuffers();
GL15.glBindBuffer( GL15.GL_ELEMENT_ARRAY_BUFFER, vboIId );
GL15.glBufferData( GL15.GL_ELEMENT_ARRAY_BUFFER, indexBuf, GL15.GL_STATIC_DRAW );
GL15.glBindBuffer( GL15.GL_ELEMENT_ARRAY_BUFFER, 0 );
texId = loadPngTexture( "square.png", TEXTURE_UNIT );
exitOnGLError( "setup" );
}
private static void render()
{
GL11.glMatrixMode( GL11.GL_PROJECTION );
GL11.glLoadIdentity();
GL11.glOrtho( 0, WIDTH, 0, HEIGHT, 1, -1 );
GL11.glMatrixMode( GL11.GL_MODELVIEW );
GL11.glClear( GL11.GL_COLOR_BUFFER_BIT );
GL13.glClientActiveTexture( TEXTURE_UNIT );
GL11.glBindTexture( GL11.GL_TEXTURE_2D, texId );
GL15.glBindBuffer( GL15.GL_ARRAY_BUFFER, vboVId );
GL11.glVertexPointer ( 2, GL11.GL_FLOAT, 16, 0 );
GL11.glTexCoordPointer( 2, GL11.GL_FLOAT, 16, 8 );
GL15.glBindBuffer( GL15.GL_ELEMENT_ARRAY_BUFFER, vboIId );
GL11.glEnableClientState( GL11.GL_VERTEX_ARRAY );
GL11.glEnableClientState( GL11.GL_TEXTURE_COORD_ARRAY );
GL11.glDrawElements( GL11.GL_TRIANGLES, 6, GL11.GL_UNSIGNED_BYTE, 0 );
GL11.glDisableClientState( GL11.GL_VERTEX_ARRAY );
GL11.glDisableClientState( GL11.GL_TEXTURE_COORD_ARRAY );
GL15.glBindBuffer( GL15.GL_ARRAY_BUFFER, 0 );
GL15.glBindBuffer( GL15.GL_ELEMENT_ARRAY_BUFFER, 0 );
GL11.glBindTexture( GL11.GL_TEXTURE_2D, 0 );
exitOnGLError( "render" );
}
private static void destroy()
{
GL11.glDeleteTextures( texId );
GL15.glBindBuffer( GL15.GL_ARRAY_BUFFER, 0 );
GL15.glDeleteBuffers( vboVId );
GL15.glBindBuffer( GL15.GL_ELEMENT_ARRAY_BUFFER, 0 );
GL15.glDeleteBuffers( vboIId );
exitOnGLError( "destroy" );
Display.destroy();
}
@SuppressWarnings( "resource" )
private static int loadPngTexture( String resName, int textureUnit ) throws IOException
{
URL url = LwglSquareScene.class.getResource( resName );
InputStream in = url.openStream();
PNGDecoder decoder = new PNGDecoder( in );
int width = decoder.getWidth();
int height = decoder.getHeight();
ByteBuffer buf = ByteBuffer.allocateDirect( 4 * width * height );
decoder.decode( buf, 4 * width, Format.RGBA );
in.close();
buf.flip();
int texId = GL11.glGenTextures();
GL13.glClientActiveTexture( textureUnit );
GL11.glBindTexture( GL11.GL_TEXTURE_2D, texId );
GL11.glPixelStorei( GL11.GL_UNPACK_ALIGNMENT, 1 );
GL11.glTexImage2D( GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB, width, height, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buf );
GL30.glGenerateMipmap( GL11.GL_TEXTURE_2D );
GL11.glTexParameteri( GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE );
GL11.glTexParameteri( GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE );
GL11.glTexParameteri( GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST );
GL11.glTexParameteri( GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR_MIPMAP_LINEAR );
exitOnGLError( "loadPngTexture" );
return texId;
}
private static void exitOnGLError( String errorMessage )
{
int errorValue = GL11.glGetError();
if ( errorValue != GL11.GL_NO_ERROR )
{
String errorString = GLU.gluErrorString( errorValue );
System.err.println( "ERROR - " + errorMessage + ": " + errorString );
if ( Display.isCreated() )
{
Display.destroy();
}
System.exit( -1 );
}
}
}