Page 1 of 1

OpenGL ES From the Ground Up - Vertex way off

Posted: Wed Jun 27, 2012 4:59 pm
by Davespice
Hi everyone;
I'm trying to learn OpenGL ES from scratch and have been looking around on the net for various guides and one of the most "linked to" ones I found was Jeff LaMarche's OpenGL ES From the Ground Up.

I have been attempting to follow his guide. Although it has been written specifically for the iPhone the OpenGL ES API calls remain the same - so it has been possible for me to convert the code from objective c to c++ on the fly.

What I have done is heavily modified the hello_triangle example (supplied with the Debian SD card image) to create a basic OpenGL ES environment within which to try out his code. I removed everything to do with the cube but kept everything to do with setting up the screen and frame buffer etc.

I have got as far as stage 2; A Look at Simple Drawing and have run into a problem I am hoping someone can help me with. The aim is to just draw a red triangle on a grey background, but I can't seem to get my triangle to look right.

It appears that one of the vertexes in my triangle is way way off the edge of the screen, despite the code all looking correct. I fear this may be some kind of nasty gotcha to do with how data types are held in memory and how the ogles API deals with them.

Anyway here is my code if anyone would like to take a look.
triangle.c

The redraw_scene method is probably what you want to have a look at.

The easiest way to get this to run is to download my file and copy it into the hello_triangle folder inside /opt/vc/src/hello_pi. Just make sure you back up the original triangle.c first.

Any help would be really appreciated!
Thanks!

Dave

Re: OpenGL ES From the Ground Up - Vertex way off

Posted: Wed Jun 27, 2012 8:56 pm
by Gibble
Not near a pi atm, and can't quite remember in OpenGL ES, but just thinking, should glDrawArrays take 6 instead of 18 indices in this particular case ?

Re: OpenGL ES From the Ground Up - Vertex way off

Posted: Thu Jun 28, 2012 7:33 am
by Davespice
Gibble wrote:Not near a pi atm, and can't quite remember in OpenGL ES, but just thinking, should glDrawArrays take 6 instead of 18 indices in this particular case ?
I think it would if I was also passing in GL_TRIANGLE_STRIPS, but I am just using GL_TRIANGLES.
Since posting this I have had a few ideas, none of which have worked :cry:

Re: OpenGL ES From the Ground Up - Vertex way off

Posted: Thu Jun 28, 2012 10:02 am
by Gibble
Oddly I couldn't actually get anything displaying with your code when I tried it this morning, except the grey screen. Is there something missing from it at all, or not the latest file ?

Re: OpenGL ES From the Ground Up - Vertex way off

Posted: Thu Jun 28, 2012 10:13 am
by AndyD
Hi Dave,
I had a look at your code. I have never used OpenGL ES (or OpenGL really), so the iphone blog looks like a good place to start. I have made some headway with your code.

line 173 and 174 ... I think line 173 should be vertex2 and line 174 should be vertex3! This doesn't fix the problem though.

line 191 I think should be

Code: Select all

 glDrawArrays(GL_TRIANGLES, 0, 6);
lastly, each of the triangle is specified with z=-3.0. I changed all the -3.0f to 1.0f. With all these changes I get a rotating red square. Hopefully someone with considerably more OpenGL (ES) experience will help you out, but I guess this is a start. I am not sure why changing the Z value makes a difference. I changed it because I thought having z=-3.0 would make the triangles back-faces, and as such they are specified with a counter-clockwise winding. As I say though I have never used OpenGL (ES) before so I am more than likely wrong.

Re: OpenGL ES From the Ground Up - Vertex way off

Posted: Thu Jun 28, 2012 11:27 am
by Davespice
Thanks guys. I have actually been able to work it out now.
I managed to get some help on Freenode IRC ##OpenGL.

I needed to set the size of the field using glFrustumf, I also had the vertex order of the poylgons back to front - so you would be looking at the back face. I'll post up some fixed code in a bit.

Re: OpenGL ES From the Ground Up - Vertex way off

Posted: Thu Jun 28, 2012 11:56 am
by AndyD
Hi Dave,
Please do post back a link to the working code.

Re: OpenGL ES From the Ground Up - Vertex way off

Posted: Thu Jun 28, 2012 12:08 pm
by Davespice
AndyD wrote:Hi Dave,
Please do post back a link to the working code.
Here we go;
triangle.c

Okay the bit I was missing is in init_ogl method after my comment //DAVE - Set up screen ratio.
I also had the vertexes wrong in redraw_scene (so I changed the code there to make them go anti-clockwise so that the front face would face the view port).

Also glDrawArrays needed to have 6 passed in as everyone was correctly saying;
glDrawArrays(GL_TRIANGLES, 0, 6);

Re: OpenGL ES From the Ground Up - Vertex way off

Posted: Tue Jul 03, 2012 6:50 pm
by BBsan2k
For learning purpose (OpenGL or GL ES) I recommend using "Glut" because it does the whole OpenGL Setup for you.

Re: OpenGL ES From the Ground Up - Vertex way off

Posted: Tue Jul 03, 2012 7:12 pm
by jmacey
For learning purpose (OpenGL or GL ES) I recommend using "Glut" because it does the whole OpenGL Setup for you.
I'm afraid for OpenGL ES and the PI glut isn't that good as you need to use EGL to create a context, and if you are using ES 2.0 most of what come with glut (and free glut etc) is deprecated. At present there is no easy direct replacement for GLUT but I've written some code which you can see on my blog.

I did notice this project http://glutes.sourceforge.net/ which may be interesting to port to the pi, however I personally find glut quite evil as it forces lots of global variables and bad programming practices (I've actually banned its use in our department)

Jon

Re: OpenGL ES From the Ground Up - Vertex way off

Posted: Tue Jul 03, 2012 8:06 pm
by RichardUK
Dave if you have not already got it, buy the book OpenGL ES 2.0 Programming Guide. ISBN-13: 978-0-321-50279-7

It is very good.