Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411678 Posts in 69399 Topics- by 58453 Members - Latest Member: Arktitus

May 17, 2024, 09:33:19 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Camera management in 2d opengl game [solved]
Pages: [1]
Print
Author Topic: Camera management in 2d opengl game [solved]  (Read 1147 times)
dr.crow
Level 1
*



View Profile
« on: March 28, 2010, 06:02:35 PM »

I've recently done the jump from SDL to openGL. And I'm wondering if my old ways of doing things are not the simplest anymore.

What i want to do, is finding a way to implement:
*Zooming
*Ability to support splitscreen

The way i've solved this problem earlier, is by creating a Camera class with the following interface:

Code:
void setCenter(float centerx, float centery);
int getScreenX(float worldx);
int getScreenY(float worldy);
int scale(float v); //convert distance in world space to length on screen
void zoom(float delta);

and then passed a pointer to the camera along to the drawing functions, like this:
gameObject->draw(camera);

This works fine, but I was thinking that there might be a simpler solution involving some opengl functions.
Maybe glViewport or something (but wouldn't that move the gui too?).

How do you guys usually solve this problem?
« Last Edit: March 29, 2010, 07:52:26 PM by dr.crow » Logged
Average Software
Level 10
*****

Fleeing all W'rkncacnter


View Profile WWW
« Reply #1 on: March 28, 2010, 06:17:28 PM »

I've done split screen in the past with glViewport, and it works just fine.

Assuming an 800x600 window, the algorithm looks something like this:

Code:
glViewport(0, 300, 800, 300); // Set viewport to top half of screen.
DrawPlayer1Scene();
glViewport(0, 0, 800, 300); // Set viewport to bottom half of screen.
DrawPlayer2Scene();
SwapBuffers();

As for zooming, I think it really depends on what you're doing and what you want to zoom in on.  There are many ways to achieve the effect, but I need more information.
Logged



What would John Carmack do?
dr.crow
Level 1
*



View Profile
« Reply #2 on: March 28, 2010, 07:02:17 PM »

Thanks for the reply.
As for zooming, I think it really depends on what you're doing and what you want to zoom in on.  There are many ways to achieve the effect, but I need more information.
I mean pressing the plus/minus button on the numpad will cause everything to appear as closer or farther away.

Usually i store a zoomfactor variable inside my cameras.
Code:
int Camera::getScreenX(float worldx){
    return int( (worldx-x)*zoomfactor + w/2);

EDIT:
Found a solution. I didn't quite understand the power of glTranslate and glScale until now.

Here's my camera component if anyone else is interested:
Code:
class CameraComponent:public GameObjectComponent{
    private:
        f32 x,y;
        f32 scale;
    public:
        void update(GameObject* go);
        void recieve(u32 message);
        CameraComponent():x(0.f),y(0.f),scale(10.f){}
};

void CameraComponent::update(GameObject* go){
    x=go->x;
    y=go->y;
}

void CameraComponent::recieve(u32 message){
    switch(message){
        case SETVIEW:
            ///opengl functions
            glLoadIdentity();
            glTranslatef(-x*scale+SCREENWIDTH/2,y*scale+SCREENHEIGHT/2,0.f);
            glScalef(scale,-scale,scale);
        break;
        case ZOOMIN:
            scale+=scale*.3*Time::step;
        break;
        case ZOOMOUT:
            scale-=scale*.3*Time::step;
        break;

    }
}
« Last Edit: March 29, 2010, 08:09:11 PM by dr.crow » Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic