C++ opengl 漫反射和镜面反射参数

漫反射和镜面反射参数

开启光照后,物体表面的颜色:由 环境光+漫反射+镜面反射 组成

漫反射:
glLightfv(GL_LIGHT0,GL_DIFFUSE,whiteColor);
glMaterialfv(GL_FRONT,GL_DIFFUSE,diffuseMat);

镜面反射:
glLightfv(GL_LIGHT0,GL_SPECULAR,whiteColor);
glMaterialfv(GL_FRONT,GL_SPECULAR,specularMat);

 

伪代码如下:

void Draw() {
	glClearColor(30.0f / 255.0f, 30.0f / 255.0f, 30.0f / 255.0f, 1.0f);
	glClear(GL_COLOR_BUFFER_BIT);
	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT0);
	float lightPos[] = { 0.0f,1.0f,0.0f,0.0f };
	glLightfv(GL_LIGHT0, GL_POSITION, lightPos);

	float whiteColor[] = { 1.0f,1.0f,1.0f,1.0f };
	float ambientMat[] = { 0.07f,0.07f,0.07f,1.0f };
	glLightfv(GL_LIGHT0, GL_AMBIENT, whiteColor);
	glMaterialfv(GL_FRONT, GL_AMBIENT, ambientMat);

	float diffuseMat[] = { 0.4f,0.4f,0.4f,1.0f };
	float specularMat[] = { 0.9f,0.9f,0.9f,1.0f };
	float blackColor[] = { 0.0f,0.0f,0.0f,1.0f };

	glLightfv(GL_LIGHT0, GL_DIFFUSE, whiteColor);
	glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuseMat);

	glLightfv(GL_LIGHT0, GL_SPECULAR, whiteColor);
	glMaterialfv(GL_FRONT, GL_SPECULAR, specularMat);

	glBegin(GL_QUADS);
	glColor4ub(255, 255, 255, 255);
	glVertex3f(-0.5f, -0.2f, -0.5f);
	glVertex3f(0.5f, -0.2f, -0.5f);
	glVertex3f(0.5f, -0.2f, -1.0f);
	glVertex3f(-0.5f, -0.2f, -1.0f);
	glEnd();
}

 

你可能感兴趣的:(C/C++,OpenGL)