Sign in to follow this  
Journey

Distorted lighting

Recommended Posts

I'm having problems lighting a set of surfaces correctly when I perform scaling and rotation. The colors shift wildly from the values GLfloat mat_specular[] = { 0.0, 0.1, 0.0, 0.1 }; GLfloat mat_shininess[] = { 50.0 }; GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 }; GLfloat afWhiteLight[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat afLModelAmbient[] = { 0.25, 0.25, 0.25, 1.0 }; to some strange combination of lime green and 1.0f, 0.0f, 1.0f. (The color I use to render surface control points). Here are some screenshots These ones look fine: http://i14.photobucket.com/albums/a350/luisjoseromero/fine1.jpg http://i14.photobucket.com/albums/a350/luisjoseromero/fine2.jpg But in these the colors are wrong: http://i14.photobucket.com/albums/a350/luisjoseromero/wrong1.jpg http://i14.photobucket.com/albums/a350/luisjoseromero/wrong2.jpg http://i14.photobucket.com/albums/a350/luisjoseromero/wrong3.jpg What could I possibly be doing wrong? Oh, and by the way, here's the drawing code: (Don´t mind the commented code btw)

void CSystem::Draw(CSystem::m_eDrawMode eMode_)
{

	//OUTD("\n CSystem::Draw");
	// now make the rendering context the active one
    wglMakeCurrent(this->m_pHDCS, this->m_pHRCS);

	// clear screen and depth buffer
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);										
	glLoadIdentity();

	char acBuffer[200];

    GLfloat mat_specular[] = { 0.0, 0.1, 0.0, 0.1 };
    GLfloat mat_shininess[] = { 50.0 };
    GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
	GLfloat afWhiteLight[] = { 1.0, 1.0, 1.0, 1.0 };
	GLfloat afLModelAmbient[] = { 0.25, 0.25, 0.25, 1.0 };


	// update translation
	glTranslatef(this->m_dXTrans, this->m_dYTrans, -130.0);
	glScalef(this->m_dScale-0.25, this->m_dScale-0.25, 1.0);

	// update rotation
	glRotatef(this->m_dXRot, 0, 1, 0);
	glRotatef(this->m_dYRot, 1, 0, 0);

    //glClearColor (0.0, 0.0, 0.0, 0.0);
    glShadeModel (GL_SMOOTH);

    glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);
	glLightfv(GL_LIGHT0, GL_DIFFUSE,  afWhiteLight);
	glLightfv(GL_LIGHT0, GL_SPECULAR, afWhiteLight);
	glLightModelfv(GL_LIGHT_MODEL_AMBIENT, afLModelAmbient);

    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);



	//glPolygonMode(GL_FRONT, GL_FILL);
	//glPolygonMode(GL_BACK, GL_LINE);



    //glEnable(GL_DEPTH_TEST);

	/*// draw the landscape
	glBegin(GL_QUADS);
			


	// draw the sky

	glColor3f(0.5f, 0.5f, 1.0f);				// light blue vertices
	glVertex3f(-this->m_dBorderX, 10.0f, -5.0f);						
	glVertex3f(this->m_dBorderX, 10.0f, -5.0f);
	glColor3f(0.0f, 0.0f, 1.0f);				// dark blue vertices
	glVertex3f(this->m_dBorderX, this->m_dBorderY, -5.0f);
	glVertex3f(-this->m_dBorderX, this->m_dBorderY, -5.0f);

	glEnd();

	// draw layer 1
	glBegin(GL_QUADS);
		
	glColor3f((*this->m_pVecVelC)[0], 0.2f, 0.0f);
	glVertex3f(-this->m_dBorderX, -this->m_dBorderY, -6.0f);						
	glVertex3f(this->m_dBordGLerX, -this->m_dBorderY, -6.0f);		
	glVertex3f(this->m_dBorderX, this->m_dBorderY, -6.0f);
	glVertex3f(-this->m_dBorderX, this->m_dBorderY, -6.0f);

	glEnd();


	S3DPoint PointS;
	int nCurrVertex;
	for(UINT unCurrLayer = 0; unCurrLayer < this->m_unNumLayers; unCurrLayer++)
	{
	
		float fStep = 0.0;
		for(nCurrVertex = 0; nCurrVertex < 100; nCurrVertex++)
		{
		
			PointS = (*this->m_pVecLayersC)[unCurrLayer]->GetEvalPoint(fStep);
			memcpy(this->m_papapadLayerVertexBuffer[unCurrLayer][nCurrVertex], &PointS, sizeof(double)*3);
			fStep+=0.02;

		}// end for
	
	}// end for

	// Draw the layers
	for(UINT unCurrLayer = 1; unCurrLayer < this->m_unNumLayers-1; unCurrLayer++)
	{

		gluTessBeginPolygon(this->m_pTessObj, NULL);
        gluTessBeginContour(this->m_pTessObj);
		
		this->m_adCurrColor[0] = 0.0f; this->m_adCurrColor[1] = 0.7f; this->m_adCurrColor[2] = (*this->m_pVecVelC)[unCurrLayer];
		for(nCurrVertex = 99; nCurrVertex >= 0; nCurrVertex--)
		{

			gluTessVertex(this->m_pTessObj, (GLdouble*)(this->m_papapadLayerVertexBuffer[unCurrLayer][nCurrVertex]), (GLdouble*)(this->m_papapadLayerVertexBuffer[unCurrLayer][nCurrVertex]));

		}// end for

		this->m_adCurrColor[0] = (*this->m_pVecVelC)[unCurrLayer]; this->m_adCurrColor[1] = 0.2f; this->m_adCurrColor[2] = 0.0f;
		for(nCurrVertex = 0; nCurrVertex < 100; nCurrVertex++)
		{

			gluTessVertex(this->m_pTessObj, (GLdouble*)(this->m_papapadLayerVertexBuffer[unCurrLayer+1][nCurrVertex]), (GLdouble*)(this->m_papapadLayerVertexBuffer[unCurrLayer+1][nCurrVertex]));
			
		}// end for

        gluTessEndContour(this->m_pTessObj);
		gluTessEndPolygon(this->m_pTessObj);


	}// end for

	gluTessBeginPolygon(this->m_pTessObj, NULL);
    gluTessBeginContour(this->m_pTessObj);

	this->m_adCurrColor[0] = 0.0f; this->m_adCurrColor[1] = 0.7f; this->m_adCurrColor[2] = (*this->m_pVecVelC)[this->m_unNumLayers-1];
	for(nCurrVertex = 99; nCurrVertex >= 0; nCurrVertex--)
	{

		gluTessVertex(this->m_pTessObj, (this->m_papapadLayerVertexBuffer[this->m_unNumLayers-1][nCurrVertex]), (this->m_papapadLayerVertexBuffer[this->m_unNumLayers-1][nCurrVertex]));

	}// end for

	this->m_adCurrColor[0] = (*this->m_pVecVelC)[this->m_unNumLayers-1]; this->m_adCurrColor[1] = 0.2f; this->m_adCurrColor[2] = 0.0f;
	double adPoint1[6];
	adPoint1[0] = -this->m_dBorderX; adPoint1[1] = -this->m_dBorderY; adPoint1[2] = -5.0f;
	gluTessVertex(this->m_pTessObj, (GLdouble*)(adPoint1), (GLdouble*)(adPoint1));
	double adPoint2[6];
	adPoint2[0] = this->m_dBorderX; adPoint2[1] = -this->m_dBorderY; adPoint2[2] = -5.0f;
	gluTessVertex(this->m_pTessObj, (GLdouble*)(adPoint2), (GLdouble*)(adPoint2));

    gluTessEndContour(this->m_pTessObj);
	gluTessEndPolygon(this->m_pTessObj);*/

	C2DVector pPosC;

	/*// Draw the layer cvs
	for(UINT unCurrLayer = 1; unCurrLayer < this->m_unNumLayers; unCurrLayer++)
	{

		// draw the layer cvs
		glPointSize(5.0);

		char acBuffer[200];

		for(int nStep = 0; nStep < (*this->m_pVecLayersC)[unCurrLayer]->m_CVListC.size(); nStep++)
		{

			if(0 == nStep)
			{

				glColor3f(1.0, 1.0, 1.0);
				glRasterPos2f((*this->m_pVecLayersC)[unCurrLayer]->m_CVListC[nStep].m_dX+(1.5/this->m_dScale), (*this->m_pVecLayersC)[unCurrLayer]->m_CVListC[nStep].m_dY+(0.7/this->m_dScale));
				this->PrintString(_gcvt(((-(*this->m_pVecLayersC)[unCurrLayer]->m_CVListC[nStep].m_dY)+10.0)/7.0, 10, acBuffer));

			}else if(((*this->m_pVecLayersC)[unCurrLayer]->m_CVListC.size()-1) == nStep)
			{
			
				glColor3f(1.0, 1.0, 1.0);
				glRasterPos2f((*this->m_pVecLayersC)[unCurrLayer]->m_CVListC[nStep].m_dX-(7.5/this->m_dScale), (*this->m_pVecLayersC)[unCurrLayer]->m_CVListC[nStep].m_dY+(0.7/this->m_dScale));
				this->PrintString(_gcvt(((-(*this->m_pVecLayersC)[unCurrLayer]->m_CVListC[nStep].m_dY)+10.0)/7.0, 10, acBuffer));

			}// end else if

			if(eMode_ == CSystem::m_eDrawMode::SELECT)
			{
				
				glPushName(nStep+(unCurrLayer*4));

			}// end if

			glBegin(GL_POINTS);

				glColor3f(1.0f, 0.0f, 1.0f);
				glVertex3f((*this->m_pVecLayersC)[unCurrLayer]->m_CVListC[nStep].m_dX, (*this->m_pVecLayersC)[unCurrLayer]->m_CVListC[nStep].m_dY, -4.0f);

			glEnd();

			if(eMode_ == CSystem::m_eDrawMode::SELECT)
			{
				
				glPopName();

			}// end if

		}// end for

	}// end for*/

	glDisable(GL_LIGHTING);

	glPointSize(5.0);
	glColor3f(1.0, 1.0, 1.0);

	//char acBuffer[200];

	for(int nCurrLayer = 0; nCurrLayer < this->m_pVecLayers2C->size(); nCurrLayer++)
	{

		for(int nCurrUCV = 0; nCurrUCV < (*this->m_pVecLayers2C)[nCurrLayer]->m_CVListC.size(); nCurrUCV++)
		{
		
			for(int nCurrVCV = 0; nCurrVCV < (*this->m_pVecLayers2C)[nCurrLayer]->m_CVListC[nCurrUCV].size(); nCurrVCV++)
			{

				glBegin(GL_POINTS);

					glColor3f(1.0f, 0.0f, 1.0f);
					glVertex3f((*this->m_pVecLayers2C)[nCurrLayer]->m_CVListC[nCurrUCV][nCurrVCV].m_dX, (*this->m_pVecLayers2C)[nCurrLayer]->m_CVListC[nCurrUCV][nCurrVCV].m_dY, (*this->m_pVecLayers2C)[nCurrLayer]->m_CVListC[nCurrUCV][nCurrVCV].m_dZ);

				glEnd();
			
			}// end for

		}// end for

	}// end for

	glEnable(GL_LIGHTING);

	S3DPoint PointS;

	int nRes = 20;

	float fUInc = 1.0/nRes, fVInc = 1.0/nRes;
	float fV = 0.0;
	/*glColor3f(0.0f, 1.0f, 0.0f);

	for(int nCurrLayer = 0; nCurrLayer < (*this->m_pVecLayers2C).size(); nCurrLayer++)
	{

		fV = 0.0;

		for(int nCurrVTri = 0; nCurrVTri < nRes; nCurrVTri++)
		{

			glBegin(GL_TRIANGLE_STRIP);

				float fU = fUInc;

				PointS = (*this->m_pVecLayers2C)[nCurrLayer]->GetEvalPoint(0.0, fV);
				glVertex3f(PointS.m_dX, PointS.m_dY, PointS.m_dZ);
				PointS = (*this->m_pVecLayers2C)[nCurrLayer]->GetEvalPoint(0.0, fV+fVInc);
				glVertex3f(PointS.m_dX, PointS.m_dY, PointS.m_dZ);
				
				for(int nCurrUTri = 0; nCurrUTri < nRes; nCurrUTri++)
				{
				
					PointS = (*this->m_pVecLayers2C)[nCurrLayer]->GetEvalPoint(fU, fV);
					glVertex3f(PointS.m_dX, PointS.m_dY, PointS.m_dZ);
					PointS = (*this->m_pVecLayers2C)[nCurrLayer]->GetEvalPoint(fU, fV+fVInc);
					glVertex3f(PointS.m_dX, PointS.m_dY, PointS.m_dZ);
					fU += fUInc;

				}// end for

			glEnd();

			fV += fVInc;

		}// end for

	}// end for*/


	S3DPoint Point1S, Point2S, Point3S, Point4S;

	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LEQUAL);

	for(int nCurrLayer = 0; nCurrLayer < (*this->m_pVecLayers2C).size(); nCurrLayer++)
	{

		fV = 0.0;

		for(int nCurrVTri = 0; nCurrVTri < nRes; nCurrVTri++)
		{

			glBegin(GL_TRIANGLE_STRIP);

				float fU = 0;
				
				for(int nCurrUTri = 0; nCurrUTri < nRes; nCurrUTri++)
				{
				
					Point1S = (*this->m_pVecLayers2C)[nCurrLayer]->GetEvalPoint(fU, fV);
					Point2S = (*this->m_pVecLayers2C)[nCurrLayer]->GetEvalPoint(fU, fV+fVInc);
					Point3S = (*this->m_pVecLayers2C)[nCurrLayer]->GetEvalPoint(fU+fUInc, fV);
					Point4S = (*this->m_pVecLayers2C)[nCurrLayer]->GetEvalPoint(fU+fUInc, fV+fVInc);
					
					fU += fUInc;

					C3DVector Vec21C(Point2S.m_dX-Point1S.m_dX, Point2S.m_dY-Point1S.m_dY, Point2S.m_dZ-Point1S.m_dZ);
					C3DVector Vec34C(Point3S.m_dX-Point4S.m_dX, Point3S.m_dY-Point4S.m_dY, Point3S.m_dZ-Point4S.m_dZ);
					C3DVector Vec31C(Point3S.m_dX-Point1S.m_dX, Point3S.m_dY-Point1S.m_dY, Point3S.m_dZ-Point1S.m_dZ);
					C3DVector Vec24C(Point2S.m_dX-Point4S.m_dX, Point2S.m_dY-Point4S.m_dY, Point2S.m_dZ-Point4S.m_dZ);
					// first normal
					C3DVector Normal1C(Vec21C^Vec31C);
					Normal1C.Normalize();
					Normal1C.Negate();
					// last normal
					C3DVector Normal4C(Vec34C^Vec24C);
					Normal4C.Normalize();
					Normal4C.Negate();

					glNormal3d(Normal1C.m_dX, Normal1C.m_dY, Normal1C.m_dZ);
					glVertex3f(Point1S.m_dX, Point1S.m_dY, Point1S.m_dZ);
					glVertex3f(Point2S.m_dX, Point2S.m_dY, Point2S.m_dZ);
					glVertex3f(Point3S.m_dX, Point3S.m_dY, Point3S.m_dZ);
					glNormal3d(Normal4C.m_dX, Normal4C.m_dY, Normal4C.m_dZ);
					glVertex3f(Point4S.m_dX, Point4S.m_dY, Point4S.m_dZ);

				}// end for

			glEnd();

			fV += fVInc;

		}// end for

	}// end for



	/*PointS = (*this->m_pVecLayers2C)[0]->GetEvalPoint(0.6, 0.5);

	glBegin(GL_POINTS);

		glColor3f(1.0f, 1.0f, 1.0f);
		glVertex3f(PointS.m_dX, PointS.m_dY, PointS.m_dZ);

	glEnd();

	// draw the explosive
	glPointSize(10.0);
	glEnable(GL_POINT_SMOOTH);
	glBegin(GL_POINTS);

		glColor3f(0.4f, 0.0f, 0.0f);
		glVertex3f(this->m_ExpPosVec2DC.m_dX, this->m_ExpPosVec2DC.m_dY, -4.0f);

	glEnd();*/


	/*this->m_SplineIntersLineC.m_dX1 = -35.0;  this->m_SplineIntersLineC.m_dY1 = 40.0;
	this->m_SplineIntersLineC.m_dX2 = -20.0; this->m_SplineIntersLineC.m_dY2 = -30.0;

	/////////////////// TEST ///////////////
	glBegin(GL_LINES);
	
		glColor3f(1.0f, 1.0f, 1.0f);
		glVertex3f(this->m_SplineIntersLineC.m_dX1, this->m_SplineIntersLineC.m_dY1, -4.9f);
		glVertex3f(this->m_SplineIntersLineC.m_dX2, this->m_SplineIntersLineC.m_dY2, -4.9f);

	glEnd();


	S3DPoint IntVecOffsetVS;
	C2DVector IntersVec2DC, NormalVec2DC; double dIntU;
	C2DLine Line2DC, Line22DC;
	(*this->m_pVecLayersC)[1]->Intersect(&(this->m_SplineIntersLineC), &IntersVec2DC, &dIntU);

	IntVecOffsetVS = (*this->m_pVecLayersC)[1]->GetEvalPoint(dIntU+0.0025);

	NormalVec2DC.m_dX = IntVecOffsetVS.m_dX - IntersVec2DC.m_dX;
	NormalVec2DC.m_dY = IntVecOffsetVS.m_dY - IntersVec2DC.m_dY;

	NormalVec2DC.Normalize();
	NormalVec2DC*=100.0;
	Line22DC.m_dX1 = IntVecOffsetVS.m_dX;                       Line22DC.m_dY1 = IntVecOffsetVS.m_dY;
	Line22DC.m_dX2 = IntVecOffsetVS.m_dX+NormalVec2DC.m_dX;     Line22DC.m_dY2 = IntVecOffsetVS.m_dY+NormalVec2DC.m_dY;
	NormalVec2DC.Rotate(90.0);

	Line2DC.m_dX1 = IntersVec2DC.m_dX;                    Line2DC.m_dY1 = IntersVec2DC.m_dY;
	Line2DC.m_dX2 = IntersVec2DC.m_dX+NormalVec2DC.m_dX;  Line2DC.m_dY2 = IntersVec2DC.m_dY+NormalVec2DC.m_dY;
	

	glBegin(GL_LINES);
	
		glColor3f(1.0f, 1.0f, 1.0f);
		glVertex3f(Line2DC.m_dX1, Line2DC.m_dY1, -4.9f);
		glVertex3f(Line2DC.m_dX2, Line2DC.m_dY2, -4.9f);

	glEnd();

	glBegin(GL_LINES);
	
		glColor3f(1.0f, 1.0f, 1.0f);
		glVertex3f(Line22DC.m_dX1, Line22DC.m_dY1, -4.9f);
		glVertex3f(Line22DC.m_dX2, Line22DC.m_dY2, -4.9f);

	glEnd();*/

	/*char buffer[200];
	sprintf(buffer, "\n U is %f \n", dIntU);
	OUTD(buffer)

	glBegin(GL_POINTS);

		glPointSize(2.0);
		glColor3f(0.0f, 1.0f, 0.0f);
		glVertex3f(IntersVec2DC.m_dX, IntersVec2DC.m_dY, -4.0f);

	glEnd();*/


	/*// draw the geophones
	glPointSize(5.0);
	glBegin(GL_POINTS);


		for(int nStep = 0; nStep < this->m_nNumGeophones*2; nStep++)
		{

			glColor3f(0.0f, 1.0f, 0.0f);
			glVertex3f((*this->m_pVecGeophonesC)[nStep]->GetPos(&pPosC)->m_dX, (*this->m_pVecGeophonesC)[nStep]->GetPos(&pPosC)->m_dY, -4.0f);

		}// end for

	glEnd();


	glColor3f(1.0, 1.0, 1.0);

	char acString[200];

	// draw the geophone numbers
	for(int nStep = 0; nStep < this->m_nNumGeophones*2; nStep++)
	{

		glRasterPos2f((*this->m_pVecGeophonesC)[nStep]->GetPos(&pPosC)->m_dX-(0.5/this->m_dScale), (*this->m_pVecGeophonesC)[nStep]->GetPos(&pPosC)->m_dY+(0.7/this->m_dScale));
		this->PrintString(_itoa(nStep, acString, 10));

	}// end for

	glDisable(GL_POINT_SMOOTH);
	glPointSize(1.0);



	// draw the selected rays for the current geophone
	if((*this->m_pVecGeophonesC)[this->m_nSelGeo]->DetectedRefl() && (*this->m_pVecGeophonesC)[this->m_nSelGeo]->GetDrawRefl())
	{

		// draw the reflected ray trajectory
		for(int nCurrRay = 0; nCurrRay < (*this->m_pVecGeophonesC)[this->m_nSelGeo]->GetReflTrajectory()->size(); nCurrRay++)
		{
			
			C2DLine ReflTrajLineC((*(*this->m_pVecGeophonesC)[this->m_nSelGeo]->GetReflTrajectory())[nCurrRay]);
			// draw the reflected ray
			glBegin(GL_LINES);

				glColor3f(0.0f, 1.0f, 0.0f);
				glVertex3f(ReflTrajLineC.m_dX1, ReflTrajLineC.m_dY1, -4.9f);
				glVertex3f(ReflTrajLineC.m_dX2, ReflTrajLineC.m_dY2, -4.9f);

			glEnd();

		}// end for

	}// end if



	if((*this->m_pVecGeophonesC)[this->m_nSelGeo]->DetectedRefr() && (*this->m_pVecGeophonesC)[this->m_nSelGeo]->GetDrawRefr())
	{

		// draw the refracted ray trajectory
		for(int nCurrRay = 0; nCurrRay < (*this->m_pVecGeophonesC)[this->m_nSelGeo]->GetRefrTrajectory()->size(); nCurrRay++)
		{
			
			C2DLine RefrTrajLineC((*(*this->m_pVecGeophonesC)[this->m_nSelGeo]->GetRefrTrajectory())[nCurrRay]);
			// draw the reflected ray
			glBegin(GL_LINES);

				glColor3f(0.0f, 1.0f, 0.0f);
				glVertex3f(RefrTrajLineC.m_dX1, RefrTrajLineC.m_dY1, -4.9f);
				glVertex3f(RefrTrajLineC.m_dX2, RefrTrajLineC.m_dY2, -4.9f);

			glEnd();

		}// end for

	}// end if*/

		


		/*OUTD("\n Drawing the lines");
		// draw the temp trajectory
		for(int nCurrRay = 0; nCurrRay < this->m_VecRayTrajectoryC.size(); nCurrRay++)
		{
			
			OUTD("\n Drawing a line");
			C2DLine RefrTrajLineC(this->m_VecRayTrajectoryC[nCurrRay]);
			// draw the refracted ray
			glBegin(GL_LINES);

				glColor3f(0.0f, 1.0f, 0.0f);
				glVertex3f(RefrTrajLineC.m_dX1, RefrTrajLineC.m_dY1, -4.9f);
				glVertex3f(RefrTrajLineC.m_dX2, RefrTrajLineC.m_dY2, -4.9f);

			glEnd();

		}// end for
		OUTD("\n finished drawing lines");*/



	glFlush();

	SwapBuffers(this->m_pHDCS);			// bring backbuffer to foreground


}// end void CSystem::Draw(CSystem::m_eDrawMode eMode_)




Share this post


Link to post
Share on other sites
Try calling glColor3f(1.0f, 1.0f, 1.0f) after rendering your control points.

Right now, the control point color is also used as the vertex color for your objects.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this