Skip to content

Commit

Permalink
camera class
Browse files Browse the repository at this point in the history
  • Loading branch information
nietup committed Jun 13, 2015
1 parent 8762c12 commit a531018
Show file tree
Hide file tree
Showing 13 changed files with 163 additions and 102 deletions.
Binary file modified Bamboo.sdf
Binary file not shown.
108 changes: 108 additions & 0 deletions Camera.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#include "Camera.h"


Camera::Camera() {
position.z = 10.f;
position.y = 1.f;
position.x = position.lx = position.angleVertical = position.ly = position.angle = position.deltaAngle = position.deltaAngleVertical = position.deltaMove = 0;
position.lz = -10.f;
}


Camera::~Camera() {
}

void Camera::computePos() {
position.x += position.deltaMove * position.lx * 0.1f;
position.y += position.deltaMove * position.ly * 0.1f;
position.z += position.deltaMove * position.lz * 0.1f;
}

void Camera::computeDir() {
position.angle += position.deltaAngle;
position.lx = sin(position.angle);
position.lz = -cos(position.angle);
}

void Camera::computeDirVertical() {
if (position.deltaAngleVertical >= 0) {
if (position.angleVertical < 3.14f / 2.f) {
position.angleVertical += position.deltaAngleVertical;
position.ly = 2 * sin(position.angleVertical);
}
else {
position.ly = 5.f;
}
}
else {
if (position.angleVertical > -3.14f / 2.f) {
position.angleVertical += position.deltaAngleVertical;
position.ly = 2 * sin(position.angleVertical);
}
else {
position.ly = -5.f;
}
}
}

void Camera::refresh() {
//if (position.deltaMove != 0)
computePos();
//if (position.deltaAngle != 0)
computeDir();
//if (position.deltaAngleVertical != 0)
computeDirVertical();

gluLookAt(position.x, position.y, position.z,
position.x + position.lx, position.y + position.ly, position.z + position.lz,
0.0f, 1.0f, 0.0f);
}

void Camera::update(Action a, int key) {
if (a == NORMAL_DOWN) {
switch (key) {
case 'w':
position.deltaMove = 1.8f;
break;
case 's':
position.deltaMove = -0.5f;
break;
}
}
if (a == NORMAL_UP) {
switch (key) {
case 'w':
case 's':
position.deltaMove = 0;
break;
}
}
if (a == ARROW_DOWN) {
switch (key) {
case GLUT_KEY_LEFT:
position.deltaAngle = -0.01f;
break;
case GLUT_KEY_RIGHT:
position.deltaAngle = 0.01f;
break;
case GLUT_KEY_UP:
position.deltaAngleVertical = 0.01f;
break;
case GLUT_KEY_DOWN:
position.deltaAngleVertical = -0.01f;
break;
}
}
if (a == ARROW_UP) {
switch (key) {
case GLUT_KEY_LEFT:
case GLUT_KEY_RIGHT:
position.deltaAngle = 0.0f;
break;
case GLUT_KEY_UP:
case GLUT_KEY_DOWN:
position.deltaAngleVertical = 0.0f;
break;
}
}
}
39 changes: 39 additions & 0 deletions Camera.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once

#include <windows.h>
#include <GL/gl.h>
#include "glut.h"
#include <math.h>

class Camera {
private:
struct Position {
float x;
float y;
float z;
float lx;
float ly;
float lz;
float angle;
float angleVertical;
float deltaAngle;
float deltaAngleVertical;
float deltaMove;
} position;
public:
enum Action {NORMAL_UP, NORMAL_DOWN, ARROW_UP, ARROW_DOWN};

Camera();
~Camera();

void update(Action a, int key);
void refresh();
void computePos();
void computeDir();
void computeDirVertical();

Position * getPosition() {
return &position;
}
};

Binary file modified Debug/Bamboo.tlog/CL.read.1.tlog
Binary file not shown.
Binary file modified Debug/Bamboo.tlog/cl.command.1.tlog
Binary file not shown.
Binary file modified Debug/Bamboo.tlog/cl.write.1.tlog
Binary file not shown.
Binary file modified Debug/Bamboo.tlog/link.command.1.tlog
Binary file not shown.
Binary file modified Debug/Bamboo.tlog/link.read.1.tlog
Binary file not shown.
Binary file modified Debug/Bamboo.tlog/link.write.1.tlog
Binary file not shown.
Binary file modified Debug/vc120.idb
Binary file not shown.
2 changes: 2 additions & 0 deletions Labka03.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,14 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="BambooStick.cpp" />
<ClCompile Include="Camera.cpp" />
<ClCompile Include="Entity.cpp" />
<ClCompile Include="Floor.cpp" />
<ClCompile Include="gkom.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="BambooStick.h" />
<ClInclude Include="Camera.h" />
<ClInclude Include="Entity.h" />
<ClInclude Include="Floor.h" />
</ItemGroup>
Expand Down
6 changes: 6 additions & 0 deletions Labka03.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
<ClCompile Include="Floor.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Camera.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="BambooStick.h">
Expand All @@ -38,5 +41,8 @@
<ClInclude Include="Floor.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Camera.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>
110 changes: 8 additions & 102 deletions gkom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,15 @@
#include "Entity.h"
#include "BambooStick.h"
#include "Floor.h"
#include "Camera.h"

int width;
int height;
double lasttime;
double timediff;
double fps;
std::vector<Entity *> * entities;
struct Position {
float x;
float y;
float z;
float lx;
float ly;
float lz;
float angle;
float angleVertical;
float deltaAngle;
float deltaAngleVertical;
float deltaMove;
} cameraPosition;
Camera * camera;

void init() {
GLfloat mat_ambient[] = {1.0, 1.0, 1.0, 1.0}; //mat - wspolczynniki odbicia
Expand All @@ -52,6 +41,7 @@ void init() {
glEnable(GL_DEPTH_TEST);

entities = new std::vector<Entity *>();
camera = new Camera();
GLUquadric * qobj = gluNewQuadric();
Entity * e;
e = new BambooStick(qobj, 10, 0, -30);
Expand All @@ -60,10 +50,6 @@ void init() {
entities->push_back(e);
e = new Floor(0.f, 0.f, -30.f, 10.f, 10.f);
entities->push_back(e);
cameraPosition.z = 10.f;
cameraPosition.y = 1.f;
cameraPosition.x = cameraPosition.lx = cameraPosition.angleVertical = cameraPosition.ly = cameraPosition.angle = cameraPosition.deltaAngle = cameraPosition.deltaAngleVertical = cameraPosition.deltaMove = 0;
cameraPosition.lz = -10.f;
}

void displayObjects() {
Expand All @@ -76,55 +62,11 @@ void displayObjects() {
glPopMatrix();
}

void computePos(float deltaMove) {
cameraPosition.x += deltaMove * cameraPosition.lx * 0.1f;
cameraPosition.y += deltaMove * cameraPosition.ly * 0.1f;
cameraPosition.z += deltaMove * cameraPosition.lz * 0.1f;
}

void computeDir(float deltaAngle) {
cameraPosition.angle += deltaAngle;
cameraPosition.lx = sin(cameraPosition.angle);
cameraPosition.lz = -cos(cameraPosition.angle);
}

void computeDirVertical(float deltaAngle) {
if (deltaAngle >= 0) {
if (cameraPosition.angleVertical < 3.14f / 2.f) {
cameraPosition.angleVertical += deltaAngle;
cameraPosition.ly = 2 * sin(cameraPosition.angleVertical);
}
else {
cameraPosition.ly = 5.f;
}
}
else {
if (cameraPosition.angleVertical > -3.14f / 2.f) {
cameraPosition.angleVertical += deltaAngle;
cameraPosition.ly = 2 * sin(cameraPosition.angleVertical);
}
else {
cameraPosition.ly = -5.f;
}
}
}

void render() {

if (cameraPosition.deltaMove)
computePos(cameraPosition.deltaMove);
if (cameraPosition.deltaAngle)
computeDir(cameraPosition.deltaAngle);
if (cameraPosition.deltaAngleVertical)
computeDirVertical(cameraPosition.deltaAngleVertical);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //wypelnienie bufora domyslnymi wartosciami
glLoadIdentity();

gluLookAt(cameraPosition.x, cameraPosition.y, cameraPosition.z,
cameraPosition.x + cameraPosition.lx, cameraPosition.y + cameraPosition.ly, cameraPosition.z + cameraPosition.lz,
0.0f, 1.0f, 0.0f);

camera->refresh();
displayObjects();

glFlush(); //wywalenie wszystkich oczekujacych wejsc
Expand Down Expand Up @@ -152,59 +94,23 @@ void reshape(GLsizei w, GLsizei h) //width || heigt
}

void pressNormalKey(unsigned char key, int x, int y) {

if (key == 27) //escape
exit(0);

switch (key) {
case 'w':
cameraPosition.deltaMove = 1.8f;
break;
case 's':
cameraPosition.deltaMove = -0.5f;
break;
}
camera->update(Camera::NORMAL_DOWN, key);
}

void releaseNormalKey(unsigned char key, int x, int y) {
switch (key) {
case 'w':
case 's':
cameraPosition.deltaMove = 0;
break;
}
camera->update(Camera::NORMAL_UP, key);
}

void pressKey(int key, int xx, int yy) {

switch (key) {
case GLUT_KEY_LEFT:
cameraPosition.deltaAngle = -0.01f;
break;
case GLUT_KEY_RIGHT:
cameraPosition.deltaAngle = 0.01f;
break;
case GLUT_KEY_UP:
cameraPosition.deltaAngleVertical = 0.01f;
break;
case GLUT_KEY_DOWN:
cameraPosition.deltaAngleVertical = -0.01f;
break;
}
camera->update(Camera::ARROW_DOWN, key);
}

void releaseKey(int key, int x, int y) {
camera->update(Camera::ARROW_UP, key);

switch (key) {
case GLUT_KEY_LEFT:
case GLUT_KEY_RIGHT:
cameraPosition.deltaAngle = 0.0f;
break;
case GLUT_KEY_UP:
case GLUT_KEY_DOWN:
cameraPosition.deltaAngleVertical = 0.0f;
break;
}
}

int main(int argc, char** argv) {
Expand Down

0 comments on commit a531018

Please sign in to comment.