New to OpenGL -- Transformation Help - Printable Version
+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Graphics & Audio Programming (/forum-9.html)
+--- Thread: New to OpenGL -- Transformation Help (/thread-2730.html)
New to OpenGL -- Transformation Help - Dash Rantic - Mar 9, 2008 10:06 AM
I'm pretty new to OpenGL, and have gotten stuck on something I'm trying to work out. What I'm trying to do is create a very basic hex map--2D, all one color, just very simple for now.
I've got everything setup, but when I run my program, it appears to just have randomly scattered my hexes around. Now, I output all the coordinates that my hexes *should* be trying to build at, and those coordinates are correct. So, I'm assuming my problem is with the glTraslatef I have (line 45, link to my code is below). I think the error is within my buildHex function (starts line 41). I'm running the glTranslatef(x, y, z); before glBegin, in where I build the individual hexagon.
Now, according to most of the tutorials I've seen, it seems I need to run a glLoadIdentity() before calling that translate function, but if I do, it clears everything off the screen and nothing is dispayed. As it is now, it scatters the hexes seemingly at random across the screen.
Any help or insight on this issue would be greatly appriciated. Thank you!
New to OpenGL -- Transformation Help - OneSadCookie - Mar 9, 2008 12:34 PM
glTranslate is relative to previous translations, so all your translations are piling up.
You want to wrap your hex drawing in glPushMatrix/glPopMatrix to stop this happening.
New to OpenGL -- Transformation Help - wyrmmage - Mar 9, 2008 12:43 PM
well, I'm not an OpenGL guru, and I just briefly looked at your code, but I see a few things that might fix your problem:
Why are you drawing vertice #0? Since you're drawing a polygon, it has to be convex...what you're drawing essentially does this for the outline of the polygon:
draw a line between 0-1, 1-2, 2-3, 3-4, 4-5, 5-6, 6-7, 7-0. This means that you're going to have an unnecessary line (or two lines, since it draws once between 0 and 7, and once between 7 and 0), and this may even mean that OpenGL is interpreting your polygon as concave, instead of convex, which is seriously going to mess up things.
I could definitely be wrong here, though XD
New to OpenGL -- Transformation Help - AnotherJake - Mar 9, 2008 03:32 PM
wyrmmage is right, you don't need to put in the first vertex of your hexagons. You can use glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); to see more clearly what is being drawn.
Specifically what OSC was talking about, you'll need to do:
What that does is just do your translations inside buildHex only temporarily for that hex, on top of the existing transform(s). Otherwise, every time you wanted to draw something you'd have to load the identity matrix and multiply all the transforms for everything over and over. glPush/Pop makes it easy and efficient to multiply a temporary transform and then pop it to back up to where it was without having to redo everything up to that point again.
New to OpenGL -- Transformation Help - Dash Rantic - Mar 9, 2008 06:08 PM
Hey hey, thank you all! I've got my basic map working now. AnotherJake, thanks for the glPolygonModel tip--made this much easier. And thanks wyrmmage, for pointing out I didn't need that Vertex #0--I guess I left that in there because when I was calculating it out by hand at first, having that original point made things a bit easier. I didn't realize that could cause problems, though.