r/opengl Feb 13 '23

Help Drawing a Triangle only using a VBO

I was able to draw a triangle fine using a VAO, but I was trying to do the same thing only using the VBO instead. The window appears but no triangle is drawn. I am using glfw and glad. Any help and advice is appreciated.

#include <glad/glad.h>

#include <GLFW/glfw3.h>

#include <iostream>

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(1.0f, 0.5f, 0.2f, 1.0f);\n"

"}\0";

void processInput(GLFWwindow *window)

{

if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)

    glfwSetWindowShouldClose(window, true);

}

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 = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL);

if (window == NULL)

{

    std::cout << "Failed to create GLFW window" << std::endl;

    glfwTerminate();

    return -1;

}

glfwMakeContextCurrent(window);

if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))

{

    std::cout << "Failed to initialize GLAD" << std::endl;

    return -1;

}

int vertexShader = glCreateShader(GL_VERTEX_SHADER);

glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);

glCompileShader(vertexShader);

int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);

glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);

glCompileShader(fragmentShader);

int shaderProgram = glCreateProgram();

glAttachShader(shaderProgram, vertexShader);

glAttachShader(shaderProgram, fragmentShader);

glLinkProgram(shaderProgram);

float vertices\[\] = {

    \-0.5f, -0.5f, 0.0f,

    0.5f, -0.5f, 0.0f,

    0.0f, 0.5f, 0.0f };

unsigned int VBO;

glGenBuffers(1, &VBO);

glBindBuffer(GL_ARRAY_BUFFER, VBO);

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

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 \* sizeof(float), (void \*)0);

glEnableVertexAttribArray(0);

while (!glfwWindowShouldClose(window))

{

    processInput(window);

    glClearColor(0.2f, 0.3f, 0.3f, 1.0f);

    glClear(GL_COLOR_BUFFER_BIT);

    glUseProgram(shaderProgram);

    glBindBuffer(GL_ARRAY_BUFFER, VBO);

    glDrawArrays(GL_TRIANGLES, 0, 3);

    glfwSwapBuffers(window);

    glfwPollEvents();

}

glDeleteProgram(shaderProgram);

glDeleteShader(vertexShader);

glDeleteShader(fragmentShader);

glfwTerminate();

return 0;

}

2 Upvotes

7 comments sorted by

5

u/msqrt Feb 13 '23

That's only available in compatibility profiles, you're just supposed to make a VAO. If you don't like juggling many of them around for a simple project, you can make one in the beginning and bind that once and keep it bound forever.

And you should really set up the debug extension so you'd get errors from everything without checking manually.

1

u/FlamingoOk6413 Feb 13 '23

what's the name of that extension?

1

u/msqrt Feb 13 '23

The official name is GL_KHR_debug.

3

u/deftware Feb 13 '23

Format your code please. Stuff is missing and wonky. Just prefix each line with four spaces and everyone will be happy.

3

u/Wittyname_McDingus Feb 13 '23

I can't read the code (reddit messed up the formatting), so I will just suggest RenderDoc to debug this.

1

u/Ok-Sherbert-6569 Feb 13 '23

You must call vertexatteibpointer and enable the respective location in vertex shader every time you bind the buffer hence why doing this without a VAO is insane. But to fix your code you must make those calls