Skip to main content
gamedev.net
🔒 Locked

opengl order

Started by bcware Nov 5, 2000 at 12:48 PM 3 replies 250+ views
Original Post
bcware
bcware
ok my problem is i have a cube and i want to have this cube fly around the screen controlled by the user, the user can rotate the cube and make it fly in any direction they want, my problem is that my cube is always rotating around the center of the screen instead of its center, now i know what your gonna say, your gonna say well thats easy your simply translating first and then rotating, AHA but i am not i am rotating first and then translating but opengl doesn''t seem to understant what the hell i am telling it, in my drawing code i AM rotating before translating but opengl is not it seems to do whatever it wants and its translating and THEN rotating = bad because my "cube" is always rotating around the center of the screen, how can i solve this? in a nutshell my code looks like this: glLoadIdentity(); glRotatef(zrot,0.0f,0.0f,1.0f); glTranslatef(x,y,-25.0f); glBegin(GL_QUADS); . . .
Ferrit
Ferrit
This is taken straight from "OpenGL Superbible SE" by Waite Group Press.
I''m not employed or in any way associated with them, but this book
is AMAZING. Try this:



// Camera initial position and orientation
// Initially at Origin, slightly above the ground
cameraData.position[0] = 0.0f;
cameraData.position[1] = -0.2f;
cameraData.position[2] = -3.0f;

// Looking down negative Z Axis (North). Positive X is to the right
cameraData.orientation[0] = 0.0f;
cameraData.orientation[1] = 0.0f;
cameraData.orientation[2] = 0.0f;




void UpdatePosition(struct CAMERA *pCamera)
{
float fLinearVelocity = 0.0f;
float fAngularVelocityY = 0.0f;
float fAngularVelocityX = 0.0f;
float fAngularVelocityZ = 0.0f;
float fTime,fAngle,fXDelta,fZDelta;
JOYINFO ji;
LARGE_INTEGER currentTime;

// Check Joystick if present
if(joyGetPos(JOYSTICKID1, &ji) == JOYERR_NOERROR)
{
// Button 1 moves forward, button 2 moves backwards
if(ji.wButtons & JOY_BUTTON1)
fLinearVelocity = 0.8f;

if(ji.wButtons & JOY_BUTTON2)
fLinearVelocity = -0.8f;

// Check for spin left/right (Y Axis rotation)
// Move quicker if joystick is moved hard
if(ji.wXpos < 20000)
fAngularVelocityY = -15.0f;
if(ji.wXpos < 3000)
fAngularVelocityY = -22.0f;

if(ji.wXpos > 45536)
fAngularVelocityY = 15.0f;
if(ji.wXpos > 62536)
fAngularVelocityY = 22.0f;

// Check for looking up and down
if(ji.wYpos < 20000)
fAngularVelocityX = 14.0f;
if(ji.wYpos < 3000)
fAngularVelocityX = 21.0f;

if(ji.wYpos > 45536)
fAngularVelocityX = -14.0f;
if(ji.wYpos > 63536)
fAngularVelocityX = - 21.0f;
}


// Keyboard Input
// Check for forward or backwards Motion
if(GetAsyncKeyState(VK_UP))
fLinearVelocity = 0.8f;

if(GetAsyncKeyState(VK_DOWN))
fLinearVelocity = -0.8f;

// Check for spin left/right (Y Axis rotation)
if(GetAsyncKeyState(VK_LEFT))
fAngularVelocityY = -14.0f;

if(GetAsyncKeyState(VK_RIGHT))
fAngularVelocityY = 14.0f;

// Check for looking up and down
if(GetAsyncKeyState(0x41)) // A key
fAngularVelocityX = 14.0f;

if(GetAsyncKeyState(0x5a)) // Z key
fAngularVelocityX = -14.0f;


// Adjust position and orientation. Get the time since the last
// check. If the velocity = 0 (no keypress or mouse movement)
// then the motion will be nil...
// D = vt
QueryPerformanceCounter(&currentTime);
fTime = (float)(currentTime.QuadPart - lastTime.QuadPart)/
(float)timerFrequency.QuadPart;
lastTime = currentTime;

// Update Rotation angles (clamp the X rotation)
fAngle = fTime * fAngularVelocityX;
fAngle += pCamera->orientation[0];

if((fAngle < 90.0f) && (fAngle > -90.0f))
pCamera->orientation[0] = fAngle;

pCamera->orientation[1] += fTime * fAngularVelocityY;

// Update linear position
fTime = fTime * fLinearVelocity;
fXDelta = fTime * (float)(sin(DEGTORAD(pCamera->orientation[1])));
fXDelta += pCamera->position[0];
fZDelta = fTime * (float)(cos(DEGTORAD(pCamera->orientation[1])));
fZDelta += pCamera->position[2];


pCamera->position[0] = fXDelta;
pCamera->position[2] = fZDelta;

// Halt the camera at the boundaries of the Virtual World.
// The maximum range for the person is +/- 95 from the center
// The boundaries of the world are +/- 100 in all directions
if(pCamera->position[0] > 95.0f)
pCamera->position[0] = 95.0f;

if(pCamera->position[0] < -95.0f)
pCamera->position[0] = -95.0f;

if(pCamera->position[2] > 95.0f)
pCamera->position[2] = 95.0f;

if(pCamera->position[2] < -95.0f)
pCamera->position[2] = -95.0f;
}

void PositionCamera()
{
glLoadIdentity(); // Z-X-Y (Boom specific)

glRotatef(cameraData.orientation[2], 0.0f, 0.0f, 1.0f);
glRotatef(cameraData.orientation[0], 1.0f, 0.0f, 0.0f);
glRotatef(cameraData.orientation[1], 0.0f, 1.0f, 0.0f);
glTranslatef(-cameraData.position[0],cameraData.position[1],cameraData.position[2]);
}


I know its for a camera, but it works for cubes as well. Hope that helps

bcware
bcware
there has to be a simpler way i am not making a camera with all this other crap all i want to do is get opengl to do things in the order i tell it to, which it is not...
zedzeek
zedzeek
just use gluLookAt
first 3 numbers are where the camera is, 2nd 3 is where its looking at, and the last 3 speciy up

http://members.xoom.com/myBollux

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.