Simple OpenGL 2d test - nothing displaying - Printable Version
+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Graphics & Audio Programming (/forum-9.html)
+--- Thread: Simple OpenGL 2d test - nothing displaying (/thread-1903.html)
Simple OpenGL 2d test - nothing displaying - SaxMan - Jan 18, 2009 09:32 AM
I'm sure there is something simple I'm missing here, but I can't seem to spot it. I'm just trying a simple program to draw a triangle to the screen, but the display just shows black. When I put some simple debug in the drawView method, I can verify that 'drawView' is getting called, but I don't see anything drawn to the screen in the iPhone simulator.
Here's the code I'm using.
First, I'm basing the project off of the GLGravity example. In AppController, the 'setupView' method looks like this:
glEnable2D is just a 'c' function, as such
And finally, here's the 'drawView' method
Does anyone see anything obvious? My first thought is maybe with the glOrthof method, but I'm not sure....
Simple OpenGL 2d test - nothing displaying - AnotherJake - Jan 18, 2009 10:10 AM
On a glance, I don't see anything obvious except that your vertices are just a couple pixels away from each other and your projection matrix is presumably set to the size of your screen in glOrtho, which is many pixels. IOW, what you're drawing may be a tiny triangle on a big screen. Try making your vertices more spread out or use glScalef(100.0f, 100.0f, 1.0f), or something like that.
.. and I presume glEnable2D is a typo
Simple OpenGL 2d test - nothing displaying - SaxMan - Jan 18, 2009 10:51 AM
Hmm...interesting. First, what did you mean by 'glEnable2d' being a typo? Because all of the things in the 'code' blocks in my original post are copied directly from the code.
But one thing I did notice was that there was a gl error being generated I hadn't noticed before.
I tracked it down, and something isn't right with the glOrthof call, because I'm getting an openGL '501' error after that line.
I think the real problem is that the vPort array isn't being set correctly for some reason. After the call to glGetIntegerv(GL_VIEWPORT, vPort), vPort and  are both zero. That doesn't seem right....
My glEnable2D() method is in a separate .c file, but that shouldn't matter....
Simple OpenGL 2d test - nothing displaying - arekkusu - Jan 18, 2009 11:09 AM
Let's try an experiment, and follow my recent debugging tips in the OpenGL FAQ thread.
First, check for errors. API arguments looks good to me, so let's assume there aren't any.
Next, inspect the state. AnotherJake's conclusion is reasonable-- this is likely a problem with your vertex transformation. So, what's the state that could interact and cause the problem?
Here, you need to understand how transformations work, which is explained in great detail in the documentation, and examples given in the wiki. To summarize:
You can always do all of the math involved here manually to doublecheck the results you'll get-- you provided all of the input data (matrices, viewport, and vertex positions), so you can multiply it yourself for debugging.
Simple OpenGL 2d test - nothing displaying - AnotherJake - Jan 18, 2009 12:27 PM
SaxMan Wrote:My glEnable2D() method is in a separate .c file, but that shouldn't matter....
The gl prefix is reserved for the OpenGL API namespace. It's not technically "wrong" that you used it, but it is not proper coding practice, and forces us to "presume" you made a typo somehow. Without the gl prefix we can instantly identify that it is your own custom function, or at least not an OpenGL call. So it is highly recommended that you avoid using the "gl" prefix for your own functions. The only time it is acceptable is if you're actually writing a replacement for one not included in the API you're using (which might be necessary from time to time with OpenGL ES, but still unusual).
Simple OpenGL 2d test - nothing displaying - SaxMan - Jan 18, 2009 02:35 PM
Good point. I'll make sure to change that function name.
Now, as I'm tracing this bug, I'm running into a very strange problem. Part of the idea with my design is to separate as much (if not all) of the OpenGL calls to separate .c files, in order to make my openGL code as adaptable as possible, with the hope that it wouldn't be too much effort to take just the OpenGL .c files and move them between platforms and re-compile.
That being said, something strange seems to be happening to argument passing as I attempt to do this. Take this simple C function, for example:
And the following call from AppController.m (note: 'view' is an OpenGLCoreView which is passed in to setupView, where this code is located)
If I put the 'testWidthAndHeight' method in the AppController.m file, everything works fine, and I see 320 and 480 printed to the debug console. If, however, all I do is move that function to a separate .c file (graphicsFW.c) I get the following output:
width: 0.000000, height 3.812500
Any idea why this is happening?
Simple OpenGL 2d test - nothing displaying - arekkusu - Jan 18, 2009 02:42 PM
Did you create a corresponding graphicsFW.h, to declare the function protoype, and include that header everywhere you use the function?
If you fail to declare the prototype, the compiler will default the argument types to int. There should be a compiler warning about this, too.
Simple OpenGL 2d test - nothing displaying - SaxMan - Jan 18, 2009 02:51 PM
Bah. That's what happens when I'm on a new platform - I forget some of the basics
I had created the .h file, but forgotten the 'import' statement. The compiler wasn't giving me errors, but for some reason I was ignoring the 'implicit declaration' warnings.
Also, you guys were right about the size of the triangle. I adjusted the vertices and now everything shows up on the screen!
Thanks for the help....I'm sure I'll be back for more
Simple OpenGL 2d test - nothing displaying - TythosEternal - Jan 19, 2009 12:04 AM
Heh... I was going to suggest making sure you flush / swap your buffer, which seemed to slip between the cracks sometimes when I was starting out. Guess you're taking care of everything already... Good luck!