Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5513712
  • 博文数量: 763
  • 博客积分: 12108
  • 博客等级: 上将
  • 技术积分: 15717
  • 用 户 组: 普通用户
  • 注册时间: 2007-09-28 21:21
个人简介

业精于勤,荒于嬉

文章分类

全部博文(763)

文章存档

2018年(6)

2017年(15)

2016年(2)

2015年(31)

2014年(14)

2013年(87)

2012年(75)

2011年(94)

2010年(190)

2009年(38)

2008年(183)

2007年(28)

分类: C/C++

2010-05-27 12:33:14

例子001:

#include<GL/glut.h>


float angle = 0.0;

void changeSize(int w, int h) {

    // Prevent a divide by zero, when window is too short

    // (you cant make a window of zero width).

    float ratio;
    if(h == 0)
        h = 1;

    ratio = 1.0* w / h;

    // Reset the coordinate system before modifying

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    // Set the viewport to be the entire window

    glViewport(0, 0, w, h);

    // Set the correct perspective.

    gluPerspective(45,ratio,1,1000);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(0.0,0.0,5.0,
        0.0,0.0,-1.0,
        0.0f,1.0f,0.0f);


}

void renderScene(void) {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glPushMatrix();
    glRotatef(angle,0.0,1.0,0.0);
    glBegin(GL_TRIANGLES);
    glVertex3f(-0.5,-0.5,0.0);
    glVertex3f(0.5,0.0,0.0);
    glVertex3f(0.0,0.5,0.0);
    glEnd();
    glPopMatrix();
    angle++;
    glutSwapBuffers();
}

void main(int argc, char **argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowPosition(100,100);
    glutInitWindowSize(320,320);
    glutCreateWindow("Lighthouse 3D - GLUT Tutorial");
    glutDisplayFunc(renderScene);
    glutIdleFunc(renderScene);
    glutReshapeFunc(changeSize);
    glutMainLoop();
}

 

例子002:

#include<GL/glut.h>


#define RED 1
#define GREEN 2
#define BLUE 3
#define WHITE 4


float angle = 0.0;
float red=1.0, blue=1.0, green=1.0;

void changeSize(int w, int h) {

    // Prevent a divide by zero, when window is too short

    // (you cant make a window of zero width).

    if(h == 0)
        h = 1;

    float ratio = 1.0* w / h;

    // Reset the coordinate system before modifying

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    
    // Set the viewport to be the entire window

    glViewport(0, 0, w, h);

    // Set the correct perspective.

    gluPerspective(45,ratio,1,1000);
    gluLookAt(0.0,0.0,5.0,
         0.0,0.0,-1.0,
             0.0f,1.0f,0.0f);
    glMatrixMode(GL_MODELVIEW);


}


void renderScene(void) {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    glRotatef(angle,0.0,1.0,0.0);

    glColor3f(red,green,blue);

    glBegin(GL_TRIANGLES);
        glVertex3f(-0.5,-0.5,0.0);
        glVertex3f(0.5,0.0,0.0);
        glVertex3f(0.0,0.5,0.0);
    glEnd();
    angle++;
    glutSwapBuffers();
}



void processMenuEvents(int option) {

    switch (option) {
        case RED : red = 1.0; green = 0.0; blue = 0.0; break;
        case GREEN : red = 0.0; green = 1.0; blue = 0.0; break;
        case BLUE : red = 0.0; green = 0.0; blue = 1.0; break;
        case WHITE : red = 1.0; green = 1.0; blue = 1.0; break;
    }
}

void createGLUTMenus() {

    int submenu;

    submenu = glutCreateMenu(processMenuEvents);
    glutAddMenuEntry("Red",RED);
    glutAddMenuEntry("Blue",BLUE);
    glutAddMenuEntry("Green",GREEN);
    glutAddMenuEntry("White",WHITE);
    glutAttachMenu(GLUT_RIGHT_BUTTON);
}



void main(int argc, char **argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowPosition(100,100);
    glutInitWindowSize(320,320);
    glutCreateWindow("SnowMen@Lighthouse3D");
    glutDisplayFunc(renderScene);
    glutIdleFunc(renderScene);
    glutReshapeFunc(changeSize);

    //call our function to create the menu

    createGLUTMenus();


    glutMainLoop();
}

 

例子003

#include <stdlib.h>
 #include <GL/glut.h>
 
void background(void)
 {
    glClearColor(0.0,0.0,0.0,0.0);//设置背景颜色为黑色

 }
 
 void myDisplay(void)
 {
     glClear(GL_COLOR_BUFFER_BIT);//buffer设置为颜色可写

    
     glBegin(GL_TRIANGLES);//开始画三角形

     glShadeModel(GL_SMOOTH);//设置为光滑明暗模式

    
     glColor3f(1.0,0.0,0.0);//设置第一个顶点为红色

     glVertex2f(-1.0,-1.0);//设置第一个顶点的坐标为(-1.0,-1.0)

    
     glColor3f(0.0,1.0,0.0);//设置第二个顶点为绿色

     glVertex2f(0.0,-1.0);//设置第二个顶点的坐标为(0.0,-1.0)

    
     glColor3f(0.0,0.0,1.0);//设置第三个顶点为蓝色

     glVertex2f(-0.5,1.0);//设置第三个顶点的坐标为(-0.5,1.0)

     glEnd();//三角形结束

    
     glFlush();//强制OpenGL函数在有限时间内运行

 }
 
 void myReshape(GLsizei w,GLsizei h)
 {
     glViewport(0,0,w,h);//设置视口

    
     glMatrixMode(GL_PROJECTION);//指明当前矩阵为GL_PROJECTION

     glLoadIdentity();//将当前矩阵置换为单位阵

    
     if(w <= h)
     gluOrtho2D(-1.0,1.5,-1.5,1.5*(GLfloat)h/(GLfloat)w);//定义二维正视投影矩阵

     else
     gluOrtho2D(-1.0,1.5*(GLfloat)w/(GLfloat)h,-1.5,1.5);
     glMatrixMode(GL_MODELVIEW);//指明当前矩阵为GL_MODELVIEW

 }
 
 int main(int argc,char ** argv)
 {
     /*初始化*/
     glutInit(&argc,argv);
     glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
     glutInitWindowSize(400,400);
     glutInitWindowPosition(200,200);
    
     /*创建窗口*/
     glutCreateWindow("Triangle");
    
     /*绘制与显示*/
     background();
     glutReshapeFunc(myReshape);
     glutDisplayFunc(myDisplay);
    
    
     glutMainLoop();
     return(0);
 }

 

例子004:

#include<GL/glut.h>

#define RED 1
#define GREEN 2
#define BLUE 3
#define WHITE 4


float angle = 0.0;
float red=1.0, blue=1.0, green=1.0;

void changeSize(int w, int h) {

    // Prevent a divide by zero, when window is too short

    // (you cant make a window of zero width).

    if(h == 0)
        h = 1;

    float ratio = 1.0* w / h;

    // Reset the coordinate system before modifying

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    
    // Set the viewport to be the entire window

    glViewport(0, 0, w, h);

    // Set the correct perspective.

    gluPerspective(45,ratio,1,1000);
    gluLookAt(0.0,0.0,5.0,
         0.0,0.0,-1.0,
             0.0f,1.0f,0.0f);
    glMatrixMode(GL_MODELVIEW);


}


void renderScene(void) {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    glRotatef(angle,0.0,1.0,0.0);

    glColor3f(red,green,blue);

    glBegin(GL_TRIANGLES);
        glVertex3f(-0.5,-0.5,0.0);
        glVertex3f(0.5,0.0,0.0);
        glVertex3f(0.0,0.5,0.0);
    glEnd();
    angle++;
    glutSwapBuffers();
}



void processMenuEvents(int option) {

    switch (option) {
        case RED : red = 1.0; green = 0.0; blue = 0.0; break;
        case GREEN : red = 0.0; green = 1.0; blue = 0.0; break;
        case BLUE : red = 0.0; green = 0.0; blue = 1.0; break;
        case WHITE : red = 1.0; green = 1.0; blue = 1.0; break;
    }
}

void createGLUTMenus() {

    int menu,submenu;

    submenu = glutCreateMenu(processMenuEvents);
    glutAddMenuEntry("Red",RED);
    glutAddMenuEntry("Blue",BLUE);
    glutAddMenuEntry("Green",GREEN);

    menu = glutCreateMenu(processMenuEvents);
    glutAddMenuEntry("White",WHITE);
    glutAddSubMenu("RGB Menu",submenu);
    glutAttachMenu(GLUT_RIGHT_BUTTON);
}



void main(int argc, char **argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowPosition(100,100);
    glutInitWindowSize(320,320);
    glutCreateWindow("SnowMen@Lighthouse3D");
    glutDisplayFunc(renderScene);
    glutIdleFunc(renderScene);
    glutReshapeFunc(changeSize);

    //call our function to create the menu

    createGLUTMenus();


    glutMainLoop();
}


阅读(2260) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~