Merge pull request #3 from E-Almqvist/scene

Added scenes, cameras, and base rendering classes
pull/4/head
E. Almqvist 2 years ago committed by GitHub
commit 015d459e35
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. BIN
      assets/textures/meep.jpg
  2. 67
      headers/renderer.hpp
  3. 11
      shaders/vertex.glsl
  4. 139
      src/main.cpp
  5. 141
      src/renderer.cpp
  6. 2
      src/textures.cpp

Binary file not shown.

After

Width:  |  Height:  |  Size: 752 KiB

@ -14,16 +14,56 @@
#define VERT_SHADER_SRC_FILE "shaders/vertex.glsl" #define VERT_SHADER_SRC_FILE "shaders/vertex.glsl"
#define FRAG_SHADER_SRC_FILE "shaders/fragment.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 NEAR_PLANE 0.1f
#define FAR_PLANE 100.0f #define FAR_PLANE 100.0f
namespace Renderer { namespace Renderer {
class RenderObject { class Object {
public: public:
RenderObject(std::vector<float> verts, std::vector<unsigned int> indices); Object();
void render(GLFWwindow* win, glm::mat4 cameraTransform, glm::mat4 projectionTransform); Object(glm::vec3 pos);
Object(glm::vec3 pos, glm::vec3 angle);
glm::mat4 getModelTransform();
void transform(glm::mat4 T); void transform(glm::mat4 T);
void scale(glm::vec3 vscale);
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);
private:
void updatePositionTransform();
void updateRotationTransform();
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 projection = glm::mat4(1.0f);
private:
GLFWwindow* window;
};
class RenderObject : public Object {
public:
RenderObject(std::vector<float> verts, std::vector<unsigned int> indices);
void render(GLFWwindow* win, Camera cam);
void preRenderHook(); void preRenderHook();
private: private:
Shaders::Shader shader; Shaders::Shader shader;
@ -34,23 +74,22 @@ namespace Renderer {
std::vector<unsigned int> indicesVec; std::vector<unsigned int> indicesVec;
}; };
class Renderer3D { class Scene {
public: public:
Renderer3D(GLFWwindow* win); Camera camera;
Renderer3D(GLFWwindow* win, std::vector<RenderObject> ROs);
void setCamera(glm::vec3 pos); Scene(GLFWwindow* win);
void setFOV(float deg); Scene(GLFWwindow* win, std::vector<RenderObject*> ROs);
void spawnObject(RenderObject ro);
void setCamera(Camera cam);
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 Obj2D : public RenderObject { class TexturedObject : public RenderObject {
public: public:
using RenderObject::RenderObject; using RenderObject::RenderObject;
void setTexture(const char* t_src); void setTexture(const char* t_src);

@ -6,12 +6,17 @@ layout (location = 2) in vec2 aTexCoord;
out vec4 VertexColor; out vec4 VertexColor;
out vec2 TexCoord; out vec2 TexCoord;
uniform mat4 modelRotation = mat4(1.0);
uniform mat4 modelPosition = mat4(1.0);
uniform mat4 model = 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() { void main() {
gl_Position = projection * view * model * vec4(aPos, 1.0); // projection { view } { model }
gl_Position = camProjection * (camRot * camPos) * (modelRotation * modelPosition * model) * vec4(aPos, 1.0);
VertexColor = vec4(aColor, 1.0); VertexColor = vec4(aColor, 1.0);
TexCoord = aTexCoord; TexCoord = aTexCoord;
} }

@ -7,17 +7,11 @@
#include <vector> #include <vector>
#include <math.h> #include <math.h>
#include "../headers/renderer.hpp" #include "renderer.hpp"
#include "glm/trigonometric.hpp"
// #include "../headers/shaders.hpp"
// #include "../headers/textures.hpp"
#define WINDOW_WIDTH 640 #define WINDOW_WIDTH 640
#define WINDOW_HEIGHT 480 #define WINDOW_HEIGHT 480
#define VERT_SHADER_SRC_FILE "shaders/vertex.glsl"
#define FRAG_SHADER_SRC_FILE "shaders/fragment.glsl"
#define RUSTY_METAL_TEXTURE "assets/textures/rusty_metal.jpg" #define RUSTY_METAL_TEXTURE "assets/textures/rusty_metal.jpg"
void framebuffer_size_callback(GLFWwindow* win, int w, int h) { void framebuffer_size_callback(GLFWwindow* win, int w, int h) {
@ -33,8 +27,10 @@ void processInput(GLFWwindow *win) {
void renderCallback() { void renderCallback() {
// Make background // Make background
glClearColor(0.1f, 0.1f, 0.1f, 1.0f); // glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT); // glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
} }
int main() { int main() {
@ -49,8 +45,6 @@ int main() {
return 1; return 1;
} }
Renderer::Renderer3D renderer(win);
glfwMakeContextCurrent(win); glfwMakeContextCurrent(win);
if ( !gladLoadGLLoader((GLADloadproc)glfwGetProcAddress) ) { if ( !gladLoadGLLoader((GLADloadproc)glfwGetProcAddress) ) {
@ -59,30 +53,116 @@ int main() {
} }
std::vector<float> verts({ std::vector<float> verts({
0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, -0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 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,
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, 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, 1.0f, 1.0f, 1.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, 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,
-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
}); });
// Vert struc: x y z r g b tx ty // Vert struc: x y z r g b tx ty
std::vector<unsigned int> indices({ std::vector<unsigned int> indices({
0, 1, 3, 0, 1, 3,
1, 2, 3 1, 2, 3,
5, 6, 7,
7, 8, 9,
9, 10, 11,
11, 12, 13,
0, 1, 3,
3, 4, 5,
5, 6, 7,
7, 8, 9,
9, 10, 11,
11, 12, 13,
0, 1, 3,
3, 4, 5,
5, 6, 7,
7, 8, 9,
9, 10, 11,
11, 12, 13,
0, 1, 3,
3, 4, 5,
5, 6, 7,
7, 8, 9,
9, 10, 11,
11, 12, 13,
0, 1, 3,
3, 4, 5,
5, 6, 7,
7, 8, 9,
9, 10, 11,
11, 12, 13,
}); });
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
glfwSetFramebufferSizeCallback(win, framebuffer_size_callback); glfwSetFramebufferSizeCallback(win, framebuffer_size_callback);
glEnable(GL_DEPTH_TEST);
float borderColor[] = {1.0f, 1.0f, 1.0f, 1.0f}; float borderColor[] = {1.0f, 1.0f, 1.0f, 1.0f};
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor); glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor);
Renderer::Obj2D ro(verts, indices); // Create scene
renderer.spawnObject(ro); Renderer::Scene scene(win);
renderer.setCamera(glm::vec3(0.0f, 0.0f, -8.0f));
Renderer::TexturedObject ro(verts, indices);
ro.setPosition(glm::vec3(0.2f, -1.0f, -8.0f));
Renderer::TexturedObject ro2(verts, indices);
ro2.setPosition(glm::vec3(0.5f, 0.0, -8.0f));
ro2.setTexture("assets/textures/meep.jpg");
ro.setTexture(RUSTY_METAL_TEXTURE); ro.setTexture(RUSTY_METAL_TEXTURE);
// Window width & height scene.spawnObject(&ro);
scene.spawnObject(&ro2);
while (!glfwWindowShouldClose(win)) { while (!glfwWindowShouldClose(win)) {
// Handle input // Handle input
processInput(win); processInput(win);
@ -90,20 +170,19 @@ int main() {
// rendering // rendering
renderCallback(); renderCallback();
/* OBJECT RENDERING */
float time = glfwGetTime(); float time = glfwGetTime();
float gVal = sin(time) / 10.5f; float gVal = sin(time);
renderer.setCamera(glm::vec3(0.0f, 0.0f, -gVal));
// Transformation // Move the camera left and right
float rotang = time; scene.camera.setRotation(glm::vec3(0.0f, 5.0f, 0.0f));
scene.camera.translate(glm::vec3(0.0f, 0.0f, 0.02f + gVal/100.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;
T = glm::scale(T, glm::vec3(0.5, 0.5, 0.5)); ro.translate(glm::vec3(0.0f, 0.0f, -0.021f));
ro.transform(T);
renderer.render(); // Render new frame
scene.render();
// glfw // glfw
glfwSwapBuffers(win); glfwSwapBuffers(win);

@ -1,41 +1,123 @@
#include <glad/glad.h> #include <glad/glad.h>
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp> #include <glm/gtc/matrix_transform.hpp>
#include <iostream>
#include "GLFW/glfw3.h" #include "GLFW/glfw3.h"
#include "glm/ext/matrix_transform.hpp"
#include "glm/gtx/string_cast.hpp"
#include "glm/fwd.hpp"
#include "renderer.hpp" #include "renderer.hpp"
#include "shaders.hpp" #include "shaders.hpp"
namespace Renderer { namespace Renderer {
// Object
Object::Object() {}
// Renderer3D Object::Object(glm::vec3 pos) : Object() {
Renderer3D::Renderer3D(GLFWwindow* win) { setPosition(pos);
}
Object::Object(glm::vec3 pos, glm::vec3 angle) : Object(pos) {
setRotation(angle);
}
glm::mat4 Object::getModelTransform() { return modelTransform; }
void Object::scale(glm::vec3 vscale) {
glm::mat4 T = glm::mat4(1.0f);
T = glm::scale(T, vscale);
modelTransform = T;
}
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(GLFWwindow* win) : camera(win) {
window = win; window = win;
setFOV(DEFAULT_FOV); camera.setFOV(DEFAULT_FOV);
} }
Renderer3D::Renderer3D(GLFWwindow* win, std::vector<RenderObject> ROs) : Renderer3D(win) { Scene::Scene(GLFWwindow* win, std::vector<RenderObject*> ROs) : Scene(win) {
renderObjects = ROs; renderObjects = ROs;
} }
void Renderer3D::spawnObject(RenderObject ro) { void Scene::spawnObject(RenderObject *ro) {
renderObjects.push_back(ro); renderObjects.push_back(ro);
} }
void Renderer3D::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::render() {
for ( RenderObject *ro: renderObjects )
ro->render(window, camera);
}
// Camera
Camera::Camera(GLFWwindow* win) {
window = win;
} }
void Renderer3D::setCamera(glm::vec3 pos) { Camera::Camera(GLFWwindow* win, glm::vec3 pos) : Camera(win) {
cameraTransform = glm::translate(cameraTransform, pos); setPosition(pos);
} }
void Renderer3D::render() { Camera::Camera(GLFWwindow* win, glm::vec3 pos, glm::vec3 angle) : Camera(win, pos) {
for ( RenderObject ro: renderObjects ) setRotation(angle);
ro.render(window, cameraTransform, projectionTransform); }
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
@ -58,13 +140,11 @@ namespace Renderer {
glGenBuffers(1, &VBO); glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO); glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, vertsVec.size() * sizeof(vertsVec[0]), vertsArray, GL_DYNAMIC_DRAW); // for moving stuff 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 // Copy the indices for the verts into another buffer
glGenBuffers(1, &EBO); glGenBuffers(1, &EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesVec.size() * sizeof(indicesVec[0]), indicesArray, GL_STATIC_DRAW); glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesVec.size() * sizeof(indicesVec[0]), indicesArray, GL_STATIC_DRAW);
// printf("%u\n",indicesVec.size() * sizeof(indicesVec[0]));
// Shader stuff // Shader stuff
// Pos // Pos
@ -82,36 +162,35 @@ 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
shader.setMat4("view", cameraTransform); void RenderObject::render(GLFWwindow* win, Camera cam) {
shader.setMat4("projection", projectionTransform); shader.setMat4("modelPosition", getPositionTransform());
shader.setMat4("modelRotation", getRotationTransform());
shader.setMat4("model", getModelTransform());
shader.use(); shader.setMat4("camPos", cam.getPositionTransform());
shader.setMat4("camRot", cam.getRotationTransform());
shader.setMat4("camProjection", cam.projection);
preRenderHook(); shader.use();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glDrawElements(GL_TRIANGLES, indicesVec.size(), GL_UNSIGNED_INT, 0); glDrawArrays(GL_TRIANGLES, 0, indicesVec.size());
}
void RenderObject::transform(glm::mat4 T) {
shader.setMat4("model", T);
} }
// Obj2D // TexturedObject
void Obj2D::setTexture(const char* t_src) { void TexturedObject::setTexture(const char* t_src) {
texture.texture_src = t_src; texture.texture_src = t_src;
texture.load(); texture.load();
} }
void Obj2D::preRenderHook() { void TexturedObject::preRenderHook() {
printf("OBJ2D was here!\n");
if (texture.loaded) if (texture.loaded)
texture.bind(); texture.bind();
} }
// Private stuff // Private stuff
void Obj2D::bind_texture(Textures::Texture2D new_texture) { void TexturedObject::bind_texture(Textures::Texture2D new_texture) {
texture = new_texture; texture = new_texture;
} }
} }

@ -19,7 +19,7 @@ namespace Textures {
void Texture2D::load() { void Texture2D::load() {
// Bind this texture // Bind this texture
glGenTextures(1, &id); glGenTextures(1, &id);
glBindTexture(GL_TEXTURE_2D, id); bind();
// Repeat/bounded etc // Repeat/bounded etc
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);

Loading…
Cancel
Save