OpenGL ES From the Ground Up - Vertex way off


11 posts
by Davespice » Wed Jun 27, 2012 4:59 pm
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
User avatar
Raspberry Pi Foundation Employee & Forum Moderator
Raspberry Pi Foundation Employee & Forum Moderator
Posts: 1446
Joined: Fri Oct 14, 2011 8:06 pm
Location: London, United Kingdom
by Gibble » Wed Jun 27, 2012 8:56 pm
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 ?
Posts: 56
Joined: Wed May 09, 2012 9:52 am
by Davespice » Thu Jun 28, 2012 7:33 am
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:
User avatar
Raspberry Pi Foundation Employee & Forum Moderator
Raspberry Pi Foundation Employee & Forum Moderator
Posts: 1446
Joined: Fri Oct 14, 2011 8:06 pm
Location: London, United Kingdom
by Gibble » Thu Jun 28, 2012 10:02 am
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 ?
Posts: 56
Joined: Wed May 09, 2012 9:52 am
by AndyD » Thu Jun 28, 2012 10:13 am
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.
Posts: 973
Joined: Sat Jan 21, 2012 8:13 am
Location: Melbourne, Australia
by Davespice » Thu Jun 28, 2012 11:27 am
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.
User avatar
Raspberry Pi Foundation Employee & Forum Moderator
Raspberry Pi Foundation Employee & Forum Moderator
Posts: 1446
Joined: Fri Oct 14, 2011 8:06 pm
Location: London, United Kingdom
by AndyD » Thu Jun 28, 2012 11:56 am
Hi Dave,
Please do post back a link to the working code.
Posts: 973
Joined: Sat Jan 21, 2012 8:13 am
Location: Melbourne, Australia
by Davespice » Thu Jun 28, 2012 12:08 pm
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);
User avatar
Raspberry Pi Foundation Employee & Forum Moderator
Raspberry Pi Foundation Employee & Forum Moderator
Posts: 1446
Joined: Fri Oct 14, 2011 8:06 pm
Location: London, United Kingdom
by BBsan2k » Tue Jul 03, 2012 6:50 pm
For learning purpose (OpenGL or GL ES) I recommend using "Glut" because it does the whole OpenGL Setup for you.
Posts: 27
Joined: Sat May 19, 2012 2:18 pm
by jmacey » Tue Jul 03, 2012 7:12 pm
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
Posts: 135
Joined: Thu May 31, 2012 1:05 pm
by RichardUK » Tue Jul 03, 2012 8:06 pm
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.
User avatar
Posts: 131
Joined: Fri Jun 01, 2012 5:12 pm