用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;
}
显示出立方体了,但为什么立方体不会动呢?? 展开
#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;
}
显示出立方体了,但为什么立方体不会动呢?? 展开
1个回答
展开全部
#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;
}
#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;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询