OK, ive been tweaking this one way and another for a couple of hours and I cant see anything wrong with it. It should show a square, one half drawn directly, the other half drawn via a vertex array. But its only showing the direct one, and I’m damned if i know why! Any one got any ideas?
Thanks
Jacko
import org.lwjgl.*;
import org.lwjgl.opengl.*;
import java.nio.*;
import org.lwjgl.input.*;
import org.khaos.lib.*;
public final class VertexArray
{
static {
try
{
int mode = -1;
DisplayMode[] modes = Display.getAvailableDisplayModes();
for (int i = 0; i < modes.length; i++)
{
if (modes[i].width == 640
&& modes[i].height == 480
&& modes[i].bpp >= 16)
{
mode = i;
break;
}
}
Display.create(modes[mode], false);
}
catch (Exception e)
{
System.exit(1);
}
}
public static final GL gl = new GL(); // The Opengl Context
public static final GLU glu = new GLU(gl); // The Opengl Utility Context
static {
try
{
gl.create(); // Create The Opengl Context
}
catch (Exception e)
{
System.exit(1);
}
}
private static boolean finished;
static float kSpeed = 50.0f; // This is how fast our camera moves
// Variables for creating the vertex buffer
private static int vertexBufferStride = 0;
static MemoryBuffer vertexBuffer;
static int vertexBufferAddress;
static int vertexBufferOffset = 0;
static int maxVertices = 0;
static DoubleBuffer vertexBufferDouble;
// Variables for creating the color buffer
private static int colorBufferStride = 0;
static MemoryBuffer colorBuffer;
static int colorBufferAddress;
static int colorBufferOffset = 0;
static DoubleBuffer colorBufferDouble;
static int numberOfVertices = 50;
static int frameCounter = 0;
private VertexArray()
{
}
public static void main(String args[])
{
try
{
init(); // Init Opengl
while (!finished)
{
Keyboard.poll(); // Poll The Keyboard
AbsMouse.poll(); // Poll The Mouse
mainLoop(); // Launch The Main Loop
render(); // Render To Screen
gl.swapBuffers(); // Swap Opengl Buffers
}
}
catch (Throwable t)
{
t.printStackTrace();
}
finally
{
cleanup();
}
}
private final static void init() throws Exception
{
int bytesPerDouble = 8;
int numberOfItems = 3;
vertexBufferStride = bytesPerDouble * numberOfItems;
vertexBuffer = new MemoryBuffer(vertexBufferStride * numberOfVertices);
vertexBufferAddress=vertexBuffer.getAddress();
vertexBufferDouble=vertexBuffer.bytes.asDoubleBuffer();
vertexBufferOffset=0;
maxVertices=numberOfVertices;
numberOfItems=4;
colorBufferStride = bytesPerDouble * numberOfItems;
colorBuffer = new MemoryBuffer(colorBufferStride * numberOfVertices);
colorBufferAddress=colorBuffer.getAddress();
colorBufferDouble=colorBuffer.bytes.asDoubleBuffer();
colorBufferOffset=0;
Keyboard.create(); // Create The Keyboard
Mouse.create(); // Create The Mouse
gl.clearColor(0.0f, 0.0f, 0.0f, 0.0f);
gl.matrixMode(GL.PROJECTION); // Select The Projection Matrix
gl.loadIdentity(); // Reset The Projection Matrix
// The closest distance to the camera before it clips,
// FOV // Ratio // The farthest distance before it stops drawing)
glu.perspective(45.0f,(float)Display.getWidth()/(float)Display.getHeight(), 0.5f ,150.0f);
gl.matrixMode(GL.MODELVIEW); // Select The Modelview Matrix
gl.loadIdentity(); // Reset The Modelview Matrix
gl.wglSwapIntervalEXT(1);
}
private final static void mainLoop()
{
processKeyboard(); // Get Keyboard Events
}
private final static void render()
{
gl.clear(GL.COLOR_BUFFER_BIT | GL.DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
gl.loadIdentity(); // Reset The matrix
gl.translatef(0.0f, 0.0f,-35.0f);
addColorToBuffer(1,0,0);
addVertexToBuffer(10,10,0);
addColorToBuffer(0,1,0);
addVertexToBuffer(10,-10,0);
addColorToBuffer(0,0,1);
addVertexToBuffer(-10,10,0);
stats();
drawTriangleVertexArrayInt();
/*
gl.begin(GL.TRIANGLES);
gl.color3f(1f,0f,0f);
gl.vertex3i(10,10,0);
gl.color3f(0f,1f,0f);
gl.vertex3i(10,-10,0);
gl.color3f(0f,0f,1f);
gl.vertex3i(-10,10,0);
gl.end();
*/
gl.begin(GL.TRIANGLES);
gl.color3f(1f,0f,0f);
gl.vertex3i(-10,-10,0);
gl.color3f(0f,1f,0f);
gl.vertex3i(10,-10,0);
gl.color3f(0f,0f,1f);
gl.vertex3i(-10,10,0);
gl.end();
gl.flush();
}
private static void stats()
{
if(++frameCounter>100)
{
frameCounter=0;
System.out.println("Buffer size: "+vertexBufferDouble.position());
System.out.println("Vertex 1: "+vertexBufferDouble.get(0)+", "+vertexBufferDouble.get(1)+", "+vertexBufferDouble.get(2));
System.out.println("Vertex 2: "+vertexBufferDouble.get(3)+", "+vertexBufferDouble.get(4)+", "+vertexBufferDouble.get(5));
System.out.println("Vertex 3: "+vertexBufferDouble.get(6)+", "+vertexBufferDouble.get(7)+", "+vertexBufferDouble.get(8));
System.out.println("Color 1: "+colorBufferDouble.get(0)+", "+colorBufferDouble.get(1)+", "+colorBufferDouble.get(2)+", "+colorBufferDouble.get(3));
System.out.println("Color 2: "+colorBufferDouble.get(4)+", "+colorBufferDouble.get(5)+", "+colorBufferDouble.get(6)+", "+colorBufferDouble.get(7));
System.out.println("Color 3: "+colorBufferDouble.get(8)+", "+colorBufferDouble.get(9)+", "+colorBufferDouble.get(10)+", "+colorBufferDouble.get(11));
System.out.println("");
}
}
public static void drawTriangleVertexArrayInt()
{
gl.enableClientState(GL.VERTEX_ARRAY);
gl.enableClientState(GL.COLOR_ARRAY);
gl.vertexPointer(3, gl.DOUBLE, 0, vertexBufferAddress);
gl.colorPointer(4, gl.DOUBLE, 0, colorBufferAddress);
gl.drawArrays(GL.TRIANGLES, 0, 9*3);
checkError();
gl.disableClientState(GL.VERTEX_ARRAY);
gl.disableClientState(GL.COLOR_ARRAY);
vertexBufferOffset=0;
colorBufferOffset=0;
vertexBufferDouble.rewind();
colorBufferDouble.rewind();
}
private static void checkError()
{
int error = gl.getError();
if(error!=GL.NO_ERROR)
{
System.out.println("error");
System.out.println(glu.errorString(error));;
}
}
public static void addVertexToBuffer(double x, double y, double z)
{
vertexBufferDouble.put(x);
vertexBufferDouble.put(y);
vertexBufferDouble.put(z);
}
public static void addColorToBuffer(double red, double green, double blue)
{
colorBufferDouble.put(red);
colorBufferDouble.put(green);
colorBufferDouble.put(blue);
colorBufferDouble.put(0.5); //alpha
}
/** Process keyboard events
*/
private final static void processKeyboard()
{
if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE))
finished = true;
}
/** Cleanup
*/
private final static void cleanup()
{
Keyboard.destroy(); // Destroy The Keyboard
Mouse.destroy(); // Destroy The Mouse
gl.destroy(); // Destroy The Opengl Context
Display.destroy(); // Destroy The Display
}
}