Jump to content
  • Advertisement
Sign in to follow this  
VitaliBR

OpenGL Camera

This topic is 3085 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I tried to apply a camera in my code,
but their movements are not correct, I not get fix it.

If someone can look at the code and try to help fix it
I'm using Code::Blocks (Opengl w/ SDL)

http://www.easy-share.com/1911197554/GL_GAME.rar

The functions of camera are:

In your render function before drawing anything you have to call Cam.setView(); To rotate around the (global) y-axis you call Cam.rotateGlob(degree, 0,1,0); while to look up/down you would call Cam.rotateLoc(degree, 1,0,0); Moving forward would be Cam.moveLoc(0,0,distance); while being shoved in a fixed direction would be Cam.moveGlob(deltaX, deltaY, deltaZ);


I am trying to fix almost 2hours
Thanks

Share this post


Link to post
Share on other sites
Advertisement
what is the difference between GLfloat and float?

[Edited by - VitaliBR on July 3, 2010 12:34:25 AM]

Share this post


Link to post
Share on other sites
Quote:
what is the difference between GLfloat and float?


nothing.

typedef float GLfloat; //in gl.h i think

Share this post


Link to post
Share on other sites
Thanks

I managed to fix the camera,
I'm trying now is to make works with two keystrokes.
For example 'w' and 'a' it will move forward and turning left to go.

Here is the code:

//We read the state of the keys
Uint8* keys = SDL_GetKeyState(NULL);

//With the Left arrow is pressed?
if (keys[SDLK_LEFT])
{
Camera.StrafeRight(-0.1);
}
//With the Right arrow is pressed?
else if (keys[SDLK_RIGHT])
{
Camera.StrafeRight(0.1);
}
//With the Up arrow is pressed?
else if(keys[SDLK_UP])
{
Camera.RotateX(0.5);
}
//With the Down arrow is pressed?
else if(keys[SDLK_DOWN])
{
Camera.RotateX(-0.5);
}
//With the 'W' arrow is pressed?
else if(keys[SDLK_w])
{
Camera.MoveForward(-0.1);
}
//With the 'S' arrow is pressed?
else if(keys[SDLK_s])
{
Camera.MoveForward(0.1);
}
//With the 'A' arrow is pressed?
else if(keys[SDLK_a])
{
Camera.RotateY(0.5);
}
//With the 'D' arrow is pressed?
else if(keys[SDLK_d])
{
Camera.RotateY(-0.5);
}




Thanks

[Edited by - VitaliBR on July 3, 2010 2:10:03 PM]

Share this post


Link to post
Share on other sites
I've just got back from holiday so I'm a little tired and may be being stupid. Basically IIRC if the user presses the left key down, none of the other logic will be checked because the first 'if' statement was met.

Break it down into sections. It makes it a bit easier to read and keeps the related code together. The example below shows that if 'w' is not pressed it checks for 's' and that the end of the MoveForward related code.

// Forward/backward code
if(keys[SDLK_w])
{
Camera.MoveForward(-0.1);
}
else if(keys[SDLK_s])
{
Camera.MoveForward(0.1);
}

// Rotation code
if(keys[SDLK_a])
{
Camera.RotateY(0.5);
}
else if(keys[SDLK_d])
{
Camera.RotateY(-0.5);
}


The only kink is if 'w' and 's' are pressed in which case the code for 'w' is executed. One way round would be to replace all 'else if' statements with 'if'. That would solve the problem by running the MoveForward code twice, once to go forward and then once to return you to your original position.

Share this post


Link to post
Share on other sites
friend, I'm embarrassed.
How could not I see this?

I'm sorry, sometimes it is something so simple that goes unnoticed.
Thank you very much!! :)


As for handling the mouse, I have an idea(generic), something like:


void camera (void) {
glRotatef(xrot,1.0,0.0,0.0); //rotate our camera on teh x-axis (left and right)
glRotatef(yrot,0.0,1.0,0.0); //rotate our camera on the y-axis(up and down)
glTranslated(-xpos,-ypos,-zpos); //translate the screen to the position of our camera
}

void mouseMovement(int x, int y) {
int diffx=x-lastx; //check the difference between the current x and the //last x position
int diffy=y-lasty; //check the difference between the current y and the //last y position
lastx=x; //set lastx to the current x position
lasty=y; //set lasty to the current y position
xrot += (float) diffy; //set the xrot to xrot with the addition
//of the difference in the y position
yrot += (float) diffx; //set the xrot to yrot with the addition
// of the difference in the x position
}




the logic is correct? would use a function from the SDL to get the x and y position of the mouse right?

thanks

Share this post


Link to post
Share on other sites
Can someone explain to me the theory of how to use the mouse to move the camera (like the games FSP)?

Share this post


Link to post
Share on other sites
I'm trying to get my camera with the mouse.

When moving the mouse (on the axis X and Y), the camera makes strange rotations. I'm putting my full code for download (Source + Executable).

http://www.mediafire.com/?vdzjuocjim2

Please help me, I'm days trying to solve this problem

Thanks

Share this post


Link to post
Share on other sites
Quote:
I'm putting my full code for download (Source + Executable).

http://www.mediafire.com/?vdzjuocjim2
Perhaps you could also post the relevant code here (you might get more folks to look at it that way).

Share this post


Link to post
Share on other sites
Sorry, I'll put the most important parts then.

About Camera:
//...
//as we know the up vector, we can easily use gluLookAt:
gluLookAt( Position.x,Position.y,Position.z,
ViewPoint.x,ViewPoint.y,ViewPoint.z,
UpVector.x,UpVector.y,UpVector.z);
//...



Function of the mouse motion:


void CCamera::Mouse(const int Width, const int Height)
{
//Mid x/y
int mid_x = Width >> 1;
int mid_y = Height >> 1;
//Mouse coord
int mouseX,mouseY;

//Get the position of mouse in the screen
SDL_GetMouseState(&mouseX,&mouseY);

//Difference between the midpoint and the mouse position after of movimented
float relative_mouseX = mid_x - mouseX;
float relative_mouseY = mid_y - mouseY;

//the mouse cursor is at the center of the screen
SDL_WarpMouse(mid_x, mid_y);

//ViewPoint
SF3dVector ViewPoint = Position+ViewDir;

//Angles
const float
angle_y = relative_mouseX/10,
angle_x = relative_mouseY/10,
angle_z = 0;

//Viewpoint Y
ViewPoint.y += angle_y * 2;
if((ViewPoint.y - Position.y) > 8) ViewPoint.y = Position.y + 8;
if((ViewPoint.y - Position.y) < -8) ViewPoint.y = Position.y - 8;

//Viewpoint X
ViewPoint.x += angle_x * 2;
if((ViewPoint.x - Position.x) > 8) ViewPoint.x = Position.x + 8;
if((ViewPoint.x - Position.x) < -8) ViewPoint.x = Position.x - 8;

//Rotate methods
if(angle_y)
RotateY(angle_y);
if(angle_x)
RotateX(angle_x);
if(angle_z)
RotateZ(angle_z);
}



Events of Keyboard and Mouse:

        
//With the Left arrow is pressed? - Rotate to left
if (keys[SDLK_LEFT])
{
Camera.RotateY(0.5);
}
//With the Right arrow is pressed? - Rotate to right
else if (keys[SDLK_RIGHT])
{
Camera.RotateY(-0.5);
}

//With the Up arrow is pressed? - Look to up
if(keys[SDLK_UP])
{
Camera.RotateX(0.5);
}
//With the Down arrow is pressed? - Look to down
else if(keys[SDLK_DOWN])
{
Camera.RotateX(-0.5);
}

case SDL_MOUSEMOTION:
{
Camera.Mouse(800,600);
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!