trackball working with rectangle
git-svn-id: svn://anubis/misc/wfobj-view@30 bd8a9e45-a331-0410-811e-c64571078777
This commit is contained in:
parent
3abb848fac
commit
dd1f9a834d
120
wfobj-view.cc
120
wfobj-view.cc
@ -1,5 +1,6 @@
|
|||||||
|
|
||||||
/* Libraries we use */
|
/* Libraries we use */
|
||||||
|
#include <SDL/SDL.h>
|
||||||
#include <GL/gl.h>
|
#include <GL/gl.h>
|
||||||
#include <GL/glu.h>
|
#include <GL/glu.h>
|
||||||
|
|
||||||
@ -8,58 +9,113 @@
|
|||||||
#define HEIGHT 800
|
#define HEIGHT 800
|
||||||
#define TITLE "Josh's Wavefront Object Viewer"
|
#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 */
|
/* Some global variables */
|
||||||
float rotationMatrix[16];
|
float rotationMatrix[16];
|
||||||
int startx, starty;
|
int startx, starty;
|
||||||
|
bool dragging = false;
|
||||||
|
|
||||||
/* The program's main entry point */
|
void initgl(void)
|
||||||
int main(int argc, char *argv[])
|
|
||||||
{
|
{
|
||||||
/* OpenGL initialization */
|
glClearColor(0.0, 0.0, 0.0, 0.0);
|
||||||
glShadeModel(GL_SMOOTH);
|
|
||||||
glEnable(GL_DEPTH_TEST);
|
glEnable(GL_DEPTH_TEST);
|
||||||
|
glShadeModel(GL_SMOOTH);
|
||||||
glEnable(GL_LIGHTING);
|
glEnable(GL_LIGHTING);
|
||||||
glEnable(GL_LIGHT0);
|
glEnable(GL_LIGHT0);
|
||||||
float lpos[] = {0.0f, -6.0f, 0.0f, 1.0f};
|
float pos[] = {0.0, -1.0, 0.0, 0.0};
|
||||||
glLightfv(GL_LIGHT0, GL_POSITION, lpos);
|
glLightfv(GL_LIGHT0, GL_POSITION, pos);
|
||||||
glEnable(GL_CULL_FACE);
|
glEnable(GL_CULL_FACE);
|
||||||
glClearColor(0, .3, 0.5, 1);
|
glViewport(0, 0, WIDTH, HEIGHT);
|
||||||
/* Initialize the default rotation matrix */
|
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);
|
glMatrixMode(GL_MODELVIEW);
|
||||||
glLoadIdentity();
|
glLoadIdentity();
|
||||||
glGetFloatv(GL_MODELVIEW_MATRIX, rotationMatrix);
|
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);
|
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
|
||||||
glLoadIdentity();
|
glLoadIdentity();
|
||||||
glMultMatrixf(rotationMatrix);
|
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 */
|
/* The program's main entry point */
|
||||||
void mouse(int button, int state, int x, int y)
|
int main(int argc, char * argv[])
|
||||||
{
|
{
|
||||||
startx = x;
|
if (SDL_Init(SDL_INIT_VIDEO))
|
||||||
starty = y;
|
{
|
||||||
}
|
printf("Failed to initialize SDL!\n");
|
||||||
|
return 1;
|
||||||
/* handle mouse motion events */
|
}
|
||||||
void motion(int x, int y)
|
|
||||||
{
|
atexit(SDL_Quit);
|
||||||
glLoadIdentity();
|
|
||||||
glRotatef(y-starty, 1, 0, 0);
|
SDL_Surface * screen;
|
||||||
glRotatef(x-startx, 0, 0, 1);
|
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
||||||
glMultMatrixf(rotationMatrix);
|
if (!(screen = SDL_SetVideoMode(WIDTH, HEIGHT, 32, SDL_OPENGL)))
|
||||||
glGetFloatv(GL_MODELVIEW_MATRIX, rotationMatrix);
|
{
|
||||||
startx=x;
|
printf("Failed to set video mode!\n");
|
||||||
starty=y;
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user