Problem with GL_POLYGON - (Hexagon)

Started by
34 comments, last by superpig 17 years, 2 months ago
Hey guys,

I have a problem with classes, still kind of new to C++ syntax, just moved from 5 years of Delphi (pascal)

I 'think' the class is fine actually, it's a problem with the array, or I have created an instance of the class object wrong ??

ok so heres the class for the above example CrimsonSun suggested.

struct hexCoords{	float x, y, z;	float r, g, b, a;	float u, v;};class Chexagon{public:  // Constructor  Chexagon();  // Destructor  ~Chexagon();  void draw();protected:  // This means that all the variables under this, until a new type of  //  restriction is placed, will only be accessible to other functions in the  //  class.	hexCoords center, point1, point2, point3, point4, point5, point6;};Chexagon::Chexagon(){  //Constructors can accept arguments, but this one does not}Chexagon::~Chexagon(){  //Destructors do not accept arguments}void Chexagon::draw(){	glBegin(GL_TRIANGLE_FAN);		glVertex3f(center.x, center.y, center.z);		glVertex3f(point1.x, point1.y, point1.z);		glVertex3f(point2.x, point2.y, point2.z);		glVertex3f(point3.x, point3.y, point3.z);		glVertex3f(point4.x, point4.y, point4.z);		glVertex3f(point5.x, point5.y, point5.z);		glVertex3f(point6.x, point6.y, point6.z);		glVertex3f(point1.x, point1.y, point1.z);	glEnd();}



And here is me trying to create an instance and populate the array,
with the hexagonal grid data

void genertaeHexArray(float tileSize){Chexagon hexagon[tilesPerCol][tilesPerRow];	if (tileSize <= 0.0f)	// Just an error check										{		tileSize = 1.0f;											}	// tile placement & alignment for odd/even rows	bool rowOdd = TRUE;	// Set TRUE, & will toggle FALSE/TRUE for each Row Loop (Y)	// Calculating tile offsets	// The magic number	//----------------------------------------------------------------	//The amount to budge EVERY tile in a col (except tile 0)	float colOffset = 0.866f * tileSize;	// The magic number	//The amount to budge every row (except tile 0)	float rowOffset = 0.75f * tileSize;		// The magic number	// FILL ARRAY DATA HERE	//----------------------------------------------------------------	for (float row = 0; row < tilesPerRow; row++)	// ROWS ( Y )	{						rowOdd = !rowOdd; // toggle for each row, hence in first loop		for (float col = 0; col < tilesPerCol; col++)	// COLS ( X )		{			if (rowOdd)				hexagon[col][row].point1.x = (col * colOffset) + (colOffset/2);				hexagon[col][row].point1.y = 0.0f;	// Y always 0, flat grid				hexagon[col][row].point1.z = (row * rowOffset);			else				hexagon[col][row].point1.x = (col * colOffset);				hexagon[col][row].point1.y = 0.0f;	// Y always 0, flat grid				hexagon[col][row].point1.z = (row * rowOffset);			}	}


OK, so now the problem (This seems to be 2-fold ...)

example 1 .....
this line ..
hexagon[col][row].point1.x = (col * colOffset) + (colOffset/2);

causes compile error : subscript is not of integral type

Note I origanlly declared array as
Chexagon hexagon[tilesPerCol * tilesPerRow];
and changed it too
Chexagon hexagon[tilesPerCol][tilesPerRow];
Which is the better way firstly ??

example 2 .....
this line .. (HARDCODING cols and rows to 1 & 2)
hexagon[1][2].point1.x = (col * colOffset) + (colOffset/2);

causes compile error : 'Chexagon::point1' : cannot access protected member declared in class 'Chexagon'

I knew there was a problem with the first one straight away, as it would not show available members after I typed the '.' (period)
But hardcoding array values, it worked.

What have I done wrong ??
----------------------------------------Now just hit that link that says 'Rate This User' [wink]
Advertisement
OK problem solved.

firstly, I defined
for (float row = 0; row < tilesPerRow; row++) // ROWS ( Y )
SHould of been an INT

secondly, I defined

hexCoords center, point1, point2, point3, point4, point5, point6;

under protected no public ...DOH !!

But I still wonder what is the better way to declare an array ???

myArray[100][100]

or

myArray[100 * 100]

?????
----------------------------------------Now just hit that link that says 'Rate This User' [wink]
BRAIN FART !!

Lets cover this again ...

I am creating a grid of tiles, lets say 100 (cols) x 100 (rows).
Note: grid will be flat (no Y), across the X & Z axis

so I need to store 10,000 tile co-ordinates (of x,y,z)
(although I would never try to render this much of a map at once, it should be pre-generated before render, and scroll or move round the map as desired)


THEN:
Each tile will be a hexagon (poly) made of a triangle_fan.
Hence each Hexagon Tile will require 8 points to be created.
(center vertex, the 6 points of a hexagon, then the final point to finish the fan)

SO:
for every one of the 10,000 tiles (x,y,z), I need to store an additional 8 * (x,y,z) for the hexagon points)

I'm brain farting today as the best method for this.

Should I have a GridArray[100][100] of (co-ords x,y,z)
AND
a hexArray[ (size of GridArray) ][8] of (co-ords x,y,z)

Maybe I've spent too long thinking this over, and confused the issue.
The system worked great before, I just know the best thing to improve the whole engine structure and performance, is to get this into an array (vertex array) prior to render.

Display lists seem to be out and a poor option.
And since the grid is pretty much static, there is no point doing this every frame.

Although I can select only a portion of the grid/map to render as desired, move, scroll, or even teleport to a location, with out having to recalulate everything.

and yes culling etc will help the frame rate and perfomance, but the point is to avoid unrequired calls at runtime.

** EDIT **

For those that don't want to read the whole 2 pages of previous posts, just take a look at one of the screen shots, and you'll realize, apart from the excessive amount of calls required for this.
Every vertex is rendered 3 times, as every hexagon, touches 3 others (except the outside rows of course)

Although CrimsonSuns idea is GREAT, to build an array prior to runtime(render even) I would still have to render 10,000 hexagons and the same vertex 3 times over for each one.
(forget the culling etc, lets assume I want to see the whole map for some reason, and require all 10,000 rendering)

**********

Wireframe :


Colored :


**********

Cheers

[Edited by - darren_mfuk on March 1, 2007 8:56:04 PM]
----------------------------------------Now just hit that link that says 'Rate This User' [wink]
Has anyone actually seen successful examples of hexagonal tiling in this forum ?

I found a few articles on the thoery, and much on mouse and world handling events.
But not really much on the rendering issue.

Any ideas on this one guys ??

I know, I'm keen to get this baby rolling.. plus I have time on my hands right now (wooohoo no work). just want to make the most of the next couple of days before good old 'Monday' comes knocking again.

Cheers ;-)
----------------------------------------Now just hit that link that says 'Rate This User' [wink]
Quote:Original post by darren_mfuk
SO:
for every one of the 10,000 tiles (x,y,z), I need to store an additional 8 * (x,y,z) for the hexagon points)


10,000 * 8 = 80,000 * 3 = 240,000 floats?

I'm confused as to what you are saying/trying to do. Why not just draw the same hexagons over again and again?

Read this again:
Quote:Original post by CrimsonSun
As for vertex arrays, I think the performance benefit you'll receive from using them will be quite negligible. How many hexagons do you intend to render on a screen? The hexagons will have to be large enough for the player to understand anything inside of the hexagons. So if you're rendering a 10x10 grid of hexagons on each screen, the worst case you'll be rendering is 10*10*8*3 = 2400 vertices...which is not that much.


Suppose you had an array of your textures to texture you hexagons.
int Tiles[100 * 100] or int Tiles[100][100]

Every scene you draw the same hexagons, at the same spot on the screen. And pass your x and y location to draw, and the texture ID. (Or just the texture ID, and store only the 100 hexagons' info)
int Tile[100][100];Chexagon Hexagon[10][10];for(int x = 0; x < 10; x++){   for(int z = 0; z < 10; z++)   {      Hexagon.draw(x*tilesize,z*tilesize, Tile[CameraX + x][CameraZ + z]);      //OR... If you store the X,Z in each hexagon class      Hexagon[x][z].draw(Tile[CameraX + x][CameraZ + z]);   }}void Chexagon::draw(float x, float z, int textureID){   //...}


Maybe I'm missing what you are asking altogether.
You may want to look into a technique known as "indexed vertices." Basically, rather than specifying the vertex coordinates over and over, you just lay out one set of coordinates, and then say "ok, this triangle uses vertices #1, #3, and #7; this one uses #2, #3, #6," and so on. It minimizes the amount of data stored by stopping you from repeating vertex data for neighbouring polygons - as well as ensuring that it will always be pixel-perfectly aligned.

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

This topic is closed to new replies.

Advertisement