Skip to content

Cross platform 3D renderer in C++ and OpenGL with lighting, shadows and materials.

License

Notifications You must be signed in to change notification settings

MorcilloSanz/RendererGL

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RendererGL

RendererGL is a basic 3D renderer written in C++ and OpenGL that allows working with 3D graphics without the need to know computer graphics or OpenGL. So that both beginners and more experienced programmers can create a 3D scene with lighting, shadows and materials.

Important

This project is still under development

Take a look at some screenshots. 3D assets are not included

Features

  • Trackball and first person shooter camera
  • Anti aliasing (MSAA)
  • Textures
  • Load 3D models and textures from files
  • Skybox (cubemap)
  • FrameCapturer: create a texture of the scene
  • Blinn-Phong lighting: Ambient, Diffuse, Specular, Emission
  • Physically Based Rendering (PBR): Albedo, Metallic, Normal, Roughness, Ambient Occlusion, Emission
  • Shadow Mapping: percentage closer filtering
  • Normal Mapping
  • Gamma correction
  • HDR
  • Mouse ray casting: object selection

Scene Graph

A scene graph is a general data structure which arranges the logical and often spatial representation of a graphical scene. It is a collection of nodes in a graph or tree structure:

  • Polytope: A set of vertices and indices (optional) that defines a shape
  • Group: A set of polytopes. It also defines the primitive (triangles, quads...) which the polytopes inside of it will be drawn.
  • Model: A group which contains a set of polytopes that are loaded from a file (.obj, .dae, ...)
  • Scene: Contains a set of groups, models and other scenes
  • Renderer: Contains a set of scenes. It's the one who deals with all the graphics stuff

Take a look at the example below

Example: rotating cube with lighting

#include <iostream>
#include <vector>

#include <engine/renderer/Renderer.h>
#include <engine/renderer/TrackballCamera.h>
#include <engine/shapes/Cube.h>

#include <GLFW/glfw3.h>

const int WIDTH = 1280;
const int HEIGHT = 900;
GLFWwindow* window;

Renderer::Ptr renderer;

int main() {

    // Create window
    if (!glfwInit()) {
        std::cout << "Couldn't initialize window" << std::endl;
        return -1;
    }

    window = glfwCreateWindow(WIDTH, HEIGHT, "Cube example", NULL, NULL);
    
    if (!window) glfwTerminate();

    glfwMakeContextCurrent(window);

    // Renderer
    renderer = Renderer::New(WIDTH, HEIGHT);
    renderer->enableLight();

    // Camera
    double aspectRatio = static_cast<double>(WIDTH) / HEIGHT;
    TrackballCamera::Ptr camera = TrackballCamera::perspectiveCamera(glm::radians(45.0f), aspectRatio, 0.1, 1000);
    renderer->setCamera(std::dynamic_pointer_cast<Camera>(camera));
    camera->zoom(-2.5);

    // Light
    DirectionalLight light(glm::vec3(1));
    light.setColor(glm::vec3(1));
    renderer->addLight(light);

    // Scene
    Cube::Ptr cube = Cube::New();

    Group::Ptr group = Group::New();
    group->add(cube);

    Scene::Ptr scene = Scene::New();
    scene->addGroup(group);

    renderer->addScene(scene);

    // Main loop
    while (!glfwWindowShouldClose(window)) {

        // Update scene
        cube->rotate(0.55, glm::vec3(1, 0, 1));

        // Draw scene
        renderer->clear();
        renderer->draw();
        
        // Update window
        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    // Destroy window
    glfwTerminate();

    return 0;
}

Custom mesh

std::vector<Vec3f> vertices { ... };
std::vector<unsigned int> indices { ... };

Polytope::Ptr mesh = Polytope::New(vertices, indices);

Group::Ptr group = Group::New();
group->add(mesh);

Screenshots

Lighting (Blinn-Phong), shadow mapping and emission

PBR Materials

Point Cloud

Gabriel archangel by greypixel geometrics

360 image visualization example

Contribution

RendererGL is an open source project under the MIT licence. Feel free to fork it and contribute

Compilation

CMake is required for compilation. Take a look at compile.md

Dependencies

  • GLEW for loading OpenGL extensions
  • GLM for linear algebra stuff
  • ASSIMP for loading 3D models from files (.obj, .dae, ...)
  • STB for loading images from files (.png, .tga, .jpg, ...)

Optional dependencies used in tests:

  • GLFW for creating a window with an OpenGL context
  • ImGui for GUI

References

Dealing with OpenGL was much easier thanks to: