New to OpenGL -- Transformation Help

Dash Rantic
Post: #1
Hi all,

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.

Code link:

Any help or insight on this issue would be greatly appriciated. Thank you!

Quote this message in a reply
Posts: 5,143
Joined: 2002.04
Post: #2
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.
Quote this message in a reply
Posts: 370
Joined: 2006.08
Post: #3
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

Worlds at War (Current Project) -
Quote this message in a reply
Posts: 3,570
Joined: 2003.06
Post: #4
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:

buildHex(origin_x, origin_y, origin_z, side_length); // build the hex

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.
Quote this message in a reply
Dash Rantic
Post: #5
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.

Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Transformation from unit cube to view frustum TomorrowPlusX 10 6,107 Jul 12, 2005 11:32 AM
Last Post: TomorrowPlusX
  Getting position after transformation without drawing? hyn 19 6,718 Aug 6, 2004 02:02 PM
Last Post: kberg
  Current World Transformation Matrix Phate 1 2,450 Sep 15, 2003 03:15 PM
Last Post: OneSadCookie