I want to learn OpenGL, but all tutorials I find about it on the net use an extra library which I don't want to learn... I have noticed Allegro 5 has the ability to integrate OpenGL, so I wanted to use it.
Now I've made a program that, in my view, should produce a white triangle facing the viewer. But all the program outputs is a black screen...
Here's the code:
I looked at ex_opengl.c(it compiled and ran fine) but it was way too complicated for me to figure out what it did... I just want a single triangle drawn for now.
I ran this trough the debugger and I saw that the al_flip_display call is getting reached. I also tried feeding other coordinates to the glVertex3f calls(including negative z), but for no avail...
Any ideas?
You haven't set a projection.
(e.g. void glOrtho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble nearVal, GLdouble farVal);)
You haven't set the viewport: void glViewport(GLint x, GLint y, GLsizei width, GLsizei height)
Thanks ^^
EDIT:
I tried putting this in render(), but I still get the same output ._.
Sorry for me being so n00bish, but I really want to understand how I can make OpenGL work...
Have you looked at allegro-5blahblah/examples/ex_opengl.c?
Yes I did(see my first post), but I can't really understand what's happening in the drawing function... The OpenGL reference page isn't really helpful at understanding it(WTH are vertical/horizontal clipping panes used in glOrtho???).
Does somebody have some code lying around to just show how you can setup an allegro opengl screen and draw a simple polygon(e.g. a triangle/quad) to it?
EDIT: Am I right that glViewport positions the camera? Or is it's effect the opposite(change the coordinates at which everything is drawn)?
EDIT: Am I right that glViewport positions the camera? Or is it's effect the opposite(change the coordinates at which everything is drawn)?
glViewport changes how "wide" the scene is, to position the camera you'd do a glTranslate(x,y,z) and glRotatef(x,y,z) or similar (my internets too broken to google properly).
Maybe this would help (uses gluLookAt())
Swiped from glfw demo program.
See here: http://www.opengl.org/sdk/docs/man/xhtml/glViewport.xml
As it says there, it's window coordinates. In your case you want: glViewport(0, 0, 800, 600); (Note that this is the default so you can leave out the call if you want.)
As for glOrtho: http://www.opengl.org/sdk/docs/man/xhtml/glOrtho.xml
You can't use OpenGL without understanding at least most of that. Your glOrtho call looks fine though.
EDIT: It just doesn't work ._. I'm starting to get fired up about this, since this code seems like it should work, but it doesn't... I just want a plain white triangle across my screen. Here's my latest attempt(can someone try and test this to make sure it doesn't have anything to do with my comp?):
Your viewport is definitely wrong, and I don't think you want your ortho transform like that either. Try this:
glViewport(0,0,800,600); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0,800,600,0,-200,200); glClearColor(0,0,0,1);
Also, if you keep depth testing enabled, you need to clear the depth buffer.
Fixed, but still nothing to see
@Elias: Is this done like this?
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
If so, I already did this, but for no avail...
Well after my changes I do see something... not sure it's what you indended but I see a white triangle, thin down the middle of the screen.
I don't see it... What coordinates should I give it to let it fill almost the whole/half of the screen? I already tried this:
glVertex3f(0.0f,0.0f,0.0f); glVertex3f(800.0f,0.0f,0.0f); glVertex3f(800.0f,600.0f,0.0f);
But it didn't work out. I thought it would fill up half the screen... what's wrong with my understanding of coordinates?
That should work, providing you don't have face culling on or something like that. And that you clear the depth buffer or turn it off.
I haven't called glEnable(GL_CULL_FACE) so I don't think face culling is enabled. I also tried commenting out the call to glEnable(GL_DEPTH_BUFFER) and deleting the glClear(GL_DEPTH_BUFFER_BIT), but again: nothing to be seen ._.
EDIT: wait a minute... I see a small white dot in the upper-left corner of my application, 1 pixel is lit
EDIT2: This only happens when I set the coordinates of the last two points to this or greater:
glVertex3f(0.0f,0.0f,0.0f); glVertex3f(800.0f,0.0f,0.0f); glVertex3f(800.0f,600.0f,0.0f);
If you didn't fix your viewport or ortho transform nothing's going to work.
I fixed the argument order already...
FIXED: I forgot to put glLoadIdentity() after glMatrixMode(GL_PROJECTION) facepalm, so the matrix was probably filled with garbage ._.
Output:
{"name":"604240","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/6\/0\/606655c858c99c3ba968be97f8840a45.png","w":814,"h":639,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/6\/0\/606655c858c99c3ba968be97f8840a45"}
Here's the full code again:
Now make that triangle properly counter-clockwise.
What do you mean with counter-clockwise?
This?
{"name":"604241","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/a\/7\/a70bf49d9c1e63febcd246a9ceecf75a.png","w":814,"h":635,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/a\/7\/a70bf49d9c1e63febcd246a9ceecf75a"}
I mean defining your vertices in a counter-clockwise rather than clockwise order:
In the picture above, that would be e.g. (A, B, C), instead of (A, C, B).
By convention (inherited from mathematics), a face's vertices are ordered counter-clockwise (you can change this in OpenGL, but don't). This allows you to tell which way a triangle is facing - the (A, B, C) triangle would be facing towards you, while the (A, C, B) triangle would be facing away from you. This allows you to cull the away-facing triangles. Which you want to do, even for trivial projects (there's no additional cost to it, and it cuts the number of triangles you have to render roughly in half).
Hi... My example of opengl, is simple, but maybe help you in something!
Picture: http://i55.tinypic.com/29yp01t.jpg
A red triangle in the center of the window:
@gnolam: understood
@JA: I already made a program that draws a triangle... I just had some problems setting up OpenGL
Quick question about OpenGL in Allegro. Can you combine drawing from both libraries. Like say I'm using allegro bitmaps to make my 2d space game, can I use OpenGL just for lighting effects. Or say use 2d bitmaps for everything in my game except perhaps a sun which I'd draw using OpenGL. Can you combine this way?
You can AFAIK... But I don't think the lighting will have an effect on the 2d bitmaps drawn with allegro...
EDIT: I don't seem to get it to work... I put al_draw_rectangle(300,200,400,250,al_map_rgb(255,255,255)); both in front and after the first/last openGL call and before al_flip_display(); but it doesn't show anything ._.(my background is 0,0,0 and the window is 800x600)
If you modify the matrices you need to store them first and then restore again before calling Allegro commands. Same for any other OpenGL state. If you do that it will work.
@Elias: do you mean that allegro drawing routines change/use OpenGL matrices?
Of course, all drawing is done using OpenGL (if you use OpenGL mode).