glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient)// 设置环境光
glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse)// 设置漫射光
glLightfv(GL_LIGHT1, GL_POSITION,LightPosition)// 设置光源位置
2.启用光源
glEnable(GL_LIGHT1)// 启用一号光源
2.设定各表面法线
glNormal3f( 0.0f, 0.0f,-1.0f)// 法线背向观察者
2.禁用光源
glDisable(GL_LIGHTING)// 禁用光源
标号相同即无顺序区别。
来源:NEHEOPENGL中文教程
MORE iNFORMATION , 度娘一下~
这个的话先用glEnable(GL_LIGHT0)开启0号光源,之后设置光源位置,position={x,y,z,w},x,y,z是一个向量,代表方向。x/w,t/w,z/w是位置坐标。w为零时,前面三者趋近无穷大,代表平行光。
之后输入glLightfv(GL_LIGHT0, GL_POSITION, position),这样0号光源位置确定。接下来设置环境光线强度,漫反射光线强度,镜面反射光线强度,都是r,g,b,alpha参数,这个知道怎么设吧。
设定好参数后,输入
glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse)
glMaterialfv(GL_FRONT, GL_SPECULAR, specular)
glMaterialfv(GL_FRONT, GL_EMISSION, emission)
光源设定完成。
材质设定是glMaterialfv(GL_FRONT, XXXXX,XXXXX)和光线类似,第一个XXXX写GL_DIFFUSE,GL_SPECULAR, GL_EMISSION这类的。多出一个shininess,这个是代表镜面指数,越大耗费资源越多,越精细。
呼~打得好累,最后给你编了一个例子。打了这么多字,别忘了采纳。
#include<gl/glut.h>
#include<stdio.h>
void myDisplay()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(75, 1, 9, 60)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
gluLookAt(0, -20, 20, 0, 0, 0, 0, 0, 1)
{
GLfloat light_position[]={0.0f,0.0f,0.0f,1.0f}
GLfloat light_ambient[]={0.0f,0.0f,0.0f,1.0f}
GLfloat light_diffuse[]={1.0f,1.0f,1.0f,1.0f}
GLfloat light_specular[]={1.0f,1.0f,1.0f,1.0f}
glLightfv(GL_LIGHT0,GL_POSITION,light_position)
glLightfv(GL_LIGHT0,GL_AMBIENT,light_ambient)
glLightfv(GL_LIGHT0,GL_DIFFUSE,light_diffuse)
glLightfv(GL_LIGHT0,GL_SPECULAR,light_specular)
glEnable(GL_LIGHT0)
glEnable(GL_LIGHTING)
glEnable(GL_DEPTH_TEST)
}
{
GLfloat mat_ambient[]={0.4f,0.4f,0.4f,1.0f}
GLfloat mat_diffuse[]={0.4f,0.4f,0.4f,1.0f}
GLfloat mat_specular[]={0.7f,0.7f,0.7f,1.0f}
GLfloat mat_emission[]={0.0f,0.0f,0.0f,1.f}
GLfloat mat_shininess=60.0f
glMaterialfv(GL_FRONT,GL_AMBIENT,mat_ambient)
glMaterialfv(GL_FRONT,GL_DIFFUSE,mat_diffuse)
glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular)
glMaterialfv(GL_FRONT,GL_EMISSION,mat_emission)
glMaterialf(GL_FRONT,GL_SHININESS, mat_shininess)
glTranslatef(15, 5, 5)
glutSolidSphere(10, 100, 20)
}
glFlush()
glutSwapBuffers()
}
int main(int argc,char *argv[])
{
glutInit(&argc,argv)
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE)
glutInitWindowPosition(100,100)
glutInitWindowSize(800,800)
glutCreateWindow("一个灰球")
glutDisplayFunc(&myDisplay)
glutMainLoop()
return 0
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)