1. 程式人生 > >本節將演示在3D空間中繪製圖形的幾個簡單例項:

本節將演示在3D空間中繪製圖形的幾個簡單例項:

複製程式碼
#include <GL/glut.h>
#pragma comment(linker,"/subsystem:\"windows\" /entry:\"mainCRTStartup\"")

GLfloat yRot = 0;

//用來畫一個座標軸的函式
void axis(double length)
{
    glColor3f(1.0f, 1.0f, 1.0f);
    glPushMatrix();
    glBegin(GL_LINES);
    glVertex3d(0.0, 0.0, 0.0);
    glVertex3d(0.0, 0.0, length);
    glEnd();
    
//將當前操作點移到指定位置 glTranslated(0.0, 0.0, length - 0.2); glColor3f(1.0, 0.0, 0.0); glutWireCone(0.04, 0.3, 8, 8); glPopMatrix(); } void paint(void) { glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-2.0, 2.0, -2.0, 2.0, -100, 100); glPointSize(1); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(
1.3, 1.6, 2.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); //畫座標系 axis(2); glPushMatrix(); glRotated(90.0, 0, 1.0, 0);//繞y軸正方向旋轉90度 axis(2); glPopMatrix(); glPushMatrix(); glRotated(-90.0, 1.0, 0.0, 0.0);//繞x軸負方向旋轉 axis(2); glPopMatrix(); glPushMatrix();//旋轉除座標軸之外的物體 glRotated(yRot, 0.0, yRot, 0.0
); glPushMatrix(); glColor3f(0.0f, 0.0f, 1.0f); glTranslated(0.125, 0.125, 0.125); glutWireCube(0.25); glPopMatrix(); glPushMatrix(); glTranslated(1.125,0.125,0.125); glutWireTeapot(0.25); glPopMatrix(); glPushMatrix(); glTranslated(0.125, 0.125, 1.125); glutWireSphere(0.25, 12, 8); glPopMatrix(); glPushMatrix(); glTranslated(1.125,0.125,1.125); glRotated(-90, 1.0, 0.0, 0.0); glutWireTorus(0.1, 0.25, 12, 12); glPopMatrix(); glPushMatrix(); glTranslated(0.125, 1.125, 0.125); glScaled(0.5*0.25, 0.5*0.25, 0.5*0.25); glRotated(-90, 0.0, 0.0, 1.0); glutWireDodecahedron(); glPopMatrix(); glPushMatrix(); glTranslated(0.0, 1.0, 1.0); GLUquadricObj * quadricObj = gluNewQuadric(); gluQuadricDrawStyle(quadricObj, GLU_LINE); gluCylinder(quadricObj, 0.2, 0.2, 0.3, 6, 6); glRotated(-90, 0.0, 0.0, 0.0); glPopMatrix(); glPopMatrix();//用來整體繞y軸旋轉 glutSwapBuffers(); } void Init() { glClearColor(0.8,0.8,0.8,1.0); } void reshape(int w, int h) { glViewport(0, 0, (GLsizei)w, (GLsizei)h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(60.0, (GLfloat)w / (GLfloat)h, 1.0, 20.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(1.3, 1.6, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); } void SpecialKeys(int key, int x, int y) { if (key == GLUT_KEY_LEFT) yRot -= 5.0f; if (key == GLUT_KEY_RIGHT) yRot += 5.0f; if (key> 356.0f) yRot = 0.0f; if (key< -1.0f) yRot = 355.0f; glutPostRedisplay(); } int main(int argv, char *argc[]) { glutInit(&argv, argc); glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); glutInitWindowSize(400, 400); glutInitWindowPosition(400, 300); glutCreateWindow("3D空間繪製各種系統自帶立方體"); Init(); glutDisplayFunc(paint); glutReshapeFunc(reshape); glutSpecialFunc(SpecialKeys); glutMainLoop(); }
複製程式碼