opengl--光照

使能光

glEnable(GL_LIGHTING); 

材料设置

方法一

// Enable color tracking 
glEnable(GL_COLOR_MATERIAL); 
// Set Material properties to follow glColor values 
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE); 

方法二

GLfloat  specref[] =  { 1.0f, 1.0f, 1.0f, 1.0f }; 
// All materials hereafter have full specular reflectivity 
// with a high shine 
glMaterialfv(GL_FRONT, GL_SPECULAR,specref); 
glMateriali(GL_FRONT,GL_SHININESS,128); 

计算法向量

// Reduces a normal vector specified as a set of three coordinates, 
// to a unit normal vector of length 1. 
void ReduceToUnit(float vector[3]) 
{ 
	float length; 
	 
	// Calculate the length of the vector 
	length = (float)sqrt((vector[0]*vector[0]) +  
	 (vector[1]*vector[1]) + 
	 (vector[2]*vector[2])); 
	 
	 // Keep the program from blowing up by providing an acceptable zero. 
	if(length == 0.0f) 
	length = 1.0f; 
	 
	 // Dividing each element by the length will result in a 
	// unit normal vector. 
	vector[0] /= length; 
	vector[1] /= length; 
	vector[2] /= length; 
} 

// Points p1, p2, & p3 specified in counterclockwise order 
void calcNormal(float v[3][3], float out[3]) 
{ 
	float v1[3],v2[3]; 
	static const int x = 0; 
	static const int y = 1; static const int z = 2; 
	 
	 // Calculate two vectors from the three points 
	v1[x] = v[0][x] - v[1][x]; 
	v1[y] = v[0][y] - v[1][y]; 
	v1[z] = v[0][z] - v[1][z]; 
	 
	v2[x] = v[1][x] - v[2][x]; 
	v2[y] = v[1][y] - v[2][y]; 
	v2[z] = v[1][z] - v[2][z]; 
	 
	 // Take the cross product of the two vectors to get 
	 // the normal vector which will be stored in out[] 
	out[x] = v1[y]*v2[z] - v1[z]*v2[y]; 
	out[y] = v1[z]*v2[x] - v1[x]*v2[z]; 
	out[z] = v1[x]*v2[y] - v1[y]*v2[x]; 
	 
	 // Normalize the vector (shorten length to one) 
	ReduceToUnit(out); 
} 

绘制物体

// Draw the 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值