Question

I am trying to create a 3-D hex map. Where the hexagon's match up next and below. like this hex map

A user would enter (x) rows , (X) Columns and it would create a map like that.

I need 3d Due to i would like to be able to stack the hexagons at some point and also texture them, But now to question. How do i draw a hexagon. I am still pretty new to OpenGL but i have got this to draw a hex

   glBegin(GL_POLYGON);
for I := 0 to 6 do
begin
  glVertex3f(sin(i/6.0*2*PI),cos(i/6.0*2*pi),-2);
end;
glEnd;

not sure thats the best way and its only one face.. But also it just makes one HUGE hexagon. and of course i will need smaller ones. Any help on how to take this project on would be great! In the project i have

prxleft  = -1
prxRight = 1
prxTop = 1
prxBottom   -1
Was it helpful?

Solution

You're heading in the right direction. There are many ways to draw using OpenGL, and while folks will argue this is the the fastest way, it will get you a hexagon on the screen. To draw multiple hexagons, wrap your glBegin(GL_POLYGON) ... glEnd() snippet in a for loop that renders the appropriate number of hexagons.

To adjust the size of a hexagon, you need to adjust the values that you pass into glVertex3f. In your original post, you're drawing a hexagon that's contained in a circle of radius one (since the coefficients to your sin and cos functions is also one). If you use a smaller radius (i.e., a value less than one), you'll get a smaller hexagon.

Finally, each of your hexagons is currently centered at the origin. To move them to a different place within your viewing area of [-1,1] in x and y, merely add offsets to the x and y coordinates that you pass into glVertex3f. Since each hexagon will presumably have its own offset, you'll probably want to use an array sized for the number of hexagons, and index that by your outer loop.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top