Advertisement Jump to content
Sign in to follow this  
Mellkor

Pointer, array and struct problem?..

This topic is 1802 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, 

 

Experimenting more with c++, but have encountered a problem i cant solve. (driving me mad lol)

The program runs perfectly with this code, Drawing a Triangle.

struct Vertex
{
   unsigned int color;
   float x, y, z;
};


struct Vertex vertices[3]= 
{
       {0xFF0000FF, -0.0f, -0.0f, 0.0f}, 
       {0xffffff00, 0.0f, 50.0f, 0.0f}, 
       {0xFF00FF00, 50.0f, 0.0f, 0.0f}, 
       
       //{0xffffff00, 0.0f, 50.0f, 0.0f},
       //{0xffffff00, 50.0f, 50.0f, 0.0f},
       //{0xffffff00, 50.0f, 00.0f, 0.0f}
};

vertices are passed off to the drawcall at the end, Everything works fine.

 

But i'm trying to "build" the verticies in a loop. The goal is to eventually build a tilegrid out of the vertices, and push them all at the drawcall. 

 

I Can get the solution to compile but, alas no triangle.

struct Vertex
{
   unsigned int color;
   float x, y, z;
};


struct Vertex vertices[3]; 
struct Vertex *pVerticies = &vertices[0];

void BuildVerts(struct Vertex *pVerticies)
{
	int i;
	for (i = 0; i < 1; i++)
	{
		
		(pVerticies+i)->color = 0xFF0000FF;
		(pVerticies+i)->x = 0.0f;
		(pVerticies+i)->y = 0.0f;
		(pVerticies+i)->z = 0.0f;
		
		(pVerticies+i)->color = 0xFF0000FF;
		(pVerticies+i)->x = 0.0f;
		(pVerticies+i)->y = 50.0f;
		(pVerticies)->z = 0.0f;

		(pVerticies+i)->color = 0xFF0000FF;
		(pVerticies+i)->x = 50.0f;
		(pVerticies+i)->y = 0.0f;
		(pVerticies+i)->z = 0.0f;
	}
}


int main(int argc, char* argv[])
{
	
	BuildVerts(pVerticies);
...

I suspect that i'm not using pointers correctly or i need to initialize array elements 'inline' like ie verticies = {..,}; ?

Edited by Mellkor

Share this post


Link to post
Share on other sites
Advertisement

Hi, 

 

Experimenting more with c++, but have encountered a problem i cant solve. (driving me mad lol)

The program runs perfectly with this code, Drawing a Triangle.

struct Vertex
{
   unsigned int color;
   float x, y, z;
};


struct Vertex vertices[3]= 
{
       {0xFF0000FF, -0.0f, -0.0f, 0.0f}, 
       {0xffffff00, 0.0f, 50.0f, 0.0f}, 
       {0xFF00FF00, 50.0f, 0.0f, 0.0f}, 
       
       //{0xffffff00, 0.0f, 50.0f, 0.0f},
       //{0xffffff00, 50.0f, 50.0f, 0.0f},
       //{0xffffff00, 50.0f, 00.0f, 0.0f}
};

vertices are passed off to the drawcall at the end, Everything works fine.

 

But i'm trying to "build" the verticies in a loop. The goal is to eventually build a tilegrid out of the vertices, and push them all at the drawcall. 

 

I Can get the solution to compile but, alas no triangle.

struct Vertex
{
   unsigned int color;
   float x, y, z;
};


struct Vertex vertices[3]; 
struct Vertex *pVerticies = &vertices[0];

void BuildVerts(struct Vertex *pVerticies)
{
	int i;
	//---not used---for (i = 0; i < 1; i++)
	{
		
		(pVerticies)->color = 0xFF0000FF;
		(pVerticies)->x = 0.0f;
		(pVerticies)->y = 0.0f;
		(pVerticies)->z = 0.0f;
		
		(pVerticies+1)->color = 0xFF0000FF;
		(pVerticies+1)->x = 0.0f;
		(pVerticies+1)->y = 50.0f;
		(pVerticies+1)->z = 0.0f;

		(pVerticies+2)->color = 0xFF0000FF;
		(pVerticies+2)->x = 50.0f;
		(pVerticies+2)->y = 0.0f;
		(pVerticies+2)->z = 0.0f;
	}
}


int main(int argc, char* argv[])
{
	
	BuildVerts(pVerticies);
...

I suspect that i'm not using pointers correctly or i need to initialize array elements 'inline' like ie verticies = {..,}; ?

Changes inlined above. You might want to do a tutorial on pointers and arrays to get more familiar with how they work. You might also want to learn how to read the vertex data in from a file instead. Most intro-level tutorials for OpenGL or DirectX go over that process. I can't tell if you're using OpenGL or DX though, so I won't put a bunch of links yet.

 

Edit: apparently code blocks don't allow strikethrough and such. Shame.

Edited by richardurich

Share this post


Link to post
Share on other sites

Your for loop only execute once... so you're effectively assigning to vertices[0] 3 times.

struct Vertex
{
   unsigned int color;
   float x, y, z;
};

Vertex vertices[3]; 
Vertex *pVerticies = &vertices[0];

float xVal[3] = {0.0f, 0.0f, 50.0f};
float yVal[3] = {0.0f, 50.0f, 0.0f};
float zVal[3] = {0.0f, 0.0f, 0.0f};

void BuildVerts(Vertex *pVerticies, int NumVerts)
{
	int i;
	for (i = 0; i < NumVerts; i++)
	{
		pVerticies[i]->color = 0xFF0000FF;
		pVerticies[i]->x = xVal[i];
		pVerticies[i]->y = yVal[i];
		pVerticies[i]->z = zVal[i];
	}
}

int main(int argc, char* argv[])
{
    
    BuildVerts(pVerticies, 3);
...
}

I removed the useless structs statements, fixed the loop counter and the pointers array indexes. I dont really like this code(by that i just mean it would be better to load the vertices from a file) but for a simple test it's perfectly fine. I noticed i should have used your Vertex structure to hard code the vertice but this work too.

 

EDIT: damn... ninja'ed! smile.png

 

EDIT2: That line

Vertex *pVerticies = &vertices[0];

is totally useless. Try this instead:

    BuildVerts(&vertices[0], 3);

Edited by Vortez

Share this post


Link to post
Share on other sites

 

Your for loop only execute once... so you're effectively assigning to vertices[0] 3 times.

struct Vertex
{
   unsigned int color;
   float x, y, z;
};

Vertex vertices[3]; 
Vertex *pVerticies = &vertices[0];

float xVal[3] = {0.0f, 0.0f, 50.0f};
float yVal[3] = {0.0f, 50.0f, 0.0f};
float zVal[3] = {0.0f, 0.0f, 0.0f};

void BuildVerts(Vertex *pVerticies, int NumVerts)
{
	int i;
	for (i = 0; i < NumVerts; i++)
	{
		pVerticies[i]->color = 0xFF0000FF;
		pVerticies[i]->x = xVal[i];
		pVerticies[i]->y = yVal[i];
		pVerticies[i]->z = zVal[i];
	}
}

int main(int argc, char* argv[])
{
    
    BuildVerts(pVerticies, 3);
...
}

I removed the useless structs statements, fixed the loop counter and the pointers array indexes. I dont really like this code(by that i just mean it would be better to load the vertices from a file) but for a simple test it's perfectly fine. I noticed i should have used your Vertex structure to hard code the vertice but this work too.

 

EDIT: damn... ninja'ed! smile.png

 

EDIT2: That line

Vertex *pVerticies = &vertices[0];

is totally useless. Try this instead:

    BuildVerts(&vertices[0], 3);

 

 

Thanks for your reply!, Going through your code now. Getting some build errors atm.

 

"error : expected '=', ',', ';', 'asm' or '__attribute__' before 'vertices' "?

Edit: fixed the error by putting back in the "struct" - program now compiles and draws a single vertical line down the screen lol.

 

 

 

Question: Curious about the theory you used to here, They are defined as type INT, but the member uses a float arguement? - 

int xVal[3] = {0.0f, 0.0f, 50.0f};
int yVal[3] = {0.0f, 50.0f, 50.0f};
int zVal[3] = {0.0f, 0.0f, 50.0f};
Edited by Mellkor

Share this post


Link to post
Share on other sites

Sorry, i did not compile the code before posting, just replace "pVerticies->" by "pVerticies." and it should compile. Also, remove the "..." in main too, of course smile.png

 

Are you compiling in c++ or c?

Edited by Vortez

Share this post


Link to post
Share on other sites


Sorry, i did not compile the code before posting, just replace "pVerticies->" by "pVerticies." and it should compile. Also, remove the "..." in main too, of course smile.png

 

Are you compiling in c++ or c?

 

Using c++ and gcc, It works and compiles now. Thanks a bunch 

 

After actually reading and understanding the code(learnt some new tricks), it wasn't quite what i was after i was hoping to define a triangle with 3 vertices and a color.

and use an iteration for loop to make more triangles, not make a triangle out of an iteration of vertices like you posted smile.png

 

Something like this maybe:

for (int i = 1; i < 6; i++)
{
         //Orthographic triangle
	 {0xFF0000FF, -0.0f*i, -0.0f*i, 0.0f}, 
	 {0xffffff00, 0.0f*i, 50.0f*i, 0.0f}, 
         {0xFF00FF00, 50.0f*i, 0.0f*i, 0.0f}, 
}

If you have time to offer some more assistance that would be excellent, if not thanks again smile.png

Edited by Mellkor

Share this post


Link to post
Share on other sites

Why the minus sign in -0.0f?

 

Also, if you want to create triangles using a formula, that's fine, but that formula will create a small triangle inside a larger triangle i think, so you might need something a little different, or all you'll see is 1 triangle with z-fighting in some part of it.

 

As for the integer issue, just a typo of my part.

Edited by Vortez

Share this post


Link to post
Share on other sites
struct Vertex
{
   unsigned int color;
   float x, y, z;
};


struct Vertex vertices[6 * 3]; 

void BuildVerts(struct Vertex *pVerticies)
{
	int i;
	for (i = 0; i < 3 * 6; i+=3)//increment by 3 (vertices per triangle)
	{
		
		(pVerticies+i)->color = 0xFF0000FF;
		(pVerticies+i)->x = 0.0f*i;
		(pVerticies+i)->y = 0.0f*i;
		(pVerticies+i)->z = 0.0f;
		
		(pVerticies+i+1)->color = 0xFF0000FF;
		(pVerticies+i+1)->x = 0.0f*i;
		(pVerticies+i+1)->y = 50.0f*i;
		(pVerticies+i+1)->z = 0.0f;

		(pVerticies+i+2)->color = 0xFF0000FF;
		(pVerticies+i+2)->x = 50.0f*i;
		(pVerticies+i+2)->y = 0.0f*i;
		(pVerticies+i+2)->z = 0.0f;
	}
}


int main(int argc, char* argv[])
{
	
	BuildVerts(vertices);
...

You don't need the struct in "void BuildVerts(struct Vertex *pVerticies)" if you're using C++, so gcc must think you want it compiled as C code for some reason. You might want to make sure your file ends in .cpp, call g++ instead of gcc, or do whatever is needed to make sure you're building in C++ instead of C if that's what you intend.

 

You can also use pVertices.x instead of (pVertices+i)->x and such if you find it easier to think of as an array.

Share this post


Link to post
Share on other sites

Humm, i just understood what you mean by re-reading your post, you're trying to build a grid of triangles/quads, right?

 

It should look more or less like this:

struct Vertex
{
   UINT  color;
   float x,y,z;
};

Vertex *pVerts   = NULL;
UINT   *pIndices = NULL;

// 101x101 vertices will create 100x100 squares tiles...
const int GridWidth  = 101; 
const int GridHeight = 101; 

void BuildGrid()
{
	// Don't forget to do this before the app. finish too, or use a vector instead
	if(pVerts){delete [] pVerts;}
	if(pIndices){delete [] pIndices;}

	// Calculate the number of cells
	int NumCells = GridWidth * GridHeight;

	// Allocate n cells
	pVerts = new Vertex[NumCells];
	// Set allocated memory to zero
	ZeroMemory(pVerts, NumCells * sizeof(Vertex));
	
	// Build the grid
	for(int y = 0; y < GridHeight; y++){
		for(int x = 0; x < GridWidth; x++){
	
			int Indx = (y * GridWidth) + x;

			pVerts[Indx].color = 0xFF0000FF;

			pVerts[Indx].x = x;
			pVerts[Indx].y = y;
		}
	}

	// Now, allocate the index array 
	// (so we reuse vertices instead of duplicating them)

	// This allocate 6 indices per tiles (2 triangles = 6 vertices)
	int NumIndices = ((GridWidth-1) * (GridHeight-1)) * 6;
	pIndices = new UINT[NumIndices];
	// Set allocated memory to zero
	ZeroMemory(pIndices, NumIndices * sizeof(UINT));

	// Now, connect the vertices to form triangles

	int i = 0;
	for(int y = 0; y < GridHeight-1; y++){
		for(int x = 0; x < GridWidth-1; x++){
	
			int Indx = (y * GridWidth) + x;
			
			pIndices[i]   = Indx;
			pIndices[i+1] = Indx+1;
			pIndices[i+2] = Indx+GridWidth;

			pIndices[i+3] = Indx+GridWidth;
			pIndices[i+4] = Indx+1;
			pIndices[i+5] = Indx+GridWidth+1;

			i += 6;
		}
	}
}

I know it's a lot of code im trowing at you, especially the bottom part, but that's how it is. I haven't compiled the code but it should give you a good start.

Note that you could use a mix of the upper and lower part and only use a Vertex array, but it's a waste of memory and speed.

 

If you have any problem, let me know.

Edited by Vortez

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!