trackball working with rectangle

git-svn-id: svn://anubis/misc/wfobj-view@30 bd8a9e45-a331-0410-811e-c64571078777
This commit is contained in:
josh 2008-01-30 03:51:23 +00:00
parent 3abb848fac
commit dd1f9a834d

View File

@ -1,5 +1,6 @@
/* Libraries we use */
#include <SDL/SDL.h>
#include <GL/gl.h>
#include <GL/glu.h>
@ -8,58 +9,113 @@
#define HEIGHT 800
#define TITLE "Josh's Wavefront Object Viewer"
/* Some function prototypes */
void display(void);
void mouse(int button, int state, int x, int y);
void motion(int x, int y);
/* Some global variables */
float rotationMatrix[16];
int startx, starty;
bool dragging = false;
/* The program's main entry point */
int main(int argc, char *argv[])
void initgl(void)
{
/* OpenGL initialization */
glShadeModel(GL_SMOOTH);
glClearColor(0.0, 0.0, 0.0, 0.0);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
float lpos[] = {0.0f, -6.0f, 0.0f, 1.0f};
glLightfv(GL_LIGHT0, GL_POSITION, lpos);
float pos[] = {0.0, -1.0, 0.0, 0.0};
glLightfv(GL_LIGHT0, GL_POSITION, pos);
glEnable(GL_CULL_FACE);
glClearColor(0, .3, 0.5, 1);
/* Initialize the default rotation matrix */
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);
return 0;
}
/* This function is called to display the scene each refresh */
void display(void)
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();
}
/* handle mouse button press/release events */
void mouse(int button, int state, int x, int y)
/* The program's main entry point */
int main(int argc, char * argv[])
{
startx = x;
starty = y;
}
/* handle mouse motion events */
void motion(int x, int y)
{
glLoadIdentity();
glRotatef(y-starty, 1, 0, 0);
glRotatef(x-startx, 0, 0, 1);
glMultMatrixf(rotationMatrix);
glGetFloatv(GL_MODELVIEW_MATRIX, rotationMatrix);
startx=x;
starty=y;
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;
}