OpenGL camera direction/movement

Hi,
I made a camera class to use it in opengl, but i have some problem with the moving. (Still very unfinished, I still test it)
First here my header:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class PNDCAMERA
{
public:
	static float modelviewMatrix[16];
        static float projectionMatrix[16];

	PNDVECTOR cameraPosition;
	double horizontalAngle;
	double verticalAngle;

	PNDVECTOR directionVector;
        PNDVECTOR rightVector;
	PNDVECTOR upVector;
	
	void renderMatrix();

	void changePosForward();
	void changePosBackward();
	void changePosRight();
	void changePosLeft();
	void changehAngle(float,bool);
	void changevAngle(float,bool);

	PNDCAMERA();
	PNDCAMERA(GLfloat,GLfloat,GLfloat,GLfloat,GLfloat);
};


and cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "GLSLProgram.h"
#include "pndcamera.h"

#define C_SPEED 0.00000001 //still for testing, i will replace it with cursor movement and frame time
#define _CRT_TERMINATE_DEFINED

float PNDCAMERA::modelviewMatrix[16];
float PNDCAMERA::projectionMatrix[16];

PNDCAMERA::PNDCAMERA()
{
	cameraPosition.cx=0;
	cameraPosition.cy=10;
	cameraPosition.cz=5;

	horizontalAngle=45;
	verticalAngle=0.0;
};

PNDCAMERA::PNDCAMERA(GLfloat posx,GLfloat posy,GLfloat posz,GLfloat hAngle,GLfloat vAngle)
{
	cameraPosition.cx=posx;
	cameraPosition.cy=posy;
	cameraPosition.cz=posz;

	horizontalAngle=hAngle;
	verticalAngle=vAngle;
};

void PNDCAMERA::renderMatrix()
{
    PNDVECTOR rightVector(sin(horizontalAngle - 3.14f/2.0f),0,cos(horizontalAngle - 3.14f/2.0f));

	PNDVECTOR directionVector(
    cos(verticalAngle) * sin(horizontalAngle), sin(verticalAngle), cos(verticalAngle) * cos(horizontalAngle));

	PNDVECTOR upVector(
		(PNDVECTOR::cross_product(rightVector,directionVector)).cx,
		(PNDVECTOR::cross_product(rightVector,directionVector)).cy,
		(PNDVECTOR::cross_product(rightVector,directionVector)).cz);

	glMatrixMode(GL_MODELVIEW);

	gluLookAt(
		cameraPosition.cx,
	        cameraPosition.cy,
	        cameraPosition.cz,

		(PNDVECTOR::add_vector(cameraPosition,directionVector)).cx, //
		(PNDVECTOR::add_vector(cameraPosition,directionVector)).cy,
		(PNDVECTOR::add_vector(cameraPosition,directionVector)).cz,

		upVector.cx,
		upVector.cy,
		upVector.cz);

	glGetFloatv(GL_MODELVIEW_MATRIX,modelviewMatrix);
	glGetFloatv(GL_PROJECTION_MATRIX,projectionMatrix);

	shaderProgram->sendUniform4x4("modelview_matrix",modelviewMatrix);
	shaderProgram->sendUniform4x4("projection_matrix",projectionMatrix);

};

void PNDCAMERA::changePosForward()
{cameraPosition=PNDVECTOR::add_vector(cameraPosition,directionVector*C_SPEED); //now I have overloadedoperators for these
};

void PNDCAMERA::changePosBackward()
{cameraPosition=PNDVECTOR::sub_vector(cameraPosition,directionVector*C_SPEED);};
void PNDCAMERA::changePosRight()
{cameraPosition=PNDVECTOR::add_vector(cameraPosition,rightVector*C_SPEED);};
void PNDCAMERA::changePosLeft()
{cameraPosition=PNDVECTOR::sub_vector(cameraPosition,rightVector*C_SPEED);};

void PNDCAMERA::changehAngle(float hangle,bool dir)
{if(dir==true)
horizontalAngle+=hangle;
else horizontalAngle-=hangle;};

void PNDCAMERA::changevAngle(float vangle,bool dir)
{if(dir==true)
verticalAngle+=vangle;
else verticalAngle-=vangle;};


My problem is that when i move the camera, it only moves in the direction vec3(1.0,1.0,1.0). I can rotate it well, but when i press the UP button/or down/ it moves only in that one direction. I dont know what can cause the problem, I think my right/direction vectors are good.

My vector class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#ifndef  PNDVECTOR_H
#define  PNDVECTOR_H

class PNDVECTOR{
public:
	float cx;
	float cy;
	float cz;
	static PNDVECTOR cross_product(PNDVECTOR,PNDVECTOR,PNDVECTOR);
	static PNDVECTOR cross_product(PNDVECTOR,PNDVECTOR);
	static PNDVECTOR average_product(PNDVECTOR*);
	static PNDVECTOR add_vector(PNDVECTOR,PNDVECTOR);
	static PNDVECTOR sub_vector(PNDVECTOR,PNDVECTOR);

	PNDVECTOR operator*(double constant);
	PNDVECTOR operator/(double constant);
	PNDVECTOR operator+(double constant);
	PNDVECTOR operator-(double constant);

	PNDVECTOR operator+(PNDVECTOR& vector);
	PNDVECTOR operator-(PNDVECTOR& vector);

	static PNDVECTOR nullVector;
	PNDVECTOR(float,float,float);
	PNDVECTOR();
};
#endif

//cpp:

#include "pndvector.h"

PNDVECTOR::PNDVECTOR(float x,float y, float z):cx(x),cy(y),cz(z){};
PNDVECTOR PNDVECTOR::nullVector(0,0,0);
PNDVECTOR::PNDVECTOR(){};

PNDVECTOR PNDVECTOR::cross_product(PNDVECTOR vec1,PNDVECTOR vec2)
{
	return(PNDVECTOR(
		vec1.cy * vec2.cz - vec1.cz * vec2.cy,
		vec1.cz * vec2.cx - vec1.cx * vec2.cz,
		vec1.cx * vec2.cy - vec1.cy * vec2.cx));
};

PNDVECTOR PNDVECTOR::cross_product(PNDVECTOR vec1,PNDVECTOR vec2,PNDVECTOR vec_basepos)
{
	PNDVECTOR A, B;

	A.cx=vec1.cx-vec_basepos.cx;
	A.cy=vec1.cy-vec_basepos.cy;
	A.cz=vec1.cz-vec_basepos.cz;

	B.cx=vec2.cx-vec_basepos.cx;
	B.cy=vec2.cy-vec_basepos.cy;
	B.cz=vec2.cz-vec_basepos.cz;

	return(PNDVECTOR(
		A.cy * B.cz - A.cz * B.cy,
		A.cz * B.cx - A.cx * B.cz,
		A.cx * B.cy - A.cy * B.cx));
}

PNDVECTOR PNDVECTOR::average_product(PNDVECTOR* vNormals)
{
	PNDVECTOR vAverageNormal;
	vAverageNormal.cx=0;
	vAverageNormal.cy=0;
	vAverageNormal.cz=0;

	for (int i=0; i<8; i++)
	{
		vAverageNormal.cx+=(vNormals[i].cx);
		vAverageNormal.cy+=(vNormals[i].cy);
		vAverageNormal.cz+=(vNormals[i].cz);
	}

	return vAverageNormal;
}

PNDVECTOR PNDVECTOR::add_vector(PNDVECTOR vec1,PNDVECTOR vec2)
{
	return(PNDVECTOR(vec1.cx+vec2.cx,vec1.cy+vec2.cy,vec1.cz+vec2.cz));
};

PNDVECTOR PNDVECTOR::sub_vector(PNDVECTOR vec1,PNDVECTOR vec2)
{
	return(PNDVECTOR(vec1.cx-vec2.cx,vec1.cy-vec2.cy,vec1.cz-vec2.cz));
};

//my first ever overloading attempt, there are may problems here too
PNDVECTOR PNDVECTOR::operator*(double constant)
{
	return(PNDVECTOR(this->cx*constant,this->cy*constant,this->cz*constant));
};

PNDVECTOR PNDVECTOR::operator/(double constant)
{
	return(PNDVECTOR(this->cx/constant,this->cy/constant,this->cz/constant));
};

PNDVECTOR PNDVECTOR::operator+(double constant)
{
	return(PNDVECTOR(this->cx+constant,this->cy+constant,this->cz+constant));
};

PNDVECTOR PNDVECTOR::operator-(double constant)
{
	return(PNDVECTOR(this->cx-constant,this->cy-constant,this->cz-constant));
};

PNDVECTOR PNDVECTOR::operator+(PNDVECTOR &vector)
{
	return(PNDVECTOR(this->cx*vector.cx,this->cy*vector.cy,this->cz*vector.cz));
};

PNDVECTOR PNDVECTOR::operator-(PNDVECTOR &vector)
{
	return(PNDVECTOR(this->cx/vector.cx,this->cy/vector.cy,this->cz/vector.cz));
};


So, the camera movement doesn't affected by the angles, or direction vector it seems. I followed a tutorial for these on opengl-tutorial.org.
I appreciate the help with vector class operators too, but mainly my biggest problem is with the camera movement.
Thanks in advance, and for reading this:D
Last edited on
Topic archived. No new replies allowed.