Added Camera
This commit is contained in:
parent
89e47fa56c
commit
3842a877ac
7 changed files with 233 additions and 67 deletions
169
main.cpp
169
main.cpp
|
|
@ -12,6 +12,12 @@
|
|||
#include <engine/mesh.h>
|
||||
#include <engine/shader.h>
|
||||
#include <engine/texture.h>
|
||||
#include <engine/camera.h>
|
||||
|
||||
void window_resize_callback(GLFWwindow* window, int width, int height);
|
||||
void mouse_callback(GLFWwindow* window, double xpos, double ypos);
|
||||
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
|
||||
void processInput(GLFWwindow *window);
|
||||
|
||||
const int SCREEN_WIDTH = 1920;
|
||||
const int SCREEN_HEIGHT = 1080;
|
||||
|
|
@ -19,34 +25,14 @@ const int SCREEN_HEIGHT = 1080;
|
|||
static bool drawWireframe = false;
|
||||
static bool wireframeHeld = false;
|
||||
|
||||
void window_resize_callback(GLFWwindow* window, int width, int height)
|
||||
{
|
||||
glViewport(0, 0, width, height);
|
||||
}
|
||||
Camera camera(glm::vec3(0.0f, 0.0f, 3.0f));
|
||||
float lastX = SCREEN_WIDTH / 2.0f;
|
||||
float lastY = SCREEN_HEIGHT / 2.0f;
|
||||
bool firstMouse = true;
|
||||
|
||||
void processInput(GLFWwindow* window)
|
||||
{
|
||||
// Close on Escape
|
||||
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
||||
glfwSetWindowShouldClose(window, true);
|
||||
|
||||
// Wireframe toggle
|
||||
if (glfwGetKey(window, GLFW_KEY_F1) == GLFW_PRESS && !wireframeHeld)
|
||||
{
|
||||
wireframeHeld = true;
|
||||
drawWireframe = !drawWireframe;
|
||||
if (drawWireframe)
|
||||
{
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
||||
}
|
||||
else
|
||||
{
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
||||
}
|
||||
}
|
||||
if (glfwGetKey(window, GLFW_KEY_F1) == GLFW_RELEASE)
|
||||
wireframeHeld = false;
|
||||
}
|
||||
float deltaTime = 0.0f; // time between current frame and last frame
|
||||
float lastFrame = 0.0f;
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
|
|
@ -76,6 +62,16 @@ int main(int argc, char* argv[])
|
|||
// Initialize Viewport & setup resize callback
|
||||
glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
|
||||
glfwSetFramebufferSizeCallback(window, window_resize_callback);
|
||||
glfwSetCursorPosCallback(window, mouse_callback);
|
||||
glfwSetScrollCallback(window, scroll_callback);
|
||||
|
||||
// Hide & Lock Cursor
|
||||
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
||||
glfwSetInputMode(window, GLFW_RAW_MOUSE_MOTION, GLFW_TRUE);
|
||||
|
||||
/*
|
||||
Testing
|
||||
*/
|
||||
|
||||
// Load Shader
|
||||
Shader shaderTest("assets/shaders/basicVertex.vert", "assets/shaders/basicFragment.frag");
|
||||
|
|
@ -87,7 +83,7 @@ int main(int argc, char* argv[])
|
|||
{ -0.5f, 0.5f, -0.5f, 1.0f, 0.2f, 0.2f, 0.0f, 1.0f },
|
||||
{ -0.5f, -0.5f, -0.5f, 1.0f, 0.2f, 0.2f, 0.0f, 0.0f },
|
||||
|
||||
// Back face (+z) – green-ish
|
||||
// Back face (+z) – green-ish
|
||||
{ -0.5f, -0.5f, 0.5f, 0.2f, 1.0f, 0.2f, 0.0f, 0.0f },
|
||||
{ 0.5f, -0.5f, 0.5f, 0.2f, 1.0f, 0.2f, 1.0f, 0.0f },
|
||||
{ 0.5f, 0.5f, 0.5f, 0.2f, 1.0f, 0.2f, 1.0f, 1.0f },
|
||||
|
|
@ -95,7 +91,7 @@ int main(int argc, char* argv[])
|
|||
{ -0.5f, 0.5f, 0.5f, 0.2f, 1.0f, 0.2f, 0.0f, 1.0f },
|
||||
{ -0.5f, -0.5f, 0.5f, 0.2f, 1.0f, 0.2f, 0.0f, 0.0f },
|
||||
|
||||
// Left face (-x) – blue-ish
|
||||
// Left face (-x) – blue-ish
|
||||
{ -0.5f, 0.5f, 0.5f, 0.2f, 0.2f, 1.0f, 1.0f, 0.0f },
|
||||
{ -0.5f, 0.5f, -0.5f, 0.2f, 0.2f, 1.0f, 1.0f, 1.0f },
|
||||
{ -0.5f, -0.5f, -0.5f, 0.2f, 0.2f, 1.0f, 0.0f, 1.0f },
|
||||
|
|
@ -103,7 +99,7 @@ int main(int argc, char* argv[])
|
|||
{ -0.5f, -0.5f, 0.5f, 0.2f, 0.2f, 1.0f, 0.0f, 0.0f },
|
||||
{ -0.5f, 0.5f, 0.5f, 0.2f, 0.2f, 1.0f, 1.0f, 0.0f },
|
||||
|
||||
// Right face (+x) – yellow-ish
|
||||
// Right face (+x) – yellow-ish
|
||||
{ 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 0.2f, 1.0f, 0.0f },
|
||||
{ 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.2f, 1.0f, 1.0f },
|
||||
{ 0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 0.2f, 0.0f, 1.0f },
|
||||
|
|
@ -111,7 +107,7 @@ int main(int argc, char* argv[])
|
|||
{ 0.5f, -0.5f, 0.5f, 1.0f, 1.0f, 0.2f, 0.0f, 0.0f },
|
||||
{ 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 0.2f, 1.0f, 0.0f },
|
||||
|
||||
// Bottom face (-y) – magenta-ish
|
||||
// Bottom face (-y) – magenta-ish
|
||||
{ -0.5f, -0.5f, -0.5f, 1.0f, 0.2f, 1.0f, 0.0f, 1.0f },
|
||||
{ 0.5f, -0.5f, -0.5f, 1.0f, 0.2f, 1.0f, 1.0f, 1.0f },
|
||||
{ 0.5f, -0.5f, 0.5f, 1.0f, 0.2f, 1.0f, 1.0f, 0.0f },
|
||||
|
|
@ -119,7 +115,7 @@ int main(int argc, char* argv[])
|
|||
{ -0.5f, -0.5f, 0.5f, 1.0f, 0.2f, 1.0f, 0.0f, 0.0f },
|
||||
{ -0.5f, -0.5f, -0.5f, 1.0f, 0.2f, 1.0f, 0.0f, 1.0f },
|
||||
|
||||
// Top face (+y) – cyan-ish
|
||||
// Top face (+y) – cyan-ish
|
||||
{ -0.5f, 0.5f, -0.5f, 0.2f, 1.0f, 1.0f, 0.0f, 1.0f },
|
||||
{ 0.5f, 0.5f, -0.5f, 0.2f, 1.0f, 1.0f, 1.0f, 1.0f },
|
||||
{ 0.5f, 0.5f, 0.5f, 0.2f, 1.0f, 1.0f, 1.0f, 0.0f },
|
||||
|
|
@ -154,20 +150,21 @@ int main(int argc, char* argv[])
|
|||
glm::vec3(-1.3f, 1.0f, -1.5f)
|
||||
};
|
||||
|
||||
|
||||
// Render Loop
|
||||
double lastTime = 0.0;
|
||||
int nbFrames = 0;
|
||||
float lastUpdate = 0;
|
||||
while (!glfwWindowShouldClose(window))
|
||||
{
|
||||
// Calculate FPS
|
||||
double currentTime = glfwGetTime();
|
||||
nbFrames++;
|
||||
if (currentTime - lastTime >= 1.0) { // Update every second
|
||||
double fps = nbFrames / (currentTime - lastTime);
|
||||
std::cout << fps << std::endl;
|
||||
// per-frame time logic
|
||||
// --------------------
|
||||
float currentFrame = static_cast<float>(glfwGetTime());
|
||||
deltaTime = currentFrame - lastFrame;
|
||||
lastFrame = currentFrame;
|
||||
|
||||
nbFrames = 0;
|
||||
lastTime += 1.0;
|
||||
// crappy fps printout
|
||||
if (currentFrame - lastUpdate >= 1.0) {
|
||||
std::cout << 1.0f / deltaTime << std::endl;
|
||||
lastUpdate += 1.0f;
|
||||
}
|
||||
|
||||
// Input processing
|
||||
|
|
@ -185,21 +182,14 @@ int main(int argc, char* argv[])
|
|||
|
||||
shaderTest.Use();
|
||||
|
||||
// Test
|
||||
glm::mat4 model = glm::mat4(1.0f);
|
||||
model = glm::rotate(model, (float)glfwGetTime() * glm::radians(50.0f), glm::vec3(0.5f, 1.0f, 0.0f));
|
||||
|
||||
glm::mat4 view = glm::mat4(1.0f);
|
||||
// note that we're translating the scene in the reverse direction of where we want to move
|
||||
view = glm::translate(view, glm::vec3(0.0f, 0.0f, -3.0f));
|
||||
|
||||
glm::mat4 projection;
|
||||
projection = glm::perspective(glm::radians(45.0f), 800.0f / 600.0f, 0.1f, 100.0f);
|
||||
|
||||
shaderTest.setMat4("model", model);
|
||||
shaderTest.setMat4("view", view);
|
||||
// pass projection matrix to shader (note that in this case it could change every frame)
|
||||
glm::mat4 projection = glm::perspective(glm::radians(camera.zoom), (float)SCREEN_WIDTH / (float)SCREEN_HEIGHT, 0.1f, 100.0f);
|
||||
shaderTest.setMat4("projection", projection);
|
||||
|
||||
// camera/view transformation
|
||||
glm::mat4 view = camera.GetViewMatrix();
|
||||
shaderTest.setMat4("view", view);
|
||||
|
||||
|
||||
//testMesh.Draw();
|
||||
|
||||
|
|
@ -224,3 +214,74 @@ int main(int argc, char* argv[])
|
|||
glfwTerminate();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void window_resize_callback(GLFWwindow* window, int width, int height)
|
||||
{
|
||||
glViewport(0, 0, width, height);
|
||||
}
|
||||
|
||||
void processInput(GLFWwindow* window)
|
||||
{
|
||||
// Close on Escape
|
||||
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
||||
glfwSetWindowShouldClose(window, true);
|
||||
|
||||
// Wireframe toggle
|
||||
if (glfwGetKey(window, GLFW_KEY_F1) == GLFW_PRESS && !wireframeHeld)
|
||||
{
|
||||
wireframeHeld = true;
|
||||
drawWireframe = !drawWireframe;
|
||||
if (drawWireframe)
|
||||
{
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
||||
}
|
||||
else
|
||||
{
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
||||
}
|
||||
}
|
||||
if (glfwGetKey(window, GLFW_KEY_F1) == GLFW_RELEASE)
|
||||
wireframeHeld = false;
|
||||
|
||||
// Camera
|
||||
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
|
||||
camera.processKeyboard(FORWARD, deltaTime);
|
||||
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
|
||||
camera.processKeyboard(BACKWARD, deltaTime);
|
||||
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
|
||||
camera.processKeyboard(LEFT, deltaTime);
|
||||
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
|
||||
camera.processKeyboard(RIGHT, deltaTime);
|
||||
}
|
||||
|
||||
|
||||
// glfw: whenever the mouse moves, this callback is called
|
||||
// -------------------------------------------------------
|
||||
void mouse_callback(GLFWwindow* window, double xposIn, double yposIn)
|
||||
{
|
||||
float xpos = static_cast<float>(xposIn);
|
||||
float ypos = static_cast<float>(yposIn);
|
||||
|
||||
if (firstMouse)
|
||||
{
|
||||
lastX = xpos;
|
||||
lastY = ypos;
|
||||
firstMouse = false;
|
||||
}
|
||||
|
||||
float xoffset = xpos - lastX;
|
||||
float yoffset = lastY - ypos; // reversed since y-coordinates go from bottom to top
|
||||
|
||||
lastX = xpos;
|
||||
lastY = ypos;
|
||||
|
||||
camera.processMouseMovement(xoffset, yoffset);
|
||||
}
|
||||
|
||||
// glfw: whenever the mouse scroll wheel scrolls, this callback is called
|
||||
// ----------------------------------------------------------------------
|
||||
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
|
||||
{
|
||||
camera.processMouseScroll(static_cast<float>(yoffset));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue