1. 程式人生 > >利用openGL純手工打造一個金字塔

利用openGL純手工打造一個金字塔

環境:linux(ubuntu16.4)、gcc

第一步:先出一個三角形線框

  效果:

  

  實現程式碼:

    Makefile

BDIR = -L/usr/X11R6/lib
CC = gcc
CFLAGS = $(COMPILERFLAGS)
LIBRARIES = -lX11 -lXi  -lglut -lGL -lGLU -lm

pyramid : pyramid.o
    $(CC) $(CFLAGS) -o [email protected] $(LIBDIR) $? $(LIBRARIES)

clean:
    rm -f *.o

  pyramid.c

#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <GL/glext.h>
#include <sys/time.h>
#include <math.h>
#define GL_PI 3.1415f

void RenderScene(void){
    //用線框繪製
    glPolygonMode(GL_FRONT,GL_LINE);
    //用當前顏色清除螢幕(也就是SetupRC中的 glClearColor 指定的顏色)
    glClear(GL_COLOR_BUFFER_BIT);

    
//此三角形位於x、y軸的平面上 glBegin(GL_TRIANGLES); glVertex3f(50.0f,0.0f,0.0f);//右頂點 glVertex3f(0.0f,50.0f,0.0f);//上頂點 glVertex3f(-50.0f,0.0f,0.0f);//左頂點 glEnd(); glutSwapBuffers(); } void SetupRC() { glClearColor(0.0f, 0.0f, 0.0f, 1.0f ); // 設定繪製顏色為綠色 glColor3f(0.0f, 1.0f, 0.0f); } void ChangeSize(int
w, int h) { GLfloat nRange = 100.0f; // 防止清零 if(h == 0) h = 1; // 設定視口大小為當前視窗 glViewport(0, 0, w, h); //重置操作矩陣棧 glMatrixMode(GL_PROJECTION); glLoadIdentity(); // 這是透視投影裁截體 (left, right, bottom, top, near, far) if (w <= h) glOrtho (-nRange, nRange, -nRange*h/w, nRange*h/w, -nRange, nRange); else glOrtho (-nRange*w/h, nRange*w/h, -nRange, nRange, -nRange, nRange); //重置模型檢視矩陣 glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } int main(int argc, char* argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutCreateWindow("Pyramid Example"); glutReshapeFunc(ChangeSize); glutDisplayFunc(RenderScene); SetupRC(); glutMainLoop(); return 0; }

 第二步:實現旋轉

  效果(當按上下左右箭頭時實現旋轉):

  

  實現程式碼:

#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <GL/glext.h>

#include <sys/time.h>

#include <math.h>

#define GL_PI 3.1415f

static GLfloat xRot = 0.0f;
static GLfloat yRot = 0.0f;


void RenderScene(void){
    //用線框繪製
    glPolygonMode(GL_FRONT,GL_LINE);
    glPolygonMode(GL_BACK,GL_LINE);

    //用當前顏色清除螢幕(也就是SetupRC中的 glClearColor 指定的顏色)
    glClear(GL_COLOR_BUFFER_BIT);
    
    //儲存當前操作矩陣
    glPushMatrix();

    //繞x軸旋轉
    glRotatef(xRot, 1.0f, 0.0f, 0.0f);
    //繞y軸旋轉
    glRotatef(yRot, 0.0f, 1.0f, 0.0f);


    //此三角形位於x、y軸的平面上
    glBegin(GL_TRIANGLES);
        glVertex3f(50.0f,0.0f,0.0f);//右頂點
        glVertex3f(0.0f,50.0f,0.0f);//上頂點
        glVertex3f(-50.0f,0.0f,0.0f);//左頂點
    glEnd();
    //恢復當前操作矩陣
    glPopMatrix();

    glutSwapBuffers();
}

void SetupRC()
    {
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f );

    // 設定繪製顏色為綠色
    glColor3f(0.0f, 1.0f, 0.0f);
    }
void ChangeSize(int w, int h)
    {
    GLfloat nRange = 100.0f;

    // 防止清零
    if(h == 0)
        h = 1;

    // 設定視口大小為當前視窗
    glViewport(0, 0, w, h);

    //重置操作矩陣棧
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    // 這是透視投影裁截體 (left, right, bottom, top, near, far)
    if (w <= h) 
        glOrtho (-nRange, nRange, -nRange*h/w, nRange*h/w, -nRange, nRange);
    else 
        glOrtho (-nRange*w/h, nRange*w/h, -nRange, nRange, -nRange, nRange);

    //重置模型檢視矩陣
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    }
//控制鍵
void ControlKeys(int key, int x, int y)
    {
    if(key == GLUT_KEY_UP)
        xRot-= 5.0f;

    if(key == GLUT_KEY_DOWN)
        xRot += 5.0f;

    if(key == GLUT_KEY_LEFT)
        yRot -= 5.0f;

    if(key == GLUT_KEY_RIGHT)
        yRot += 5.0f;

    if(key > 356.0f)
        xRot = 0.0f;

    if(key < -1.0f)
        xRot = 355.0f;

    if(key > 356.0f)
        yRot = 0.0f;

    if(key < -1.0f)
        yRot = 355.0f;

    // Refresh the Window
    glutPostRedisplay();
    }

int main(int argc, char* argv[])
    {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutCreateWindow("Pyramid Example");
    glutReshapeFunc(ChangeSize);
    glutSpecialFunc(ControlKeys);
    glutDisplayFunc(RenderScene);

    SetupRC();
    glutMainLoop();

    return 0;
    }