LWJGL Forum

Programming => OpenGL => Topic started by: Akzyl on November 21, 2012, 22:37:42

Title: OpenGL 3.x basic problems (LWJGL Wiki Tutorial)
Post by: Akzyl on November 21, 2012, 22:37:42
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
(http://img26.imageshack.us/img26/4019/pic3rw.png)
but only when square is colored or textured

2. The square is display in top right corner not in the middle
(http://img339.imageshack.us/img339/9468/picys.png)
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)
(http://img526.imageshack.us/img526/4217/pic2kr.png)
Source code is from http://lwjgl.org/wiki/index.php?title=The_Quad_textured (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
(http://lwjgl.org/wiki/images/4/48/Result_textured.png)
Title: Re: OpenGL 3.x basic problems (LWJGL Wiki Tutorial)
Post by: Izman on December 02, 2012, 10:04:30
Funny enough, I just attempted to do this myself and I encountered the exact same problem :P

Bump for solution I guess?
Title: Re: OpenGL 3.x basic problems (LWJGL Wiki Tutorial)
Post by: Rednax on December 15, 2012, 16:58:13
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.
Title: Re: OpenGL 3.x basic problems (LWJGL Wiki Tutorial)
Post by: IchBinMude on January 15, 2013, 17:09:38
http://lwjgl.org/forum/index.php/topic,4733.0.html (http://lwjgl.org/forum/index.php/topic,4733.0.html)

Just in case somebody comes to this post looking for the answer.