点击下载源文件
/*
坐标观察程序,
ESC:退出
F5:复位
LEFT/RIGHT:以绿色Y为轴心旋转
UP/DOWN:以红色X为轴心旋转
PAGEUP/PAGEDOWN:以蓝色z为轴心旋转
*/
#include <GL/gl.h>
#include <SDL/SDL.h>
bool running=true;
float x=0.0,y=0.0,z=0.0;
enum Target{modelview,projection};
Target target=modelview;
void initGL()
{
SDL_Init(SDL_INIT_VIDEO);
SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY,SDL_DEFAULT_REPEAT_INTERVAL);
SDL_SetVideoMode(600,300,16,SDL_OPENGL);
}
void destroyGL()
{
SDL_Quit();
}
void drawCoordinate()
{
glBegin(GL_LINES);
glLineWidth(10.0f);
glColor3f(1,0,0);
glVertex3f(0,0,0);
glVertex3f(1,0,0);
glColor3f(0,1,0);
glVertex3f(0,0,0);
glVertex3f(0,1,0);
glColor3f(0,0,1);
glVertex3f(0,0,0);
glVertex3f(0,0,1);
glEnd();
}
void draw()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
if(target==modelview)
{
glMatrixMode(GL_MODELVIEW);
}
else
{
glMatrixMode(GL_PROJECTION);
}
glLoadIdentity();
glRotatef(x,1,0,0);
glRotatef(y,0,1,0);
glRotatef(z,0,0,1);
glMatrixMode(GL_MODELVIEW);
drawCoordinate();
SDL_GL_SwapBuffers();
}
void switchTarget()
{
if(((int)target+1)>((int)projection))
{
target=modelview;
}else
{
target=(Target)((int)target+1);
}
}
void reset()
{
x=0;
y=0;
z=0;
}
void quit()
{
running=false;
}
void onKeyDown(const SDL_Event& event)
{
switch(event.key.keysym.sym)
{
case SDLK_ESCAPE:
quit();
break;
case SDLK_TAB:
switchTarget();
break;
case SDLK_F5:
reset();
break;
case SDLK_LEFT:
y+=1;
break;
case SDLK_RIGHT:
y-=1;
break;
case SDLK_UP:
x+=1;
break;
case SDLK_DOWN:
x-=1;
break;
case SDLK_PAGEUP:
z+=1;
break;
case SDLK_PAGEDOWN:
z-=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:52
四海 阅读(1155)
评论(0) 编辑 收藏 引用