Skip to main content
gamedev.net
🔒 Locked

Multitexturing with lighting

Started by Winegums Apr 9, 2006 at 10:41 AM 1 replies 650+ views
Original Post
Winegums
Winegums
I have a terrain with multitexturing. However when i turn on lighting things go all odd. I've played with the material values and the lighting values, but i dont think its them (varying them doesn't appear to have an effect on the actual appearance of the world). Screenshots: Terrain with lighting and the primary texture only: http://img222.imageshack.us/my.php?image=noblend9ax.jpg Terrain with primary texture, lighting, and second (blended) texture: http://img466.imageshack.us/my.php?image=materials1yl.jpg Terrain with primary and blended texture, no lighting: http://img233.imageshack.us/my.php?image=unlit0cs.jpg code:

   GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
   GLfloat mat_shininess[] = { 50.0 };
   GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
   GLfloat light_ambient[] = {1.0f, 1.0f, 1.0f, 1.0f};

void Init(HWND hwnd)
{
    ...

   glLightfv(GL_LIGHT0, GL_POSITION, light_position);
   glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
   glEnable(GL_LIGHT0);
}


void RenderHeightMap(BYTE pHeightMap[])			// This Renders The Height Map As Quads
{
	int X = 0, Y = 0;							// Create Some Variables To Walk The Array With.
	int x, y, z;								// Create Some Variables For Readability
	int i;										// Counter
	char buffer[200];
	int height[4];								//Y value of the 4 vertices making up each quad

	if(!pHeightMap) return;						// Make Sure Our Height Data Is Valid
	float pointCoords[4][3];					//The xyz of the points outlining the quad
	float normal[3];

//////////////////////////////////////////////
///First Pass - Render whole world in grass(!)
//////////////////////////////////////////////

glDisable(GL_BLEND);  //Disable blending

	for ( X = 0; X < (MAP_SIZE-STEP_SIZE); X += STEP_SIZE )
	{
		for ( Y = 0; Y < (MAP_SIZE-STEP_SIZE); Y += STEP_SIZE )
		{	
			height[0] = Height(pHeightMap, X, Y );
			height[1] = Height(pHeightMap, X, Y + STEP_SIZE );  
			height[2] = Height(pHeightMap, X + STEP_SIZE, Y + STEP_SIZE ); 
			height[3] = Height(pHeightMap, X + STEP_SIZE, Y );
		
		glBindTexture(GL_TEXTURE_2D, terrain[1].texture);  //Bind the grass texture
		


		// Get The (X, Y, Z) Value For The Bottom Left Vertex
		pointCoords[0][0] = X;
		pointCoords[0][1] = Height(pHeightMap, X, Y );
		pointCoords[0][2] = Y;
		//Top Left
		pointCoords[1][0]= X;										
		pointCoords[1][1] = Height(pHeightMap, X, Y + STEP_SIZE );  
		pointCoords[1][2]= Y + STEP_SIZE;
		//Top Right
		pointCoords[2][0] = X + STEP_SIZE; 
		pointCoords[2][1] = Height(pHeightMap, X + STEP_SIZE, Y + STEP_SIZE ); 
		pointCoords[2][2] = Y + STEP_SIZE;
		//Bottom Right
		pointCoords[3][0] = X + STEP_SIZE; 
		pointCoords[3][1] = Height(pHeightMap, X + STEP_SIZE, Y ); 
		pointCoords[3][2] = Y;

		FindNormal(pointCoords[0][0], pointCoords[0][1], pointCoords[0][2], 
		pointCoords[1][0], pointCoords[1][1], pointCoords[1][2], 
		pointCoords[2][0], pointCoords[2][1], pointCoords[2][2], normal);

		if(bRender)											// What We Want To Render (if polygon mode)
		glBegin( GL_QUADS );								// Render Polygons
		else 
		glBegin( GL_LINES );								// Render Lines Instead (grid, no filler)
					
	glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
   glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);

			glNormal3f(normal[0], normal[1], normal[2]);
			glTexCoord2f(0,0); 
			glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
			glVertex3f(pointCoords[0][0],pointCoords[0][1],	pointCoords[0][2]);	 
						
			glNormal3f(normal[0], normal[1], normal[2]);
			glTexCoord2f(1,0); 
			glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
			glVertex3f(pointCoords[1][0],pointCoords[1][1],	pointCoords[1][2]);	 

			glNormal3f(normal[0], normal[1], normal[2]);
			glTexCoord2f(1,1); 
			glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
			glVertex3f(pointCoords[2][0],pointCoords[2][1],	pointCoords[2][2]);	 
	
			glNormal3f(normal[0], normal[1], normal[2]);
			glTexCoord2f(0,1); 
			glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
			glVertex3f(pointCoords[3][0],pointCoords[3][1],	pointCoords[3][2]);	 	
		glEnd();
		}
	}		//end of For x



///////////////////
///////Second Pass
///////Blend the rock texture with the grass to create "gradual rockification"
///////////////////

glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);	
glEnable(GL_BLEND);			//Enable blending
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);		//Our method of blendation

	for ( X = 0; X < (MAP_SIZE-STEP_SIZE); X += STEP_SIZE )
	{
		for ( Y = 0; Y < (MAP_SIZE-STEP_SIZE); Y += STEP_SIZE )
		{	
			height[0] = Height(pHeightMap, X, Y );
			height[1] = Height(pHeightMap, X, Y + STEP_SIZE );  
			height[2] = Height(pHeightMap, X + STEP_SIZE, Y + STEP_SIZE ); 
			height[3] = Height(pHeightMap, X + STEP_SIZE, Y );
		
		glBindTexture(GL_TEXTURE_2D, terrain[2].texture);

		pointCoords[0][0] = X;
		pointCoords[0][1] = Height(pHeightMap, X, Y );
		pointCoords[0][2] = Y;
		//Top Left
		pointCoords[1][0]= X;										
		pointCoords[1][1] = Height(pHeightMap, X, Y + STEP_SIZE );  
		pointCoords[1][2]= Y + STEP_SIZE;
		//Top Right
		pointCoords[2][0] = X + STEP_SIZE; 
		pointCoords[2][1] = Height(pHeightMap, X + STEP_SIZE, Y + STEP_SIZE ); 
		pointCoords[2][2] = Y + STEP_SIZE;
		//Bottom Right
		pointCoords[3][0] = X + STEP_SIZE; 
		pointCoords[3][1] = Height(pHeightMap, X + STEP_SIZE, Y ); 
		pointCoords[3][2] = Y;
			
		FindNormal(pointCoords[0][0], pointCoords[0][1], pointCoords[0][2], 
		pointCoords[1][0], pointCoords[1][1], pointCoords[1][2], 
		pointCoords[2][0], pointCoords[2][1], pointCoords[2][2], normal);

		if(bRender)							// What We Want To Render (if polygon mode)
		glBegin( GL_QUADS );				// Render Polygons
		else 
		glBegin( GL_LINES );				// Render Lines Instead (grid, no filler :D )

			glNormal3f(normal[0], normal[1], normal[2]);
			glTexCoord2f(0,0); 
			glColor4f(1.0f, 1.0f, 1.0f, SetAlpha(Height(pHeightMap, X, Y )));
			glVertex3f(pointCoords[0][0],pointCoords[0][1],	pointCoords[0][2]);	 
			
			glNormal3f(normal[0], normal[1], normal[2]);
			glTexCoord2f(1,0); 
			glColor4f(1.0f, 1.0f, 1.0f, SetAlpha(Height(pHeightMap, X, Y + STEP_SIZE )));
			glVertex3f(pointCoords[1][0],pointCoords[1][1],	pointCoords[1][2]);	 

			glNormal3f(normal[0], normal[1], normal[2]);
			glTexCoord2f(1,1); 
			glColor4f(1.0f, 1.0f, 1.0f, SetAlpha(Height(pHeightMap, X, Y + STEP_SIZE )));
			glVertex3f(pointCoords[2][0],pointCoords[2][1],	pointCoords[2][2]);	 

			glNormal3f(normal[0], normal[1], normal[2]);
			glTexCoord2f(0,1); 
			glColor4f(1.0f, 1.0f, 1.0f, SetAlpha(Height(pHeightMap, X, Y + STEP_SIZE )));
			glVertex3f(pointCoords[3][0],pointCoords[3][1],	pointCoords[3][2]);	 
	
		glEnd();
		}
	}		//end of For x

	glDisable(GL_BLEND);		//No more blending
	glBindTexture(GL_TEXTURE_2D, NULL);					// Clear the bound texture (for prosterity)
}

void DrawScene() 
{	
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Clear Screen And Depth Buffer
	glLoadIdentity();									// Reset The Current Modelview Matrix
	
	float sy = sin(yaw);
	float cy = cos(yaw);
	float sp = sin(pitch);
	float cp = cos(pitch);

				WWLA[0] = WWA[0] + sy * cp;
				WWLA[1] = WWA[1] - sp;
				WWLA[2] = WWA[2] + cy * cp;

gluLookAt(
    WWA[0],				//Where we're at
    WWA[1],
    WWA[2],
    WWLA[0],	//Where we're looking at (based on where we're at)
    WWLA[1],
    WWLA[2],
    0,					//Which direction is up
    1,
    0
);


	GetTileHeight(User->position[0], User->position[2]);
	DrawModels();
	glTranslatef(0.0f, 0.0f, 0.0f);
 	RenderHeightMap(g_HeightMap);						// Render The Height Map

	SwapBuffers(ghdc);// Swap the frame buffers.

//CheckMouse();	//Check for mouse input
	glFlush();

}




[Edited by - Winegums on April 9, 2006 6:08:36 PM]
zedzeek
zedzeek
codes to long to look properly but
A/ are u using multitexture (it didnt look like it)
B/ dont use vertex lighting for a terrain (or else the following post here will be why does my lighting look crap), 99.9% of cards support dot3, use that instead
Winegums
Winegums
crap lighting or no crap lighting id like to use vertex lighting first (infact its a coursework spec...though i dunno if using dot3 exceeds it...in any case i daresay its probibally more complicated).

I looked around and found a multitexture tutorial on NeHe (of all places ;) ) though its for bumpmapping. is this a good place to take the explanation from, or is there a more direct tutorial somewhere? started pulling it apart in any case..

EDIT: Disabling lighting for the second pass seems to have some effect, though i doubt its the bleeding edge technique used on the latest games -.-

[Edited by - Winegums on April 9, 2006 6:12:44 PM]

Topic Locked

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

Sign in to reply to this topic.