Jump to content
  • Advertisement
Sign in to follow this  
TreveonMaxwell

Camera?! (OpenGL)

This topic is 2691 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 searched high and low and no matter what I can't seem to find anything that is dumbed down enough apparently.

I have a camera class that is pretty simple. It has a constructor that initializes its vectors...

It even has a pointer that is used to get the object that it is supposed to be followings vectors as well...

I am using gluLookAt (cause from what I gather, thats what you are supposed to use if you want to change what the viewport is currently looking at) in my OnCalculate function in the Modelview matrix...

However, when I start my program and my player object starts moving, when it gets to the edge of the screen the camera doesn't follow.

Normally, I'd hack this out until I figure it out but I've been at this one problem now for two weeks and I've learned a few things but this ONE concept continues to elude me!

How do I make the viewport (camera) follow my player object and keep it in the center of the screen? This is a 2D project that was converted from SDL to SDL/OGL. If you'd like to see the poorly formatted code I have, I'll post it, but at this point I'm not sure that will help anyone. I have my screen currently set up like so:


#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600

// .....

int CEngine::Initialize()
{
// Initialize SDL and Subsystems
if( SDL_Init(SDL_INIT_EVERYTHING) == -1)
{
cout << "ALERT! There was a problem setting up SDL!"
<< endl
<< "SDL_Error = "
<< SDL_GetError()
<< endl;
return 1;
}

SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

Display = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32, SDL_OPENGL);

glEnable(GL_TEXTURE_2D);
glClearColor(0.0f, 0.0f, 1.0f, 0.0f);
glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
glClear(GL_COLOR_BUFFER_BIT);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

glOrtho(0.0f, SCREEN_WIDTH, SCREEN_HEIGHT, 0.0f, -1.0f, 10.0f);

glMatrixMode(GL_MODELVIEW);
//glLoadIdentity();

if( Player.GetCData("./data/Xdata.txt") != 0)
{
cout << "Problem detected in Player.GetCData!" << endl;
return 4;
}

Camera.SetTarget(&Player);

return 0;
}

// ....


If I could get someone to show me, VERY SIMPLY, wth I should be doing here, you would have gained my undying affection :D

Seriously I'd really appreciate it if someone would dumb this way down for me cause apparently all this reading I'm doing is over my head or something.

Share this post


Link to post
Share on other sites
Advertisement
If the project was converted from SDL to OpenGL, that means the camera class probably also controls drawing to some extent and scrolling is done in software. Perhaps the camera position should remain static in that case?

Share this post


Link to post
Share on other sites
The only thing SDL does is initialize OGL and allow me to use SDL Event/Sound/etc. Therefore I need to learn how changing the what the viewport currently has it in as is done in OGL. My camera has absolutely no drawing code in it at all.

Share this post


Link to post
Share on other sites
I can't think of anything obvious that would cause the symptom you described. Have you tried stepping through in the debugger and doing analysis that way?

Share this post


Link to post
Share on other sites
Hey awesome I figured it out finally!

Before doing any transformations on objects and whatnot I'm supposed to use glTranslatef with the inverse value of the object I want to follow! It seems so simple now but this eluded me for quite some time! Thanks for the responses and yes I was using the debugger it wouldn't tell me something like that though :D

Thanks again!

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!