model viewer

Started by
3 comments, last by FippyDarkpaw 15 years, 10 months ago
Hey guys. I need to make something like a model viewer so I need some code to rotate the model usng the mouse. Now I've heard that quaternions would be my best bet here since I want a smooth intuitive rotation style. However Ive never worked with quaternions and I couldnt really find a good tutorial on rotating objets with the mouse so does anyone have any good resources. The simpler the better. Thanks in advance. -CProgrammer
Advertisement
The 'mouse' is really just an x,y position. Depending on what mechanism you're using to detect mouse movement, it may provide both an absolute position, and a relative (the change in x,y) position. You use the change in the position to make changes to your scene. The mouse moved -5,0? Well 5 is 5/800 of my screen's width. I want to rotate my object by (5/800)*some_constant_factor.

some non-quaternion pseudo code:

const float screenWidth  = 800;const float screenHeight = 600;const float factor = 10;int changeInX = 0;int changeInY = 0;getMouseInput(&changeInX, &changeInY);float rotateX = (changeInX/screenWidth)  * factor;float rotateY = (changeInY/ScreenHeight) * factor;glPushMatrix();glTranslatef(...);glRotatef(rotateX, 1, 0, 0);glRotatef(rotateY, 0, 1, 0);drawObject();glPopMatrix();
Here is one I made that's exactly what you are describing. It is a single file of code so should be easy to read:

ModelViewer.zip

Here is a pic, click to enlarge:




Edit: oops you didn't specify language, this is C#/XNA. Also it doesn't use quaternions. It's very simple though. =)
What graphics API are you using?
It's C#/XNA (so I guess techinically DirectX is the graphics API?).

This topic is closed to new replies.

Advertisement