OpenGL 3.x basic problems (LWJGL Wiki Tutorial)

Started by Akzyl, November 21, 2012, 22:37:42

Previous topic - Next topic

Akzyl

Helo.
I'm new in OpenGL 1.x but I know sam basic stuf (maybe more then basic) and now
I want to try OpenGL 3.x. I started from tutorial placed on LWJGL Wiki and i have some problem
if I run the final code for example from this page
http://lwjgl.org/wiki/index.php?title=The_Quad_with_DrawElements
three problems occur which also repeat in the rest tutorials.

1. To correctly display square the 'indices' table must look like this
byte[] indices = {
        0, 1, 2,
	1, 3, 0
};

not like this
byte[] indices = {
		// Left bottom triangle
		0, 1, 2,
		// Right top triangle
		2, 3, 0
};

because with this indices table square look like this

but only when square is colored or textured

2. The square is display in top right corner not in the middle

I have to change glViewport to
GL11.glViewport(-60, -60, WIDTH, HEIGHT);


3. Problem with displaying texture (I'm assuming it's connected with 'indices' table)

Source code is from http://lwjgl.org/wiki/index.php?title=The_Quad_textured

So can anyone tell me how to fix this three problems.
If I'm fixing  first and second problem correctly how to fix third problem with texture which
should be displayed in this way

Izman

Funny enough, I just attempted to do this myself and I encountered the exact same problem :P

Bump for solution I guess?

Rednax

The example code is indeed not optimal.

1&3. You are completely right about the wrong indices. Seems to be a plain mistake in the tutorial.
I myself define my triangles for a square in the following order:
2--3
| \ |
0--1
(fency text art of the square)
Then the triangles are 0,1,2 and 3,2,1
OR, use TRIANGLE_STRIP, which results in 4 indices, simple in the order: 0,1,2,3 :D
This should also fix the texturing issue.
e
2. I'm not sure if changing the view port is the right solution here. It may cause the triangle to be drawn right, but other stuff might get broken in the future because of this fix. I myself have never used the viewport command in openGL 3. Can you see what happens if you remove both viewport calls? (why are there 2 viewport calls in there anyway??).

General hint:
The tutorial code binds fixxed numbers to the attributes. Since the compiler has already done this, it is better to retrieve the attribute locations after compiling and store them in variables. The biggest advantage is that you make it impossible for magic numbers to exist, since you are forced to use the variable name.
instead of GL20.glEnableVertexAttribArray(0);, you would get GL20.glEnableVertexAttribArray(vertexLoc); which reads a lot easier.
setting vertexLoc: vertexLoc = glGetAttribLocation(pId, "in_Position");
In this way it is also possible to check if vertexLoc is -1, which may indicate that the attribute has been optimized out of you shader, hence it has no location.

IchBinMude