Skip to main content
GameDev.net gamedev.net
🔒 Locked

Texturing a HexTile

Started by Jannes Jul 1, 2004 at 9:30 AM 9 replies 700+ views
Original Post
Jannes
Jannes
Hi, how do u create hextiles with Direct3D? I was thinking about defining a LineStrip VertexBuffer with 6 Vertices (actually 7 cause the last one needs to be same as the first). Then lay a texture on it. Is this possible, or are just triangles supported? How would u solve this? Thx in advance :)
Agony
Agony
Just use a few triangles. GPUs are optimized for triangles, since all other polygons can be represented as a group of triangles anyway. You could get away with 4 triangles per hex:
    o-----o   /|   / |\   o |  /  | o   \| /   |/    o-----o

You'll have to do a wee bit of calculation to figure out the texture coordinates, and the texture, unless you stretch it in areas, is going to have unused portions, outside of the hexagonal region.

Alternatively, you might try using a quad, and drawing your tiles as basic sprites, and when creating the textures themselves, be sure to add transparency to all the regions of the image outside of the hexagon.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas nly, and not for things themselves." - John Locke o
Telastyn
Telastyn
I didn't. I cheated. Instead of making my hexes hexes, I made them squares with every other row offset by 1/2 a square:

x x x x x x x xx x x x




So every tile has 6 neighbors, but rendering and figuring out what tile is clicked on becomes much simpiler.
Jannes
Jannes
i came to another solution:

Using a triangle fan of 6 triangles. Each side of these triangles has same lenght. Now i try implement this...
Jannes
Jannes
i coded a bit, but it doesnt seem to work, look at this:

#define HEXDIMENSION 32struct HEXAGONVERTEX {	float x;	float y;	float z;	float rhw;	D3DCOLOR  color; 	float tu,tv; //TextureCoords};


HEXAGONVERTEX* pPointVertices ;hexVB->Lock(0, 0, (VOID**)&pPointVertices, 0);//first Vertex (Center):pPointVertices[0].x = position.x - 0.5f;pPointVertices[0].y = position.y - 0.5f;pPointVertices[0].tu = 0.5f;pPointVertices[0].tv = 0.5f;//second Vertex:pPointVertices[1].x = position.x - 0.5f;pPointVertices[1].y = position.y + (float)(HEXDIMENSION / 2) -0.5f;pPointVertices[1].tu = 0.5f;pPointVertices[1].tv = 0.0f;//third Vertex:pPointVertices[2].x = position.x + (float)(HEXDIMENSION / 2) - 0.5f;pPointVertices[2].y = position.y + (float)(HEXDIMENSION / 4) - 0.5f;pPointVertices[2].tu = 1.0f;pPointVertices[2].tv = 0.25f;//fourth Vertex:pPointVertices[3].x = position.x + (float)(HEXDIMENSION / 2) - 0.5f;pPointVertices[3].y = position.y - (float)(HEXDIMENSION / 4) - 0.5f;pPointVertices[3].tu = 1.0f;pPointVertices[3].tv = 0.75f;//fifth Vertex:pPointVertices[4].x = position.x - 0.5f;pPointVertices[4].y = position.y - (float)(HEXDIMENSION / 2) - 0.5f;pPointVertices[4].tu = 0.5f;pPointVertices[4].tv = 1.0f;//sixth Vertex:pPointVertices[5].x = position.x - (float)(HEXDIMENSION / 2) - 0.5f;pPointVertices[5].y = position.y - (float)(HEXDIMENSION / 4) - 0.5f;pPointVertices[5].tu = 0.0f;pPointVertices[5].tv = 0.75f;//seventh Vertex:pPointVertices[6].x = position.x - (float)(HEXDIMENSION / 2) - 0.5f;pPointVertices[6].y = position.y + (float)(HEXDIMENSION / 4) - 0.5f;pPointVertices[6].tu = 0.0f;pPointVertices[6].tv = 0.25f;//last Vertex same as second:pPointVertices[7] = pPointVertices[1];//Values for all Vertices:for(int i = 0; i < 8; i++){ pPointVertices.z = position.z; //set to 0.0f pPointVertices.color = D3DCOLOR_XRGB(255,255,255); pPointVertices.rhw = 1.0f;}hexVB->Unlock();


Doesnt draw anything on screen.

[Edited by - Jannes on July 1, 2004 3:11:08 PM]
Feral
Feral
Why 6 tris for a triangle fan? Or rather, why not 4 tris..

    o-----o   /|   /  \   o | /___--o   \|/-    /    o-----o


i.e.

    o-----o   /|3  /2 \   o4| /___--o   \|/- 1   /    o-----o


Gah, fear the ascii art! [totally]
Supernat02
Supernat02
Can you post your code used to draw it? Also, what is HEXDIMENSION defined as?

EDIT: OOPS, nevermind I see it now.

Chris

[Edited by - Supernat02 on July 2, 2004 6:49:25 PM]
Chris ByersMicrosoft DirectX MVP - 2005
Jannes
Jannes
Quote:
Original post by FeralOfEclecticWizard
Why 6 tris for a triangle fan? Or rather, why not 4 tris..


Thx for the info, i will try this later

Quote:
Original post by Supernat02
Can you post your code used to draw it? Also, what is HEXDIMENSION defined as?


HEXDIMENSION is defined as 32 (its in the first code snippet ;))
I am using the same drawing code for a textured quad, where all works well. So i dont think the error is in there.
Jannes
Jannes
Now, it works :). Thx alot
DrunkenHyena
DrunkenHyena
If you use Fans then you'll only be able to draw a single hex at a time. Using the quad approach you could batch many hexes together and get a nice performance boost.

Even if you don't need the performance now, you can do single quad draws for now and later if needed it will be easier to convert to something more optimal.

Just a suggestion.
Stay Casual,KenDrunken Hyena</FON
Jannes
Jannes
I prefered the trifan attempt, cause i want to make a mapeditor which is open to public. The mapdesigner does not have to care about transparency and the hex geometry this way, if he wants to make his own tiletemplates. But thx for your info, i will think about it. (and read about batching. didnt get so far yet)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.