I just recently got my feet wet with OpenGL, and I'm having an issue with drawing a wireframe with a vertex array. Drawing a quad works just fine, but when I use GL_LINE_LOOP, a fifth vertex (which I believe is at 0,0,0) appears:
(http://sphotos-b.xx.fbcdn.net/hphotos-ash4/483043_10151385825962357_1360410721_n.jpg)
I'm sure the problem is a novice mistake. :P
import java.nio.FloatBuffer;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.*;
public class VectorArrayTest
{
FloatBuffer vertBuff;
int vertArrayID;
int vertBufferID;
int numVerts;
public VectorArrayTest()
{
try
{
Display.setDisplayMode(new DisplayMode(300,200));
Display.create();
}
catch (LWJGLException e)
{
e.printStackTrace();
}
initOpenGL();
initData();
while(!Display.isCloseRequested())
{
doCycle();
Display.update();
}
onExit();
}
public void initOpenGL()
{
GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(-1, 1.0, -1, 1.0, 0.1, 3.0);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glTranslatef(0.0f,0.0f,-0.5f);
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
}
public void initData()
{
float[] verts = {
0.75f, 0.75f, -0.9f,
0.25f, 0.75f, -0.9f,
0.25f, 0.25f, -0.9f,
0.75f, 0.25f, -0.9f };
vertBuff = org.lwjgl.BufferUtils.createFloatBuffer(verts.length);
vertBuff.put(verts);
vertBuff.flip();
numVerts = verts.length;
vertArrayID = GL30.glGenVertexArrays();
vertBufferID = GL15.glGenBuffers();
}
public void doCycle()
{
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
GL11.glColor3d(1.0, 1.0, 1.0);
GL30.glBindVertexArray(vertArrayID);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vertArrayID);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vertBuff, GL15.GL_STATIC_DRAW);
GL20.glVertexAttribPointer(0, 3, GL11.GL_FLOAT, false, 0, 0);
GL20.glEnableVertexAttribArray(0);
GL11.glDrawArrays(GL11.GL_LINE_LOOP, 0, numVerts);
GL20.glDisableVertexAttribArray(0);
GL30.glBindVertexArray(0);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
GL15.glDeleteBuffers(vertBufferID);
GL30.glBindVertexArray(0);
GL30.glDeleteVertexArrays(vertArrayID);
}
public void onExit()
{
GL11.glDisableClientState(GL11.GL_VERTEX_ARRAY);
}
public static void main(String[] args)
{
new VectorArrayTest();
}
}
FIXED: My numVerts variable was way too big.
numVerts = verts.length;
numVerts = verts.length/3;