finished chapter "Move around"

This commit is contained in:
Daniel 2023-08-02 18:27:40 +02:00
parent 6a51a7a0ae
commit 0ea858953b

View File

@ -12,7 +12,7 @@
#include "util/stb_image.h"
// Continue: https://learnopengl.com/Getting-started/Camera
// Chapter: Walk around
// Chapter: Look around
//
// TODO: look at project->properties->VC++ Directories-> change everything to the Environment var
// For the "real" Nebulix Engine setup everything so it can be compiled/used with VSCode/CLion and not just VS
@ -32,6 +32,12 @@
typedef glm::mat4 mat4;
typedef glm::vec3 vec3;
vec3 cameraPosition = vec3(0.0f, 0.0f, 3.0f);
vec3 cameraFront = vec3(0.0f, 0.0f, -1.0f);
vec3 cameraUp = vec3(0.0f, 1.0f, 0.0f);
float deltaTime = 0.0f;
float lastFrameTime = 0.0f;
void OnWindowResize(GLFWwindow* window, int width, int height)
{
@ -40,8 +46,24 @@ void OnWindowResize(GLFWwindow* window, int width, int height)
void ProcessInput(GLFWwindow* window)
{
const float cameraSpeed = 3.0f * deltaTime;
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
cameraPosition += cameraSpeed * cameraFront;
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
cameraPosition -= cameraSpeed * cameraFront;
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
cameraPosition -= glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
cameraPosition += glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS)
cameraFront -= glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed * 0.25f;
if (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS)
cameraFront += glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed * 0.25f;
}
void Configure()
@ -141,7 +163,7 @@ int main()
vec3(-1.3f, 1.0f, -1.5f)
};
// VBO == all verticies; EBO == connection of vertices (faces) and is optional although it is a bit more efficient than using VBOs;
// VBO == all verticies; EBO == connection of vertices (faces) and is optional although it is a bit more efficient than using VBOs, since it reuses vertices;
// VAO == collection of VBOs/EBOs used for rendering
GLuint vertexBufferObject, elementBufferObject, vertexArrayObject;
glGenVertexArrays(1, &vertexArrayObject);
@ -183,33 +205,21 @@ int main()
Texture2D containerImage("images/container.jpg");
Texture2D faceImage("images/awesomeface.png", 0, GL_RGBA);
vec3 cameraPosition = vec3(0.0f, 0, 3.0f);
vec3 cameraTarget = vec3(0.0f, 0.0f, 0.0f);
vec3 cameraDirection = glm::normalize(cameraPosition - cameraTarget);
vec3 worldUp = vec3(0.0f, 1.0f, 0.0f);
vec3 cameraRight = glm::normalize(glm::cross(worldUp, cameraDirection)); // vector pointing to the right of the camera
vec3 cameraUp = glm::cross(cameraDirection, cameraRight);
//mat4 viewMatrix = glm::lookAt(cameraPosition, cameraTarget, worldUp);
mat4 projectionMatrix = glm::perspective(glm::radians(45.0f), 800.0f / 600.0f, 0.1f, 100.0f);
float timeLastFrame = glfwGetTime();
// main loop
while(!glfwWindowShouldClose(window))
{
float currentFrameTime = glfwGetTime();
deltaTime = currentFrameTime - lastFrameTime;
lastFrameTime = currentFrameTime;
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
ProcessInput(window);
const float radius = 10.0f;
float camX = sin(glfwGetTime()) * radius;
float camZ = cos(glfwGetTime()) * radius;
mat4 viewMatrix = glm::lookAt(vec3(camX, 0, camZ), cameraTarget, worldUp);
mat4 viewMatrix = glm::lookAt(cameraPosition, cameraPosition + cameraFront, cameraUp);
shader->Use();
shader->SetInt("ourTexture1", 0);
@ -222,14 +232,13 @@ int main()
glBindVertexArray(vertexArrayObject);
float time = glfwGetTime();
for (int i = 0; i < 10; i++) // somehow cubePositions->length() does not return the correct number?
{
mat4 model = mat4(1.0f);
model = glm::translate(model, cubePositions[i]);
float angle = (20.0f * i);
if (i % 3 == 0)
angle = time * 25;
angle = currentFrameTime * 25;
model = glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f));
shader->SetMatrix("modelMatrix", model);
@ -239,7 +248,6 @@ int main()
glfwSwapBuffers(window);
glfwPollEvents();
timeLastFrame = glfwGetTime();
}
// optional, as we are at the end of the program.