Position fix

pull/3/head
E. Almqvist 2 years ago
parent c5bde46c08
commit 2a874825a9
  1. 3
      headers/renderer.hpp
  2. 3
      shaders/vertex.glsl
  3. 20
      src/main.cpp
  4. 8
      src/renderer.cpp

@ -21,8 +21,11 @@
namespace Renderer {
class RenderObject {
public:
glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f);
RenderObject(std::vector<float> verts, std::vector<unsigned int> indices);
void render(GLFWwindow* win, glm::mat4 cameraTransform, glm::mat4 projectionTransform);
void setRotation(float angle, glm::vec3 axis);
void transform(glm::mat4 T);
void preRenderHook();
private:

@ -6,12 +6,13 @@ layout (location = 2) in vec2 aTexCoord;
out vec4 VertexColor;
out vec2 TexCoord;
uniform mat4 baseModel = mat4(1.0);
uniform mat4 model = mat4(1.0);
uniform mat4 view = mat4(1.0);
uniform mat4 projection = mat4(1.0);
void main() {
gl_Position = projection * view * model * vec4(aPos, 1.0);
gl_Position = projection * view * model * baseModel * vec4(aPos, 1.0);
VertexColor = vec4(aColor, 1.0);
TexCoord = aTexCoord;
}

@ -34,7 +34,7 @@ void processInput(GLFWwindow *win) {
void renderCallback() {
// Make background
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
int main() {
@ -69,41 +69,52 @@ int main() {
-0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 1.0f, 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.0f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f
@ -112,7 +123,7 @@ int main() {
std::vector<unsigned int> indices({
0, 1, 3,
3, 4, 5,
1, 2, 3,
5, 6, 7,
7, 8, 9,
9, 10, 11,
@ -145,18 +156,20 @@ int main() {
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
glfwSetFramebufferSizeCallback(win, framebuffer_size_callback);
glEnable(GL_DEPTH_TEST);
float borderColor[] = {1.0f, 1.0f, 1.0f, 1.0f};
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor);
Renderer::Obj2D ro(verts, indices);
Renderer::Obj2D ro2(verts, indices);
ro.position = glm::vec3(-1.0f, -1.0f, -4.0f);
ro2.setTexture("assets/textures/meep.jpg");
ro.setTexture(RUSTY_METAL_TEXTURE);
renderer.spawnObject(ro);
renderer.spawnObject(ro2);
// renderer.spawnObject(ro2);
renderer.setCamera(glm::vec3(0.0f, 0.0f, -4.0f));
// Window width & height
@ -179,6 +192,7 @@ int main() {
T = glm::rotate(T, rotang, glm::vec3(1.0, 0.0, 1.0));
T = glm::scale(T, glm::vec3(0.5, 0.5, 0.5));
ro.transform(T);
// ro2.transform(T);
renderer.render();

@ -3,6 +3,8 @@
#include <glm/gtc/matrix_transform.hpp>
#include "GLFW/glfw3.h"
#include "glm/ext/matrix_transform.hpp"
#include "glm/fwd.hpp"
#include "renderer.hpp"
#include "shaders.hpp"
@ -58,13 +60,11 @@ namespace Renderer {
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, vertsVec.size() * sizeof(vertsVec[0]), vertsArray, GL_DYNAMIC_DRAW); // for moving stuff
// printf("%u\n", sizeof(vertsVec[0]) * vertsVec.size());
// Copy the indices for the verts into another buffer
glGenBuffers(1, &EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesVec.size() * sizeof(indicesVec[0]), indicesArray, GL_STATIC_DRAW);
// printf("%u\n",indicesVec.size() * sizeof(indicesVec[0]));
// Shader stuff
// Pos
@ -83,6 +83,10 @@ namespace Renderer {
void RenderObject::preRenderHook() {}
void RenderObject::render(GLFWwindow* win, glm::mat4 cameraTransform, glm::mat4 projectionTransform) {
glm::mat4 posT = glm::mat4(1.0f);
posT = glm::translate(posT, position);
shader.setMat4("baseModel", posT);
shader.setMat4("view", cameraTransform);
shader.setMat4("projection", projectionTransform);

Loading…
Cancel
Save