Refactored camera & such (WIP)

pull/3/head
E. Almqvist 2 years ago
parent 2df4f51f29
commit 25f9beb3d4
  1. 56
      headers/renderer.hpp
  2. 5
      shaders/vertex.glsl
  3. 21
      src/main.cpp
  4. 117
      src/renderer.cpp

@ -19,15 +19,56 @@
#define FAR_PLANE 100.0f #define FAR_PLANE 100.0f
namespace Renderer { namespace Renderer {
class RenderObject { class Object {
public: public:
Object();
Object(glm::vec3 pos);
Object(glm::vec3 pos, glm::vec3 angle);
glm::mat4 getPositionTransform();
void setPosition(glm::vec3 pos);
void translate(glm::vec3 dpos);
glm::mat4 getRotationTransform();
void setRotation(glm::vec3 angle);
void rotate(glm::vec3 dangle);
void transform(glm::mat4 T);
private:
void updatePositionTransform();
void updateRotationTransform();
glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f); 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);
glm::mat4 rotationTransform = glm::mat4(1.0f);
glm::mat4 modelTransform = glm::mat4(1.0f);
};
class Camera : public Object {
public:
Camera(GLFWwindow* win);
Camera(GLFWwindow* win, glm::vec3 pos);
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;
};
class RenderObject : public Object {
public:
RenderObject(std::vector<float> verts, std::vector<unsigned int> indices); RenderObject(std::vector<float> verts, std::vector<unsigned int> indices);
void render(GLFWwindow* win, glm::mat4 cameraTransform, glm::mat4 projectionTransform); void render(GLFWwindow* win, Camera cam);
void setRotation(float angle, glm::vec3 axis);
void transform(glm::mat4 T);
void preRenderHook(); void preRenderHook();
void transform(glm::mat4 T);
void setPosition(glm::vec3 pos);
void setRotation(glm::vec3 angle);
private: private:
Shaders::Shader shader; Shaders::Shader shader;
unsigned int EBO; unsigned int EBO;
@ -39,18 +80,17 @@ namespace Renderer {
class Scene { class Scene {
public: public:
Camera camera;
Scene(GLFWwindow* win); Scene(GLFWwindow* win);
Scene(GLFWwindow* win, std::vector<RenderObject> ROs); Scene(GLFWwindow* win, std::vector<RenderObject> ROs);
void setCamera(glm::vec3 pos); void setCamera(Camera cam);
void setFOV(float deg);
void spawnObject(RenderObject ro); void spawnObject(RenderObject ro);
void render(); void render();
private: private:
std::vector<RenderObject> renderObjects = std::vector<RenderObject>(); std::vector<RenderObject> renderObjects = std::vector<RenderObject>();
GLFWwindow* window; GLFWwindow* window;
glm::mat4 cameraTransform = glm::mat4(1.0f);
glm::mat4 projectionTransform = glm::mat4(1.0f);
}; };
class TexturedObject : public RenderObject { class TexturedObject : public RenderObject {

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

@ -149,17 +149,17 @@ int main() {
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor); glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor);
Renderer::TexturedObject ro(verts, indices); Renderer::TexturedObject ro(verts, indices);
ro.position = glm::vec3(0.2f, -1.0f, -4.0f); ro.setPosition(glm::vec3(0.2f, -1.0f, -4.0f));
Renderer::TexturedObject ro2(verts, indices); Renderer::TexturedObject ro2(verts, indices);
ro2.position = glm::vec3(0.5f, 0.0, -2.0f); ro2.setPosition(glm::vec3(0.5f, 0.0, -2.0f));
ro2.setTexture("assets/textures/meep.jpg");
ro.setTexture(RUSTY_METAL_TEXTURE); ro.setTexture(RUSTY_METAL_TEXTURE);
ro2.setTexture("assets/textures/meep.jpg");
scene.spawnObject(ro); scene.spawnObject(ro);
scene.spawnObject(ro2); scene.spawnObject(ro2);
scene.setCamera(glm::vec3(0.0f, 0.0f, -8.0f)); scene.camera.setPosition(glm::vec3(0.0f, 0.0f, -8.0f));
// Window width & height // Window width & height
while (!glfwWindowShouldClose(win)) { while (!glfwWindowShouldClose(win)) {
@ -169,18 +169,17 @@ int main() {
// rendering // rendering
renderCallback(); renderCallback();
/* OBJECT RENDERING */
float time = glfwGetTime(); float time = glfwGetTime();
float gVal = sin(time) / 10.5f; float gVal = sin(time) / 10.5f;
scene.setCamera(glm::vec3(gVal/10.0f, 0.0f, 0.0f));
// Transformation // Move the camera left and right
float rotang = time; scene.camera.setPosition(glm::vec3(gVal/10.0f, 0.0f, 0.0f));
glm::mat4 T = glm::mat4(1.0f); // Move the objects & stuff
T = glm::rotate(T, rotang, glm::vec3(1.0, 0.0, 1.0)); float rotang = time;
ro.transform(T); ro.rotate(glm::vec3(time, 0.0f, time));
// Render new frame
scene.render(); scene.render();
// glfw // glfw

@ -9,12 +9,69 @@
#include "shaders.hpp" #include "shaders.hpp"
namespace Renderer { namespace Renderer {
// Object
Object::Object() {}
Object::Object(glm::vec3 pos) : Object() {
setPosition(pos);
}
Object::Object(glm::vec3 pos, glm::vec3 angle) : Object(pos) {
setRotation(angle);
}
void Object::transform(glm::mat4 T) {
modelTransform = T;
}
glm::mat4 Object::getPositionTransform() { return positionTransform; }
void Object::updatePositionTransform() {
glm::mat4 T = glm::mat4(1.0f);
positionTransform = glm::translate(T, position);
}
void Object::setPosition(glm::vec3 pos) {
position = pos;
updatePositionTransform();
}
void Object::translate(glm::vec3 dpos) {
position += dpos;
updatePositionTransform();
}
glm::mat4 Object::getRotationTransform() { return rotationTransform; }
void Object::updateRotationTransform() {
float x_Ang, y_Ang, z_Ang;
x_Ang = angle[0];
y_Ang = angle[1];
z_Ang = angle[2];
glm::mat4 T = glm::mat4(1.0f);
T = glm::rotate(T, glm::radians(x_Ang), glm::vec3(1.0f, 0.0f, 0.0f));
T = glm::rotate(T, glm::radians(y_Ang), glm::vec3(0.0f, 1.0f, 0.0f));
T = glm::rotate(T, glm::radians(z_Ang), glm::vec3(0.0f, 0.0f, 1.0f));
rotationTransform = T;
}
void Object::setRotation(glm::vec3 ang) {
angle = ang;
updateRotationTransform();
}
void Object::rotate(glm::vec3 dangle) {
angle += dangle;
updateRotationTransform();
}
// Scene // Scene
Scene::Scene(GLFWwindow* win) { Scene::Scene(GLFWwindow* win) : camera(win) {
window = win; window = win;
setFOV(DEFAULT_FOV); camera.setFOV(DEFAULT_FOV);
} }
Scene::Scene(GLFWwindow* win, std::vector<RenderObject> ROs) : Scene(win) { Scene::Scene(GLFWwindow* win, std::vector<RenderObject> ROs) : Scene(win) {
@ -25,19 +82,32 @@ namespace Renderer {
renderObjects.push_back(ro); renderObjects.push_back(ro);
} }
void Scene::setFOV(float fov) { void Scene::setCamera(Camera cam) {
int width, height; camera = cam;
glfwGetWindowSize(window, &width, &height);
projectionTransform = glm::perspective(glm::radians(fov), (float)width / (float)height, NEAR_PLANE, FAR_PLANE);
}
void Scene::setCamera(glm::vec3 pos) {
cameraTransform = glm::translate(cameraTransform, pos);
} }
void Scene::render() { void Scene::render() {
for ( RenderObject ro: renderObjects ) for ( RenderObject ro: renderObjects )
ro.render(window, cameraTransform, projectionTransform); ro.render(window, camera);
}
// Camera
Camera::Camera(GLFWwindow* win) {
window = win;
}
Camera::Camera(GLFWwindow* win, glm::vec3 pos) : Camera(win) {
setPosition(pos);
}
Camera::Camera(GLFWwindow* win, glm::vec3 pos, glm::vec3 angle) : Camera(win, pos) {
setRotation(angle);
}
void Camera::setFOV(float fov) {
int width, height;
glfwGetWindowSize(window, &width, &height);
projection = glm::perspective(glm::radians(fov), (float)width / (float)height, NEAR_PLANE, FAR_PLANE);
} }
// RenderObject // RenderObject
@ -82,23 +152,32 @@ namespace Renderer {
void RenderObject::preRenderHook() {} void RenderObject::preRenderHook() {}
void RenderObject::render(GLFWwindow* win, glm::mat4 cameraTransform, glm::mat4 projectionTransform) { // TODO: Make prerender instead of render
glm::mat4 posT = glm::mat4(1.0f); void RenderObject::render(GLFWwindow* win, Camera cam) {
posT = glm::translate(posT, position); shader.setMat4("position", getPositionTransform());
shader.setMat4("rotation", getRotationTransform());
shader.setMat4("baseModel", posT); shader.setMat4("view", cam.view);
shader.setMat4("view", cameraTransform); shader.setMat4("projection", cam.projection);
shader.setMat4("projection", projectionTransform);
shader.use(); shader.use();
preRenderHook();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glDrawArrays(GL_TRIANGLES, 0, indicesVec.size()); glDrawArrays(GL_TRIANGLES, 0, indicesVec.size());
} }
void RenderObject::setPosition(glm::vec3 pos) {
Object::setPosition(pos);
shader.setMat4("position", getPositionTransform());
}
void RenderObject::setRotation(glm::vec3 angle) {
Object::setRotation(angle);
shader.setMat4("rotation", getRotationTransform());
}
void RenderObject::transform(glm::mat4 T) { void RenderObject::transform(glm::mat4 T) {
Object::transform(T);
shader.setMat4("model", T); shader.setMat4("model", T);
} }

Loading…
Cancel
Save