收录日期:2020/05/27 18:01:28 时间:2016/07/19 11:33:45 标签:图形处理/算法
#include <GL/gl.h>

#include <GL/glu.h>

#include <GL/glaux.h>

 

/* 定义旋转和比例因子的初始值 */

 

GLfloat step=0.0,s=0.1;

 

/* 定义立方体的顶点坐标值 */

 

static GLfloat  p1[]={0.5,-0.5,-0.5}, p2[]={0.5,0.5,-0.5},

                p3[]={0.5,0.5,0.5},   p4[]={0.5,-0.5,0.5},

                p5[]={-0.5,-0.5,0.5}, p6[]={-0.5,0.5,0.5},

                p7[]={-0.5,0.5,-0.5}, p8[]={-0.5,-0.5,-0.5};

 

/* 定义立方体的顶点方向值 */

 

static GLfloat  m1[]={1.0,0.0,0.0}, m2[]={-1.0,0.0,0.0},

                m3[]={0.0,1.0,0.0}, m4[]={0.0,-1.0,0.0},

                m5[]={0.0,0.0,1.0}, m6[]={0.0,0.0,-1.0};

 

/* 定义立方体的顶点颜色值 */

 

static GLfloat  c1[]={0.0,0.0,1.0},  c2[]={0.0,1.0,1.0},

                c3[]={1.0,1.0,1.0},  c4[]={1.0,0.0,1.0},

                c5[]={1.0,0.0,0.0},  c6[]={1.0,1.0,0.0},

                c7[]={0.0,1.0,0.0},  c8[]={1.0,1.0,1.0};

 

 

void myinit(void);

void CALLBACK myReshape(GLsizei w, GLsizei h);

void CALLBACK stepDisplay (void);

void CALLBACK startIdleFunc (AUX_EVENTREC *event);

void CALLBACK stopIdleFunc (AUX_EVENTREC *event);

void CALLBACK display(void);

void DrawColorBox(void);

 

void myinit(void)

{

    GLfloat light_ambient[]={0.3,0.2,0.5};

    GLfloat light_diffuse[]={1.0,1.0,1.0};

    GLfloat light_position[] = { 2.0, 2.0, 2.0, 1.0 };

 

    GLfloat light1_ambient[]={0.3,0.3,0.2};

    GLfloat light1_diffuse[]={1.0,1.0,1.0};

    GLfloat light1_position[] = { -2.0, -2.0, -2.0, 1.0 };

 

 

    glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);

    glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);

    glLightfv(GL_LIGHT0, GL_POSITION, light_position);

 

    glLightfv(GL_LIGHT1, GL_AMBIENT, light1_ambient);

    glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_diffuse);

    glLightfv(GL_LIGHT1, GL_POSITION, light1_position);

 

    glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE);

    glEnable(GL_LIGHTING);

    glEnable(GL_LIGHT0);

    glEnable(GL_LIGHT1);

 

    glDepthFunc(GL_LESS);

    glEnable(GL_DEPTH_TEST);

 

     glColorMaterial(GL_FRONT_AND_BACK,GL_DIFFUSE);

     glEnable(GL_COLOR_MATERIAL);

 

}

 

 

void CALLBACK display(void)

{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

 

      s+=0.005;

      if(s>1.0)s=0.1;

 

    glPushMatrix();

     glScalef(s,s,s);

     glRotatef(step,0.0,1.0,0.0);

     glRotatef(step,0.0,0.0,1.0);

     glRotatef(step,1.0,0.0,0.0);

     DrawColorBox();

    glPopMatrix();

 

    glFlush();

 

    auxSwapBuffers();     /*  交换缓存  */

 

}

 

 

void CALLBACK stepDisplay (void)

{

    step = step + 1.0;

    if (step > 360.0)

    step = step - 360.0;

    display();

}

 

 

void CALLBACK startIdleFunc (AUX_EVENTREC *event)

{

    auxIdleFunc(stepDisplay);

}

 

 

void CALLBACK stopIdleFunc (AUX_EVENTREC *event)

{

    auxIdleFunc(0);

}

 

 

void CALLBACK myReshape(GLsizei w, GLsizei h)

{

 

    glViewport(0, 0, w, h);

    glMatrixMode(GL_PROJECTION);

    glLoadIdentity();

    if (w <= h)

    glOrtho (-1.5, 1.5, -1.5*(GLfloat)h/(GLfloat)w,

        1.50*(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 DrawColorBox(void)

{

   glBegin (GL_QUADS);

 

         glColor3fv(c1);

         glNormal3fv(m1);

        glVertex3fv(p1);

        glColor3fv(c2);

        glVertex3fv(p2);

        glColor3fv(c3);

        glVertex3fv(p3);

        glColor3fv(c4);

        glVertex3fv(p4);

 

         glColor3fv(c5);

         glNormal3fv(m5);

        glVertex3fv(p5);

        glColor3fv(c6);

        glVertex3fv(p6);

        glColor3fv(c7);

        glVertex3fv(p7);

        glColor3fv(c8);

        glVertex3fv(p8);

 

         glColor3fv(c5);

         glNormal3fv(m3);

        glVertex3fv(p5);

        glColor3fv(c6);

        glVertex3fv(p6);

        glColor3fv(c3);

        glVertex3fv(p3);

        glColor3fv(c4);

        glVertex3fv(p4);

 

         glColor3fv(c1);

         glNormal3fv(m4);

        glVertex3fv(p1);

        glColor3fv(c2);

        glVertex3fv(p2);

        glColor3fv(c7);

        glVertex3fv(p7);

        glColor3fv(c8);

        glVertex3fv(p8);

 

         glColor3fv(c2);

         glNormal3fv(m5);

        glVertex3fv(p2);

        glColor3fv(c3);

        glVertex3fv(p3);

        glColor3fv(c6);

        glVertex3fv(p6);

        glColor3fv(c7);

        glVertex3fv(p7);

 

         glColor3fv(c1);

         glNormal3fv(m6);

        glVertex3fv(p1);

        glColor3fv(c4);

        glVertex3fv(p4);

        glColor3fv(c5);

        glVertex3fv(p5);

        glColor3fv(c8);

        glVertex3fv(p8);

 

    glEnd();

 

 

}

 

void main(void)

{

 

    auxInitDisplayMode (AUX_DOUBLE | AUX_RGBA); /* 设置双缓存和RGBA颜色模式*/

    auxInitPosition (0, 0, 500,400);

    auxInitWindow ("Double_Buffer_Animation");

    myinit();

    auxReshapeFunc (myReshape);

    auxIdleFunc (stepDisplay);

    auxMouseFunc (AUX_LEFTBUTTON, AUX_MOUSEDOWN, startIdleFunc);

    auxMouseFunc (AUX_RIGHTBUTTON, AUX_MOUSEDOWN, stopIdleFunc);

    auxMainLoop(display);

}
以上程序运行结果是在屏幕上显示一个不断旋转和由小到大变化的五彩立方体。

有错,错在哪里?帮我改一下,谢谢!
楼主,没有问题!不知道你说的错误是什么?显示不出来?我用的显示,感觉可以,就是太快了!
step = step + 1.0; 我改成 step = step + 0.1; 
s+=0.005; 我改成 s+=0.0005; 
这样看,感觉舒服多了,不然,眼睛很难受!
我要做个课程设计,三维建模,这个又没学过,不懂.运行后c:\documents and settings\administrator\桌面\新建文件夹\11\1.cpp(1) : fatal error C1083: Cannot open include file: 'glos.h': No such file or directory,你的文件夹里有这个文件吗,能不能发给我,邮箱:wellbeing200888@126.com
你这是编译错误!
我给你发一个我自己编译的文件吧!
好啊
已经发送了!你自己收一下!也可以自己编译一下!
谢谢

简单问题,如何把数据传到另外一个页面中? 有哪位肯帮帮我啊?? 在外国销售,用国内的那种银行卡作为支付卡,比较方便? 如何用CryptoAPI实现RSA非对称加解密? 请教:如何快速定位到某个字符串 请教关于vs2005下的TableAdapter Query的问题? 装载数据的问题 关于CWnd::SetWindowText(LPCTSTR lpszString) 100分求助:初学Infragistics(WinForm),关于柱状图的两个问题,很急……! 如何改变窗口大小,使client rect的大小变为我需要的大小呢? This file has been modified outside of the source editor.Do you want to reload it?什么东东? 大家轻松来找碴(9) 请教:sql查找问题?(急用) 求正则表达式 指针问题--请教空指针的用法,请高人指点,谢谢! 有握奇的技术开发人员吗? 求DataGrid中关于列锁定的解决方法(在线等---急) 现在学ASP.NET也就两个月了 不知道以要能考些什么 powerdesigner11的汉化 DATAGRID里面如何实现行选择?webfrom版 特别关注: 性能问题 谁有msn8 的邀请阿?给哥们发一个来啊!!!!!!!!!!!!!!!!!!!!!!1 握奇/格尔的开发人员? 新年好!热烈欢迎Delphi中高级程序员加入我公司! SSE优化代码 请教,怎么在VS 2003的代码区显示行号?? 简单的菜鸟问题啊~~ 帮忙 请问如何编程实现知道一个单元格是否是合并单元格?如果是则合并之前是由哪些单元格组成的?谢谢! 求助:谁有jGraph的用户手册(User Manual)? 一个简单的问题!