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

时间: 2024-11-06 17:28:50

Lane-学习OpenGL(1)-七个简单例子的相关文章

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

运行环境: (1)Windows 7 (2)CodeBlocks (3)GLUT (4)Author:Lane 2014-12-03 1.行星系统,按d,D,y,Y旋转. #include <windows.h> #include <GL/gl.h> #include <GL/glu.h> #include <GL/glut.h> #include <cstdlib> #include <cstdio> #include <cm

mybatis入门学习之(1)+简单例子测试

Mybatis 入门之(1)环境配置+简单例子测试 什么是MyBatis? 它是支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis 消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索.MyBatis 使用简单的XML或注解用于配置和原始映射,将接口和POJOs(Plan Old Java Objects,普通的 Java对象)映射成数据库中的记录. 其实简单一点说mybatis就是一直访问数据库的技术,它是现在比较流行的一个持久层框架,如果你对JDBC熟悉那就更容易

Java学习(一):反射的简单例子

Java反射机制主要提供了以下功能: 在运行时判断任意一个对象所属的类:在运行时构造任意一个类的对象:在运行时判断任意一个类所具有的成员变量和方法:在运行时调用任意一个对象的方法. public class JavaTest { public static void main(String[] args) { try { Class myClass = Class.forName("Student"); Object student = (Object)myClass.newInsta

从零开始学习OpenGL ES之一 – 基本概念

我曾写过一些文章介绍iPhone OpenGL ES编程,但大部分针对的是已经至少懂得一些3D编程知识的人.作为起点,请下载我的OpenGL Xcode项目模板,而不要使用Apple提供的模板.你可以解压到下面位置安装此模板:/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Project Templates/Application/ 已经有大量有关OpenGL的好教程和书籍.但是,却没有多少是关于OpenGL ES,而

ThinkPHP 学习(2)---一个简单的起步的例子

文件夹目录是核心目录ThinkPHP,入口文件是index.php,ThinkPHP里面的文件含义下次详解,接下来是启动一个自己的程序,现在可以修改一下入口文件,一般情况下会将自己的文件放在一个文件夹,我现在是先建立自己的文件夹,一个前台Home文件夹,一个后台文件夹Admin,怎么建立一个结构拥有ThinkPHP功能的文件夹呢?打开index.php,加入如下代码, <?php    //1.确定应用名称Home    define('APP_NAME','Admin');    //2.确定

mjson学习的简单例子分享

01#include <mjson/json.h>02#include <stdio.h>03#include <stdlib.h>04int main()05{06    json_t *entry, *root, *head, *body, *label, *value;07    char *document;08    root = json_new_object();09     10    /*--------Head------*/11 12    hea

GDI+学习笔记(七)保存简单图像

请尊重本人的工作成果,转载请留言,并说明转载地址,谢谢.地址如下: http://blog.csdn.net/fukainankai/article/details/27710883 前几节中,我们利用GDI+在窗口中绘制了各种各样的图形.图像,这一节,我们将会将这些图像保存成简单图像.所谓简单图像,指的是bmp/jpg/png等图像或者单帧的gif图像.保存成多帧的gif图像稍微复杂一点,本节中暂时不做说明.保存成动态的tiff文件也比较简单,但这里也不做说明,下次有机会和gif一起介绍. 另

Excel导出学习之道:Java Web利用POI导出Excel简单例子

采用Spring mvc架构: Controller层代码如下 [java] view plaincopy @Controller public class StudentExportController{ @Autowired private StudentExportService studentExportService; @RequestMapping(value = "/excel/export") public void exportExcel(HttpServletReq

[转] 3个学习Socket编程的简单例子:TCP Server/Client, Select

以前都是采用ACE的编写网络应用,最近由于工作需要,需要直接只用socket接口编写CS的代码,重新学习这方面的知识,给出自己所用到的3个简单例子,都是拷贝别人的程序.如果你能完全理解这3个例子,估计socket编程就已经基本入门了. 建议:1) 多多查查所用到的网络接口; 2) 最好有一本书,如UNIX环境高级编程,UNIX网络编程,可查询:3) 可以直接使用书上的例子更好. http://blog.csdn.net/zhenjing/article/details/4770490 TCP C