Advertisement

Faulty Triangles Direction in Plane, Help Please

Started by October 20, 2018 05:19 PM
6 comments, last by Josheir 6 years, 3 months ago

I have rewritten this function a few times now and I am having trouble getting every other row to have the triangles face the reverse direction.  The image enclosed demonstrates the faulty behavior which includes only one row facing the reversed direction.  

 I am also confused by how many indexes I should have for the g_vertex_buffer_data_land variable, could someone show me a breakdown like : 18 vertices times 2 sets  times 8 columns times 8 depth.

In another post fleabay mentioned I was not setting a VAO in core profile, however It seems to be.  

here is the code:

 


float* getVertices(void) {

	//using defines
	int incol = _colus;
	int depth = _depth;

	
	int i = 0;
	
	
	

	float scaleit = .5;
	float tempdepth = 0;
	
	int startindexat = 0;
	int counter = 0;
	int secondcounter = 0;


	
		//for (; (tempdepth+1) <= (depth);)
		//don't forget to change this back!
		for (int q=0;q<3;q++)
		{
		


		//odd rows
		for (int col = 0; (col+1) <= (incol ); col++)
		{

		
			GLfloat matrix1[3][3] = { {(col + 1),0,(tempdepth)},{ (col),0,(tempdepth)}, {(col),0,(tempdepth + 1) } };

		//	//vertex 1
			g_vertex_buffer_data_land[startindexat + 0 + counter] = matrix1[0][0] * scaleit;
			g_vertex_buffer_data_land[startindexat + 1 + counter] = matrix1[0][1] * scaleit;
			g_vertex_buffer_data_land[startindexat + 2 + counter] = matrix1[0][2] * scaleit;
			//vertex 2
			g_vertex_buffer_data_land[startindexat + 3 + counter] = matrix1[1][0] * scaleit;
			g_vertex_buffer_data_land[startindexat + 4 + counter] = matrix1[1][1] * scaleit;
			g_vertex_buffer_data_land[startindexat + 5 + counter] = matrix1[1][2] * scaleit;

			g_vertex_buffer_data_land[startindexat + 6 + counter] = matrix1[2][0] * scaleit;
			g_vertex_buffer_data_land[startindexat + 7 + counter] = matrix1[2][1] * scaleit;
			g_vertex_buffer_data_land[startindexat + 8 + counter] = matrix1[2][2] * scaleit;

			int matrix2[3][3] = { { (col + 1),0,(tempdepth + 1)},{ (col + 1),0,(tempdepth)}, {(col),0,(tempdepth + 1) } };


			g_vertex_buffer_data_land[startindexat + 9 + counter] = matrix2[0][0] * scaleit;
			g_vertex_buffer_data_land[startindexat + 10 + counter] = matrix2[0][1] * scaleit;
			g_vertex_buffer_data_land[startindexat + 11 + counter] = matrix2[0][2] * scaleit;

			g_vertex_buffer_data_land[startindexat + 12 + counter] = matrix2[1][0] * scaleit;
			g_vertex_buffer_data_land[startindexat + 13 + counter] = matrix2[1][1] * scaleit;
			g_vertex_buffer_data_land[startindexat + 14 + counter] = matrix2[1][2] * scaleit;

			g_vertex_buffer_data_land[startindexat + 15 + counter] = matrix2[2][0] * scaleit;
			g_vertex_buffer_data_land[startindexat + 16 + counter] = matrix2[2][1] * scaleit;
			g_vertex_buffer_data_land[startindexat + 17 + counter] = matrix2[2][2] * scaleit;


			counter = counter + 18;
		}//end col
		

		startindexat =  17 + counter+ 1;
		

		for (int col2 = 0; (col2+1) <= (incol); col2++)
		{
			

			//first triangle : even rows
			GLfloat matrix3[3][3] = { {(col2 + 1) ,0,(tempdepth + 2)} , {(col2 + 1),0,(tempdepth + 1)}, {(col2),0,(tempdepth +1)} };




				//		//vertex 1
				g_vertex_buffer_data_land[(startindexat + secondcounter)] = matrix3[0][0] * scaleit;
				g_vertex_buffer_data_land[(startindexat + 1 + secondcounter)] = matrix3[0][1] * scaleit;
				g_vertex_buffer_data_land[(startindexat + 2 + secondcounter)] = matrix3[0][2] * scaleit;
				//vertex 2
				g_vertex_buffer_data_land[(startindexat + 3 + secondcounter)] = matrix3[1][0] * scaleit;
				g_vertex_buffer_data_land[(startindexat + 4 + secondcounter)] = matrix3[1][1] * scaleit;
				g_vertex_buffer_data_land[(startindexat + 5 + secondcounter)] = matrix3[1][2] * scaleit;

				g_vertex_buffer_data_land[(startindexat + 6 + secondcounter)] = matrix3[2][0] * scaleit;
				g_vertex_buffer_data_land[(startindexat + 7 + secondcounter)] = matrix3[2][1] * scaleit;
				g_vertex_buffer_data_land[(startindexat + 8 + secondcounter)] = matrix3[2][2] * scaleit;


				
				//		even (2)
				int matrix4[3][3] = { {(col2 + 1),0,(tempdepth+ 2)},{ (col2),0,(tempdepth+ 1)}, {(col2),0,(tempdepth + 2) } };


				g_vertex_buffer_data_land[(startindexat+9 + secondcounter)] = matrix4[0][0] * scaleit;
				g_vertex_buffer_data_land[(startindexat+10 + secondcounter)] = matrix4[0][1] * scaleit;
				g_vertex_buffer_data_land[(startindexat+11 + secondcounter)] = matrix4[0][2] * scaleit;

				g_vertex_buffer_data_land[(startindexat+12 + secondcounter)] = matrix4[1][0] * scaleit;
				g_vertex_buffer_data_land[(startindexat+13 + secondcounter)] = matrix4[1][1] * scaleit;
				g_vertex_buffer_data_land[(startindexat+14 + secondcounter)] = matrix4[1][2] * scaleit;

				g_vertex_buffer_data_land[(startindexat+15 + secondcounter)] = matrix4[2][0] * scaleit;
				g_vertex_buffer_data_land[(startindexat+16 + secondcounter)] = matrix4[2][1] * scaleit;
				g_vertex_buffer_data_land[(startindexat+17 + secondcounter)] = matrix4[2][2] * scaleit;


				//one column of 4 triangles 
				//(three vetices per triangle)
				secondcounter = secondcounter + 18;
			}
			
				startindexat =  17 + secondcounter + 1;
		
	
		tempdepth = tempdepth - 1;

}







	return gvertices;
}

 

I am hoping someone might have the experience to help me solve this problem.  Or, what could I check and do I need to show more repo code? 

 

Thank you,

Josheir

problemimage.png

I hesitate to ask this because it's tangential, but could you repost the code with different formatting? For me at least, consistent 2- or 4-space indentation and less vertical whitespace would make it easier to read.

Advertisement

Sure, I'll repost it later this Sunday.

Josheir

Here is the code reformatted.  If there is even more cleanup to attempt please let me know (exactly) too, this is how I learn!


void drawWorld(void) {
	//using defines
	int incol = _colus;
	int depth = _depth;
	float scaleit = .5;
	float tempdepth = 0;
	int startindexat = 0;
	int counter = 0;
	int secondcounter = 0;
    //for (; (tempdepth+1) <= (depth);)
	//don't forget to change this back!
	//this is the depth to draw the next two rows of triangles across the screen
	for (int q=0;q<3;q++)
	{
		//odd rows.  Draws a row of double triangles across the screen
		for (int col = 0; (col + 1) <= (incol); col++)
		{
			GLfloat matrix1[3][3] = { {(col + 1),0,(tempdepth)},{ (col),0,(tempdepth)}, {(col),0,(tempdepth + 1) } };
			//vertex 1
			g_vertex_buffer_data_land[startindexat + 0 + counter] = matrix1[0][0] * scaleit;
			g_vertex_buffer_data_land[startindexat + 1 + counter] = matrix1[0][1] * scaleit;
			g_vertex_buffer_data_land[startindexat + 2 + counter] = matrix1[0][2] * scaleit;
			//vertex 2
			g_vertex_buffer_data_land[startindexat + 3 + counter] = matrix1[1][0] * scaleit;
			g_vertex_buffer_data_land[startindexat + 4 + counter] = matrix1[1][1] * scaleit;
			g_vertex_buffer_data_land[startindexat + 5 + counter] = matrix1[1][2] * scaleit;
			//vertex3
			g_vertex_buffer_data_land[startindexat + 6 + counter] = matrix1[2][0] * scaleit;
			g_vertex_buffer_data_land[startindexat + 7 + counter] = matrix1[2][1] * scaleit;
			g_vertex_buffer_data_land[startindexat + 8 + counter] = matrix1[2][2] * scaleit;

			int matrix2[3][3] = { { (col + 1),0,(tempdepth + 1)},{ (col + 1),0,(tempdepth)}, {(col),0,(tempdepth + 1) } };
			g_vertex_buffer_data_land[startindexat + 9 + counter] = matrix2[0][0] * scaleit;
			g_vertex_buffer_data_land[startindexat + 10 + counter] = matrix2[0][1] * scaleit;
			g_vertex_buffer_data_land[startindexat + 11 + counter] = matrix2[0][2] * scaleit;
			
			g_vertex_buffer_data_land[startindexat + 12 + counter] = matrix2[1][0] * scaleit;
			g_vertex_buffer_data_land[startindexat + 13 + counter] = matrix2[1][1] * scaleit;
			g_vertex_buffer_data_land[startindexat + 14 + counter] = matrix2[1][2] * scaleit;

			g_vertex_buffer_data_land[startindexat + 15 + counter] = matrix2[2][0] * scaleit;
			g_vertex_buffer_data_land[startindexat + 16 + counter] = matrix2[2][1] * scaleit;
			g_vertex_buffer_data_land[startindexat + 17 + counter] = matrix2[2][2] * scaleit;
			counter = counter + 18;
		}
		startindexat =  17 + counter+ 1;
		for (int col2 = 0; (col2+1) <= (incol); col2++)
		{
			//first triangle : even rows
			GLfloat matrix3[3][3] = { {(col2 + 1) ,0,(tempdepth + 2)} , {(col2 + 1),0,(tempdepth + 1)}, {(col2),0,(tempdepth +1)} };
			
			g_vertex_buffer_data_land[(startindexat + secondcounter)] = matrix3[0][0] * scaleit;
			g_vertex_buffer_data_land[(startindexat + 1 + secondcounter)] = matrix3[0][1] * scaleit;
			g_vertex_buffer_data_land[(startindexat + 2 + secondcounter)] = matrix3[0][2] * scaleit;
			
			g_vertex_buffer_data_land[(startindexat + 3 + secondcounter)] = matrix3[1][0] * scaleit;
			g_vertex_buffer_data_land[(startindexat + 4 + secondcounter)] = matrix3[1][1] * scaleit;
			g_vertex_buffer_data_land[(startindexat + 5 + secondcounter)] = matrix3[1][2] * scaleit;

			g_vertex_buffer_data_land[(startindexat + 6 + secondcounter)] = matrix3[2][0] * scaleit;
			g_vertex_buffer_data_land[(startindexat + 7 + secondcounter)] = matrix3[2][1] * scaleit;
			g_vertex_buffer_data_land[(startindexat + 8 + secondcounter)] = matrix3[2][2] * scaleit;
			//even rows, second triangle
			int matrix4[3][3] = { {(col2 + 1),0,(tempdepth+ 2)},{ (col2),0,(tempdepth+ 1)}, {(col2),0,(tempdepth + 2) } };
			
			g_vertex_buffer_data_land[(startindexat+9 + secondcounter)] = matrix4[0][0] * scaleit;
			g_vertex_buffer_data_land[(startindexat+10 + secondcounter)] = matrix4[0][1] * scaleit;
			g_vertex_buffer_data_land[(startindexat+11 + secondcounter)] = matrix4[0][2] * scaleit;

			g_vertex_buffer_data_land[(startindexat+12 + secondcounter)] = matrix4[1][0] * scaleit;
			g_vertex_buffer_data_land[(startindexat+13 + secondcounter)] = matrix4[1][1] * scaleit;
			g_vertex_buffer_data_land[(startindexat+14 + secondcounter)] = matrix4[1][2] * scaleit;

			g_vertex_buffer_data_land[(startindexat+15 + secondcounter)] = matrix4[2][0] * scaleit;
			g_vertex_buffer_data_land[(startindexat+16 + secondcounter)] = matrix4[2][1] * scaleit;
			g_vertex_buffer_data_land[(startindexat+17 + secondcounter)] = matrix4[2][2] * scaleit;
			secondcounter = secondcounter + 18;
		}
		startindexat =  17 + secondcounter + 1;
		tempdepth = tempdepth - 1;
	}
	
}

Thank you,

Josheir

I have an example of how to do what you are trying to accomplish. This example is from the book OpenGL Development Cookbook (2013)


int count = 0;
int i=0, j=0;

for( j=0;j<=NUM_Z;j++) 
{
	for( i=0;i<=NUM_X;i++) 
	{
		vertices[count++] = glm::vec3(
		((float(i)/(NUM_X-1)) *2-1)* HALF_SIZE_X, 0,
		((float(j)/(NUM_Z-1))*2-1)*HALF_SIZE_Z);
	}
}

GLushort* id=&indices[0];

for (i = 0; i < NUM_Z; i++) 
{
	for (j = 0; j < NUM_X; j++) 
	{
		int i0 = i * (NUM_X+1) + j;
		int i1 = i0 + 1;
		int i2 = i0 + (NUM_X+1);
		int i3 = i2 + 1;
		
		if ((j+i)%2) 
		{
		*id++ = i0; *id++ = i2; *id++ = i1;
		*id++ = i1; *id++ = i2; *id++ = i3;
		} else {
		*id++ = i0; *id++ = i2; *id++ = i3;
		*id++ = i0; *id++ = i3; *id++ = i1;
		}
	}
}

"We generate a simple 3D planar grid in the XZ plane. The geometry is stored in the vertices
global array. The total number of vertices on the X axis is stored in a global constant NUM_X,
whereas the total number of vertices on the Z axis is stored in another global constant NUM_Z.
The size of the planar grid in world space is stored in two global constants, SIZE_X and
SIZE_Z, and half of these values are stored in the HALF_SIZE_X and HALF_SIZE_Z global
constants. Using these constants, we can change the mesh resolution and world space size.


The loop simply iterates (NUM_X+1)*(NUM_Z+1) times and remaps the current vertex
index first into the 0 to 1 range and then into the -1 to 1 range, and finally multiplies it
by the HALF_SIZE_X and HALF_SIZE_Z constants to get the range from –HALF_SIZE_X
to HALF_SIZE_X and –HALF_SIZE_Z to HALF_SIZE_Z.

In order to alternate the triangle directions and maintain their winding order, we take two
different combinations, one for an even iteration and second for an odd iteration."

*** EDIT: I added about 10 lines at the top of the code block for the base geometry.

🙂🙂🙂🙂🙂<←The tone posse, ready for action.

I forgot the code for the base geometry at the top. I update the code. Sorry about that.

🙂🙂🙂🙂🙂<←The tone posse, ready for action.

Advertisement

Alas, there are better ways, and I could really benefit with help with my attempt.

Josheir

 

This topic is closed to new replies.

Advertisement