Fixed stuff

pull/3/head
E. Almqvist 2 years ago
parent f6803ced78
commit 6df9a10c49
  1. 8
      headers/renderer.hpp
  2. 2
      shaders/vertex.glsl
  3. 19
      src/main.cpp
  4. 11
      src/renderer.cpp

@ -14,13 +14,14 @@
#define VERT_SHADER_SRC_FILE "shaders/vertex.glsl"
#define FRAG_SHADER_SRC_FILE "shaders/fragment.glsl"
#define DEFAULT_FOV 45.0f
#define DEFAULT_FOV 60.0f
#define NEAR_PLANE 0.1f
#define FAR_PLANE 100.0f
namespace Renderer {
class Object {
public:
const char* name = "Object";
Object();
Object(glm::vec3 pos);
Object(glm::vec3 pos, glm::vec3 angle);
@ -34,10 +35,10 @@ namespace Renderer {
void rotate(glm::vec3 dangle);
void transform(glm::mat4 T);
private:
void updatePositionTransform();
void updateRotationTransform();
private:
glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f);
glm::vec3 angle = glm::vec3(0.0f, 0.0f, 0.0f);
glm::mat4 positionTransform = glm::mat4(1.0f);
@ -47,6 +48,7 @@ namespace Renderer {
class Camera : public Object {
public:
const char* name = "Camera";
Camera(GLFWwindow* win);
Camera(GLFWwindow* win, glm::vec3 pos);
Camera(GLFWwindow* win, glm::vec3 pos, glm::vec3 angle);
@ -60,6 +62,7 @@ namespace Renderer {
class RenderObject : public Object {
public:
const char* name = "RenderObject";
RenderObject(std::vector<float> verts, std::vector<unsigned int> indices);
void render(GLFWwindow* win, Camera cam);
void preRenderHook();
@ -93,6 +96,7 @@ namespace Renderer {
class TexturedObject : public RenderObject {
public:
const char* name = "TexturedObject";
using RenderObject::RenderObject;
void setTexture(const char* t_src);
void preRenderHook();

@ -16,7 +16,7 @@ uniform mat4 camProjection = mat4(1.0);
void main() {
// projection { view } { model }
gl_Position = camProjection * (camPos * camRot) * modelPosition * modelRotation * model * vec4(aPos, 1.0);
gl_Position = camProjection * (camRot * camPos) * (modelRotation * modelPosition * model) * vec4(aPos, 1.0);
VertexColor = vec4(aColor, 1.0);
TexCoord = aTexCoord;
}

@ -150,18 +150,17 @@ int main() {
Renderer::Scene scene(win);
Renderer::TexturedObject ro(verts, indices);
ro.setPosition(glm::vec3(0.2f, -1.0f, -4.0f));
ro.setPosition(glm::vec3(0.2f, -1.0f, -8.0f));
ro.name = "Hej";
Renderer::TexturedObject ro2(verts, indices);
ro2.setPosition(glm::vec3(0.5f, 0.0, -2.0f));
ro2.setPosition(glm::vec3(0.5f, 0.0, -8.0f));
ro.setTexture(RUSTY_METAL_TEXTURE);
ro2.setTexture("assets/textures/meep.jpg");
scene.spawnObject(ro);
scene.spawnObject(ro2);
scene.camera.setPosition(glm::vec3(0.0f, 0.0f, -8.0f));
scene.camera.setFOV(60.0f);
// scene.spawnObject(ro2);
// Window width & height
while (!glfwWindowShouldClose(win)) {
@ -175,12 +174,16 @@ int main() {
float gVal = sin(time);
// Move the camera left and right
scene.camera.setPosition(glm::vec3(gVal, 0.0f, 0.0f));
scene.camera.rotate(glm::vec3(0.0f, 0.0f, 1.0f));
// scene.camera.setPosition(glm::vec3(0.0f, 0.0f, 8*gVal-4.0f));
scene.camera.translate(glm::vec3(0.0f, 0.0f, 0.01f));
scene.camera.setRotation(glm::vec3(0.0f, 5.0f, 0.0f));
//scene.camera.rotate(glm::vec3(0.0f, -0.4f, 0.0f));
// Move the objects & stuff
float rotang = time;
ro.rotate(glm::vec3(time, 0.0f, time));
// ro.setRotation(glm::vec3(time*10, 0.0f, time));
ro.translate(glm::vec3(0.0f, 0.0f, -0.015f));
ro.rotate(glm::vec3(20.0f, 0.0f, 0.0f));
// Render new frame
scene.render();

@ -1,9 +1,11 @@
#include <glad/glad.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <iostream>
#include "GLFW/glfw3.h"
#include "glm/ext/matrix_transform.hpp"
#include "glm/gtx/string_cast.hpp"
#include "glm/fwd.hpp"
#include "renderer.hpp"
#include "shaders.hpp"
@ -55,6 +57,8 @@ namespace Renderer {
T = glm::rotate(T, glm::radians(z_Ang), glm::vec3(0.0f, 0.0f, 1.0f));
rotationTransform = T;
// std::cout << "Name: " << name << " | ";
// std::cout << glm::to_string(T) << std::endl;
}
void Object::setRotation(glm::vec3 ang) {
@ -161,6 +165,9 @@ namespace Renderer {
shader.setMat4("camRot", cam.getRotationTransform());
shader.setMat4("camProjection", cam.projection);
std::cout << "Name: " << name << " | ";
std::cout << glm::to_string(getPositionTransform()) << std::endl;
shader.use();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
@ -169,12 +176,12 @@ namespace Renderer {
void RenderObject::setPosition(glm::vec3 pos) {
Object::setPosition(pos);
shader.setMat4("position", getPositionTransform());
shader.setMat4("modelPosition", getPositionTransform());
}
void RenderObject::setRotation(glm::vec3 angle) {
Object::setRotation(angle);
shader.setMat4("rotation", getRotationTransform());
shader.setMat4("modelRotation", getRotationTransform());
}
void RenderObject::transform(glm::mat4 T) {

Loading…
Cancel
Save