LWJGL Forum

Programming => OpenGL => Topic started by: shadowstryker10 on April 12, 2015, 04:35:26

Title: Using pixel coordinates for VBO data? (instead of a float -1 through 1) [SOLVED]
Post by: shadowstryker10 on April 12, 2015, 04:35:26
Whenever I try drawing a quad using a VBO, I have to use a float between -1 and 1, with coordinate 0, 0 being the center of the screen. Is there any way I can get it to use pixel coordinates with (0, 0) being at the bottom left of the screen instead of the middle? I tried using glOrtho like I would with immediate rendering, but the only thing that affects the VBO with glOrtho is the zNear and zFar.

Here's my code so far (I just started learning VBOs):

OpenGL Setup

glViewport(0, 0, Display.getWidth(), Display.getHeight());
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, Display.getWidth(), 0, Display.getHeight(), 0, 0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);


Creating vertex/color data:

amountOfVertices = 4;
vertexSize = 2;
colorSize = 3;

// Create buffer with vertex data
FloatBuffer vertexData = BufferUtils.createFloatBuffer(amountOfVertices * vertexSize);
vertexData.put(new float[] {
0, 0,
0, -1,
-1, -1,
-1, 0});
vertexData.flip();

// Create buffer with color data
FloatBuffer colorData = BufferUtils.createFloatBuffer(amountOfVertices * colorSize);
colorData.put(new float[] {
1, 0, 0,
0, 1, 0,
0, 0, 1,
1, 1, 0});
colorData.flip();


Rendering:

glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
glVertexPointer(vertexSize, GL_FLOAT, 0, 0L);

glBindBuffer(GL_ARRAY_BUFFER, vboColorHandle);
glColorPointer(colorSize, GL_FLOAT, 0, 0L);

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glDrawArrays(GL_QUADS, 0, amountOfVertices);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);


I think that's all the relevant code you guys may need.


This is what the above code produces:
(http://i.imgur.com/poPBWkj.png)
Title: Re: Using pixel coordinates for VBO data? (instead of a float -1 through 1)
Post by: Kai on April 12, 2015, 10:54:33
glOrtho does not allow any differences of left-right, top-bottom or near-far to be zero.
Therefore your current invocation of glOrtho is likely to generate a GL_INVALID_VALUE (see: https://www.opengl.org/sdk/docs/man2/xhtml/glOrtho.xml (https://www.opengl.org/sdk/docs/man2/xhtml/glOrtho.xml)) and have no effect.
Just use as near = -1 and as far = +1 for example, and of course update your VBO to not use 0.0 and -1.0 but those pixel coordinates in [0..width] and [0..height]. Then it should work. It does for me at least. :)
Title: Re: Using pixel coordinates for VBO data? (instead of a float -1 through 1)
Post by: shadowstryker10 on April 13, 2015, 00:46:38
I want to say I'm an idiot for not fixing this myself, but I had no idea that was even a thing. The reason I couldn't get it to work when I used -1 and 1 for the near/far values is because I never thought to convert the vertex data back into pixel coordinates. Thank you so much!