(如有翻譯或解釋不太洽當,請多多指教)
glViewport
投影預覽
glMatrixMode
矩陣模式
gluPerspective
透視
gluLookAt
視點
glLightfv
光線
glLightModelfv
光線模式
glutSolidSphere
實心球面
glRotatef
旋轉
Code:
#include <GL/glut.h>
#include <stdlib.h>
GLuint list;
float spinx = 0;
float spiny = 0;
float spinz = 0;
GLfloat tdist = 0.0;
GLfloat polyfactor = 1.0;
GLfloat polyunits = 1.0;
float delang = 5.0;
int xAbs, yAbs;
void display (void)
{
GLfloat gray[] = { 0.8, 0.8, 0.8, 1.0 };
GLfloat black[] = { 0.0, 0.0, 0.0, 1.0 };
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix ();
glTranslatef (0.0, 0.0, tdist);
glRotatef ((GLfloat) spinx, 1.0, 0.0, 0.0);
glRotatef ((GLfloat) spiny, 0.0, 1.0, 0.0);
glRotatef((GLfloat) spinz, 0.0, 0.0, 1.0);
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
glMaterialfv(GL_FRONT, GL_SPECULAR, black);
glMaterialf(GL_FRONT, GL_SHININESS, 0.0);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_POLYGON_OFFSET_FILL);
glPolygonOffset(polyfactor, polyunits);
glCallList (list);
glDisable(GL_POLYGON_OFFSET_FILL);
glDisable(GL_LIGHTING);
glDisable(GL_LIGHT0);
glColor3f (1.0, 1.0, 0.0);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glCallList (list);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glPopMatrix ();
glFlush ();
}
void gfxinit (void)
{
GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
GLfloat global_ambient[] = { 0.2, 0.2, 0.2, 1.0 };
glClearColor (0.0, 0.0, 0.0, 1.0);
list = glGenLists(1);
glNewList (list, GL_COMPILE);
glutSolidSphere(1.0, 20, 12);
glEndList ();
glEnable(GL_DEPTH_TEST);
glLightfv (GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv (GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv (GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv (GL_LIGHT0, GL_POSITION, light_position);
glLightModelfv (GL_LIGHT_MODEL_AMBIENT, global_ambient);
}
// 重塑
void reshape(int width, int height)
{
glViewport (0, 0, width, height);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective(45.0, (GLdouble)width/(GLdouble)height,
1.0, 10.0);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}
void CheckRot()
{
if (spinx > 360.0f)
spinx = 0.0f;
if (spinx < 0.0f)
spinx = 360.0-delang;
if (spiny > 360.0f)
spiny = 0.0f;
if (spiny < 0.0f)
spiny = 360.0f-delang;
if (spinz > 360.0f)
spinz = 0.0f;
if (spinz < 0.0f)
spinz = 360.0f-delang;
}
// 滑鼠移動
void MouseMove( int x, int y) {
spinx += (y-yAbs);
yAbs = y;
spiny -= (x-xAbs);
xAbs = x;
CheckRot();
glutPostRedisplay();
}
// 滑鼠按下
void MousePress(int button, int state, int x, int y)
{
if ((button == GLUT_LEFT_BUTTON) && (state == GLUT_DOWN))
{
xAbs = x;
yAbs = y;
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH); //設定初始顯示模組
glutCreateWindow(argv[0]);
glutReshapeFunc(reshape); // 重塑
glutMotionFunc (MouseMove); // 滑鼠移動事件
glutDisplayFunc(display); // 顯示
gfxinit();
glutMainLoop();
return 0;
}
=====================================================
光源 glLightfv
glLightfv( 光源, 光源屬性, RGBA );
// 設定發光體的光源的特性glEnable(GL_DEPTH_TEST); //啟動深度測試
glLightfv( GL_LIGHT0, GL_AMBIENT, light_ambient); //設定 環境光 GL_AMBIENT
glLightfv( GL_LIGHT0, GL_DIFFUSE, light_diffuse); //設定 散射光 GL_DIFFUSE
glLightfv( GL_LIGHT0, GL_SPECULAR,light_specular); //設定 反射光 GL_SPECULAR
glLightfv( GL_LIGHT0, GL_POSITION,light_position); //光的座標
glLightModelfv (GL_LIGHT_MODEL_AMBIENT, global_ambient); // 光線模型