Fixed camera bug

pull/3/head
E. Almqvist 2 years ago
parent 25f9beb3d4
commit f6803ced78
  1. 2
      headers/renderer.hpp
  2. 13
      shaders/vertex.glsl
  3. 11
      src/main.cpp
  4. 9
      src/renderer.cpp

@ -52,8 +52,6 @@ namespace Renderer {
Camera(GLFWwindow* win, glm::vec3 pos, glm::vec3 angle);
void setFOV(float deg);
glm::mat4 view = glm::mat4(1.0f);
glm::mat4 projection = glm::mat4(1.0f);
private:
GLFWwindow* window;

@ -6,14 +6,17 @@ layout (location = 2) in vec2 aTexCoord;
out vec4 VertexColor;
out vec2 TexCoord;
uniform mat4 rotation = mat4(1.0);
uniform mat4 position = mat4(1.0);
uniform mat4 modelRotation = mat4(1.0);
uniform mat4 modelPosition = mat4(1.0);
uniform mat4 model = mat4(1.0);
uniform mat4 view = mat4(1.0);
uniform mat4 projection = mat4(1.0);
uniform mat4 camPos = mat4(1.0);
uniform mat4 camRot = mat4(1.0);
uniform mat4 camProjection = mat4(1.0);
void main() {
gl_Position = projection * view * position * rotation * model * vec4(aPos, 1.0);
// projection { view } { model }
gl_Position = camProjection * (camPos * camRot) * modelPosition * modelRotation * model * vec4(aPos, 1.0);
VertexColor = vec4(aColor, 1.0);
TexCoord = aTexCoord;
}

@ -43,8 +43,6 @@ int main() {
return 1;
}
Renderer::Scene scene(win);
glfwMakeContextCurrent(win);
if ( !gladLoadGLLoader((GLADloadproc)glfwGetProcAddress) ) {
@ -148,6 +146,9 @@ int main() {
float borderColor[] = {1.0f, 1.0f, 1.0f, 1.0f};
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor);
// Create scene
Renderer::Scene scene(win);
Renderer::TexturedObject ro(verts, indices);
ro.setPosition(glm::vec3(0.2f, -1.0f, -4.0f));
@ -160,6 +161,7 @@ int main() {
scene.spawnObject(ro);
scene.spawnObject(ro2);
scene.camera.setPosition(glm::vec3(0.0f, 0.0f, -8.0f));
scene.camera.setFOV(60.0f);
// Window width & height
while (!glfwWindowShouldClose(win)) {
@ -170,10 +172,11 @@ int main() {
renderCallback();
float time = glfwGetTime();
float gVal = sin(time) / 10.5f;
float gVal = sin(time);
// Move the camera left and right
scene.camera.setPosition(glm::vec3(gVal/10.0f, 0.0f, 0.0f));
scene.camera.setPosition(glm::vec3(gVal, 0.0f, 0.0f));
scene.camera.rotate(glm::vec3(0.0f, 0.0f, 1.0f));
// Move the objects & stuff
float rotang = time;

@ -154,11 +154,12 @@ namespace Renderer {
// TODO: Make prerender instead of render
void RenderObject::render(GLFWwindow* win, Camera cam) {
shader.setMat4("position", getPositionTransform());
shader.setMat4("rotation", getRotationTransform());
shader.setMat4("modelPosition", getPositionTransform());
shader.setMat4("modelRotation", getRotationTransform());
shader.setMat4("view", cam.view);
shader.setMat4("projection", cam.projection);
shader.setMat4("camPos", cam.getPositionTransform());
shader.setMat4("camRot", cam.getRotationTransform());
shader.setMat4("camProjection", cam.projection);
shader.use();

Loading…
Cancel
Save