首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网络游戏 >

OpenGL怎样使光源不动?该如何处理

2012-04-09 
OpenGL怎样使光源不动?#include gl/glut.h#include math.h#define pi 3.1415926float m0,r1float c

OpenGL怎样使光源不动?
#include <gl/glut.h>
#include <math.h>

#define pi 3.1415926

float m=0,r=1;
float c=pi/180;

static bool first=true;

void init(){


GLfloat light0_diffuse[]= { 0.0f, 0.0f, 1.0f, 1.0f};
GLfloat light0_position[] = {0.2, 0.6, 1.2, 1.0f };


glClearColor(0,0,0,1);
glColor3f(1.0,0.0,0.0);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.5,1.5,-1.5,1.5,-4,4);


glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();


glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
glLightfv(GL_LIGHT0, GL_POSITION,light0_position);



}
void myidle(){

m+=1;
if(m>360)
m-=360;
glutPostRedisplay();
}

void mymouse(int button,int state,int x,int y){

if(state==GLUT_DOWN && button==GLUT_LEFT_BUTTON){
if(first){
glutIdleFunc(myidle);
first=!first;
}
else{
glutIdleFunc(0);
first=!first;
}
}

if(state==GLUT_DOWN && button==GLUT_RIGHT_BUTTON){
r+=0.1;
if(r>1.2)
r=0.5;
glutPostRedisplay();
}
}

void mydisplay(){

float a,b,x,y,z,d=10;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glRotated(m,1,1,1);

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


glBegin(GL_QUAD_STRIP);

for(a=0;a<=180;a+=d){
for(b=0;b<=360;b+=d){

x=r*sin(a*c)*cos(b*c);
y=r*sin(a*c)*sin(b*c);
z=r*cos(a*c);
glVertex3f(x,y,z);

x=r*sin(a*c)*cos((b+d)*c);
y=r*sin(a*c)*sin((b+d)*c);
z=r*cos(a*c);
glVertex3f(x,y,z);


}
}

glEnd();

glBegin(GL_QUAD_STRIP);

for(b=0;b<=360;b+=d){
for(a=0;a<=180;a+=d){

x=r*sin(a*c)*cos(b*c);
y=r*sin(a*c)*sin(b*c);
z=r*cos(a*c);
glVertex3f(x,y,z);

x=r*sin(a*c)*cos((b+d)*c);
y=r*sin(a*c)*sin((b+d)*c);
z=r*cos(a*c);
glVertex3f(x,y,z);

}
}

glEnd();

glutSwapBuffers();
}

int main(int argc,char * argv[]){

glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(600,600);
glutCreateWindow("旋转的球");

init();

glutDisplayFunc(mydisplay);
glutMouseFunc(mymouse);

glutMainLoop();
return 0;
}

[解决办法]
glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
glLightfv(GL_LIGHT0, GL_POSITION,light0_position);

放在GLfloat light0_position[] = {0.2, 0.6, 1.2, 1.0f };这句话后面试试

热点排行