PDA

View Full Version : Camera problem



DirtyBlasion
01-12-2017, 07:48 AM
#ifndef PLAYERCLASS_H_INCLUDED
#define PLAYERCLASS_H_INCLUDED

#include <iostream>
#include <windows.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <GL/freeglut.h>
#include <math.h>

using namespace std;

#define Pi 3.14159265359

void Display(void);

double degtorad(int angle)
{
return angle*Pi/180;
}

class PlayerClass
{
public:
int x, y, z, xzdir = 0, ydir = 0;
PlayerClass(int _x, int _y, int _z)
{
x = _x;
y = _y;
z = _z;
}
void Camera(int _x, int _y)
{
glutWarpPointer(glutGet(GLUT_WINDOW_WIDTH)/2, glutGet(GLUT_WINDOW_HEIGHT)/2);
xzdir += (glutGet(GLUT_WINDOW_WIDTH)/2 - _x)/8;
ydir += (glutGet(GLUT_WINDOW_HEIGHT)/2 - _y)/8;
if(xzdir > 360) xzdir -= 360;
if(xzdir < 0) xzdir += 360;
if(ydir > 90) ydir = 90; else if(ydir < -90) ydir = -90;
gluLookAt(x, y + 32, z,
x + cos(degtorad(xzdir)),
y + sin(degtorad(ydir)),
z - sin(degtorad(xzdir)), 0, 1, 0);
Display();
//cout << "xzdir: " << xzdir << endl << "ydir: " << ydir << endl;
}
};

#endif // PLAYERCLASS_H_INCLUDED


This is what I did for a camera class. But I have a problem, it's not working... I draw a piramid and it should look around... but it dont. I use freeglut and glew. I call it from glutPassiveMotionFunc

rivten
01-12-2017, 08:07 AM
Hi !

What does exactly happen ?

Do you see the pyramid and it doesn't move ? Or do you see nothing at all ?
Does you commented cout instruction pops up if you uncomment it ?

DirtyBlasion
01-12-2017, 08:16 AM
So it simply dont move, my pyramid is in the center of the screen. This camera should be supposed to act like a first person. The debug show the values pretty good. They increare, or decrease like it was supposed to.But the camera is stuck in the same pos.

DirtyBlasion
01-13-2017, 11:34 AM
Nobody? Please, help me! So I use freeglut. I have this class
class PlayerClass
{
public:
int x, y, z, xzdir = 0, ydir = 0;
PlayerClass(int _x, int _y, int _z)
{
x = _x;
y = _y;
z = _z;
}
void FirstPerson(int _x, int _y)
{
glutWarpPointer(glutGet(GLUT_WINDOW_WIDTH)/2, glutGet(GLUT_WINDOW_HEIGHT)/2);
xzdir += (glutGet(GLUT_WINDOW_WIDTH)/2 - _x)/8;
ydir += (glutGet(GLUT_WINDOW_HEIGHT)/2 - _y)/8;
if(xzdir > 360) xzdir -= 360;
if(xzdir < 0) xzdir += 360;
if(ydir > 90) ydir = 90; else if(ydir < -90) ydir = -90;

Display();
cout << "xzdir: " << xzdir << endl << "ydir: " << ydir << endl;
gluLookAt(x, 1.0f, z,
x + sin(degtorad(xzdir)),
1.0f,
z - cos(degtorad(xzdir)), 0, 1, 0);
cout << x + sin(degtorad(xzdir));
}
void FPRender()
{
gluLookAt(x, y + 32, z,
x + sin(degtorad(xzdir)),
1,
z - cos(degtorad(xzdir)), 0, 1, 0);
cout << x + sin(degtorad(xzdir));
}
};
Here I call it:

#ifndef BASEFUNCT_H_INCLUDED
#define BASEFUNCT_H_INCLUDED

#include <iostream>
#include <windows.h>
#include <stdlib.h>
#include <GL/glew.h>
//#include <GL/eglew.h>
#include <GL/freeglut.h>

void Frame();

PlayerClass Player(-2, 1, 2);

void pyramid()
{
glBegin( GL_TRIANGLES );
glColor3f( 1.0f, 0.0f, 0.0f ); glVertex3f( 0.0f, 1.f, 0.0f );
glColor3f( 0.0f, 1.0f, 0.0f ); glVertex3f( -1.0f, -1.0f, 1.0f );
glColor3f( 0.0f, 0.0f, 1.0f ); glVertex3f( 1.0f, -1.0f, 1.0f);

glColor3f( 1.0f, 0.0f, 0.0f ); glVertex3f( 0.0f, 1.0f, 0.0f);
glColor3f( 0.0f, 1.0f, 0.0f ); glVertex3f( -1.0f, -1.0f, 1.0f);
glColor3f( 0.0f, 0.0f, 1.0f ); glVertex3f( 0.0f, -1.0f, -1.0f);

glColor3f( 1.0f, 0.0f, 0.0f ); glVertex3f( 0.0f, 1.0f, 0.0f);
glColor3f( 0.0f, 1.0f, 0.0f ); glVertex3f( 0.0f, -1.0f, -1.0f);
glColor3f( 0.0f, 0.0f, 1.0f ); glVertex3f( 1.0f, -1.0f, 1.0f);


glColor3f( 1.0f, 0.0f, 0.0f ); glVertex3f( -1.0f, -1.0f, 1.0f);
glColor3f( 0.0f, 1.0f, 0.0f ); glVertex3f( 0.0f, -1.0f, -1.0f);
glColor3f( 0.0f, 0.0f, 1.0f ); glVertex3f( 1.0f, -1.0f, 1.0f);
glEnd();

}

int x1, y1;

void PassiveMouse(int x, int y)
{
x1 = x;
y1 = y;
}

void Display(void)
{
//Clear information from last draw
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
Player.FPRender();
glColor3f(1, 0, 0);
glBegin(GL_QUADS);
glVertex3f(-100.0f, 0.0f, -100.0f);
glVertex3f(-100.0f, 0.0f, 100.0f);
glVertex3f( 100.0f, 0.0f, 100.0f);
glVertex3f( 100.0f, 0.0f, -100.0f);
glEnd();
glutSwapBuffers();
//Player.FPRender();
//glTranslated(Player.x, Player.y, Player.z); //location of the pyramid
//pyramid(); // call out rutine to draw a pyramid
//cout << "Display - working \n";
//Sleep(500);
}

void Keyboard(unsigned char key, int x, int y)
{
switch(key)
{
case 27: exit(0); break;
default: break;
}
//Frame();
}

void Frame()
{
Player.FirstPerson(x1, y1);
}

#endif // BASEFUNCT_H_INCLUDED
But if I disable it... it draw the pyramid, but when I activate it, I cant see my drawing. The debug values are perfect...

Dark Photon
01-13-2017, 06:07 PM
Nobody? Please, help me! So I use freeglut. I have this class ... But if I disable it... it draw the pyramid, but when I activate it, I cant see my drawing. The debug values are perfect...

Couple things. In the code you posted, I don't see any way for the PassiveMouse event to affect your drawing. Trace the path from here to your camera update.

Second, if you want your display function to be called again, you have to request it. Try calling glutPostRedisplay() from your Display() method and right after you register your display callback.

Third, what you're doing with trying to capture mouse motion and warp the pointer is dubious at best. See these posts:

* https://gamedev.stackexchange.com/questions/10100/trapping-mouse-inside-window-in-opengl-with-glut
* https://stackoverflow.com/questions/728049/glutpassivemotionfunc-and-glutwarpmousepointer

for some commentary and suggestions.