Problem making a single array for multiple shape vertices

Viewed 43

Here is my code:

#include <iostream>
#include <glad/glad.h>
#include <GLFW/glfw3.h>

const char* vertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"void main()\n"
"{\n"
"   gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
"}\0";
const char* fragmentShaderSource = "#version 330 core\n"
"out vec4 FragColor;\n"
"void main()\n"
"{\n"
"   FragColor = vec4(0.8f, 0.3f, 0.02f, 1.0f);\n"
"}\n\0";

int main()
{
    glfwInit();

    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    GLFWwindow* window;

    int windowWidth, windowHeight, resAnswer, aspectRatFirst, aspectRatSecond, aspectRatDeformation;

    do {
        std::cout << "Choose the preset for window resolution:" << std::endl;
        std::cout << "   1. 800x800 (1:1)" << std::endl;
        std::cout << "   2. 1024x768 (16:9)" << std::endl;
        std::cout << "Answer: "; std::cin >> resAnswer;
    } while (resAnswer != 1 && resAnswer != 2);
    switch (resAnswer)
    {
    case 1:
        windowWidth = 800;
        windowHeight = 800;
        aspectRatFirst = 1;
        aspectRatSecond = 1;
        break;
    case 2:
        windowWidth = 1024;
        windowHeight = 768;
        aspectRatFirst = 16;
        aspectRatSecond = 9;
        break;
    }

    aspectRatDeformation = aspectRatSecond / aspectRatFirst;

    int shapeAnswer;

    do {
        std::cout << std::endl;
        std::cout << "Choose your shape:" << std::endl;
        std::cout << "   1. Triangle" << std::endl;
        std::cout << "   2. Square" << std::endl;
        std::cout << "Answer: "; std::cin >> shapeAnswer;
    } while (shapeAnswer != 1 && shapeAnswer != 2);

64  GLfloat preVertices[];

    if (shapeAnswer == 1) {
        preVertices = {
            -0.5f, -0.5f * float(sqrt(3)) / 3, 0.0f,  //B
            0.5f, -0.5f * float(sqrt(3)) / 3, 0.0f,   //C
            0.0f, 0.5f * float(sqrt(3)) * 2 / 3, 0.0f //A
        };
    }
    else if (shapeAnswer == 2) {
        preVertices = {
            -0.5f, -0.5f * float(sqrt(3)) / 3, 0.0f,  //B
            0.5f, -0.5f * float(sqrt(3)) / 3, 0.0f,   //C
            0.0f, 0.5f * float(sqrt(3)) * 2 / 3, 0.0f //A
        };
    }

81  GLfloat vertices[] = preVertices;



    window = glfwCreateWindow(windowWidth, windowHeight, "OpenGLCourse", NULL, NULL);
    if (window == NULL) {
        std::cout << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);

    gladLoadGL();

    glViewport(0, 0, windowWidth, windowHeight);

    GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
    glCompileShader(vertexShader);

    GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
    glCompileShader(fragmentShader);

    GLuint shaderProgram = glCreateProgram();

    glAttachShader(shaderProgram, vertexShader);
    glAttachShader(shaderProgram, fragmentShader);
    glLinkProgram(shaderProgram);

    glDeleteShader(vertexShader);
    glDeleteShader(fragmentShader);

    GLuint VBO;

    glGenBuffers(1, &VBO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);

119 glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);


    glClearColor(0.07f, 0.13f, 0.17f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);
    glfwSwapBuffers(window);

    while (!glfwWindowShouldClose(window)) {
        glfwPollEvents();
    }

    glfwDestroyWindow(window);
    glfwTerminate();
    return 0;
}

My problem is between lines 64-81 and the line 119.

What I am trying to do is to create an array "vertices" for the specified shape vertices coordinates. The problem is that the triangle shape has 3 vertices and the square has 4 vertices, the array tho can only have one number of elements. I have another array "preVertices" just because I tried a lot of things to fix this problem.

The code wasn't working mainly because at line 119 I have to get the size of vertices, and the vertices seems to be undefined (and i think its because its getting defined inside of an if statement, or it was, at first, as i said, i changed a lot of things trying to resolve the problem), now, I think one solution would be just to make two different arrays with the different arrays and actually use an if statement to determine which i need at line 119, but I was wondering if there exists a solution with only one array, cuz I m thinking if there would be 100 different shapes it would be more time consuming to make 100 different arrays and it will also occupy more ram than needed, am I right?

0 Answers
Related