opengl Test

时间:2022-05-12 14:11:28
LIBS += -lGL -lGLU -lglut -lGLEW
 #include <GL/glut.h>
#include <iostream>
using namespace std;
GLfloat light0_position[] = { 1.0, 1.0, 1.0, 0.0 }; GLuint fogMode[]= { GL_EXP, GL_EXP2, GL_LINEAR }; // 雾气的模式 GLuint fogfilter= ; // 使用哪一种雾气 GLfloat fogColor[]= {0.5f, 0.5f, 0.5f, 1.0f}; // 雾的颜色设为白色
void init(void)
{ glClearColor(0.5f,0.5f,0.5f,1.0f); GLfloat mat_ambient[]= { 0.2, 0.2, 0.2, 1.0 };
GLfloat mat_diffuse[]= { 0.8, 0.8, 0.8, 1.0 };
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess[] = { 60.0 }; GLfloat light0_diffuse[]= { 0.0, 0.0, 1.0, 1.0};
// GLfloat light0_position[] = { 1.0, 1.0, 1.0, 0.0 }; GLfloat light1_ambient[]= { 0.2, 0.2, 0.2, 1.0 };
GLfloat light1_diffuse[]= { 1.0, 0.0, 0.0, 1.0 };
GLfloat light1_specular[] = { 1.0, 0.6, 0.6, 1.0 };
GLfloat light1_position[] = { -3.0, -3.0, 3.0, 1.0 }; GLfloat spot_direction[]={ 1.0,1.0,-1.0}; GLfloat emitioncolor[] = { 0.0, 1.0, 0.0, 1.0 };
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_SHININESS,mat_shininess);
// glMaterialfv(GL_FRONT, GL_EMISSION, emitioncolor); glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
glLightfv(GL_LIGHT0, GL_POSITION,light0_position); glLightfv(GL_LIGHT1, GL_AMBIENT, light1_ambient);
glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_diffuse);
glLightfv(GL_LIGHT1, GL_SPECULAR,light1_specular);
glLightfv(GL_LIGHT1, GL_POSITION,light1_position); glLightf (GL_LIGHT1, GL_SPOT_CUTOFF, 30.0);
glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION,spot_direction); glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glFogi(GL_FOG_MODE, fogMode[fogfilter]); // 设置雾气的模式 glFogfv(GL_FOG_COLOR, fogColor); // 设置雾的颜色 glFogf(GL_FOG_DENSITY, 0.55f); // 设置雾的密度 glHint(GL_FOG_HINT, GL_NICEST); // 设置系统如何计算雾气 glFogf(GL_FOG_START, -10.0f); // 雾气的开始位置 glFogf(GL_FOG_END, 10.0f); // 雾气的结束位置 glEnable(GL_FOG); // 使用雾气
}
void display(void)
{
GLfloat mat_ambient[]= { 0.2, 0.2, 0.2, 1.0 };
GLfloat mat_diffuse[]= { 0.8, 0.8, 0.8, 1.0 };
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess[] = { 60.0 }; GLfloat light0_diffuse[]= { 0.0, 0.0, 1.0, 1.0};
// GLfloat light0_position[] = { 1.0, 1.0, 1.0, 0.0 }; GLfloat light1_ambient[]= { 0.2, 0.2, 0.2, 1.0 };
GLfloat light1_diffuse[]= { 1.0, 0.0, 0.0, 1.0 };
GLfloat light1_specular[] = { 1.0, 0.6, 0.6, 1.0 };
GLfloat light1_position[] = { -3.0, -3.0, 3.0, 1.0 }; GLfloat spot_direction[]={ 1.0,1.0,-1.0}; GLfloat emitioncolor[] = { 0.0, 1.0, 0.0, 1.0 };
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_SHININESS,mat_shininess);
// glMaterialfv(GL_FRONT, GL_EMISSION, emitioncolor); glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
glLightfv(GL_LIGHT0, GL_POSITION,light0_position); glLightfv(GL_LIGHT1, GL_AMBIENT, light1_ambient);
glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_diffuse);
glLightfv(GL_LIGHT1, GL_SPECULAR,light1_specular);
glLightfv(GL_LIGHT1, GL_POSITION,light1_position); glLightf (GL_LIGHT1, GL_SPOT_CUTOFF, 30.0);
glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION,spot_direction); glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER,GL_FALSE); glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(0.5,,);
glutSolidSphere(1.0,,);
glPopMatrix();
glFlush();
}
void reshape(int w,int h)
{
glViewport(,,(GLsizei)w,(GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(w<=h)
glOrtho(-1.5,1.5,-1.5*(GLfloat)h/(GLfloat)w,1.5*(GLfloat)h/(GLfloat)w,-10.0,10.0);
else
glOrtho(-1.5*(GLfloat)w/(GLfloat)h,1.5*(GLfloat)w/(GLfloat)h,-1.5,1.5,-10.0,10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void keyAction(unsigned char key,int x,int y)
{
switch(key) {
case 'q' :
light0_position[]=light0_position[]+;
break;
case 'w' :
light0_position[]=light0_position[]+;
break;
case 'e' :
light0_position[]=light0_position[]+;
break;
case 'a' :
light0_position[]=light0_position[]-;
break;
case 's' :
light0_position[]=light0_position[]-;
break;
case 'd' :
light0_position[]=light0_position[]-;
break; case 't':
fogfilter++;
fogfilter=fogfilter%;
glFogi (GL_FOG_MODE, fogMode[fogfilter]);
break;
}
glutPostRedisplay();
}
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(,);
glutInitWindowPosition(,);
glutCreateWindow("hello,GL");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyAction);
glutMainLoop();
return ;
}
#include <GL/glut.h>
#include <iostream>
using namespace std;
GLfloat light0_position[] = { 1.0, 1.0, 1.0, 0.0 }; GLuint fogMode[]= { GL_EXP, GL_EXP2, GL_LINEAR }; // 雾气的模式 GLuint fogfilter= ; // 使用哪一种雾气 GLfloat fogColor[]= {0.5f, 0.5f, 0.5f, 1.0f}; // 雾的颜色设为白色
void init(void)
{ glClearColor(0.5f,0.5f,0.5f,1.0f); GLfloat mat_ambient[]= { 0.2, 0.2, 0.2, 1.0 };
GLfloat mat_diffuse[]= { 0.8, 0.8, 0.8, 1.0 };
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess[] = { 60.0 }; GLfloat light0_diffuse[]= { 0.0, 0.0, 1.0, 1.0};
// GLfloat light0_position[] = { 1.0, 1.0, 1.0, 0.0 }; GLfloat light1_ambient[]= { 0.2, 0.2, 0.2, 1.0 };
GLfloat light1_diffuse[]= { 1.0, 0.0, 0.0, 1.0 };
GLfloat light1_specular[] = { 1.0, 0.6, 0.6, 1.0 };
GLfloat light1_position[] = { -3.0, -3.0, 3.0, 1.0 }; GLfloat spot_direction[]={ 1.0,1.0,-1.0}; GLfloat emitioncolor[] = { 0.0, 1.0, 0.0, 1.0 };
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_SHININESS,mat_shininess);
// glMaterialfv(GL_FRONT, GL_EMISSION, emitioncolor); glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
glLightfv(GL_LIGHT0, GL_POSITION,light0_position); glLightfv(GL_LIGHT1, GL_AMBIENT, light1_ambient);
glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_diffuse);
glLightfv(GL_LIGHT1, GL_SPECULAR,light1_specular);
glLightfv(GL_LIGHT1, GL_POSITION,light1_position); glLightf (GL_LIGHT1, GL_SPOT_CUTOFF, 30.0);
glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION,spot_direction); glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glFogi(GL_FOG_MODE, fogMode[fogfilter]); // 设置雾气的模式 glFogfv(GL_FOG_COLOR, fogColor); // 设置雾的颜色 glFogf(GL_FOG_DENSITY, 0.55f); // 设置雾的密度 glHint(GL_FOG_HINT, GL_NICEST); // 设置系统如何计算雾气 glFogf(GL_FOG_START, -10.0f); // 雾气的开始位置 glFogf(GL_FOG_END, 10.0f); // 雾气的结束位置 glEnable(GL_FOG); // 使用雾气
}
void display(void)
{
GLfloat mat_ambient[]= { 0.2, 0.2, 0.2, 1.0 };
GLfloat mat_diffuse[]= { 0.8, 0.8, 0.8, 1.0 };
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess[] = { 60.0 }; GLfloat light0_diffuse[]= { 0.0, 0.0, 1.0, 1.0};
// GLfloat light0_position[] = { 1.0, 1.0, 1.0, 0.0 }; GLfloat light1_ambient[]= { 0.2, 0.2, 0.2, 1.0 };
GLfloat light1_diffuse[]= { 1.0, 0.0, 0.0, 1.0 };
GLfloat light1_specular[] = { 1.0, 0.6, 0.6, 1.0 };
GLfloat light1_position[] = { -3.0, -3.0, 3.0, 1.0 }; GLfloat spot_direction[]={ 1.0,1.0,-1.0}; GLfloat emitioncolor[] = { 0.0, 1.0, 0.0, 1.0 };
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_SHININESS,mat_shininess);
// glMaterialfv(GL_FRONT, GL_EMISSION, emitioncolor); glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
glLightfv(GL_LIGHT0, GL_POSITION,light0_position); glLightfv(GL_LIGHT1, GL_AMBIENT, light1_ambient);
glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_diffuse);
glLightfv(GL_LIGHT1, GL_SPECULAR,light1_specular);
glLightfv(GL_LIGHT1, GL_POSITION,light1_position); glLightf (GL_LIGHT1, GL_SPOT_CUTOFF, 30.0);
glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION,spot_direction); glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER,GL_FALSE); glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(0.5,,);
glutSolidSphere(1.0,,);
glPopMatrix();
glFlush();
}
void reshape(int w,int h)
{
glViewport(,,(GLsizei)w,(GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(w<=h)
glOrtho(-1.5,1.5,-1.5*(GLfloat)h/(GLfloat)w,1.5*(GLfloat)h/(GLfloat)w,-10.0,10.0);
else
glOrtho(-1.5*(GLfloat)w/(GLfloat)h,1.5*(GLfloat)w/(GLfloat)h,-1.5,1.5,-10.0,10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void keyAction(unsigned char key,int x,int y)
{
switch(key) {
case 'q' :
light0_position[]=light0_position[]+;
break;
case 'w' :
light0_position[]=light0_position[]+;
break;
case 'e' :
light0_position[]=light0_position[]+;
break;
case 'a' :
light0_position[]=light0_position[]-;
break;
case 's' :
light0_position[]=light0_position[]-;
break;
case 'd' :
light0_position[]=light0_position[]-;
break; case 't':
fogfilter++;
fogfilter=fogfilter%;
glFogi (GL_FOG_MODE, fogMode[fogfilter]);
break;
}
glutPostRedisplay();
}
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(,);
glutInitWindowPosition(,);
glutCreateWindow("hello,GL");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyAction);
glutMainLoop();
return ;
}