上一篇中我们绘制了一个彩色的三角型
这次我们让它动起来
#include <GL/gl.h>
#include <SDL/SDL.h>
bool running=true;
float y=0.0;
void initGL()
{
SDL_Init(SDL_INIT_VIDEO);
SDL_SetVideoMode(600,300,16,SDL_OPENGL);
}
void destroyGL()
{
SDL_Quit();
}
void draw()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glRotatef(y,0,1,0);
glBegin(GL_TRIANGLES);
glColor3f(1,0,0);
glVertex3f(0,0,0);
glColor3f(0,1,0);
glVertex3f(1,0,0);
glColor3f(0,0,1);
glVertex3f(0,1,0);
glEnd();
SDL_GL_SwapBuffers();
}
void quit()
{
running=false;
}
void onKeyDown(const SDL_Event& event)
{
switch(event.key.keysym.sym)
{
case SDLK_ESCAPE:
quit();
break;
case SDLK_LEFT:
y+=0.1;
break;
case SDLK_RIGHT:
y-=0.1;
break;
}
}
void loop()
{
SDL_Event event;
while(running)
{
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_QUIT:
quit();
break;
case SDL_KEYDOWN:
onKeyDown(event);
break;
}
}
draw();
SDL_Delay(50);
}
}
int main(int argc,char* argv[])
{
initGL();
loop();
destroyGL();
return 0;
}
#include <GL/gl.h>
#include <SDL/SDL.h>
bool running=true;
float y=0.0;
void initGL()
{
SDL_Init(SDL_INIT_VIDEO);
SDL_SetVideoMode(600,300,16,SDL_OPENGL);
}
void destroyGL()
{
SDL_Quit();
}
void draw()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glRotatef(y,0,1,0);
glBegin(GL_TRIANGLES);
glColor3f(1,0,0);
glVertex3f(0,0,0);
glColor3f(0,1,0);
glVertex3f(1,0,0);
glColor3f(0,0,1);
glVertex3f(0,1,0);
glEnd();
SDL_GL_SwapBuffers();
}
void quit()
{
running=false;
}
void onKeyDown(const SDL_Event& event)
{
switch(event.key.keysym.sym)
{
case SDLK_ESCAPE:
quit();
break;
case SDLK_LEFT:
y+=0.1;
break;
case SDLK_RIGHT:
y-=0.1;
break;
}
}
void loop()
{
SDL_Event event;
while(running)
{
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_QUIT:
quit();
break;
case SDL_KEYDOWN:
onKeyDown(event);
break;
}
}
draw();
SDL_Delay(50);
}
}
int main(int argc,char* argv[])
{
initGL();
loop();
destroyGL();
return 0;
}
posted on 2006-09-28 16:46
四海 阅读(532)
评论(0) 编辑 收藏 引用