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 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 RenderObject {
class Object {
public:
RenderObject(std::vector<float> verts, std::vector<unsigned int> indices);
void render(GLFWwindow* win, glm::mat4 cameraTransform, glm::mat4 projectionTransform);
Object();
Object(glm::vec3 pos);
Object(glm::vec3 pos, glm::vec3 angle);
glm::mat4 getModelTransform();
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();
private:
Shaders::Shader shader;
@ -34,23 +74,22 @@ namespace Renderer {
std::vector<unsigned int> indicesVec;
};
class Renderer3D {
class Scene {
public:
Renderer3D(GLFWwindow* win);
Renderer3D(GLFWwindow* win, std::vector<RenderObject> ROs);
Camera camera;
void setCamera(glm::vec3 pos);
void setFOV(float deg);
void spawnObject(RenderObject ro);
Scene(GLFWwindow* win);
Scene(GLFWwindow* win, std::vector<RenderObject*> ROs);
void setCamera(Camera cam);
void spawnObject(RenderObject *ro);
void render();
private:
std::vector<RenderObject> renderObjects = std::vector<RenderObject>();
std::vector<RenderObject*> renderObjects = std::vector<RenderObject*>();
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:
using RenderObject::RenderObject;
void setTexture(const char* t_src);

@ -6,12 +6,17 @@ layout (location = 2) in vec2 aTexCoord;
out vec4 VertexColor;
out vec2 TexCoord;
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 * 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);
TexCoord = aTexCoord;
}

@ -7,17 +7,11 @@
#include <vector>
#include <math.h>
#include "../headers/renderer.hpp"
#include "glm/trigonometric.hpp"
// #include "../headers/shaders.hpp"
// #include "../headers/textures.hpp"
#include "renderer.hpp"
#define WINDOW_WIDTH 640
#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"
void framebuffer_size_callback(GLFWwindow* win, int w, int h) {
@ -33,8 +27,10 @@ void processInput(GLFWwindow *win) {
void renderCallback() {
// Make background
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
// 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() {
@ -49,8 +45,6 @@ int main() {
return 1;
}
Renderer::Renderer3D renderer(win);
glfwMakeContextCurrent(win);
if ( !gladLoadGLLoader((GLADloadproc)glfwGetProcAddress) ) {
@ -59,30 +53,116 @@ int main() {
}
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.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
-0.5f, 0.5f, 0.0f, 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, 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, 1.0f, 1.0f, 0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f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, 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
std::vector<unsigned int> indices({
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);
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.spawnObject(ro);
renderer.setCamera(glm::vec3(0.0f, 0.0f, -8.0f));
// Create scene
Renderer::Scene scene(win);
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);
// Window width & height
scene.spawnObject(&ro);
scene.spawnObject(&ro2);
while (!glfwWindowShouldClose(win)) {
// Handle input
processInput(win);
@ -90,20 +170,19 @@ int main() {
// rendering
renderCallback();
/* OBJECT RENDERING */
float time = glfwGetTime();
float gVal = sin(time) / 10.5f;
renderer.setCamera(glm::vec3(0.0f, 0.0f, -gVal));
float gVal = sin(time);
// Transformation
float rotang = time;
// Move the camera left and right
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);
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);
// Move the objects & stuff
float rotang = time;
ro.translate(glm::vec3(0.0f, 0.0f, -0.021f));
renderer.render();
// Render new frame
scene.render();
// glfw
glfwSwapBuffers(win);

@ -1,41 +1,123 @@
#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"
namespace Renderer {
// Object
Object::Object() {}
// Renderer3D
Renderer3D::Renderer3D(GLFWwindow* win) {
Object::Object(glm::vec3 pos) : Object() {
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;
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;
}
void Renderer3D::spawnObject(RenderObject ro) {
void Scene::spawnObject(RenderObject *ro) {
renderObjects.push_back(ro);
}
void Renderer3D::setFOV(float fov) {
int width, height;
glfwGetWindowSize(window, &width, &height);
projectionTransform = glm::perspective(glm::radians(fov), (float)width / (float)height, NEAR_PLANE, FAR_PLANE);
void Scene::setCamera(Camera cam) {
camera = cam;
}
void Scene::render() {
for ( RenderObject *ro: renderObjects )
ro->render(window, camera);
}
// Camera
Camera::Camera(GLFWwindow* win) {
window = win;
}
void Renderer3D::setCamera(glm::vec3 pos) {
cameraTransform = glm::translate(cameraTransform, pos);
Camera::Camera(GLFWwindow* win, glm::vec3 pos) : Camera(win) {
setPosition(pos);
}
void Renderer3D::render() {
for ( RenderObject ro: renderObjects )
ro.render(window, cameraTransform, projectionTransform);
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
@ -58,13 +140,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
@ -82,36 +162,35 @@ namespace Renderer {
void RenderObject::preRenderHook() {}
void RenderObject::render(GLFWwindow* win, glm::mat4 cameraTransform, glm::mat4 projectionTransform) {
shader.setMat4("view", cameraTransform);
shader.setMat4("projection", projectionTransform);
// TODO: Make prerender instead of render
void RenderObject::render(GLFWwindow* win, Camera cam) {
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);
glDrawElements(GL_TRIANGLES, indicesVec.size(), GL_UNSIGNED_INT, 0);
}
void RenderObject::transform(glm::mat4 T) {
shader.setMat4("model", T);
glDrawArrays(GL_TRIANGLES, 0, indicesVec.size());
}
// Obj2D
void Obj2D::setTexture(const char* t_src) {
// TexturedObject
void TexturedObject::setTexture(const char* t_src) {
texture.texture_src = t_src;
texture.load();
}
void Obj2D::preRenderHook() {
printf("OBJ2D was here!\n");
void TexturedObject::preRenderHook() {
if (texture.loaded)
texture.bind();
}
// Private stuff
void Obj2D::bind_texture(Textures::Texture2D new_texture) {
void TexturedObject::bind_texture(Textures::Texture2D new_texture) {
texture = new_texture;
}
}

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

Loading…
Cancel
Save