0

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
Glen Morse
  • 2,437
  • 8
  • 51
  • 102

1 Answers1

2

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.

radical7
  • 8,957
  • 3
  • 24
  • 33
  • I wouldn't even start to argue if this is fast or not, it is just outright deprecated functionality. – pmr Feb 20 '13 at 08:07
  • @GlenMorse Vertex Buffer Objects. – pmr Feb 20 '13 at 08:43
  • @radical7 i have done pretty much what you suggested. but its only giving me one row no matter what offset i put for the collumn. Anyhow i have updated code , should i add it here or make new question? – Glen Morse Feb 20 '13 at 09:02
  • @pmr Sure it's slow (with respect to graphics performance) and it's deprecated, but this is a fine way to get started with the problem. However, I suspect it's faster for Glen Morse to get something going than trying to explain VBOs, shader plumbing, and all that. If compatibility profiles were so horrible, I'm sure [NVIDIA would change their tune](http://developer.download.nvidia.com/GTC/PDF/GTC2012/PresentationPDF/SS104-Open-GPU-Utility-ToolkitGL.pdf). – radical7 Feb 20 '13 at 18:47