Archived

This topic is now archived and is closed to further replies.

NeXius

Changing Polygon mode with Vertex Arrays

Recommended Posts

Hey I'm working on this terrain engine, and just recently I converted it to vertex arrays, for better framerate... The map itself loads great, but now I can't change to wireframe mode or even apply a fog to it... Any idea why? Here's some of the source in case you still can't figure it out: Any criticisms of my code at all are welcome... I'm still new.
    

void ReadInRawFile(char* fileName, int size, BYTE* file_pt)
{
	FILE*	file;

	file = fopen(fileName, "rb");
	if (!file)
	{
		return;
	}
	fread(file_pt, 1, size, file);

	fclose(file);
}

int LoadTerrain()
{
	BYTE*	image_data = new BYTE[MAP_SIZE*MAP_SIZE];
	float*	heightMap = new float[VERTICES_PER_ROW*VERTICES_PER_ROW];
	int		index=0, h_index=0;
	int		x, y;

	// read in terrain image

	ReadInRawFile("data/Terrain.raw", MAP_SIZE*MAP_SIZE, image_data);

	// get the correct number of heights in the correct order

	for (y=0; y < MAP_SIZE; y += STEP_SIZE)
	{
		for (x=0; x < MAP_SIZE; x+= STEP_SIZE)
		{
			heightMap[index] = (float) image_data[x + (y * MAP_SIZE)];

			index++;
		}
	}

	// calculate the position of each vertex

	for (y=0; y < VERTICES_PER_ROW*8; y+=8)
	{
		for (x=0; x < VERTICES_PER_ROW*8; x+=8)
		{
			index = x + (y * VERTICES_PER_ROW);

			// Texture Coordinates

			terrain_data[index  ] = (float) x / (float) (VERTICES_PER_ROW*8);
			terrain_data[index+1] = (float) y / (float) (VERTICES_PER_ROW*8);

			// Normal Coordinates

			terrain_data[index+2] = 0;
			terrain_data[index+3] = 1.0f;
			terrain_data[index+4] = 0;

			// Vertex Coordinates

			terrain_data[index+5] = (float) x * (float) (STEP_SIZE/8);
			terrain_data[index+6] = heightMap[h_index];
			terrain_data[index+7] = (float) y * (float) (STEP_SIZE/8);

			h_index++;
		}
	}

	index = 0;

	// calculate the order by which to draw them

	// we're going to be using a triangle strip

	// so it should be done (x0, y0), (x1, y0),

	// (x0, y1), (x1, y1), (x0, y2) ...


	for (x=0; x < VERTICES_PER_ROW; x++)
	{
		for (y=0; y < VERTICES_PER_ROW; y++)
		{
			t_indices[index] = (x+1) + (y * VERTICES_PER_ROW);
			index++;
			t_indices[index] = x + (y * VERTICES_PER_ROW);
			index++;
		}

		x++;

		if (x >= VERTICES_PER_ROW)
			break;

		for (y=VERTICES_PER_ROW-1; y >= 0; y--)
		{
			t_indices[index] = x + (y * VERTICES_PER_ROW);
			index++;
			t_indices[index] = (x+1) + (y * VERTICES_PER_ROW);
			index++;
		}
	}

	// Free resources


	if (image_data)
		free(image_data);

	if (heightMap)
		free(heightMap);

	return true;
}

void initObjects()
{
	heading = 0.0f;

	walk_speed = 15.0f;

	camera.pos.x = MAP_SIZE * X_SCALE / 2;
	camera.pos.y = 400.0f;
	camera.pos.z = MAP_SIZE * Z_SCALE / 2;
}

int InitGL(GLvoid)										// All Setup For OpenGL Goes Here

{
	if (!LoadTerrain())
	{
		return false;
	}

	glInterleavedArrays(GL_T2F_N3F_V3F, 0, terrain_data);

	if (!LoadTexture("data/grass.bmp", texture[0]))
	{
		return false;
	}

	glShadeModel(GL_SMOOTH);							// Enable Smooth Shading

	glEnable(GL_TEXTURE_2D);
	glClearDepth(1.0f);									// Depth Buffer Setup

	glEnable(GL_DEPTH_TEST);							// Enables Depth Testing

	glDepthFunc(GL_LEQUAL);								// The Type Of Depth Testing To Do

	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);	// Really Nice Perspective Calculations


	if (FOG_ENABLED)
	{
		// Turn on fog

		glEnable(GL_FOG);						

		// Pick a grey color for our fog with a full alpha

		float fogColor[4] = {0.5f, 0.5f, 0.5f, 1.0f};

		glFogi(GL_FOG_MODE, GL_EXP);
		glFogfv(GL_FOG_COLOR, fogColor);
		glFogf(GL_FOG_DENSITY, 0.0007f);
		glHint(GL_FOG_HINT, GL_DONT_CARE);
		glFogf(GL_FOG_START, 0.0f);
		glFogf(GL_FOG_END, 7000.0f);
		glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
	}
	else
	{
		glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
	}

	initObjects();

	return TRUE;
}

int DrawGLScene(GLvoid)
{
	// Clear The Screen And The Depth Buffer

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	

	glLoadIdentity();

	glRotatef(pitch, 1.0f, 0, 0);
	glRotatef(heading, 0, 1.0f, 0);
	glRotatef(roll, 0, 0, 1.0f);

	glTranslatef(-camera.pos.x, -camera.pos.y, -camera.pos.z);

	glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

	glScalef(X_SCALE, Y_SCALE, Z_SCALE);

	// For a triangle strip, the number of indices is double 

	// the amount of vertices, minus the number of vertices 

	// in the first and last rows.

	glDrawElements(GL_TRIANGLE_STRIP, 2*VERTICES_PER_ROW*VERTICES_PER_ROW-2*VERTICES_PER_ROW, GL_UNSIGNED_INT, t_indices);

	return TRUE;	// Keep Going

}
    
[edited by - NeXius on August 13, 2002 6:28:49 PM]

Share this post


Link to post
Share on other sites
I guess I must have scared you off with all that code...

Anyway, check this out:

glDrawElements(mode, count, type, *indices) is supposed to be the same as:

int i;
glBegin(mode);
for (i=0; i< count; i++)
glArrayElement(indices);
glEnd();

According to the OpenGL programming guide.
But it isn''t... because when I use the for loop instead of DrawElements I can actually modify it with fog and change the polygon mode.

Why is this and is there a way to do it using glDrawElements?

Share this post


Link to post
Share on other sites