首页 > 代码库 > Lane-学习OpenGL(1)-七个简单例子

Lane-学习OpenGL(1)-七个简单例子

在看了许多实例之后,

准备沿着OpenGL编程指南(第七版)学习OpenGL.

运行环境:

(1)Windows 7

(2)CodeBlocks

(3)GLUT

(4)Author:Lane

2014-12-02

1.黑色背景加白色矩形

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

#include <cstdlib>
#include <cstdio>
#include <cmath>

void display(void){
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0,1.0,1.0);//白色
    glBegin(GL_POLYGON);
        glVertex3f(0.25,0.25,0.0);
        glVertex3f(0.75,0.25,0.0);
        glVertex3f(0.75,0.75,0.0);
        glVertex3f(0.25,0.75,0.0);
    glEnd();
    glFlush();//刷新,将缓冲区数据输出
}

void init(void){
    glClearColor(0.0,0.0,0.0,0.0);//黑色背景
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0,1.0,0.0,1.0,-1.0,1.0);
}

int main(int argc,char** argv){
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
    glutInitWindowPosition(100,100);//窗口位置
    glutInitWindowSize(250,250);//窗口大小
    glutCreateWindow("hello");//窗口命名
    init();
    glutDisplayFunc(display);
    glutMainLoop();
    return 0;
}

2.单击触发旋转事件,鼠标轮轴点击停止的白色矩形

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

#include <cstdlib>
#include <cstdio>
#include <cmath>

static GLfloat spin = 0.0;

void init(void){
    glClearColor(0.0,0.0,0.0,0.0);//黑色背景
    glShadeModel(GL_FLAT);
}

void display(void){
    glClear(GL_COLOR_BUFFER_BIT);
    glPushMatrix();
        glRotatef(spin,0.0,0.0,1.0);
        glColor3f(1.0,1.0,1.0);//白色
        glRectf(-25.0,-25.0,25.0,25.0);
    glPopMatrix();
    glutSwapBuffers();//交换缓冲
}

//CPU闲暇时间执行此函数
void spinDisplay(void){
    spin = spin + 2.0;
    if(spin > 360.0)
        spin -= 360.0;
    glutPostRedisplay();//标记当前窗口重新绘制
}

void reshape(int w,int h){
    glViewport(0,0,(GLsizei) w,(GLsizei) h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();//恢复初始坐标
    glOrtho(-50.0,50.0,-50.0,50.0,-1.0,1.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();//恢复初始坐标
}

void mouse(int button,int state,int x,int y){
    switch(button){
        case GLUT_LEFT_BUTTON://单击事件
            if(state == GLUT_DOWN)
                glutIdleFunc(spinDisplay);//触发旋转事件
            break;
        case GLUT_MIDDLE_BUTTON://轮轴点击事件
            if(state == GLUT_DOWN)
                glutIdleFunc(NULL);//去除旋转事件
            break;
        default:
            break;
    }
}

int main(int argc,char** argv){
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);//双缓冲
    glutInitWindowSize(250,250);
    glutInitWindowPosition(100,100);
    glutCreateWindow(argv[0]);
    init();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);//设置变形函数
    glutMouseFunc(mouse);//设置鼠标事件
    glutMainLoop();
    return 0;
}



2014-12-03

3.虚线画线

核心函数glLineStipple(1,0x0101);

第一个参数factor,可以理解为倍数.

第二个参数二进制写出 0000 0001 0000 0001 .

1代表需要绘制,0代表忽略.顺序是二进制的逆序.

即表示绘制1个点->忽略7个点->绘制1个点->忽略7个点.

如果factor为2.

即表示绘制2个点->忽略14个点->绘制2个点->忽略14个点.

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

#include <cstdlib>
#include <cstdio>
#include <cmath>

#define drawOneLine(x1,y1,x2,y2) glBegin(GL_LINES);glVertex2f((x1),(y1));glVertex2f((x2),(y2));glEnd();

void init(void){
    glClearColor(0.0,0.0,0.0,0.0);//黑色背景
    glShadeModel(GL_FLAT);
}

void display(void){
    int i;

    glClear(GL_COLOR_BUFFER_BIT);//设置背景

    glColor3f(1.0,1.0,1.0);
    glEnable(GL_LINE_STIPPLE);//设置启用虚线
    glLineStipple(1,0x0101);
    drawOneLine(50.0,125.0,150.0,125.0);
    glLineStipple(1,0x00FF);
    drawOneLine(150.0,125.0,250.0,125.0);
    glLineStipple(1,0x1C47);
    drawOneLine(250.0,125.0,350.0,125.0);

    glLineWidth(5.0);
    glLineStipple(1,0x0101);
    drawOneLine(50.0,100.0,150.0,100.0);
    glLineStipple(1,0x00FF);
    drawOneLine(150.0,100.0,250.0,100.0);
    glLineStipple(1,0x1C47);
    drawOneLine(250.0,100.0,350.0,100.0);

    glLineWidth(1.0);
    glLineStipple(1,0x1C47);
    glBegin(GL_LINE_STRIP);
        for(i = 0; i < 7; i++)
            glVertex2f(50.0+((GLfloat) i * 50.0),75.0);
    glEnd();
    for(i = 0; i < 6; i++){
        drawOneLine(50.0 + ((GLfloat) i * 50.0),50.0,50.0+((GLfloat)(i+1)*50.0),50.0);
    }
    glLineStipple(5,0x1C47);
    drawOneLine(50.0,25.0,350.0,25.0);

    glDisable(GL_LINE_STIPPLE);
    glFlush();
}

void reshape(int w,int h){
    glViewport(0,0,(GLsizei) w,(GLsizei) h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0,(GLdouble) w,0.0,(GLdouble) h);
}

int main(int argc, char** argv){
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
    glutInitWindowSize(400,150);
    glutInitWindowPosition(100,100);
    glutCreateWindow(argv[0]);
    init();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMainLoop();
    return 0;
}

4.多边形画点

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

#include <cstdlib>
#include <cstdio>
#include <cmath>

void display(void){
    GLubyte fly[] = {
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x03,0x80,0x01,0xC0,0x06,0xC0,0x03,0x60,
        0x04,0x60,0x06,0x20,0x04,0x30,0x0C,0x20,
        0x04,0x18,0x18,0x20,0x04,0x0C,0x30,0x20,

        0x04,0x06,0x60,0x20,0x04,0x03,0xC0,0x22,
        0x44,0x01,0x80,0x22,0x44,0x01,0x80,0x22,
        0x44,0x01,0x80,0x22,0x44,0x01,0x80,0x22,
        0x44,0x01,0x80,0x22,0x44,0x01,0x80,0x22,

        0x66,0x01,0x80,0x66,0x33,0x01,0x80,0xCC,
        0x19,0x81,0x81,0x98,0x0C,0xC1,0x83,0x30,
        0x07,0xe1,0x87,0xe0,0x03,0x3f,0xfc,0xc0,
        0x03,0x31,0x8c,0xc0,0x03,0x33,0xCC,0xC0,

        0x06,0x64,0x26,0x60,0x0c,0xcc,0x33,0x30,
        0x18,0xcc,0x33,0x18,0x10,0xc4,0x23,0x08,
        0x10,0x63,0xc6,0x08,0x10,0x30,0x0c,0x08,
        0x10,0x18,0x18,0x08,0x10,0x00,0x00,0x08,
    };

    GLubyte halftone[] = {
        0xAA,0xAA,0xAA,0xAA,0x55,0x55,0x55,0x55,
        0xAA,0xAA,0xAA,0xAA,0x55,0x55,0x55,0x55,
        0xAA,0xAA,0xAA,0xAA,0x55,0x55,0x55,0x55,
        0xAA,0xAA,0xAA,0xAA,0x55,0x55,0x55,0x55,
        0xAA,0xAA,0xAA,0xAA,0x55,0x55,0x55,0x55,
        0xAA,0xAA,0xAA,0xAA,0x55,0x55,0x55,0x55,
        0xAA,0xAA,0xAA,0xAA,0x55,0x55,0x55,0x55,
        0xAA,0xAA,0xAA,0xAA,0x55,0x55,0x55,0x55,
        0xAA,0xAA,0xAA,0xAA,0x55,0x55,0x55,0x55,
        0xAA,0xAA,0xAA,0xAA,0x55,0x55,0x55,0x55,
        0xAA,0xAA,0xAA,0xAA,0x55,0x55,0x55,0x55,
        0xAA,0xAA,0xAA,0xAA,0x55,0x55,0x55,0x55,
        0xAA,0xAA,0xAA,0xAA,0x55,0x55,0x55,0x55,
        0xAA,0xAA,0xAA,0xAA,0x55,0x55,0x55,0x55,
        0xAA,0xAA,0xAA,0xAA,0x55,0x55,0x55,0x55,
        0xAA,0xAA,0xAA,0xAA,0x55,0x55,0x55,0x55,
    };

    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0,1.0,1.0);
    glRectf(25.0,25.0,125.0,125.0);
    glEnable(GL_POLYGON_STIPPLE);
    glPolygonStipple(fly);
    glRectf(125.0,25.0,225.0,125.0);
    glPolygonStipple(halftone);
    glRectf(225.0,25.0,325.0,125.0);
    glDisable(GL_POLYGON_STIPPLE);
    glFlush();
}

void init(void){
    glClearColor(0.0,0.0,0.0,0.0);
    glShadeModel(GL_FLAT);
}

void reshape(int w,int h){
    glViewport(0,0,(GLsizei)w,(GLsizei)h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0,(GLdouble)w,0.0,(GLdouble)h);
}

int main(int argc,char** argv){
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
    glutInitWindowSize(350,150);
    glutCreateWindow(argv[0]);
    init();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMainLoop();
    return 0;
}


5.反馈模式

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

#include <cstdlib>
#include <cstdio>
#include <cmath>

void init(void){
    glEnable(GL_LIGHTING);//启用光源
    glEnable(GL_LIGHT0);//启用光源0
}

void drawGeometry(GLenum mode){
    glBegin(GL_LINE_STRIP);
        glNormal3f(0.0,0.0,1.0);
        glVertex3f(30.0,30.0,0.0);
        glVertex3f(50.0,60.0,0.0);
        glVertex3f(70.0,40.0,0.0);
    glEnd();
    if(mode == GL_FEEDBACK)
        glPassThrough(1.0);
    glBegin(GL_POINTS);
        glVertex3f(-100.0,-100.0,-100.0);
    glEnd();
    if(mode == GL_FEEDBACK)
        glPassThrough(2.0);
    glBegin(GL_POINTS);
        glNormal3f(0.0,0.0,1.0);
        glVertex3f(50.0,50.0,0.0);
    glEnd();
    glFlush();
}

void print3DcolorVertex(GLint size,GLint *count,GLfloat *buffer){
    int i;
    printf(" ");
    for(i = 0; i < 7; i++){
        printf("%4.2f ",buffer[size-(*count)]);
        *count = *count - 1;
    }
    printf("\n ");
}

void printBuffer(GLint size,GLfloat *buffer){
    GLint count;
    GLfloat token;

    count = size;
    while(count){
        token = buffer[size-count];
        count--;
        if(token == GL_PASS_THROUGH_TOKEN){
            printf("GL_PASS_THROUGH_TOKEN \n ");
            printf(" %4.2f \n ",buffer[size-count]);
            count--;
        }else if(token == GL_POINT_TOKEN){
            printf("GL_POINT_TOKEN \n ");
            print3DcolorVertex(size,&count,buffer);
        }else if(token == GL_LINE_TOKEN){
            printf("GL_LINE_TOKEN \n ");
            print3DcolorVertex(size,&count,buffer);
            print3DcolorVertex(size,&count,buffer);
        }else if(token == GL_LINE_RESET_TOKEN){
            printf("GL_LINE_RESET_TOKEN \n ");
            print3DcolorVertex(size,&count,buffer);
            print3DcolorVertex(size,&count,buffer);
        }
    }
}

void display(void){
    GLfloat feedBuffer[1024];
    GLint size;
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0,100.0,0.0,100.0,0.0,1.0);

    glClearColor(0.0,0.0,0.0,0.0);
    glClear(GL_COLOR_BUFFER_BIT);
    drawGeometry(GL_RENDER);

    glFeedbackBuffer(1024,GL_3D_COLOR,feedBuffer);
    (void)glRenderMode(GL_FEEDBACK);
    drawGeometry(GL_FEEDBACK);
    size = glRenderMode(GL_RENDER);
    printBuffer(size,feedBuffer);
}

int main(int argc,char** argv){
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
    glutInitWindowSize(100,100);
    glutInitWindowSize(100,100);
    glutCreateWindow(argv[0]);
    init();
    glutDisplayFunc(display);
    glutMainLoop();
    return 0;
}


6.变换立方体

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

#include <cstdlib>
#include <cstdio>
#include <cmath>

void init(void){
    glClearColor(0.0,0.0,0.0,0.0);//黑色背景
    glShadeModel(GL_FLAT);//单调着色
}

void display(void){
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0,1.0,1.0);//白色
    glLoadIdentity();//恢复初始坐标系
    gluLookAt(0.0,0.0,5.0,0.0,0.0,0.0,0.0,1.0,0.0);
    glScalef(1.0,2.0,1.0);//缩放
    glutWireCube(1.0);//绘制立方体
    glFlush();
}

void reshape(int w,int h){
    glViewport(0,0,(GLsizei)w,(GLsizei) h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glFrustum(-1.0,1.0,-1.0,1.0,1.5,20.0);
    glMatrixMode(GL_MODELVIEW);
}

int main(int argc,char** argv){
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
    glutInitWindowPosition(100,100);
    glutInitWindowSize(500,500);
    glutCreateWindow(argv[0]);
    init();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMainLoop();
    return 0;
}

7.经过两个裁剪平面裁剪的线框球体

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

#include <cstdlib>
#include <cstdio>
#include <cmath>

void init(void){
    glClearColor(0.0,0.0,0.0,0.0);//黑色
    glShadeModel(GL_FLAT);//单调着色
}

void display(void){
    GLdouble eqn[4] = {0.0,1.0,0.0,0.0};//Y轴裁剪
    GLdouble eqn2[4] = {1.0,0.0,0.0,0.0};//X轴裁剪

    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0,1.0,1.0);

    glPushMatrix();
    glTranslatef(0.0,0.0,-5.0);
    glClipPlane(GL_CLIP_PLANE0,eqn);//启用裁剪平面0
    glEnable(GL_CLIP_PLANE0);//启用裁剪0
    glClipPlane(GL_CLIP_PLANE1,eqn2);//启用裁剪平面1
    glEnable(GL_CLIP_PLANE1);//启用裁剪1
    glRotatef(90.0,1.0,0.0,0.0);
    glutWireSphere(1.0,20,16);//绘制球体
    glPopMatrix();
    glFlush();
}

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);
}

int main(int argc,char** argv){
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
    glutInitWindowSize(500,500);
    glutInitWindowPosition(100,100);
    glutCreateWindow(argv[0]);
    init();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMainLoop();
    return 0;
}



Lane-学习OpenGL(1)-七个简单例子