0

I'm trying to draw a square plane of 10 x 10 using the VBO on OpenGL. So I have a function getPlaneCoords that calculate the vertices and the indexes however the program just ends up crashing when I run it.

fragment shader:

#version 330 core

out vec4 color;

void main()
{
    color = vec4(0.5, 0.2, 0.3, 1.0);
}

vertex shader:

#version 330 core
layout (location = 0) in vec3 position;

out vec3 FragPos;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

void main()
{
    gl_Position = projection * view *  model * vec4(position, 1.0f);
    FragPos = vec3(model * vec4(position, 1.0f));
}

Main.cpp:

constexpr GLuint dimensions{ 10 };
int numberOfPoints{ 0 };
int numberOfIndexes{ 0 };

float vertices[dimensions * dimensions * 3];
unsigned int indices[(dimensions - 1) * (dimensions - 1) * 6];

void getPlaneCoords()
{
    int half = dimensions / 2;
    for (int i{ 0 }; i < dimensions; ++i)
    {
        for (int j{ 0 }; j < dimensions; ++j)
        {
            float x = j - half;
            float y = 0;
            float z = i - half;

            vertices[numberOfPoints++] = x;
            vertices[numberOfPoints++] = y;
            vertices[numberOfPoints++] = z;

        }
    }

    for (int row{ 0 }; row < dimensions - 1; ++row)
    {
        for (int col{ 0 }; col < dimensions - 1; ++col)
        {
            indices[numberOfIndexes++] = dimensions * row + col;
            indices[numberOfIndexes++] = dimensions * row + col + dimensions;
            indices[numberOfIndexes++] = dimensions * row + col + dimensions + 1;

            indices[numberOfIndexes++] = dimensions * row + col;
            indices[numberOfIndexes++] = dimensions * row + col + dimensions + 1;
            indices[numberOfIndexes++] = dimensions * row + col + 1;
        }
    }
}

int main()
{
 glViewport(0, 0, WIDTH, HEIGHT);

    // OpenGL options
    glEnable(GL_DEPTH_TEST);

    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    getPlaneCoords();

    // Build and compile our shader program
    Shader lightingShader("lighting.vs", "lighting.frag");

    GLuint squareVAO, VBO, EBO;
    glGenVertexArrays(1, &squareVAO);
    glBindVertexArray(squareVAO);
    
    glGenBuffers(1, &VBO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, numberOfPoints * sizeof(vertices), vertices, GL_STATIC_DRAW);

    glGenBuffers(1, &EBO);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, numberOfIndexes * sizeof(GLuint), indices, GL_STATIC_DRAW);

    GLint PosAttrib{ glGetAttribLocation(lightingShader.Program, "position") };
    glVertexAttribPointer(PosAttrib, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0); // Note that we skip over the normal vectors
    glEnableVertexAttribArray(PosAttrib);

    glBindVertexArray(0);

    glm::mat4 projection = glm::perspective(camera.GetZoom(), (GLfloat)WIDTH / (GLfloat)HEIGHT, 0.1f, 1000.0f);

    while(window.running)
    {
     glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        lightingShader.Use();

        glm::mat4 view;
        view = camera.GetViewMatrix();

        // Get the uniform locations
        GLint modelLoc = glGetUniformLocation(lightingShader.Program, "model");
        GLint viewLoc = glGetUniformLocation(lightingShader.Program, "view");
        GLint projLoc = glGetUniformLocation(lightingShader.Program, "projection");

        // Pass the matrices to the shader
        glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
        glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(projection));

        glBindVertexArray(squareVAO);
        glm::mat4 model = glm::mat4();
        glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
        glDrawElements(GL_TRIANGLES, numberOfIndexes, GL_UNSIGNED_INT, 0);
        glBindVertexArray(0);
        
        
        window.display();
    }

// Missing important display code but that is related to SFML. The code shared is primarily the OpenGL methods
}

It's meant to be a 10 x 10 plane with the color rgba ratio (0.5, 0.2, 0.3, 1.0);

I'm not sure as to where I went wrong?

GigaHiga
  • 39
  • 1
  • 7
  • 3
    `numberOfPoints * sizeof(vertices)` -> `numberOfPoints * sizeof(GLfloat)` – Rabbid76 Dec 08 '20 at 13:33
  • @Rabbid76 thanks it runs now but it's blank. It only shows the color of RGBA raatio(0.1, 0.1, 0.1, 1.0). Edit: Never mind it's just flat where the normal is facing in the y axis. I just need to set the normals or just rotate the model. – GigaHiga Dec 08 '20 at 14:24
  • `glm::mat4 model = glm::mat4();` -> `glm::mat4 model = glm::mat4(1.0f);` – Rabbid76 Dec 08 '20 at 15:26

0 Answers0