It would be so awesome... it would be so cool!

This commit is contained in:
maelstrom 2024-09-27 23:01:23 +02:00
parent aaaa195167
commit 6b9bea7964
3 changed files with 57 additions and 10 deletions

View file

@ -1,16 +1,20 @@
#include <GL/glew.h>
#include <GL/gl.h>
#include <GLFW/glfw3.h>
#include <glm/ext/vector_float3.hpp>
#include <stdio.h>
#include <vector>
#include "rendering/part.h"
#include "rendering/renderer.h"
#include "camera.h"
void errorCatcher(int id, const char* str);
Camera camera(glm::vec3(0.0, 0.0, 3.0));
std::vector<Part> parts;
// void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods);
void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods);
void processInput(GLFWwindow* window);
void mouseCallback(GLFWwindow* window, double xpos, double ypos);
// void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
@ -21,12 +25,24 @@ int main() {
glfwInit();
GLFWwindow *window = glfwCreateWindow(1200, 900, "GLTest", NULL, NULL);
glfwSetKeyCallback(window, keyCallback);
glfwSetMouseButtonCallback(window, mouseButtonCallback);
glfwSetCursorPosCallback(window, mouseCallback);
glfwMakeContextCurrent(window);
glewInit();
parts.push_back(Part {
.position = glm::vec3(0),
.rotation = glm::vec3(0),
.scale = glm::vec3(1, 1, 1),
.material = Material {
.diffuse = glm::vec3(1.0f, 0.5f, 0.31f),
.specular = glm::vec3(0.5f, 0.5f, 0.5f),
.shininess = 32.0f,
}
});
renderInit(window);
do {
@ -87,3 +103,18 @@ void mouseButtonCallback(GLFWwindow* window, int button, int action, int mods) {
mouseFirst = true;
}
}
void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) {
if (key == GLFW_KEY_F && action == GLFW_PRESS) {
parts.push_back(Part {
.position = camera.cameraPos + camera.cameraFront * glm::vec3(3),
.rotation = glm::vec3(0),
.scale = glm::vec3(1, 1, 1),
.material = Material {
.diffuse = glm::vec3(1.0f, 0.5f, 0.31f),
.specular = glm::vec3(0.5f, 0.5f, 0.5f),
.shininess = 32.0f,
}
});
}
}

10
src/rendering/part.h Normal file
View file

@ -0,0 +1,10 @@
#pragma once
#include <glm/glm.hpp>
#include "material.h"
struct Part {
glm::vec3 position;
glm::vec3 rotation;
glm::vec3 scale;
Material material;
};

View file

@ -2,17 +2,21 @@
#include <GLFW/glfw3.h>
#include <GL/gl.h>
#include <glm/ext.hpp>
#include <glm/ext/matrix_transform.hpp>
#include <glm/glm.hpp>
#include <vector>
#include "shader.h"
#include "mesh.h"
#include "defaultmeshes.h"
#include "../camera.h"
#include "part.h"
#include "renderer.h"
Shader *shader = NULL;
extern Camera camera;
extern std::vector<Part> parts;
void renderInit(GLFWwindow* window) {
glViewport(0, 0, 1200, 900);
@ -67,14 +71,16 @@ void render(GLFWwindow* window) {
// Pass in the camera position
shader->set("viewPos", camera.cameraPos);
glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3( 0.0f, 0.0f, 0.0f));
// float angle = 20.0f * i;
// model = glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f));
shader->set("model", model);
glm::mat3 normalMatrix = glm::mat3(glm::transpose(glm::inverse(model)));
shader->set("normalMatrix", normalMatrix);
for (Part part : parts) {
glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, part.position);
// model = glm::rotate(model, part.position);
model = glm::scale(model, part.scale);
shader->set("model", model);
glm::mat3 normalMatrix = glm::mat3(glm::transpose(glm::inverse(model)));
shader->set("normalMatrix", normalMatrix);
CUBE_MESH->bind();
glDrawArrays(GL_TRIANGLES, 0, 36);
CUBE_MESH->bind();
glDrawArrays(GL_TRIANGLES, 0, 36);
}
}