Code Monkey home page Code Monkey logo

tessellation's Introduction

Tessellation

Tessellated sphere

Note: the relevant code for this project lives in the shapes directory and in scenegraph/ShapesScene.cpp

This a C++ parametric tessellator for cubes, cylinders, cones, and spheres.

Click here to see a quick video demo!

Running

To run this code, you will need to download QT creator here: https://www.qt.io/download-qt-installer.

Then, you can simply open the project and click run! Make sure to navigate to the 3D canvas.

Data Structures

To store triangle meshes, I created the Triangle and TriMesh data structures in TriMesh.h.

struct Triangle {
    std::vector<int> vertexIndices;
    std::vector<glm::vec3> vertexNormals;
};

struct TriMesh {
    std::vector<glm::vec3> &vertices;
    std::vector<Triangle> &faces;
};

Storing vertex indices instead of the vertices themselves helps maintain consistency and reduce the size of the mesh.

Design Decisions

For the most part, I favored composition over inheritance for this project. I factored out the circular base of the Cone and Cylinder into a CircleBase m_circleBase member variable, and added Tessellator m_tessellator utility to each shape as well.

Inheritance was helpful, however, in allowing the ShapeScene to keep track of a Shape m_shape that could be a Cube, Cylinder, Cone, or Sphere. The Shape class is a thin wrapper I created around OpenGLShape which allows setting tessellation parameters (vertical or horizontal tessellation levels) and has a pure virtual method initializeVertexData. Every class derived from Shape must implement theinitializeVertexData method in order to be rendered by OpenGL.

The Tessellator Utility Class

The class Tessellator implements functionality common to all or many shapes. It has the following methods:

std::vector<Triangle> tessellate(int gridWidth, int gridHeight);
std::vector<float> processTriMesh(TriMesh triMesh);
void setUncurvedMeshNormals(TriMesh triMesh);

The tessellate function will traverse a vertex grid and construct counter-clockwise ordered triangle faces.

The processTriMesh function converts a completed triangle mesh into the vector of floats representing vertices and normals that OpenGL requires.

And setUncurvedMeshNormals simplifies the process of setting normals for any uncurved surface, like the side of a cube or the base of a cylinder. It uses crossproducts to compute one normal for each face directly from the vertex positions. For curved surfaces, like a sphere or the side of a cyliner/cone, the 3 vertices of a face will have different normals, so we instead use parametric calculations to compute the normals separately.

tessellation's People

Contributors

petersli avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.