Chinaunix首页 | 论坛 | 博客
  • 博客访问: 566121
  • 博文数量: 94
  • 博客积分: 1631
  • 博客等级: 上尉
  • 技术积分: 586
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-28 12:16
文章分类

全部博文(94)

文章存档

2014年(1)

2013年(11)

2012年(69)

2011年(7)

2010年(6)

我的朋友

分类:

2012-02-13 23:32:20

原文地址:开始opengl编程 作者:xinyu391

第一个opengl 代码
包含了基本的glut 使用结构
如何实现动画效果
如何绘制图形,等




在linux下编译要加上 -lglut   指明 所需要的库
编译 gcc -o first first.c -lglut

// first.c


#include "GL/glut.h"

#include "stdlib.h"


void init(void);

void keyboard(unsigned char key, int x, int y);

void mouse(int button, int state, int x, int y);

void display(void);

void reShape(int width, int height);

void idle(void);

void timer(int);


GLdouble rotate_1 = 0,rotate_2=0;



int main(int argc, char** argv)

{

    glutInit(&argc, argv);

    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);

    glutInitWindowSize (800, 600);

    glutInitWindowPosition (100, 100);

    glutCreateWindow ("hello");
    init ();
    glutTimerFunc(30, timer, 1);

    glutDisplayFunc(display);

    glutReshapeFunc(reShape);

    glutKeyboardFunc(keyboard);

    glutMouseFunc(mouse);
    

    glutIdleFunc(idle);

    glutMainLoop();

    return 0;

}

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);
//    glEnable(GL_FOG);


}

void display(void)

{

    

    glClear(GL_COLOR_BUFFER_BIT);

    glColor3f(1.0,1.0,1.0);

    glLoadIdentity();//Çå¿ÕŸØÕó


    glMatrixMode(GL_MODELVIEW);

    gluLookAt(0.1,0.1,0.1,        0.0,0.0,0.0,        0.0,0.1,0.0);

    //glScalef(1.0,1.1,1.0);


    

    

    glColor3f(1.0, 0.0, 0.0);

    glBegin(GL_LINE_STRIP);

        glVertex3f(0.0, 0.0, 0.0);

        glVertex3f(2.0, 0.0, 0.0);

    glEnd();

    glColor3f(0.0, 1.0, 0.0);

    glBegin(GL_LINE_STRIP);

        glVertex3f(0.0, 0.0, 0.0);

        glVertex3f(0.0, 2.0, 0.0);

    glEnd();

    glColor3f(0.0, 0.0, 1.0);

    glBegin(GL_LINE_STRIP);

        glVertex3f(0.0, 0.0, 0.0);

        glVertex3f(0.0, 0.0, 2.0);

    glEnd();



    glColor3f(1.0, 0.0, 0.0);

    glBegin(GL_LINE_LOOP);

        glVertex3f(0.25, 0.25, 0.0);

        glVertex3f(0.25, 0.75, 0.0);

        glVertex3f(0.75, 0.55, 0.0);

    glEnd();



    glutWireCube(1.0);



    glRotatef(rotate_1,0.0,1.0,0.0);

    glColor3f(1.0,1.0,0.0);

    glutWireSphere(0.3, 19, 12);



    glRotatef(rotate_1,0.0,1.0,0.0);

    glTranslatef(1.0,0.0,0.0);

    glutWireSphere(0.2, 19, 12);



    glRotatef(rotate_2,0.0,1.0,0.0);

    glTranslatef(0.4,0.0,0.0);

    //glScalef(1.0,2.0,1.0);


    glutWireSphere(0.1, 19, 12);

    



    glutSwapBuffers();

    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 keyboard(unsigned char key, int x, int y)

{

    switch(key) {

        case 27:

        exit(0);

        break;

    }

}

void mouse(int button, int state, int x, int y)

{

    switch(button) {

        case GLUT_LEFT_BUTTON:

        exit(0);

        break;

    }

}

void idle(void){

//    rotate_1++;


//    rotate_2+=3;


//    sleep(1);


//    glutPostRedisplay();


    

}
void timer(int value){
    rotate_1++;

    rotate_2+=3;
    glutPostRedisplay();
    glutTimerFunc(30, timer, 1);
}
    

阅读(375) | 评论(0) | 转发(0) |
0

上一篇:OpenGL介绍

下一篇:OpenGL超级宝典

给主人留下些什么吧!~~