Chinaunix首页 | 论坛 | 博客
  • 博客访问: 294420
  • 博文数量: 46
  • 博客积分: 1517
  • 博客等级: 上尉
  • 技术积分: 530
  • 用 户 组: 普通用户
  • 注册时间: 2006-12-31 18:21
文章分类

全部博文(46)

文章存档

2012年(1)

2011年(1)

2010年(3)

2009年(2)

2008年(25)

2007年(13)

2006年(1)

我的朋友

分类: C/C++

2009-06-06 13:58:27

第一个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);
}
    

阅读(1118) | 评论(0) | 转发(1) |
0

上一篇:ClipboardManager

下一篇:OPhone OS2.0 鸡肋升级

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