Exemplo n.º 1
0
void RenderHeightMap(BYTE pHeightMap[])
{
	int X = 0, Y = 0;						// Create some variables to walk the array with.
	int x, y, z;							// Create some variables for readability
	bool bSwitchSides = false;

	// Make sure our height data is valid
	if(!pHeightMap) return;		
	
	// The difference from the way we render the terrain from
	// the original way we started with, is that we don't use GL_QUADS
	// anymore, we now use a GL_TRIANGLE_STIP.  This means that we don't
	// need to pass in the same vertex more than once.  Each 2 vertices
	// are connected to the next 2.  Since we want to do this in one strip,
	// we are going to need to reverse the order every other column.  It's
	// like moving the lawn.  Go to the end and turn around and come back
	// the way you came.  If you don't do it this way, you will get polygons
	// stretching across the whole terrain.  We could just do a new glBegin()
	// and glEnd() for every column, but I believe this way is faster.  
	// Not that that really matters though, because rendering a terrain
	// with glVertex*() calls in incredibly slow.  We will most likely want
	// to eventually switch this to vertex arrays.

	// Bind the terrain texture to our terrain
	glBindTexture(GL_TEXTURE_2D, g_Texture[0]);

	// We want to render triangle strips
	glBegin( GL_TRIANGLE_STRIP );			

	// Go through all of the rows of the height map
	for ( X = 0; X <= MAP_SIZE; X += STEP_SIZE )
	{
		// Chechk if we need to render the opposite way for this column
		if(bSwitchSides)
		{	
			// Render a column of the terrain, for this current X.
			// We start at MAP_SIZE and render down to 0.
			for ( Y = MAP_SIZE; Y >= 0; Y -= STEP_SIZE )
			{
				// Get the (X, Y, Z) value for the bottom left vertex		
				x = X;							
				y = Height(pHeightMap, X, Y );	
				z = Y;							

				// Set the current texture coordinate and render the vertex
				SetTextureCoord( (float)x, (float)z );
				glVertex3i(x, y, z);		

				// Get the (X, Y, Z) value for the bottom right vertex		
				x = X + STEP_SIZE; 
				y = Height(pHeightMap, X + STEP_SIZE, Y ); 
				z = Y;

				// Set the current texture coordinate and render the vertex
				SetTextureCoord( (float)x, (float)z );
				glVertex3i(x, y, z);			
			}
		}
		else
		{	
			// Render a column of the terrain, for this current X.
			// We start at 0 and render down up to MAP_SIZE.
			for ( Y = 0; Y <= MAP_SIZE; Y += STEP_SIZE )
			{
				// Get the (X, Y, Z) value for the bottom right vertex		
				x = X + STEP_SIZE; 
				y = Height(pHeightMap, X + STEP_SIZE, Y ); 
				z = Y;

				// Set the current texture coordinate and render the vertex
				SetTextureCoord( (float)x, (float)z );
				glVertex3i(x, y, z);

				// Get the (X, Y, Z) value for the bottom left vertex		
				x = X;							
				y = Height(pHeightMap, X, Y );	
				z = Y;							

				// Set the current texture coordinate and render the vertex
				SetTextureCoord( (float)x, (float)z );
				glVertex3i(x, y, z);		
			}
		}

		// Switch the direction the column renders to allow the fluid tri strips
		bSwitchSides = !bSwitchSides;
	}

	// Stop rendering triangle strips
	glEnd();
}
void RenderHeightMap(BYTE pHeightMap[])
{
	int X = 0, Y = 0;						// Create some variables to walk the array with.
	int x, y, z;							// Create some variables for readability
	bool bSwitchSides = false;

	// Make sure our height data is valid
	if(!pHeightMap) return;		
	

/////// * /////////// * /////////// * NEW * /////// * /////////// * /////////// *

	// Now comes the juice of our detail texture.  We want to combine the
	// terrain texture and the detail texture together.  To blend the two
	// textures nicely, we will want to use some ARB extension defines
	// for our texture properties.  The first one, GL_COMBINE_ARB, allows
	// us to use combine texture properties, which brings us to our second
	// one: GL_RGB_SCALE_ARB.  With this texture property, we can now 
	// increase the gamma of our second texture so that it does not darken
	// the texture beneath it.  This works great for lightmaps and detail 
	// textures.  I believe that the GL_RGB_SCALE_ARB property only accepts
	// 1, 2 or 4 as a valid scale value.  2 works just fine, where 4 is
	// too bright.  
	// 
	// To tile the detail texture appropriately, the texture matrix is
	// effected.  When we enter into the texture matrix mode, it allows us
	// to effect the current position of the selected texture.  This isn't
	// necessary to use, since we could just calculate the detail texture
	// coordinates on our own, but I thought it would be nice to introduce
	// this functionality.  All we do is just scale the texture by a certain
	// amount, which can provide different levels of detail.  By hitting the
	// SPACE bar on the keyboard, you are able to cycle through different
	// scale values to see the one that works best for you.  It is good to
	// add a third texture on top that is the same detail texture, but with
	// a different scale value.

	// Activate the first texture ID and bind the tree background to it
	glActiveTextureARB(GL_TEXTURE0_ARB);
	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, g_Texture[0]);

	// If we want detail texturing on, let's render the second texture
	if(g_bDetail)
	{
		// Activate the second texture ID and bind the fog texture to it
		glActiveTextureARB(GL_TEXTURE1_ARB);
		glEnable(GL_TEXTURE_2D);
		
		// Here we turn on the COMBINE properties and increase our RGB
		// gamma for the detail texture.  2 seems to work just right.
		glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);
		glTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, 2);
		
		// Bind the detail texture
		glBindTexture(GL_TEXTURE_2D, g_Texture[1]);
	
		// Now we want to enter the texture matrix.  This will allow us
		// to change the tiling of the detail texture.
		glMatrixMode(GL_TEXTURE);

			// Reset the current matrix and apply our chosen scale value
			glLoadIdentity();
			glScalef((float)g_DetailScale, (float)g_DetailScale, 1);

		// Leave the texture matrix and set us back in the model view matrix
		glMatrixMode(GL_MODELVIEW);
	}

/////// * /////////// * /////////// * NEW * /////// * /////////// * /////////// *


	// We want to render triangle strips
	glBegin( GL_TRIANGLE_STRIP );			

	// Go through all of the rows of the height map
	for ( X = 0; X <= MAP_SIZE; X += STEP_SIZE )
	{
		// Check if we need to render the opposite way for this column
		if(bSwitchSides)
		{	
			// Render a column of the terrain, for this current X.
			// We start at MAP_SIZE and render down to 0.
			for ( Y = MAP_SIZE; Y >= 0; Y -= STEP_SIZE )
			{
				// Get the (X, Y, Z) value for the bottom left vertex		
				x = X;							
				y = Height(pHeightMap, X, Y );	
				z = Y;							

				// Set the current texture coordinate and render the vertex
				SetTextureCoord( (float)x, (float)z );
				glVertex3i(x, y, z);		

				// Get the (X, Y, Z) value for the bottom right vertex		
				x = X + STEP_SIZE; 
				y = Height(pHeightMap, X + STEP_SIZE, Y ); 
				z = Y;

				// Set the current texture coordinate and render the vertex
				SetTextureCoord( (float)x, (float)z );
				glVertex3i(x, y, z);			
			}
		}
		else
		{	
			// Render a column of the terrain, for this current X.
			// We start at 0 and render down up to MAP_SIZE.
			for ( Y = 0; Y <= MAP_SIZE; Y += STEP_SIZE )
			{
				// Get the (X, Y, Z) value for the bottom right vertex		
				x = X + STEP_SIZE; 
				y = Height(pHeightMap, X + STEP_SIZE, Y ); 
				z = Y;

				// Set the current texture coordinate and render the vertex
				SetTextureCoord( (float)x, (float)z );
				glVertex3i(x, y, z);

				// Get the (X, Y, Z) value for the bottom left vertex		
				x = X;							
				y = Height(pHeightMap, X, Y );	
				z = Y;							

				// Set the current texture coordinate and render the vertex
				SetTextureCoord( (float)x, (float)z );
				glVertex3i(x, y, z);		
			}
		}

		// Switch the direction the column renders to allow the fluid tri strips
		bSwitchSides = !bSwitchSides;
	}

	// Stop rendering triangle strips
	glEnd();


/////// * /////////// * /////////// * NEW * /////// * /////////// * /////////// *

	// Now let's clean up our multitexturing so it doesn't effect anything else

	// Turn the second multitexture pass off
	glActiveTextureARB(GL_TEXTURE1_ARB);
    glDisable(GL_TEXTURE_2D);

	// Turn the first multitexture pass off
	glActiveTextureARB(GL_TEXTURE0_ARB);		
    glDisable(GL_TEXTURE_2D);

/////// * /////////// * /////////// * NEW * /////// * /////////// * /////////// *


}
Exemplo n.º 3
0
void RenderHeightMap(BYTE *pHeightMap, GLuint terrainTex)
{
	int X = 0, Y = 0;												// 点在高度图上的坐标
	int x, y, z;													// 点的三维坐标
	BOOL bSwitchSides = FALSE;

	if (!pHeightMap)												// 保证高度有效
	{
		return;
	}
	glBindTexture(GL_TEXTURE_2D, terrainTex);						// 绑定纹理

	glBegin(GL_TRIANGLE_STRIP);										// 开始绘制地形

	for (X = 0; X <= MAP_SIZE; X += STEP_SIZE)						// 遍历高度图
	{
		if (bSwitchSides)											// 是否从反方向读取高度图
		{	
			// 在当前X坐标上渲染地形
			for (Y = MAP_SIZE; Y >= 0; Y -= STEP_SIZE)				// 从MAP_SIZE到0
			{
				// 取得底部左顶点的 (X, Y, Z)值	
				x = X;
				y = Height(pHeightMap, X, Y);
				z = Y;

				// 提交点并设置当前纹理坐标
				SetTextureCoord((float)x, (float)z);
				glVertex3i(x, y, z);

				// 取得底部右顶点的 (X, Y, Z)值
				x = X + STEP_SIZE;
				y = Height(pHeightMap, X + STEP_SIZE, Y);
				z = Y;

				// 提交点并设置当前纹理坐标
				SetTextureCoord((float)x, (float)z);
				glVertex3i(x, y, z);
			}
		}
		else
		{
			// 在当前X坐标上渲染地形
			for (Y = 0; Y <= MAP_SIZE; Y += STEP_SIZE)				// 从0到MAP_SIZE
			{
				// 取得底部右顶点的(x, y, z)值
				x = X + STEP_SIZE;
				y = Height(pHeightMap, X + STEP_SIZE, Y);
				z = Y;

				// 提交点并设置当前纹理坐标
				SetTextureCoord((float)x, (float)z);
				glVertex3i(x, y, z);

				// 取得底部左顶点的(x, y, z)值
				x = X;
				y = Height(pHeightMap, X, Y);
				z = Y;

				// 提交点并设置当前纹理坐标
				SetTextureCoord((float)x, (float)z);
				glVertex3i(x, y, z);
			}
		}
		bSwitchSides = !bSwitchSides;								// 改变绘制方向
	}
	glEnd();														// 结束绘制地形
}
Exemplo n.º 4
0
void RenderHeightMap(BYTE pHeightMap[])
{
	int X = 0, Y = 0;						// Create some variables to walk the array with.
	int x, y, z;							// Create some variables for readability
	bool bSwitchSides = false;

	// Make sure our height data is valid
	if(!pHeightMap) return;		
	
	// Activate the first texture ID and bind the tree background to it
	glActiveTextureARB(GL_TEXTURE0_ARB);
	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, g_Texture[0]);

	// If we want detail texturing on, let's render the second texture
	if(g_bDetail)
	{
		// Activate the second texture ID and bind the fog texture to it
		glActiveTextureARB(GL_TEXTURE1_ARB);
		glEnable(GL_TEXTURE_2D);
		
		// Here we turn on the COMBINE properties and increase our RGB
		// gamma for the detail texture.  2 seems to work just right.
		glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);
		glTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, 2);
		
		// Bind the detail texture
		glBindTexture(GL_TEXTURE_2D, g_Texture[1]);
	
		// Now we want to enter the texture matrix.  This will allow us
		// to change the tiling of the detail texture.
		glMatrixMode(GL_TEXTURE);

			// Reset the current matrix and apply our chosen scale value
			glLoadIdentity();
			glScalef((float)g_DetailScale, (float)g_DetailScale, 1);

		// Leave the texture matrix and set us back in the model view matrix
		glMatrixMode(GL_MODELVIEW);
	}

	// We want to render triangle strips
	glBegin( GL_TRIANGLE_STRIP );			

	// Go through all of the rows of the height map
	for ( X = 0; X <= MAP_SIZE; X += STEP_SIZE )
	{
		// Check if we need to render the opposite way for this column
		if(bSwitchSides)
		{	
			// Render a column of the terrain, for this current X.
			// We start at MAP_SIZE and render down to 0.
			for ( Y = MAP_SIZE; Y >= 0; Y -= STEP_SIZE )
			{
				// Get the (X, Y, Z) value for the bottom left vertex		
				x = X;							
				y = Height(pHeightMap, X, Y );	
				z = Y;							


/////// * /////////// * /////////// * NEW * /////// * /////////// * /////////// *

				// Set the fog coordinate for this vertex, depending on it's height
				// and the current depth of the fog.
				SetFogCoord(g_FogDepth, (float)y);

/////// * /////////// * /////////// * NEW * /////// * /////////// * /////////// *


				// Set the current texture coordinate and render the vertex
				SetTextureCoord( (float)x, (float)z );
				glVertex3i(x, y, z);		

				// Get the (X, Y, Z) value for the bottom right vertex		
				x = X + STEP_SIZE; 
				y = Height(pHeightMap, X + STEP_SIZE, Y ); 
				z = Y;


/////// * /////////// * /////////// * NEW * /////// * /////////// * /////////// *

				// Set the fog coordinate for this vertex
				SetFogCoord(g_FogDepth, (float)y);

/////// * /////////// * /////////// * NEW * /////// * /////////// * /////////// *


				// Set the current texture coordinate and render the vertex
				SetTextureCoord( (float)x, (float)z );
				glVertex3i(x, y, z);			
			}
		}
		else
		{	
			// Render a column of the terrain, for this current X.
			// We start at 0 and render down up to MAP_SIZE.
			for ( Y = 0; Y <= MAP_SIZE; Y += STEP_SIZE )
			{
				// Get the (X, Y, Z) value for the bottom right vertex		
				x = X + STEP_SIZE; 
				y = Height(pHeightMap, X + STEP_SIZE, Y ); 
				z = Y;


/////// * /////////// * /////////// * NEW * /////// * /////////// * /////////// *

				// Set the fog coordinate for this vertex
				SetFogCoord(g_FogDepth, (float)y);

/////// * /////////// * /////////// * NEW * /////// * /////////// * /////////// *


				// Set the current texture coordinate and render the vertex
				SetTextureCoord( (float)x, (float)z );
				glVertex3i(x, y, z);

				// Get the (X, Y, Z) value for the bottom left vertex		
				x = X;							
				y = Height(pHeightMap, X, Y );	
				z = Y;							


/////// * /////////// * /////////// * NEW * /////// * /////////// * /////////// *

				// Set the fog coordinate for this vertex
				SetFogCoord(g_FogDepth, (float)y);

/////// * /////////// * /////////// * NEW * /////// * /////////// * /////////// *


				// Set the current texture coordinate and render the vertex
				SetTextureCoord( (float)x, (float)z );
				glVertex3i(x, y, z);		
			}
		}

		// Switch the direction the column renders to allow the fluid tri strips
		bSwitchSides = !bSwitchSides;
	}

	// Stop rendering triangle strips
	glEnd();

	// Turn the second multitexture pass off
	glActiveTextureARB(GL_TEXTURE1_ARB);
    glDisable(GL_TEXTURE_2D);

	// Turn the first multitexture pass off
	glActiveTextureARB(GL_TEXTURE0_ARB);		
    glDisable(GL_TEXTURE_2D);
}
Exemplo n.º 5
0
//	This renders the height map as QUADS
void Terrain::Render()
{
	int X = 0, Y = 0;						// Create some variables to walk the array with.
	int x, y, z;							// Create some variables for readability
	//float fColor = 0.0f;					// Create a variable to hold our color of the polygon
	float bSwitchSides = false;

	if (!m_HeighMap) return;					// Make sure our height data is valid

	//glBegin(GL_QUADS);					// Render Quads


	// The difference from the way we render the terrain from
	// the original way we started with, is that we don't use GL_QUADS
	// anymore, we now use a GL_TRIANGLE_STIP.  This means that we don't
	// need to pass in the same vertex more than once.  Each 2 vertices
	// are connected to the next 2.  Since we want to do this in one strip,
	// we are going to need to reverse the order every other column.  It's
	// like moving the lawn.  Go to the end and turn around and come back
	// the way you came.  If you don't do it this way, you will get polygons
	// stretching across the whole terrain.  We could just do a new glBegin()
	// and glEnd() for every column, but I believe this way is faster.  
	// Not that that really matters though, because rendering a terrain
	// with glVertex*() calls in incredibly slow.  We will most likely want
	// to eventually switch this to vertex arrays.

	// Bind the terrain texture to our terrain
	glBindTexture(GL_TEXTURE_2D, m_TerrainTexture);

	//we want to render treinagle strip 
	glBegin(GL_TRIANGLE_STRIP);

	//go throuht all of the rows of the heightmap 
	for (X = 0; X < MAP_SIZE; X += STEP_SIZE)
	{
		//check if we need to render the opposite way for this column
		if (bSwitchSides)
		{
			//render a column of the terrain, for this current x
			//we start at MAP_SIZE and render down to 0
			for (Y = MAP_SIZE; Y >= 0; Y -= STEP_SIZE)
			{
				//Get the (X, Y, Z) value for the bottom left vertex 
				x = X + STEP_SIZE;
				y = Height(X + STEP_SIZE, Y);
				z = Y;

				//Set the current texture coordinate and render the vertex
				SetTextureCoord((float)x, (float(z)));
				glVertex3i(x, y, z);
			}
		}
		else
		{
			// Render a column of the terrain, for this current X.
			// We start at 0 and render down up to MAP_SIZE.
			for (Y = 0; Y <= MAP_SIZE; Y += STEP_SIZE)
			{
				// Get the (X, Y, Z) value for the bottom right vertex		
				x = X + STEP_SIZE;
				y = Height(X + STEP_SIZE, Y);
				z = Y;

				// Set the current texture coordinate and render the vertex
				SetTextureCoord((float)x, (float)z);
				glVertex3i(x, y, z);

				// Get the (X, Y, Z) value for the bottom left vertex		
				x = X;
				y = Height(X, Y);
				z = Y;

				// Set the current texture coordinate and render the vertex
				SetTextureCoord((float)x, (float)z);
				glVertex3i(x, y, z);
			}
		}
		// Switch the direction the column renders to allow the fluid tri strips
		//bSwitchSides = !bSwitchSides;
	}

	glEnd();

		//// Now we actually draw the water blended over the reflection, which
		//// creates the effect that the reflection is in the water.  We blend
		//// with a simple alpha to one minus alpha blending function.  The color
		//// is set to a blue, with the alpha value at 50% (0.5).  We don't want
		//// the alpha too high or else we wouldn't see the reflection.
		//glEnable(GL_BLEND);
		//glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
		//glColor4f(0.5f, 0.5f, 0.9f, 0.5f);
		////RenderWater();
		//glDisable(GL_BLEND);
		//// This block of code right here is for an added effect of
		//// what is draw when you are under the water.  This isn't necessary,
		//// but I thought I would add it to improve the tutorial's realism.
		//// When we go underwater we want to be able to look up out of the
		//// water.  To do that we just render the water again, but change
		//// the culling to front-face, then change our water color.  Nothing special.
		//// We could just do a check to see if we are under or over the water,
		//// then only render the water once, but I think you can do that yourself.
		//glCullFace(GL_FRONT);
		//glEnable(GL_BLEND);
		//glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
		//glColor4f(0.2f, 0.2f, 0.9f, 0.5f);
		//RenderWater();
		//glDisable(GL_BLEND);
		//glCullFace(GL_BACK);
	
}