GPWiki.org
GPWiki.org
It is currently Tue May 21, 2013 7:49 pm

All times are UTC




Post new topic Reply to topic  [ 22 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Making a 3d hex grid
PostPosted: Sun Mar 10, 2013 4:04 pm 
Prolific Poster

Joined: Sun Mar 10, 2013 3:32 pm
Posts: 23
Hello. I know there is a ton of info on hex grids. But no good example or tutorial and I am having alot of trouble with this. Yes I am pretty new to OpenGL. But was hopping for a little help. First off I am using freeglut and glew Also using c++ but might be able to convert code of another language.

If anyone can help me make a small hex grid i would owe you my first born :)
Here is the basic layout I need.

8x8 3d hex grid

Why. Well I plan on adding more like user defined columns and widths. Stackable hex's for elevation and sooner or later making it into a game.

I figure if I could see the code needed to make the grid I would understand it and be able to edit it to my needs. Just every time i try I get ugly or slow results. So can anyone give me some help? or am I asking for alot? Cause I would think it would be pretty easy. But yet I know I have not been able to do it. So maybe it's alot of work to get something like this done?
Cheers
Squills


Top
 Profile  
 
 Post subject: Re: Making a 3d hex grid
PostPosted: Sun Mar 10, 2013 4:57 pm 
Lord of Cheesecakes

Joined: Sun Jun 24, 2012 12:49 am
Posts: 335
Read this:
http://www.gamedev.net/page/resources/_ ... maps-r1800

Rendering 3D hexagons is pretty straight forward if you understand the fundamentals of primitives (you should).
Just wrap around the perimeter with a zig-zag strip-like triangle pattern and draw the top face using a chinese fan -like triangle pattern.

Do you need any help with that?

I haven't used OpenGL for almost 2 years, but I'll try to start you off with a very rudimentary implementation:

Code:
void RenderHexagon(float radius, float height)
{
   static const float angleIncrement = 2 * M_PI / 6;
   float angle = 0.0f;
   
   // Perimeter (Zig-zag strip pattern):
   glBegin(GL_TRIANGLE_STRIP);
   for (uint v = 0; v != 7; ++v)
   {
      float x = cos(angle) * radius;
      float y = -sin(angle) * radius;
      glVertex3f(x, y, 0.0f);
      glVertex3f(x, y, height);
      angle += angleIncrement;
   }
   glEnd();
   
   angle = 0.0f;
   
   // Top face (Chinese fan pattern):
   glBegin(GL_TRIANGLE_FAN);
   for (uint v = 0; v != 6; ++v)
   {
      glVertex3f(cos(angle) * radius, -sin(angle) * radius, height);
      angle += angleIncrement;
   }
   glEnd();
}

As you learn more about OpenGL, eventually at some point you should realize much better ways to handle geometry than this (rather than with the glBegin/glEnd cheesecakes).

_________________
What most people don't understand is what I like to explain as a thing that I understand.


Last edited by Pieman on Sun Mar 10, 2013 5:44 pm, edited 5 times in total.

Top
 Profile  
 
 Post subject: Re: Making a 3d hex grid
PostPosted: Sun Mar 10, 2013 5:21 pm 
Prolific Poster

Joined: Sun Mar 10, 2013 3:32 pm
Posts: 23
I know it sounds easy. I probably after I see how it's done it will be easier for me to understand. But yes I need help with that.

- create hex at top left
...how to create the hex?
... I know I need to create 2 at different z points and connect them with rectangles
- and loop till row = 8
- go down 1 hex and add offset
- loop till Row = 8




I got everything I could want. But still I just can't do it lol. I sm missing something easy and I just think if I seen the code it would click. I think a issue I am having is I started with a 2d hex map. Using a different language. And now I think it just confused me even more lol

Would it be possible to give code to make this hex grid?
Cheers
Squills


Top
 Profile  
 
 Post subject: Re: Making a 3d hex grid
PostPosted: Sun Mar 10, 2013 5:28 pm 
Lord of Cheesecakes

Joined: Sun Jun 24, 2012 12:49 am
Posts: 335
Quote:
Would it be possible to give code to make this hex grid?


Code:
for (uint duck = 0; duck != duckQuantity; ++duck)
{
   duckArray[duck].Quack();
}

MakeHexgrid();

_________________
What most people don't understand is what I like to explain as a thing that I understand.


Top
 Profile  
 
 Post subject: Re: Making a 3d hex grid
PostPosted: Sun Mar 10, 2013 6:39 pm 
Lord of Cheesecakes

Joined: Sun Jun 24, 2012 12:49 am
Posts: 335
Code:
struct Hexagon
{
   float height;
   // Other attributes ...
};

void RenderHexRow(float originX, float originY, float radius, Hexagon* row, uint columns, float xStride)
{
   static const float angleIncrement = 2 * M_PI / 6;
   static const float edgeXc = cos(angleIncrement);
   static const float edgeYc = sin(angleIncrement);

   float edgeX = edgeXc * radius;
   float edgeY = edgeYc * radius;

   for (uint c = 0; c != columns; ++c)
   {
      // Perimeter (Zig-zag strip pattern):
      glBegin(GL_TRIANGLE_STRIP);
      
      float a = originX + radius;
      glVertex3f(a, originY, 0.0f);
      glVertex3f(a, originY, row[c].height);
      
      a = originX + edgeX;
      float b = originY - edgeY;
      glVertex3f(a, b, 0.0f);
      glVertex3f(a, b, row[c].height);
      
      a = originX - edgeX;
      glVertex3f(a, b, 0.0f);
      glVertex3f(a, b, row[c].height);
      
      b = originX - radius;
      glVertex3f(b, originY, 0.0f);
      glVertex3f(b, originY, row[c].height);
      
      b = originY + edgeY;
      glVertex3f(a, b, 0.0f);
      glVertex3f(a, b, row[c].height);
      
      a = originX + edgeX;
      glVertex3f(a, b, 0.0f);
      glVertex3f(a, b, row[c].height);
      
      a = originX + radius;
      glVertex3f(a, originY, 0.0f);
      glVertex3f(a, originY, row[c].height);
      glEnd();

      // Top face (Chinese fan pattern):
      glBegin(GL_TRIANGLE_FAN);
      glVertex3f(a, originY, row[c].height);
      
      b = originY - edgeY;
      glVertex3f(originX + edgeX, b, row[c].height);
      
      a = originX - edgeX;
      glVertex3f(a, b, row[c].height);
      
      glVertex3f(originX - radius, originY, row[c].height);
      
      b = originY + edgeY;
      glVertex3f(a, b, row[c].height);
      
      glVertex3f(originX + edgeX, b, row[c].height);
      glEnd();
      
      originX += xStride;
   }
}

void RenderHexGrid(float originX, float originY, float radius, Hexagon** hexgrid, uint rows, uint columns)
{
   static const float angleIncrement = 2 * M_PI / 6;
   
   float xStride = radius * 2;
   float yStride = sin(angleIncrement) * xStride;
   
   float originX2 = originX + radius;
   float originY2 = originY + yStride;
   yStride *= 2;

   for (uint r = 0; r < rows; r += 2)
   {
      RenderHexRow(originX, originY, radius, hexgrid[r], columns, xStride);
      originY += yStride;
   }
   
   for (uint r = 1; r < rows; r += 2)
   {
      RenderHexRow(originX2, originY2, radius, hexgrid[r], columns, xStride);
      originY2 += yStride;
   }
}


In the future, you should look into these:

http://www.songho.ca/opengl/gl_vertexarray.html
https://www.opengl.org/wiki/Vertex_Specification
http://www.songho.ca/opengl/gl_vbo.html

_________________
What most people don't understand is what I like to explain as a thing that I understand.


Last edited by Pieman on Mon Mar 11, 2013 12:05 am, edited 2 times in total.

Top
 Profile  
 
 Post subject: Re: Making a 3d hex grid
PostPosted: Sun Mar 10, 2013 6:45 pm 
Prolific Poster

Joined: Sun Mar 10, 2013 3:32 pm
Posts: 23
Awesome. Still on phone and code don't show up right. But will def give this a look at when u get home. And will update thread. Thanks again.


Top
 Profile  
 
 Post subject: Re: Making a 3d hex grid
PostPosted: Sun Mar 10, 2013 6:47 pm 
Lord of Cheesecakes

Joined: Sun Jun 24, 2012 12:49 am
Posts: 335
Alright, cool stuff. Have fun and good luck, Squills. Make some awesome games! 8)

_________________
What most people don't understand is what I like to explain as a thing that I understand.


Top
 Profile  
 
 Post subject: Re: Making a 3d hex grid
PostPosted: Sun Mar 10, 2013 8:47 pm 
Prolific Poster

Joined: Sun Mar 10, 2013 3:32 pm
Posts: 23
Ok. A question

What header is needed for
uint
M_PI


For now I changed it to int. And just did a define for PI


Second. How do I call the function. Example I tried
RenderHexGrid(1.0,1.0,2.0,?????,8,8);

No idea what to do to get the ??? Part.
I guess I need to create a Hexagon var?
Thus setting the height. ?


Cheers
Squills


Top
 Profile  
 
 Post subject: Re: Making a 3d hex grid
PostPosted: Sun Mar 10, 2013 9:05 pm 
Lord of Cheesecakes

Joined: Sun Jun 24, 2012 12:49 am
Posts: 335
uint just means "unsigned int"
so you can just do:

Code:
#define uint unsigned int

Replace 2 * M_PI with tau, and define tau like this:

Code:
#define tau 6.283185307179586476925286766559

To create a hexagon grid, you can do this:

Code:
const uint rows = 8;
const uint columns = 8;
Hexagon grid[rows][columns];


You will probably want to give the Hexagon struct a default constructor, for example:

Code:
struct Hexagon
{
   Hexagon();
   float height;
};

Hexagon::Hexagon():
   height(1.0f)
{}

_________________
What most people don't understand is what I like to explain as a thing that I understand.


Top
 Profile  
 
 Post subject: Re: Making a 3d hex grid
PostPosted: Sun Mar 10, 2013 9:36 pm 
Prolific Poster

Joined: Sun Mar 10, 2013 3:32 pm
Posts: 23
Lol starting to feel stupid.

Ok I added the constructor

The const uint rows ..... Part. I add that befor main() correct

Then in my display function I still don't know how to call RenderHexGrid(); function. I tried
RenderHexGrid(1.0,1.0,2.0,grid,8,8);


Top
 Profile  
 
 Post subject: Re: Making a 3d hex grid
PostPosted: Sun Mar 10, 2013 11:58 pm 
Lord of Cheesecakes

Joined: Sun Jun 24, 2012 12:49 am
Posts: 335
Try this:
Code:
RenderHexGrid(0.0f, 0.0f, 2.0f, (Hexagon**)grid, rows, columns);

(note: there's also another thing, I forgot to declare the type of the edgeX and edgeY variables. Just put 'float' behind both of them.) So, this is how the beginning of your RenderHexRow function should look like now:

Code:
void RenderHexRow(float originX, float originY, float radius, Hexagon* row, uint columns, float xStride)
{
   static const float angleIncrement = tau / 6;
   static const float edgeXc = cos(angleIncrement);
   static const float edgeYc = sin(angleIncrement);

   float edgeX = edgeXc * radius;
   float edgeY = edgeYc * radius;

_________________
What most people don't understand is what I like to explain as a thing that I understand.


Top
 Profile  
 
 Post subject: Re: Making a 3d hex grid
PostPosted: Mon Mar 11, 2013 12:18 am 
Lord of Cheesecakes

Joined: Sun Jun 24, 2012 12:49 am
Posts: 335
Quote:
If anyone can help me make a small hex grid i would owe you my first born :)

By the way, after we (hopefully) get this working, when can I have your first born? Go make some babies! *relax, I'm joking* :lol

_________________
What most people don't understand is what I like to explain as a thing that I understand.


Top
 Profile  
 
 Post subject: Re: Making a 3d hex grid
PostPosted: Mon Mar 11, 2013 3:53 am 
Prolific Poster

Joined: Sun Mar 10, 2013 3:32 pm
Posts: 23
Quote:
when can I have your first born?


nono, a deal is a deal!

ok so i had to get some sleep , but just tried the code again.. this time it compiles.. as i had forgot to use ( ) before ...

but it does not show anything and gives errors at
Code:
row[c].height



its showing ????? as height, but up top for the
Code:
hexagon grid[rows][columns];


it is showing 1.0f as height.
I am going to give it a shot and try some things, but want a full copy of what i have so far :D

Code:
// Game.cpp : Defines the entry point for the console application.
//



#include "stdafx.h"
#include <GL\glew.h>
#include <GL\freeglut.h>
#include <math.h>

#define tau 6.283185307179586476925286766559
#define uint unsigned int

struct hexagon
{
   hexagon();
   float height ;
   //other attributes...
};

hexagon::hexagon() :
   height(1.0f)
   {

   }

   const uint rows = 18;
   const uint columns = 18;
   hexagon grid[rows][columns];




void RenderHexRow(float originX, float originY, float radius, hexagon* row, uint columns, float xStride)
{
   static const float angleIncrement = tau / 6;
   static const float edgXc = cos(angleIncrement);
   static const float edgYc = sin(angleIncrement);


   float edgeX = edgXc * radius;
   float edgeY = edgYc * radius;


   for (int c = 0; c!= columns; ++c)
   {
      // Perimeter (zig-zag srip pattern)
      glBegin(GL_TRIANGLE_STRIP);
      
      float a = originX + radius;
      glVertex3f(a, originY, 0.0f);
      glVertex3f(a, originY, row[c].height);

      a = originX + edgeX;
      float b = originY - edgeY;
      glVertex3f(a, b, 0.0f);
      glVertex3f(a, b, row[c].height);

      a = originX - edgeX;
      glVertex3f(a, b, 0.0f);
      glVertex3f(a, b, row[c].height);

      b = originX - radius;
      glVertex3f(b, originY, 0.0f);
      glVertex3f(b, originY, row[c].height);

      b = originY + edgeY;
      glVertex3f(a, b, 0.0f);
      glVertex3f(a, b, row[c].height);

      a = originX + edgeX;
      glVertex3f(a, b, 0.0f);
      glVertex3f(a, b, row[c].height);

      a = originX + radius;
      glVertex3f(b, originY, 0.0f);
      glVertex3f(b, originY, row[c].height);
      
      glEnd();

      //top face
      glBegin(GL_TRIANGLE_FAN);
      glVertex3f(a, originY, row[c].height);

      b = originY - edgeY;
      glVertex3f(originX + edgeX, b, row[c].height);

      a = originX - edgeX;
      glVertex3f(a, b, row[c].height);

      glVertex3f(originX - radius, originY, row[c].height);

      b = originY + edgeY;
      glVertex3f(a, b, row[c].height);

      glVertex3f(originX + edgeX, b, row[c].height);
      glEnd();

      originX += xStride;
   }
}

void RenderHexGrid(float originX, float originY, float radius, hexagon** hexgrid, uint rows, uint columns)
{
   static const float angleIncrement = 2 * tau / 6;

   float xStride = radius * 2;
   float yStride = sin(angleIncrement) * xStride;

   float originX2 = originX + radius;
   float originY2 = originY + yStride;
   yStride *= 2;

   for (int r = 0; r < rows; r += 2)
   {
      RenderHexRow(originX, originY, radius, hexgrid[r],columns,xStride);
      originY += yStride;
   }

   for (int r = 1; r< rows; r+= 2)
   {
      RenderHexRow(originX2, originY2, radius, hexgrid[r],columns,xStride);
      originY2 += yStride;
   }
}


      

void display(void)
{
   glClearColor(1.f,0.f,0.f,1.f);
   glClear(GL_COLOR_BUFFER_BIT);
   glLoadIdentity();
   glFlush();
      
   
}



int _tmain(int argc, CHAR **argv)
{
   glutInit(&argc, argv);
   glutInitDisplayMode (GLUT_SINGLE);
   glutInitWindowSize(500,500);
   glutInitWindowPosition(100,100);
   glutCreateWindow("Your first opengl Window");



      glutDisplayFunc(display);

    RenderHexGrid(0.0f,0.0f,0.5f,(hexagon**)grid,rows,columns);
   glutMainLoop();
   return 0;
}


Top
 Profile  
 
 Post subject: Re: Making a 3d hex grid
PostPosted: Mon Mar 11, 2013 4:02 am 
Lord of Cheesecakes

Joined: Sun Jun 24, 2012 12:49 am
Posts: 335
Have you tried to init the grid within the main function? That's the problem I see. Also, you're calling the RenderHexGrid function in a weird place... wtf. :eek

_________________
What most people don't understand is what I like to explain as a thing that I understand.


Top
 Profile  
 
 Post subject: Re: Making a 3d hex grid
PostPosted: Mon Mar 11, 2013 4:42 am 
Prolific Poster

Joined: Sun Mar 10, 2013 3:32 pm
Posts: 23
lol, yea i was calling RenderHexGrid(); like so..

Code:
void display(void)
{
   glClearColor(1.f,0.f,0.f,1.f);
   glClear(GL_COLOR_BUFFER_BIT);

        RenderHexGrid();    //hereeeeeeeeeee

   glLoadIdentity();
   glFlush();
      
   
}


but it would just give me access errors while it was running.. when i put it outside this, then it started to show the height error..

i will try adding
hexagon grid[rows][columns] inside main() as of now i cant.. at work and cant compile...lol

Thanks though.. :thumbs


Top
 Profile  
 
 Post subject: Re: Making a 3d hex grid
PostPosted: Mon Mar 11, 2013 4:55 am 
Lord of Cheesecakes

Joined: Sun Jun 24, 2012 12:49 am
Posts: 335
Try it in the form of this:

Code:
Hexagon** globalGrid = 0;
const uint rows = 18;
const uint columns = 18;

void Display()
{
   // Stuff
   RenderHexGrid(0.0f, 0.0f, 0.5f, globalGrid, rows, columns);
}

int main()
{
   globalGrid = new Hexagon* [rows];
   for (uint r = 0; r != rows; ++r)
   {
      globalGrid[r] = new Hexagon[columns];
   }

   // Stuff ...

   for (uint r = 0; r != rows; ++r)
   {
      delete [] globalGrid[r];
   }

   delete [] globalGrid;

   return 0;
}


Here's a screenshot. As you can see, it didn't turn out exactly as I expected. Now realizing a stupid mistake I've made, I will fix it tomorrow, but I need to go to bed:

Image

_________________
What most people don't understand is what I like to explain as a thing that I understand.


Last edited by Pieman on Mon Mar 11, 2013 5:11 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Making a 3d hex grid
PostPosted: Mon Mar 11, 2013 5:08 am 
Prolific Poster

Joined: Sun Mar 10, 2013 3:32 pm
Posts: 23
my brain hurts, lets see i spent at least 6 hours on just this today.. and over 30 hours researching hex's and there forumla's .. 2 days refreshing on C++ (book was a bit dry) I say refreshing cause as you can tell, i have not used c++ in over 6 years.. back at collage. and before that i spent 2 weeks on a OpenGL book... which is out of date of course... as it seems like everybook is

So I really have been trying.. I am just to the point now where i want to see a result lol.. Last 6-7 years i have been using Delphi , which is alot different then c++ .... not so much the logic but the syntax which is usually my problem. Dont get me wrong the whole double pointer thing threw me for a loop. But i do understand it now..

But reasons like , Why cant you make hexagon grid[rows][columns] a global.. ( i assume thats where i have it)

While its not a good idea to do it ( in delphi) it would still run.. So please do bare with me as i reteach the syntax .. but do know i am trying. After all i was able to go threw the compiler and see it was missing a height value... but now that i think about it... usually an access error is due to something not being created.. in this case "grid" due to it not being in main()... correct?

Anyhow thanks again... DLing mingw just so i can try the code agian here at work .. shhhh dont tell them.

cheers
squills


Top
 Profile  
 
 Post subject: Re: Making a 3d hex grid
PostPosted: Mon Mar 11, 2013 5:20 pm 
Lord of Cheesecakes

Joined: Sun Jun 24, 2012 12:49 am
Posts: 335
Happy birthday!

Image

Here you go.

_________________
What most people don't understand is what I like to explain as a thing that I understand.


Top
 Profile  
 
 Post subject: Re: Making a 3d hex grid
PostPosted: Mon Mar 11, 2013 10:02 pm 
Prolific Poster

Joined: Sun Mar 10, 2013 3:32 pm
Posts: 23
Hehe. It was my birthday!


Well done. Not sure what is up with the random height or. The fruity colors. Lol. But it lookscorrect. I plan on using a texture on each hex. And mouse enabled click will add another hex on top

Thanks for help


Top
 Profile  
 
 Post subject: Re: Making a 3d hex grid
PostPosted: Mon Mar 25, 2013 2:18 am 
Lord of Cheesecakes

Joined: Sun Jun 24, 2012 12:49 am
Posts: 335
Hey Squills, I'm just checking in on this project. How's progress? If you need help with anything or you have any questions, please talk! :)

_________________
What most people don't understand is what I like to explain as a thing that I understand.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 22 posts ]  Go to page 1, 2  Next

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group