122 lines
2.5 KiB
C++
122 lines
2.5 KiB
C++
|
|
/* Libraries we use */
|
|
#include <SDL/SDL.h>
|
|
#include <GL/gl.h>
|
|
#include <GL/glu.h>
|
|
|
|
/* Some definitions */
|
|
#define WIDTH 800
|
|
#define HEIGHT 800
|
|
#define TITLE "Josh's Wavefront Object Viewer"
|
|
|
|
/* Some global variables */
|
|
float rotationMatrix[16];
|
|
int startx, starty;
|
|
bool dragging = false;
|
|
|
|
void initgl(void)
|
|
{
|
|
glClearColor(0.0, 0.0, 0.0, 0.0);
|
|
glEnable(GL_DEPTH_TEST);
|
|
glShadeModel(GL_SMOOTH);
|
|
glEnable(GL_LIGHTING);
|
|
glEnable(GL_LIGHT0);
|
|
float pos[] = {0.0, -1.0, 0.0, 0.0};
|
|
glLightfv(GL_LIGHT0, GL_POSITION, pos);
|
|
glEnable(GL_CULL_FACE);
|
|
glViewport(0, 0, WIDTH, HEIGHT);
|
|
glMatrixMode(GL_PROJECTION);
|
|
glLoadIdentity();
|
|
gluPerspective(60.0, (GLfloat)WIDTH/(GLfloat)WIDTH, 1.0, 30.0);
|
|
gluLookAt(0, -4.0, 0, 0, 0, 0, 0, 0, 1);
|
|
glMatrixMode(GL_MODELVIEW);
|
|
glLoadIdentity();
|
|
glGetFloatv(GL_MODELVIEW_MATRIX, rotationMatrix);
|
|
}
|
|
|
|
void display(void)
|
|
{
|
|
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
|
|
glLoadIdentity();
|
|
glMultMatrixf(rotationMatrix);
|
|
glBegin(GL_QUADS);
|
|
glVertex3f(0.5, -0.5, 0.0);
|
|
glVertex3f(0.5, 0.5, 0.0);
|
|
glVertex3f(-0.5, 0.5, 0.0);
|
|
glVertex3f(-0.5, -0.5, 0.0);
|
|
glEnd();
|
|
SDL_GL_SwapBuffers();
|
|
}
|
|
|
|
/* The program's main entry point */
|
|
int main(int argc, char * argv[])
|
|
{
|
|
if (SDL_Init(SDL_INIT_VIDEO))
|
|
{
|
|
printf("Failed to initialize SDL!\n");
|
|
return 1;
|
|
}
|
|
|
|
atexit(SDL_Quit);
|
|
|
|
SDL_Surface * screen;
|
|
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
|
if (!(screen = SDL_SetVideoMode(WIDTH, HEIGHT, 32, SDL_OPENGL)))
|
|
{
|
|
printf("Failed to set video mode!\n");
|
|
SDL_Quit();
|
|
return 2;
|
|
}
|
|
SDL_WM_SetCaption(TITLE, TITLE);
|
|
|
|
initgl();
|
|
display();
|
|
SDL_Event event;
|
|
while (SDL_WaitEvent(&event))
|
|
{
|
|
if (event.type == SDL_QUIT)
|
|
break;
|
|
else if (event.type == SDL_KEYDOWN)
|
|
{
|
|
if (event.key.keysym.sym == SDLK_ESCAPE)
|
|
break;
|
|
}
|
|
else if (event.type == SDL_MOUSEBUTTONDOWN)
|
|
{
|
|
if (event.button.button == SDL_BUTTON_LEFT)
|
|
{
|
|
if (event.button.state == SDL_PRESSED)
|
|
{
|
|
dragging = true;
|
|
startx = event.button.x;
|
|
starty = event.button.y;
|
|
}
|
|
}
|
|
}
|
|
else if (event.type == SDL_MOUSEBUTTONUP)
|
|
{
|
|
if (event.button.button == SDL_BUTTON_LEFT)
|
|
{
|
|
dragging = false;
|
|
}
|
|
}
|
|
else if (event.type == SDL_MOUSEMOTION)
|
|
{
|
|
if (dragging)
|
|
{
|
|
glLoadIdentity();
|
|
glRotatef(event.motion.y - starty, 1, 0, 0);
|
|
glRotatef(event.motion.x - startx, 0, 0, 1);
|
|
glMultMatrixf(rotationMatrix);
|
|
glGetFloatv(GL_MODELVIEW_MATRIX, rotationMatrix);
|
|
startx = event.motion.x;
|
|
starty = event.motion.y;
|
|
display();
|
|
}
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|