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 <cmath>

static int year = 0, day = 0;

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);
    glPushMatrix();
        glutWireSphere(1.0,20,16);//绘制一个球体
        glRotatef((GLfloat)year,0.0,1.0,0.0);//旋转
        glTranslatef(2.0,0.0,0.0);//位移
        glRotatef((GLfloat)day,0.0,1.0,0.0);
        glutWireSphere(0.2,10,8);
    glPopMatrix();
    glutSwapBuffers();//交换缓冲区,因为设置了双缓冲区,一个缓冲区显示,一个缓冲区绘制
}

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(0.0,0.0,5.0,0.0,0.0,0.0,0.0,1.0,0.0);
}

void keyboard(unsigned char key,int x,int y){
    switch(key){
    case 'd':
        day = (day + 10) % 360;
        glutPostRedisplay();//窗口重新绘制
        break;
    case 'D':
        day = (day - 10) % 360;
        glutPostRedisplay();
        break;
    case 'y':
        year = (year + 5) % 360;
        glutPostRedisplay();
        break;
    case 'Y':
        year = (year - 5) % 360;
        glutPostRedisplay();
        break;
    default:
        break;
    }
}

int main(int argc,char** argv){
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);//双缓冲
    glutInitWindowSize(500,500);
    glutInitWindowPosition(100,100);
    glutCreateWindow(argv[0]);
    init();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutKeyboardFunc(keyboard);//设置键盘事件
    glutMainLoop();
    return 0;
}

2.机器人手臂,按e,E,s,S变换位置

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

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

static int shoulder = 0, elbow = 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();
        glTranslatef(-1.0,0.0,0.0);
        glRotatef((GLfloat)shoulder,0.0,0.0,1.0);
        glTranslatef(1.0,0.0,0.0);
        glPushMatrix();
            glScalef(2.0,0.4,1.0);
            glutWireCube(1.0);
        glPopMatrix();
        glTranslatef(1.0,0.0,0.0);
        glRotatef((GLfloat)elbow,0.0,0.0,1.0);
        glTranslatef(1.0,0.0,0.0);
        glPushMatrix();
            glScalef(2.0,0.4,1.0);
            glutWireCube(1.0);
        glPopMatrix();
    glPopMatrix();
    glutSwapBuffers();
}

void reshape(int w,int h){
    glViewport(0,0,(GLsizei)w,(GLsizei)h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(65.0,(GLfloat)w/(GLfloat)h,1.0,20.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(0.0,0.0,-5.0);
}

void keyboard(unsigned char key,int x,int y){
    switch (key){
    case 's':
        shoulder = (shoulder + 5) % 360;
        glutPostRedisplay();
        break;
    case 'S':
        shoulder = (shoulder - 5) % 360;
        glutPostRedisplay();
        break;
    case 'e':
        elbow = (elbow + 5) % 360;
        glutPostRedisplay();
        break;
    case 'E':
        elbow = (elbow - 5) % 360;
        glutPostRedisplay();
        break;
    default:
        break;
    }
}

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

2014-12-04

3.读取鼠标位置,并确定鼠标光标位置经过变换之后在三维空间的近侧裁剪平面和远侧

裁剪平面上的点,经过计算得的物体坐标被打印到标准输出.

#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);
    glFlush();
}

void reshape(int w,int h){
    glViewport(0,0,(GLsizei)w,(GLsizei)h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0,(GLfloat)w/(GLfloat)h,1.0,100.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void mouse(int button,int state,int x,int y){
    GLint viewport[4];
    GLdouble mvmatrix[16],projmatrix[16];
    GLint realy;
    GLdouble wx,wy,wz;
    switch (button){
    case GLUT_LEFT_BUTTON:
        if(state == GLUT_DOWN){
            glGetIntegerv(GL_VIEWPORT,viewport);
            glGetDoublev(GL_MODELVIEW_MATRIX,mvmatrix);
            glGetDoublev(GL_PROJECTION_MATRIX,projmatrix);
            realy = viewport[3]-(GLint)y-1;
            printf("Coordinates at cursor are (%4d,%4d)\n ",x,realy);
            gluUnProject((GLdouble)x,(GLdouble)realy,1.0,mvmatrix,projmatrix,viewport,&wx,&wy,&wz);
            printf("World coords at z=1.0 are (%f,%f,%f)\n ",wx,wy,wz);
        }
        break;
    case GLUT_RIGHT_BUTTON:
        if(state == GLUT_DOWN)
            exit(0);
        break;
    default:
        break;
    }
}

int main(int argc,char** argv){
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
    glutInitWindowSize(500,500);
    glutInitWindowPosition(100,100);
    glutCreateWindow(argv[0]);
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMouseFunc(mouse);
    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 init(void){
    glClearColor(0.0,0.0,0.0,0.0);
    glShadeModel(GL_SMOOTH);//平滑着色
}

void triangle(void){
    glBegin(GL_TRIANGLES);
    glColor3f(1.0,0.0,0.0);//红色
    glVertex2f(5.0,5.0);
    glColor3f(0.0,1.0,0.0);//绿色
    glVertex2f(25.0,5.0);
    glColor3f(0.0,0.0,1.0);//蓝色
    glVertex2f(5.0,25.0);
    glEnd();
}

void display(void){
    glClear(GL_COLOR_BUFFER_BIT);
    triangle();
    glFlush();
}

void reshape(int w, int h){
    glViewport(0,0,(GLsizei)w,(GLsizei) h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    if(w<=h)
        gluOrtho2D(0.0,30.0,0.0,30.0*(GLfloat)h/(GLfloat)w);
    else
        gluOrtho2D(0.0,30.0*(GLfloat)w/(GLfloat)h,0.0,30.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;
}

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){
    GLfloat mat_specular[] = {1.0,1.0,1.0,1.0};
    GLfloat mat_shininess[] = {50.0};
    GLfloat light_position[] = {1.0,1.0,1.0,0.0};
    GLfloat white_light[] = {1.0,1.0,1.0,1.0};
    GLfloat lmodel_ambient[] = {0.1,0.1,0.1,1.0};
    glClearColor(0.0,0.0,0.0,0.0);
    glShadeModel(GL_SMOOTH);//平滑着色
    glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);
    glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);
    glLightfv(GL_LIGHT0,GL_POSITION,light_position);
    glLightfv(GL_LIGHT0,GL_DIFFUSE,white_light);
    glLightfv(GL_LIGHT0,GL_SPECULAR,white_light);
    glLightModelfv(GL_LIGHT_MODEL_AMBIENT,lmodel_ambient);

    glEnable(GL_LIGHTING);//开启光照
    glEnable(GL_LIGHT0);//开启光源0
    glEnable(GL_DEPTH_TEST);
}

void display(void){
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glutSolidSphere(1.0,20,16);//渲染球体
    glFlush();
}

void reshape(int w,int h){
    glViewport(0,0,(GLsizei) w, (GLsizei)h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    if(w<=h)
        glOrtho(-1.5,1.5,-1.5*(GLfloat)h/(GLfloat)w,1.5*(GLfloat)h/(GLfloat)w,-10.0,10.0);
    else
        glOrtho(-1.5*(GLfloat)w/(GLfloat)h,1.5*(GLfloat)w/(GLfloat)h,-1.5,1.5,-10.0,10.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

int main(int argc,char** argv){
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);
    glutInitWindowSize(500,500);
    glutInitWindowPosition(100,100);
    glutCreateWindow(argv[0]);
    init();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    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>

static int spin = 0;

void init(void){
    glClearColor(0.0,0.0,0.0,0.0);
    glShadeModel(GL_SMOOTH);//平滑着色
    glEnable(GL_LIGHTING);//开启光照
    glEnable(GL_LIGHT0);//开启光源0
    glEnable(GL_DEPTH_TEST);
}

void display(void){
    GLfloat position[] = {0.0,0.0,1.5,1.0};
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glTranslatef(0.0,0.0,-5.0);
        glPushMatrix();
            glRotated((GLdouble)spin,1.0,0.0,0.0);
            glLightfv(GL_LIGHT0,GL_POSITION,position);
            glTranslated(0.0,0.0,1.5);
            glDisable(GL_LIGHTING);
            glColor3f(0.0,1.0,1.0);
            glutWireCube(0.1);//绘制立方体
            glEnable(GL_LIGHTING);
        glPopMatrix();
        glutSolidTorus(0.275,0.85,8,15);//绘制球体
    glPopMatrix();
    glFlush();
}

void reshape(int w, int h){
    glViewport(0,0,(GLsizei)w,(GLsizei)h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(40.0,(GLfloat)w/(GLfloat)h,1.0,20.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){
            spin = (spin + 30) % 360;
            glutPostRedisplay();//重绘窗口
        }
        break;
    default:
        break;
    }
}

int main(int argc,char** argv){
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);
    glutInitWindowSize(500,500);
    glutInitWindowPosition(100,100);
    glutCreateWindow(argv[0]);
    init();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMouseFunc(mouse);
    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>

GLfloat diffuseMaterial[4]={0.5,0.5,0.5,1.0};

void init(void){
    GLfloat mat_specular[]={1.0,1.0,1.0,1.0};//材料颜色
    GLfloat light_position[]={1.0,1.0,1.0,0.0};//光源位置
    glClearColor(0.0,0.0,0.0,0.0);
    glShadeModel(GL_SMOOTH);//光滑着色
    glEnable(GL_DEPTH_TEST);
    glMaterialfv(GL_FRONT,GL_DIFFUSE,diffuseMaterial);
    glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);
    glMaterialf(GL_FRONT,GL_SHININESS,25.0);
    glLightfv(GL_LIGHT0,GL_POSITION,light_position);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glColorMaterial(GL_FRONT,GL_DIFFUSE);
    glEnable(GL_COLOR_MATERIAL);//开启色彩材质(材料颜色绘制)
}

void display(void){
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glutSolidSphere(1.0,20,16);//绘制球体
    glFlush();
}

void reshape(int w,int h){
    glViewport(0,0,(GLsizei)w,(GLsizei)h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    if(w<=h)
        glOrtho(-1.5,1.5,-1.5*(GLfloat)h/(GLfloat)w,1.5*(GLfloat)h/(GLfloat)w,-10.0,10.0);
    else
        glOrtho(-1.5*(GLfloat)w/(GLfloat)h,1.5*(GLfloat)w/(GLfloat)h,-1.5,1.5,-10.0,10.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){
                diffuseMaterial[0] += 0.1;
                if(diffuseMaterial[0]>1.0)
                    diffuseMaterial[0] = 0.0;
                glColor4fv(diffuseMaterial);
                glutPostRedisplay();//重新绘制
            }
            break;
        case GLUT_MIDDLE_BUTTON:
            if(state == GLUT_DOWN){
                diffuseMaterial[1]+=0.1;
                if(diffuseMaterial[1]>1.0)
                    diffuseMaterial[1]=0.0;
                glColor4fv(diffuseMaterial);
                glutPostRedisplay();//重新绘制
            }
            break;
        case GLUT_RIGHT_BUTTON:
            if(state == GLUT_DOWN){
                diffuseMaterial[2] += 0.1;
                if(diffuseMaterial[2] > 1.0)
                    diffuseMaterial[2] = 0.0;
                glColor4fv(diffuseMaterial);
                glutPostRedisplay();//重新绘制
            }
            break;
        default:
            break;
    }
}

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

时间: 2024-08-08 06:52:22

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

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> #inclu

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