用Visual Studio写一个OpenGL程序:实现立方体的转动

我用VisualStudio2008,建Win32控制台程序,新建一个test.c文件,往里面输入如下代码:发现立方体不会动,这是为什么呢?#include<window... 我用Visual Studio 2008,建Win32控制台程序,新建一个test.c文件,往里面输入如下代码:发现立方体不会动,这是为什么呢?
#include <windows.h>
#include <gl/glut.h>
GLfloat angle = 0.0f;
void DrawCube( float xPos, float yPos, float zPos )
{
// glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
// glLoadIdentity();
glPushMatrix();
// glTranslatef( xPos, yPos, zPos );
// glRotatef( 50.0f, 1.0f, 1.0f, -1.0f );
glBegin( GL_QUADS ); //top
glColor3f( 1.0f, 0.0f, 0.0f );
glNormal3f( 0.0f, 1.0f, 0.0f );
glVertex3f( 0.5f, 0.5f, 0.5f );
glVertex3f( 0.5f, 0.5f, -0.5f );
glVertex3f( -0.5f, 0.5f, -0.5f );
glVertex3f( -0.5f, 0.5f, 0.5f );
glEnd();
glBegin( GL_QUADS ); //forward
glColor3f( 0.0f, 1.0f, 0.0f );
glNormal3f( 0.0f, 0.0f, 1.0f );
glVertex3f( 0.5f, 0.5f, 0.5f );
glVertex3f( -0.5f, 0.5f, 0.5f );
glVertex3f( -0.5f, -0.5f, 0.5f );
glVertex3f( 0.5f, -0.5f, 0.5f );
glEnd();
glBegin( GL_QUADS ); //right
glColor3f( 0.0f, 0.0f, 1.0f );
glNormal3f( 1.0f, 0.0f, 0.0f );
glVertex3f( 0.5f, 0.5f, 0.5f );
glVertex3f( 0.5f, -0.5f, 0.5f );
glVertex3f( 0.5f, -0.5f, -0.5f );
glVertex3f( 0.5f, 0.5f, -0.5f );
glEnd();
glBegin( GL_QUADS ); //left
glColor3f( 1.0f, 1.0f, 0.0f );
glNormal3f( -1.0f, 0.0f, 0.0f );
glVertex3f( -0.5f, 0.5f, 0.5f );
glVertex3f( -0.5f, 0.5f, -0.5f );
glVertex3f( -0.5f, -0.5f, -0.5f );
glVertex3f( -0.5f, -0.5f, 0.5f );
glEnd();
glBegin( GL_QUADS ); //bottom
glColor3f( 1.0f, 0.0f, 1.0f );
glNormal3f( 0.0f, -1.0f, 0.0f );
glVertex3f( -0.5f, -0.5f, 0.5f );
glVertex3f( -0.5f, -0.5f, -0.5f );
glVertex3f( 0.5f, -0.5f, -0.5f );
glVertex3f( 0.5f, -0.5f, 0.5f );
glEnd();
glBegin( GL_QUADS );
glColor3f( 0.0f, 1.0f, 1.0f ); //back
glNormal3f( 0.0f, 0.0f, -1.0f );
glVertex3f( 0.5f, -0.5f, -0.5f );
glVertex3f( -0.5f, -0.5f, -0.5f );
glVertex3f( -0.5f, 0.5f, -0.5f );
glVertex3f( 0.5f, 0.5f, -0.5f );
glEnd();
glPopMatrix();
// glFlush(); //flush:刷新
}
void Render(void)
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glLoadIdentity(); //复位模型视图矩阵

glShadeModel( GL_SMOOTH );
angle = angle + 50.0f;
if( angle >= 360.0f )
angle = 0.0f;

glTranslatef( 0.0f, 0.0f, -0.1f );
glRotatef( angle, 1.0f, 1.0f, 1.0f );

DrawCube( 0.0f, 0.0f, 0.0f );

glFlush();

}

int main( int argc, char *argv[] )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGB | GLUT_SINGLE );
glutInitWindowPosition( 100, 100 );
glutInitWindowSize( 400, 400 );
glutCreateWindow( "Drawing a Cube" );
glutDisplayFunc( &Render );
glutMainLoop();
return 0;
}
显示出立方体了,但为什么立方体不会动呢??
展开
 我来答
asd8557
2011-04-05 · 超过38用户采纳过TA的回答
知道答主
回答量:104
采纳率:0%
帮助的人:0
展开全部
#define GLUT_DISABLE_ATEXIT_HACK
#include <gl/glut.h>
#include <math.h>
static float day=360;
void myDisplay(void)
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(75,1,1,400);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0,-2,2,0,0,0,0,0,1);

glRotatef(day/30.0*360.0-day/360.0*360.0,0.0f,0.0f,-1.0f);
glTranslatef(1,0.0f,0.0f);
glBegin( GL_QUADS ); //top
glColor3f( 1.0f, 0.0f, 0.0f );
glNormal3f( 0.0f, 1.0f, 0.0f );
glVertex3f( 0.5f, 0.5f, 0.5f );
glVertex3f( 0.5f, 0.5f, -0.5f );
glVertex3f( -0.5f, 0.5f, -0.5f );
glVertex3f( -0.5f, 0.5f, 0.5f );
glEnd();
glBegin( GL_QUADS ); //forward
glColor3f( 0.0f, 1.0f, 0.0f );
glNormal3f( 0.0f, 0.0f, 1.0f );
glVertex3f( 0.5f, 0.5f, 0.5f );
glVertex3f( -0.5f, 0.5f, 0.5f );
glVertex3f( -0.5f, -0.5f, 0.5f );
glVertex3f( 0.5f, -0.5f, 0.5f );
glEnd();
glBegin( GL_QUADS ); //right
glColor3f( 0.0f, 0.0f, 1.0f );
glNormal3f( 1.0f, 0.0f, 0.0f );
glVertex3f( 0.5f, 0.5f, 0.5f );
glVertex3f( 0.5f, -0.5f, 0.5f );
glVertex3f( 0.5f, -0.5f, -0.5f );
glVertex3f( 0.5f, 0.5f, -0.5f );
glEnd();
glBegin( GL_QUADS ); //left
glColor3f( 1.0f, 1.0f, 0.0f );
glNormal3f( -1.0f, 0.0f, 0.0f );
glVertex3f( -0.5f, 0.5f, 0.5f );
glVertex3f( -0.5f, 0.5f, -0.5f );
glVertex3f( -0.5f, -0.5f, -0.5f );
glVertex3f( -0.5f, -0.5f, 0.5f );
glEnd();
glBegin( GL_QUADS ); //bottom
glColor3f( 1.0f, 0.0f, 1.0f );
glNormal3f( 0.0f, -1.0f, 0.0f );
glVertex3f( -0.5f, -0.5f, 0.5f );
glVertex3f( -0.5f, -0.5f, -0.5f );
glVertex3f( 0.5f, -0.5f, -0.5f );
glVertex3f( 0.5f, -0.5f, 0.5f );
glEnd();
glBegin( GL_QUADS );
glColor3f( 0.0f, 1.0f, 1.0f ); //back
glNormal3f( 0.0f, 0.0f, -1.0f );
glVertex3f( 0.5f, -0.5f, -0.5f );
glVertex3f( -0.5f, -0.5f, -0.5f );
glVertex3f( -0.5f, 0.5f, -0.5f );
glVertex3f( 0.5f, 0.5f, -0.5f );
glEnd();
glFlush();

glutSwapBuffers();
}

void myIdle()
{

day = day + 0.1;
if (day>=360)
{
day=0;
}
myDisplay();
}

int main(int argc, char* argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE);
glutInitWindowPosition(100,100);
glutInitWindowSize(600,600);
glutCreateWindow("第一个OpenGL程序");
glutDisplayFunc(&myDisplay);
glutIdleFunc(&myIdle);
glutMainLoop();

return 0;
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式