Trying to figure out how to add 3D Rectangle and 3D cylinder to the code I currently have

  3d, c++, opengl

So i made a shape generator.h and a shape generator.cpp I am trying to add a 3d Rectangle and a 3D Cylinder. it has been done using Visual studio 2019 C++. NO USING FREEGLUT.

I have attached the code below any suggestions would be helpful.

ShapeGenerator.h

#pragma once
#include "ShapeData.h"
typedef unsigned int uint;

class ShapeGenerator
{
    static ShapeData makePlaneVerts(uint dimensions);
    static ShapeData makePlaneIndices(uint dimensions);
    
public:

    static ShapeData makePlane(uint dimensions = 10);
    static ShapeData makeSphere(uint tesselation = 20);
    static ShapeData makeCube(uint dimension =35);
    static ShapeData makeCylinder(uint dimension = 30);
    static ShapeData makeRectangle(uint dimension = 25);
};

ShapeData.h

#pragma once
#include "Vertex.h"
#include <glad/glad.h>

struct ShapeData
{
    ShapeData() :
        vertices(0), numVertices(0),
        indices(0), numIndices(0) {}
    Vertex* vertices;
    GLuint numVertices;
    GLushort* indices;
    GLuint numIndices;
    GLsizeiptr vertexBufferSize() const
    {
        return numVertices * sizeof(Vertex);
    }
    GLsizeiptr indexBufferSize() const
    {
        return numIndices * sizeof(GLushort);
    }
    void cleanup()
    {
        delete[] vertices;
        delete[] indices;
        numVertices = numIndices = 0;
    }
};
=========================================================================================================
ShapeGenerator.cpp

#include "ShapeGenerator.h"
//#include <glmglm.hpp>
//#include <glmgtcmatrix_transform.hpp>
#include "Vertex.h"

#define PI 3.14159265359
using glm::vec3;
using glm::mat4;
using glm::mat3;
#define NUM_ARRAY_ELEMENTS(a) sizeof(a) / sizeof(*a)

glm::vec3 randomColor()
{
    glm::vec3 ret;
    ret.x = rand() / (float)RAND_MAX;
    ret.y = rand() / (float)RAND_MAX;
    ret.z = rand() / (float)RAND_MAX;
    return ret;
}


ShapeData ShapeGenerator::makePlaneVerts(uint dimensions)
{
    ShapeData ret;
    ret.numVertices = dimensions * dimensions;
    int half = dimensions / 2;
    ret.vertices = new Vertex[ret.numVertices];
    for (int i = 0; i < dimensions; i++)
    {
        for (int j = 0; j < dimensions; j++)
        {
            Vertex& thisVert = ret.vertices[i * dimensions + j];
            thisVert.position.x = j - half;
            thisVert.position.z = i - half;
            thisVert.position.y = 0;
            thisVert.normal = glm::vec3(0.0f, 1.0f, 0.0f);
            thisVert.color = randomColor();
        }
    }
    return ret;
}

ShapeData ShapeGenerator::makePlaneIndices(uint dimensions)
{
    ShapeData ret;
    ret.numIndices = (dimensions - 1) * (dimensions - 1) * 2 * 3; // 2 triangles per square, 3 indices per triangle
    ret.indices = new unsigned short[ret.numIndices];
    int runner = 0;
    for (int row = 0; row < dimensions - 1; row++)
    {
        for (int col = 0; col < dimensions - 1; col++)
        {
            ret.indices[runner++] = dimensions * row + col;
            ret.indices[runner++] = dimensions * row + col + dimensions;
            ret.indices[runner++] = dimensions * row + col + dimensions + 1;

            ret.indices[runner++] = dimensions * row + col;
            ret.indices[runner++] = dimensions * row + col + dimensions + 1;
            ret.indices[runner++] = dimensions * row + col + 1;
        }
    }
    assert(runner = ret.numIndices);
    return ret;
}


ShapeData ShapeGenerator::makePlane(uint dimensions)
{
    ShapeData ret = makePlaneVerts(dimensions);
    ShapeData ret2 = makePlaneIndices(dimensions);
    ret.numIndices = ret2.numIndices;
    ret.indices = ret2.indices;
    return ret;
}

ShapeData ShapeGenerator::makeCube(uint dimension)
{
    //Add cube
};

return ShapeData();
}

ShapeData ShapeGenerator::makeCylinder(uint dimension)
{
    //Add cylinder
};

return ShapeData();
}


ShapeData ShapeGenerator::makeRectangle(uint dimension)
{
    //Add Rectangle
};

return ShapeData();
}
ShapeData ShapeGenerator::makeSphere(uint tesselation)
{
    ShapeData ret = makePlaneVerts(tesselation);
    ShapeData ret2 = makePlaneIndices(tesselation);
    ret.indices = ret2.indices;
    ret.numIndices = ret2.numIndices;

    uint dimensions = tesselation;
    const float RADIUS = 1.0f;
    const double CIRCLE = PI * 2;
    const double SLICE_ANGLE = CIRCLE / (dimensions - 1);
    for (size_t col = 0; col < dimensions; col++)
    {
        double phi = -SLICE_ANGLE * col;
        for (size_t row = 0; row < dimensions; row++)
        {
            double theta = -(SLICE_ANGLE / 2.0) * row;
            size_t vertIndex = col * dimensions + row;
            Vertex& v = ret.vertices[vertIndex];
            v.position.x = RADIUS * cos(phi) * sin(theta);
            v.position.y = RADIUS * sin(phi) * sin(theta);
            v.position.z = RADIUS * cos(theta);
            v.normal = glm::normalize(v.position);
        }
    }
    return ret;
}

Source: Windows Questions C++

LEAVE A COMMENT