Example of using new simple API

+ Clean up some TODOs
+ More docs
+ Disable clang-format in Mesh
This commit is contained in:
2022-11-26 12:20:44 -05:00
parent d37484d75c
commit 7f13b2d729
10 changed files with 371 additions and 330 deletions

View File

@@ -13,6 +13,9 @@ using namespace Qtk;
Cube::Cube(DrawMode mode) {
mDrawMode = mode;
switch(mode) {
// The order of the following assignment values helps to visualize.
// clang-format off
// Cube data for use with glDrawArrays
case QTK_DRAW_ARRAYS:
mIndices = {/* No indices needed for glDrawArrays */};
@@ -20,51 +23,76 @@ Cube::Cube(DrawMode mode) {
mNormals = {FACE_FRONT, FACE_BACK, FACE_TOP,
FACE_BOTTOM, FACE_LEFT, FACE_RIGHT};
mVertices = {// Face 1 (Front)
VERTEX_FTR, VERTEX_FTL, VERTEX_FBL, VERTEX_FBL, VERTEX_FBR,
VERTEX_FTR,
// Face 2 (Back)
VERTEX_BBR, VERTEX_BTL, VERTEX_BTR, VERTEX_BTL, VERTEX_BBR,
VERTEX_BBL,
// Face 3 (Top)
VERTEX_FTR, VERTEX_BTR, VERTEX_BTL, VERTEX_BTL, VERTEX_FTL,
VERTEX_FTR,
// Face 4 (Bottom)
VERTEX_FBR, VERTEX_FBL, VERTEX_BBL, VERTEX_BBL, VERTEX_BBR,
VERTEX_FBR,
// Face 5 (Left)
VERTEX_FBL, VERTEX_FTL, VERTEX_BTL, VERTEX_FBL, VERTEX_BTL,
VERTEX_BBL,
// Face 6 (Right)
VERTEX_FTR, VERTEX_FBR, VERTEX_BBR, VERTEX_BBR, VERTEX_BTR,
VERTEX_FTR};
// We're drawing triangles to construct the geometry of a cube.
// Each triangle is made up of three points.
// The entire cube has 12 triangles to make 6 square faces of the cube.
mVertices = {
// Face 1 (Front)
VERTEX_FTR, VERTEX_FTL, VERTEX_FBL,
VERTEX_FBL, VERTEX_FBR, VERTEX_FTR,
// Face 2 (Back)
VERTEX_BBR, VERTEX_BTL, VERTEX_BTR,
VERTEX_BTL, VERTEX_BBR, VERTEX_BBL,
// Face 3 (Top)
VERTEX_FTR, VERTEX_BTR, VERTEX_BTL,
VERTEX_BTL, VERTEX_FTL, VERTEX_FTR,
// Face 4 (Bottom)
VERTEX_FBR, VERTEX_FBL, VERTEX_BBL,
VERTEX_BBL, VERTEX_BBR, VERTEX_FBR,
// Face 5 (Left)
VERTEX_FBL, VERTEX_FTL, VERTEX_BTL,
VERTEX_FBL, VERTEX_BTL, VERTEX_BBL,
// Face 6 (Right)
VERTEX_FTR, VERTEX_FBR, VERTEX_BBR,
VERTEX_BBR, VERTEX_BTR, VERTEX_FTR
};
mColors = {// Face 1 (Front)
RED, GREEN, BLUE, BLUE, WHITE, RED,
// Face 2 (Back)
YELLOW, CYAN, MAGENTA, CYAN, YELLOW, BLACK,
// Face 3 (Top)
RED, MAGENTA, CYAN, CYAN, GREEN, RED,
// Face 4 (Bottom)
WHITE, BLUE, BLACK, BLACK, YELLOW, WHITE,
// Face 5 (Left)
BLUE, GREEN, CYAN, BLUE, CYAN, BLACK,
// Face 6 (Right)
RED, WHITE, YELLOW, YELLOW, MAGENTA, RED};
// Setting colors for each vertex that we defined above.
// These are defaults and can be overriden by the caller with setColor().
// The colors below are interpolated from vertex to vertex.
mColors = {
// Face 1 (Front)
RED, GREEN, BLUE,
BLUE, WHITE, RED,
// Face 2 (Back)
YELLOW, CYAN, MAGENTA,
CYAN, YELLOW, BLACK,
// Face 3 (Top)
RED, MAGENTA, CYAN,
CYAN, GREEN, RED,
// Face 4 (Bottom)
WHITE, BLUE, BLACK,
BLACK, YELLOW, WHITE,
// Face 5 (Left)
BLUE, GREEN, CYAN,
BLUE, CYAN, BLACK,
// Face 6 (Right)
RED, WHITE, YELLOW,
YELLOW, MAGENTA, RED
};
mTexCoords = {// Face 1 (Front)
UV_TOP, UV_ORIGIN, UV_RIGHT, UV_RIGHT, UV_CORNER, UV_TOP,
// Face 2 (Back)
UV_TOP, UV_RIGHT, UV_CORNER, UV_RIGHT, UV_TOP, UV_ORIGIN,
// Face 3 (Top)
UV_CORNER, UV_TOP, UV_ORIGIN, UV_ORIGIN, UV_RIGHT,
UV_CORNER,
// Face 4 (Bottom)
UV_TOP, UV_ORIGIN, UV_RIGHT, UV_RIGHT, UV_CORNER, UV_TOP,
// Face 5 (Left)
UV_TOP, UV_CORNER, UV_RIGHT, UV_TOP, UV_RIGHT, UV_ORIGIN,
// Face 6 (Right)
UV_TOP, UV_CORNER, UV_RIGHT, UV_RIGHT, UV_ORIGIN, UV_TOP};
// Define texture coordinates for the cube.
// This defines the orientation of the texture when applied the object.
mTexCoords = {
// Face 1 (Front)
UV_TOP, UV_ORIGIN, UV_RIGHT,
UV_RIGHT, UV_CORNER, UV_TOP,
// Face 2 (Back)
UV_TOP, UV_RIGHT, UV_CORNER,
UV_RIGHT, UV_TOP, UV_ORIGIN,
// Face 3 (Top)
UV_CORNER, UV_TOP, UV_ORIGIN,
UV_ORIGIN, UV_RIGHT, UV_CORNER,
// Face 4 (Bottom)
UV_TOP, UV_ORIGIN, UV_RIGHT,
UV_RIGHT, UV_CORNER, UV_TOP,
// Face 5 (Left)
UV_TOP, UV_CORNER, UV_RIGHT,
UV_TOP, UV_RIGHT, UV_ORIGIN,
// Face 6 (Right)
UV_TOP, UV_CORNER, UV_RIGHT,
UV_RIGHT, UV_ORIGIN, UV_TOP
};
break;
@@ -81,56 +109,64 @@ Cube::Cube(DrawMode mode) {
VERTEX_FTR, VERTEX_FTL, VERTEX_FBL, VERTEX_FBR,
// 4 5 6 7
VERTEX_BTR, VERTEX_BTL, VERTEX_BBL, VERTEX_BBR};
mIndices = {// Face 1 (Front)
0, 1, 2, 2, 3, 0,
// Face 2 (Back)
7, 5, 4, 5, 7, 6,
// Face 3 (Top)
0, 4, 5, 5, 1, 0,
// Face 4 (Bottom)
3, 2, 6, 6, 7, 3,
// Face 5 (Left)
2, 1, 5, 2, 5, 6,
// Face 6 (Right)
0, 3, 7, 7, 4, 0};
mIndices = {
// Face 1 (Front)
0, 1, 2, 2, 3, 0,
// Face 2 (Back)
7, 5, 4, 5, 7, 6,
// Face 3 (Top)
0, 4, 5, 5, 1, 0,
// Face 4 (Bottom)
3, 2, 6, 6, 7, 3,
// Face 5 (Left)
2, 1, 5, 2, 5, 6,
// Face 6 (Right)
0, 3, 7, 7, 4, 0
};
break;
// Cube shape data for using normals and UVs with glDrawElements
case QTK_DRAW_ELEMENTS_NORMALS:
mColors = {RED, GREEN, BLUE, WHITE, YELLOW, CYAN, MAGENTA, BLACK};
mVertices = {// Face 1 (Front)
// 0 1 2 3
VERTEX_FTL, VERTEX_FBL, VERTEX_FBR, VERTEX_FTR,
// Face 2 (Back)
// 4 5 6 7
VERTEX_BTL, VERTEX_BBL, VERTEX_BBR, VERTEX_BTR,
// Face 3 (Top)
// 8 9 10 11
VERTEX_FTL, VERTEX_BTL, VERTEX_BTR, VERTEX_FTR,
// Face 4 (Bottom)
// 12 13 14 15
VERTEX_FBL, VERTEX_BBL, VERTEX_BBR, VERTEX_FBR,
// Face 5 (Left)
// 16 17 18 19
VERTEX_FBL, VERTEX_BBL, VERTEX_BTL, VERTEX_FTL,
// Face 6 (Right)
// 20 21 22 23
VERTEX_FBR, VERTEX_BBR, VERTEX_BTR, VERTEX_FTR};
mVertices = {
// Face 1 (Front)
// 0 1 2 3
VERTEX_FTL, VERTEX_FBL, VERTEX_FBR, VERTEX_FTR,
// Face 2 (Back)
// 4 5 6 7
VERTEX_BTL, VERTEX_BBL, VERTEX_BBR, VERTEX_BTR,
// Face 3 (Top)
// 8 9 10 11
VERTEX_FTL, VERTEX_BTL, VERTEX_BTR, VERTEX_FTR,
// Face 4 (Bottom)
// 12 13 14 15
VERTEX_FBL, VERTEX_BBL, VERTEX_BBR, VERTEX_FBR,
// Face 5 (Left)
// 16 17 18 19
VERTEX_FBL, VERTEX_BBL, VERTEX_BTL, VERTEX_FTL,
// Face 6 (Right)
// 20 21 22 23
VERTEX_FBR, VERTEX_BBR, VERTEX_BTR, VERTEX_FTR
};
mIndices = {// Face 1 (Front)
0, 1, 2, 2, 3, 0,
// Face 2 (Back)
4, 5, 6, 6, 7, 4,
// Face 3 (Top)
8, 9, 10, 10, 11, 8,
// Face 4 (Bottom)
12, 13, 14, 14, 15, 12,
mIndices = {
// Face 1 (Front)
0, 1, 2, 2, 3, 0,
// Face 2 (Back)
4, 5, 6, 6, 7, 4,
// Face 3 (Top)
8, 9, 10, 10, 11, 8,
// Face 4 (Bottom)
12, 13, 14, 14, 15, 12,
// Face 5 (Left)
16, 17, 18, 18, 19, 16,
// Face 6 (Right)
20, 21, 22, 22, 23, 20
};
// Face 5 (Left)
16, 17, 18, 18, 19, 16,
// Face 6 (Right)
20, 21, 22, 22, 23, 20};
mNormals = {
VECTOR_FORWARD, VECTOR_FORWARD, VECTOR_FORWARD, VECTOR_FORWARD,
@@ -143,50 +179,27 @@ Cube::Cube(DrawMode mode) {
mTexCoords = {
// Face 1 (Front)
UV_TOP,
UV_RIGHT,
UV_CORNER,
UV_RIGHT,
UV_TOP,
UV_ORIGIN,
UV_TOP, UV_RIGHT, UV_CORNER,
UV_RIGHT, UV_TOP, UV_ORIGIN,
// Face 2 (Back)
UV_TOP,
UV_RIGHT,
UV_CORNER,
UV_RIGHT,
UV_TOP,
UV_ORIGIN,
UV_TOP, UV_RIGHT, UV_CORNER,
UV_RIGHT, UV_TOP, UV_ORIGIN,
// Face 3 (Top)
UV_TOP,
UV_RIGHT,
UV_CORNER,
UV_RIGHT,
UV_TOP,
UV_ORIGIN,
UV_TOP, UV_RIGHT, UV_CORNER,
UV_RIGHT, UV_TOP, UV_ORIGIN,
// Face 4 (Bottom)
UV_TOP,
UV_RIGHT,
UV_CORNER,
UV_RIGHT,
UV_TOP,
UV_ORIGIN,
UV_TOP, UV_RIGHT, UV_CORNER,
UV_RIGHT, UV_TOP, UV_ORIGIN,
// Face 5 (Left)
UV_TOP,
UV_RIGHT,
UV_CORNER,
UV_RIGHT,
UV_TOP,
UV_ORIGIN,
UV_TOP, UV_RIGHT, UV_CORNER,
UV_RIGHT, UV_TOP, UV_ORIGIN,
// Face 6 (Right)
UV_TOP,
UV_RIGHT,
UV_CORNER,
UV_RIGHT,
UV_TOP,
UV_ORIGIN,
UV_TOP, UV_RIGHT, UV_CORNER,
UV_RIGHT, UV_TOP, UV_ORIGIN,
};
break;
// clang-format on
}
}
@@ -194,6 +207,8 @@ Triangle::Triangle(DrawMode mode) {
mDrawMode = mode;
const QVector3D triangleTop = QVector3D(0.0f, 0.5f, 0.0f);
switch(mode) {
// clang-format off
case QTK_DRAW_ARRAYS:
mIndices = {/* No indices needed for glDrawArrays */};
@@ -201,29 +216,17 @@ Triangle::Triangle(DrawMode mode) {
mVertices = {
// Bottom face (Base of the pyramid)
VERTEX_BBL,
VERTEX_BBR,
VERTEX_FBR,
VERTEX_FBR,
VERTEX_FBL,
VERTEX_BBL,
VERTEX_BBL, VERTEX_BBR, VERTEX_FBR,
VERTEX_FBR, VERTEX_FBL, VERTEX_BBL,
// Front face
VERTEX_FBL,
VERTEX_FBR,
triangleTop,
VERTEX_FBL, VERTEX_FBR, triangleTop,
// Back face
VERTEX_BBR,
VERTEX_BBL,
triangleTop,
VERTEX_BBR, VERTEX_BBL, triangleTop,
// Left face
VERTEX_BBL,
VERTEX_FBL,
triangleTop,
VERTEX_BBL, VERTEX_FBL, triangleTop,
// Right face
VERTEX_FBR,
VERTEX_BBR,
triangleTop,
VERTEX_FBR, VERTEX_BBR, triangleTop,
};
// Find normals for each triangle of the mesh
@@ -238,29 +241,17 @@ Triangle::Triangle(DrawMode mode) {
mTexCoords = {
// Bottom face (Base of the pyramid)
UV_ORIGIN,
UV_RIGHT,
UV_CORNER,
UV_CORNER,
UV_TOP,
UV_ORIGIN,
UV_ORIGIN, UV_RIGHT, UV_CORNER,
UV_CORNER, UV_TOP, UV_ORIGIN,
// Front face
UV_ORIGIN,
UV_RIGHT,
UV_CORNER,
UV_ORIGIN, UV_RIGHT, UV_CORNER,
// Back face
UV_ORIGIN,
UV_RIGHT,
UV_CORNER,
UV_ORIGIN, UV_RIGHT, UV_CORNER,
// Left face
UV_ORIGIN,
UV_RIGHT,
UV_CORNER,
UV_ORIGIN, UV_RIGHT, UV_CORNER,
// Right face
UV_ORIGIN,
UV_RIGHT,
UV_CORNER,
UV_ORIGIN, UV_RIGHT, UV_CORNER,
};
break;
@@ -296,34 +287,22 @@ Triangle::Triangle(DrawMode mode) {
mVertices = {
// Bottom face
// 0 1 2
VERTEX_FBL,
VERTEX_FBR,
VERTEX_BBL,
VERTEX_FBL, VERTEX_FBR, VERTEX_BBL,
// 3 4 5
VERTEX_BBR,
VERTEX_FBR,
VERTEX_BBL,
VERTEX_BBR, VERTEX_FBR, VERTEX_BBL,
// Front face
// 6 7 8
VERTEX_FBL,
VERTEX_FBR,
triangleTop,
VERTEX_FBL, VERTEX_FBR, triangleTop,
// Back face
// 9 10 11
VERTEX_BBR,
VERTEX_BBL,
triangleTop,
VERTEX_BBR, VERTEX_BBL, triangleTop,
// Left face
// 12 13 14
VERTEX_BBL,
VERTEX_FBL,
triangleTop,
VERTEX_BBL, VERTEX_FBL, triangleTop,
// Right face
// 15 16 17
VERTEX_FBR,
VERTEX_BBR,
triangleTop,
VERTEX_FBR, VERTEX_BBR, triangleTop,
};
mIndices = {
@@ -350,30 +329,19 @@ Triangle::Triangle(DrawMode mode) {
mTexCoords = {
// Bottom face
UV_ORIGIN,
UV_RIGHT,
UV_TOP,
UV_CORNER,
UV_RIGHT,
UV_TOP,
UV_ORIGIN, UV_RIGHT, UV_TOP,
UV_CORNER, UV_RIGHT, UV_TOP,
// Front face
UV_ORIGIN,
UV_RIGHT,
UV_CORNER,
UV_ORIGIN, UV_RIGHT, UV_CORNER,
// Back face
UV_ORIGIN,
UV_RIGHT,
UV_CORNER,
UV_ORIGIN, UV_RIGHT, UV_CORNER,
// Left face
UV_ORIGIN,
UV_RIGHT,
UV_CORNER,
UV_ORIGIN, UV_RIGHT, UV_CORNER,
// Right face
UV_ORIGIN,
UV_RIGHT,
UV_CORNER,
UV_ORIGIN, UV_RIGHT, UV_CORNER,
};
break;
// clang-format on
}
}

View File

@@ -46,21 +46,22 @@ namespace Qtk {
#define VECTOR_ONE QVector3D(1.0f, 1.0f, 1.0f)
#define VECTOR_ZERO QVector3D(0.0f, 0.0f, 0.0f)
// clang-format off
// A series of direction vectors to represent cube face normal
#define FACE_TOP \
VECTOR_UP, VECTOR_UP, VECTOR_UP, VECTOR_UP, VECTOR_UP, VECTOR_UP
#define FACE_BOTTOM \
VECTOR_DOWN, VECTOR_DOWN, VECTOR_DOWN, VECTOR_DOWN, VECTOR_DOWN, VECTOR_DOWN
#define FACE_LEFT \
VECTOR_LEFT, VECTOR_LEFT, VECTOR_LEFT, VECTOR_LEFT, VECTOR_LEFT, VECTOR_LEFT
#define FACE_RIGHT \
VECTOR_RIGHT, VECTOR_RIGHT, VECTOR_RIGHT, VECTOR_RIGHT, VECTOR_RIGHT, \
VECTOR_RIGHT
#define FACE_FRONT \
VECTOR_FORWARD, VECTOR_FORWARD, VECTOR_FORWARD, VECTOR_FORWARD, \
VECTOR_FORWARD, VECTOR_FORWARD
#define FACE_BACK \
VECTOR_BACK, VECTOR_BACK, VECTOR_BACK, VECTOR_BACK, VECTOR_BACK, VECTOR_BACK
#define FACE_TOP VECTOR_UP, VECTOR_UP, VECTOR_UP, \
VECTOR_UP, VECTOR_UP, VECTOR_UP
#define FACE_BOTTOM VECTOR_DOWN, VECTOR_DOWN, VECTOR_DOWN, \
VECTOR_DOWN, VECTOR_DOWN, VECTOR_DOWN
#define FACE_LEFT VECTOR_LEFT, VECTOR_LEFT, VECTOR_LEFT, \
VECTOR_LEFT, VECTOR_LEFT, VECTOR_LEFT
#define FACE_RIGHT VECTOR_RIGHT, VECTOR_RIGHT, VECTOR_RIGHT, \
VECTOR_RIGHT, VECTOR_RIGHT, VECTOR_RIGHT
#define FACE_FRONT VECTOR_FORWARD, VECTOR_FORWARD, VECTOR_FORWARD, \
VECTOR_FORWARD, VECTOR_FORWARD, VECTOR_FORWARD
#define FACE_BACK VECTOR_BACK, VECTOR_BACK, VECTOR_BACK, \
VECTOR_BACK, VECTOR_BACK, VECTOR_BACK
// clang-format on
// Colors using QVector3Ds as RGB values
#define WHITE VECTOR_ONE

View File

@@ -110,7 +110,7 @@ namespace Qtk {
}
/*************************************************************************
* Getters
* Accessors
************************************************************************/
/**
@@ -120,6 +120,8 @@ namespace Qtk {
*/
static MeshRenderer * getInstance(const QString & name);
Transform3D & getTransform() { return mTransform; }
private:
/*************************************************************************
* Private Members

View File

@@ -29,7 +29,7 @@
namespace Qtk {
/**
* 3D models will store this data for each vertex in geometry
* 3D models will store this data for each vertex in geometry.
*/
struct QTKAPI ModelVertex {
QVector3D mPosition;
@@ -199,13 +199,7 @@ namespace Qtk {
*/
static Model * getInstance(const char * name);
/*************************************************************************
* Public Members
************************************************************************/
/* The position of this model in 3D space */
// TODO: Make private
Transform3D mTransform;
Transform3D & getTransform() { return mTransform; }
private:
/*************************************************************************
@@ -246,6 +240,9 @@ namespace Qtk {
* Private Members
************************************************************************/
/* The position of this model in 3D space */
Transform3D mTransform;
/* Static QHash used to store and access models globally. */
static ModelManager mManager;

View File

@@ -51,19 +51,31 @@ namespace Qtk {
* Accessors
************************************************************************/
inline const Colors & getColors() { return mShape.mColors; }
[[nodiscard]] inline const Colors & getColors() const {
return mShape.mColors;
}
inline const Indices & getIndexData() { return mShape.mIndices; }
[[nodiscard]] inline const Indices & getIndexData() const {
return mShape.mIndices;
}
inline const Normals & getNormals() { return mShape.mNormals; }
[[nodiscard]] inline const Normals & getNormals() const {
return mShape.mNormals;
}
[[nodiscard]] inline const Shape & getShape() const { return mShape; }
inline const TexCoords & getTexCoords() { return mShape.mTexCoords; }
[[nodiscard]] inline const TexCoords & getTexCoords() const {
return mShape.mTexCoords;
}
inline Texture & getTexture() { return mTexture; }
[[nodiscard]] inline const Texture & getTexture() const {
return mTexture;
}
inline const Vertices & getVertices() { return mShape.mVertices; }
[[nodiscard]] inline const Vertices & getVertices() const {
return mShape.mVertices;
}
/*************************************************************************
* Setters
@@ -118,25 +130,19 @@ namespace Qtk {
mProgram.release();
}
/*************************************************************************
* Public Members
************************************************************************/
QOpenGLBuffer mVBO, mNBO;
QOpenGLVertexArrayObject mVAO;
Transform3D mTransform;
Shape mShape;
Texture mTexture;
const char * mName;
bool mBound;
private:
/*************************************************************************
* Private Members
************************************************************************/
QOpenGLShaderProgram mProgram;
QOpenGLBuffer mVBO, mNBO;
QOpenGLVertexArrayObject mVAO;
Transform3D mTransform;
Shape mShape;
Texture mTexture;
const char * mName;
bool mBound;
};
} // namespace Qtk