Skip to content

Commit

Permalink
First attempt at free camera movement.
Browse files Browse the repository at this point in the history
  • Loading branch information
Angelo1211 committed Aug 12, 2018
1 parent f7baeb9 commit bd3667b
Show file tree
Hide file tree
Showing 19 changed files with 74 additions and 35 deletions.
2 changes: 1 addition & 1 deletion include/camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ struct Camera{

//Values related to orbiting mode
float radius = 2;
bool orbiting = true;
bool orbiting = false;

//Momentary fixed camera speed (FPS dependent)
float camSpeed = 0.1f;
Expand Down
2 changes: 1 addition & 1 deletion include/sceneManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class SceneManager{
bool closeScene();

std::string currentSceneID;
Scene *currentScene;
Scene* currentScene;
};


Expand Down
Binary file added scenes/teapotMetal/teapotMetal_albedo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
s teapot
s teapotMetal
//First line just checks that you are indeed reading the right config file
/
//SECTION: models
Expand All @@ -14,22 +14,22 @@ rot 90.0 0 0.0
sca 0.4 0.4 0.4
/
//SECTION: lights
l 2
l 4
l01 sun
pos 1.0 0.0 0.0
col 1.0 1.0 1.0
/
l02 sun
pos 1.0 0.0 1.0
col 1.0 1.0 1.0
pos 1.0 0.0 2.0
col 0.0 1.0 0.0
/
l03 sun
pos 0.0 0.0 1.0
col 0.0 0.0 1.0
pos 0.0 0.0 -1.0
col 0.5 0.0 0.0
/
l04 sun
pos 1.0 1.0 1.0
col 1.0 0.0 1.0
col 0.0 0.0 1.0
/
//SECTION: cameras
c
Expand Down
File renamed without changes.
Binary file added scenes/teapotMetal/teapotMetal_metal.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added scenes/teapotMetal/teapotMetal_normal.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added scenes/teapotMetal/teapotMetal_rough.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed scenes/teapotMetal/teapot_albedo.png
Binary file not shown.
12 changes: 0 additions & 12 deletions scenes/teapotMetal/teapot_mesh.mtl

This file was deleted.

Binary file removed scenes/teapotMetal/teapot_metal.png
Binary file not shown.
Binary file removed scenes/teapotMetal/teapot_normal.png
Binary file not shown.
Binary file removed scenes/teapotMetal/teapot_rough.png
Binary file not shown.
8 changes: 5 additions & 3 deletions src/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ Camera::Camera(){

void Camera::update(unsigned int deltaT){
if(orbiting){
float t = static_cast<float>(SDL_GetTicks());
float camX = std::sin(t/6000) * radius;
float camZ = std::cos(t/6000) * radius;
float ang = 2*M_PI*static_cast<float>(SDL_GetTicks())/3e4;
float camX = std::sin(ang) * radius;
float camZ = std::cos(ang) * radius;
position.x = camX;
position.y = camX;
position.z = camZ;
}
else{
// target = front;
target = position + front;

}
viewMatrix = Matrix4::lookAt(position,target,up);
cameraFrustrum.updatePlanes(viewMatrix, position);
Expand Down
5 changes: 2 additions & 3 deletions src/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ void Engine::run(){

//Main flags
bool done = false;
bool switchScene = false;

//Iteration and time keeping counters
int count = 0;
Expand All @@ -84,10 +83,10 @@ void Engine::run(){
deltaT = SDL_GetTicks() - start;
printf("%2.1d: Loop elapsed time (ms):%d\n",count, deltaT);
total += deltaT;
if(count == 500) break;
//if(count == 500) break;
}

printf("Closing down engine.\n");
printf("Average frame time over %2.1d frames:%2.fms.\n", count,total/(float)count);
printf("Average frame time over %2.1d frames:%2.fms.\n", count, total/(float)count);

}
64 changes: 58 additions & 6 deletions src/inputManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ InputManager::~InputManager(){}
bool InputManager::startUp(SceneManager &sceneManager){
sceneController = &sceneManager;
sceneCamera = (sceneController->getCurrentScene()->getCurrentCamera());

return true;

//Only really care about relative mouse motion because we're building a space camera
bool success = !SDL_SetRelativeMouseMode(SDL_TRUE);
return success;
}

void InputManager::shutDown(){
Expand Down Expand Up @@ -48,7 +50,7 @@ void InputManager::handleEvent(SDL_Event * event, bool &done, unsigned int delta
break;

case SDLK_2:
sceneID = "firehydrant";
sceneID = "teapotMetal";
break;

case SDLK_3:
Expand Down Expand Up @@ -99,14 +101,18 @@ void InputManager::handleEvent(SDL_Event * event, bool &done, unsigned int delta
case SDLK_r:
sceneCamera->position = Vector3f(0, 0, 8.0);
sceneCamera->target.zero();
sceneCamera->radius = 3;
sceneCamera->side = sceneCamera->front.crossProduct(sceneCamera->up);
sceneCamera->front = Vector3f(0, 0, -1);
sceneCamera->radius = 2;
break;

case SDLK_TAB:
sceneCamera->orbiting = !sceneCamera->orbiting;
sceneCamera->position = Vector3f(0, 0, 8.0);
sceneCamera->target.zero();
sceneCamera->radius = 3;
sceneCamera->side = sceneCamera->front.crossProduct(sceneCamera->up);
sceneCamera->front = Vector3f(0, 0, -1);
sceneCamera->radius = 2;
break;

default:
Expand All @@ -127,8 +133,54 @@ void InputManager::handleEvent(SDL_Event * event, bool &done, unsigned int delta
sceneCamera = (sceneController->getCurrentScene()->getCurrentCamera());
sceneCamera->position = Vector3f(0, 0, 8.0);
sceneCamera->target.zero();
sceneCamera->side = sceneCamera->front.crossProduct(sceneCamera->up);
sceneCamera->front = Vector3f(0, 0, -1);

}

}
}
}
//Handling Mouse Motion
else if( event->type == SDL_MOUSEMOTION){
float sens = 0.001f;
float xRot = -(float)event->motion.yrel * sens;
float yRot = -(float)event->motion.xrel * sens;

Matrix4 cameraRot = Matrix4::fullRotMat(xRot, yRot, 0.0f);

sceneCamera->front = cameraRot.matMultVec(sceneCamera->front).normalized();
sceneCamera->side = cameraRot.matMultVec(sceneCamera->side).normalized();

// Matrix4 camTransform = (sceneCamera->viewMatrix);
// Vector3f newDir = Vector3f((float)event->motion.xrel*0.01f, -(float)event->motion.yrel*0.01f, 0.0f);
// Vector3f relMov = camTransform.matMultDir(newDir);

// sceneCamera->front = (sceneCamera->front + relMov).normalized();

// if(event->motion.xrel > 0){
// sceneCamera->front = (sceneCamera->front + Vector3f((float)event->motion.xrel*0.001f, 0.0f, 0.0f)).normalized();
// printf("Moved right!\n");

// }

// if(event->motion.xrel < 0){

// printf("Moved left!\n");
// }

// if(event->motion.yrel > 0){
// printf("Moved down!\n");
// }

// if(event->motion.yrel < 0){
// printf("Moved up!\n");
// }
}
}


//Handling Mouse Input
// else if((event->type == SDL_MOUSEBUTTONDOWN) && (event->button.button == SDL_BUTTON_RIGHT)){
// printf("Right mouse Click detected\n");
// SDL_Delay(1000);
// }
1 change: 0 additions & 1 deletion src/rasterizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ void Rasterizer::drawTriangles(Vector3f *vertices, IShader &shader, Buffer<Uint3
Vector3f e, e_row, f;
Vector3f rgbVals{255,255,255};


//Transform into viewport coordinates
Rasterizer::viewportTransform(pixelBuffer, vertices);

Expand Down
1 change: 0 additions & 1 deletion src/softwareRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ void SoftwareRenderer::drawTriangularMesh(Model * currentModel){

shader.lightPos = lightPositions;


//Building worldToObject matrix
Matrix4 worldToObject = (*(currentModel->getModelMatrix())).inverse();

Expand Down

0 comments on commit bd3667b

Please sign in to comment.