Chinaunix首页 | 论坛 | 博客
  • 博客访问: 377466
  • 博文数量: 466
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 10
  • 用 户 组: 普通用户
  • 注册时间: 2015-03-16 13:59
文章分类

全部博文(466)

文章存档

2015年(466)

我的朋友

分类: C/C++

2015-03-16 14:58:35

例子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();
}


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