New to OpenGL -- Transformation Help

Dash Rantic
Unregistered
 
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:
http://stellarprism.com/basic_hex.cpp

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

-Dash
Quote this message in a reply
Luminary
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
Moderator
Posts: 373
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
-wyrmmage

Worlds at War (Current Project) - http://www.awkward-games.com/forum/
Quote this message in a reply
Moderator
Posts: 3,572
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:

Code:
glPushMatrix();
buildHex(origin_x, origin_y, origin_z, side_length); // build the hex
glPopMatrix();

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
Unregistered
 
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.

-Dash
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,436 Jul 12, 2005 11:32 AM
Last Post: TomorrowPlusX
  Getting position after transformation without drawing? hyn 19 6,958 Aug 6, 2004 02:02 PM
Last Post: kberg
  Current World Transformation Matrix Phate 1 2,534 Sep 15, 2003 03:15 PM
Last Post: OneSadCookie