PDA

View Full Version : Program crashes when trying to draw a triangle using glDrawArrays



The Shroom
11-15-2017, 07:00 AM
I'm trying to draw a triangle, like the title says, but when the code reaches the glDrawArrays function, it crashes saying that the function is null :

Exception thrown at 0x69E4B887 (nvoglv32.dll) in SEngine.exe: 0xC0000005: Access violation reading location 0x00000000.

Can't seem to figure out why this is happening, probably really obvious.


#include <GLEW/GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>

int main()
{
if (!glfwInit())
{
std::cout << "GLFW init failed." << std::endl;
}

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

GLFWwindow* window = glfwCreateWindow(1024, 720, "Title", NULL, NULL);
glfwMakeContextCurrent(window);

if (glewInit() != GLEW_OK)
{
std::cout << "GLEW init failed." << std::endl;
}

float vertices[]
{
-0.5f, -0.5f,
0.0f, 0.5f,
0.5f, -0.5f
};

unsigned int indicies[]
{
0, 1, 2,
2, 3, 0
};

unsigned int vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 6, vertices, GL_STATIC_DRAW);

glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, (void*)0);

unsigned int ibo;
glGenBuffers(1, &ibo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * 6, indicies, GL_STATIC_DRAW);

unsigned int program;
unsigned int vertex;
unsigned int fragment;

program = glCreateProgram();
fragment = glCreateShader(GL_FRAGMENT_SHADER);
vertex = glCreateShader(GL_VERTEX_SHADER);

const char* vertexSource = "#version 330 core\n\
layout(location = 0) in vec4 position;\n\
void main()\n\
{\n\
gl_Position = position;\n\
}";

const char* fragmentSource = "#version 330 core\n\
out vec4 color;\n\
void main()\n\
{\n\
color = vec4(1.0, 0.0, 0.0, 1.0);\n\
} ";

glShaderSource(vertex, 1, &vertexSource, NULL);
glCompileShader(vertex);
int vertSuccess;
char vertInfoLog[512];
glGetShaderiv(vertex, GL_COMPILE_STATUS, &vertSuccess);
if (!vertSuccess)
{
glGetShaderInfoLog(vertex, 512, NULL, vertInfoLog);
std::cout << "Failed: " << vertInfoLog << std::endl;
}

glShaderSource(fragment, 1, &fragmentSource, NULL);
glCompileShader(fragment);
int fragSuccess;
char fragInfoLog[512];
glGetShaderiv(fragment, GL_COMPILE_STATUS, &fragSuccess);
if (!fragSuccess)
{
glGetShaderInfoLog(fragment, 512, NULL, fragInfoLog);
std::cout << "Failed: " << fragInfoLog << std::endl;
}

glAttachShader(program, vertex);

glAttachShader(program, fragment);

glLinkProgram(program);
int successLink;
char linkShaderLog[512];
glGetProgramiv(program, GL_LINK_STATUS, &successLink);
glGetProgramInfoLog(program, 512, NULL, linkShaderLog);
if (!successLink)
{
std::cout << "Failed: " << linkShaderLog << std::endl;
}

glValidateProgram(program);

int successValid;
char validationLog[512];
glGetProgramiv(program, GL_VALIDATE_STATUS, &successValid);
glGetProgramInfoLog(program, 512, NULL, validationLog);
if (!successValid)
{
std::cout << "Failed: " << validationLog << std::endl;
}

glDeleteShader(vertex);
glDeleteShader(fragment);

glUseProgram(program);

while (!glfwWindowShouldClose(window))
{
glClear(GL_COLOR_BUFFER_BIT);

glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr);

glfwSwapBuffers(window);

glfwPollEvents();
}

glDeleteProgram(program);

}

There are no messages in the console either.

mhagain
11-15-2017, 12:59 PM
You're using a core profile but you don't have a VAO created. You'll need to glGenVertexArrays and glBindVertexArray before your first glEnableVertexAttribArray call.

The Shroom
11-15-2017, 01:11 PM
You're using a core profile but you don't have a VAO created. You'll need to glGenVertexArrays and glBindVertexArray before your first glEnableVertexAttribArray call.

Oh, I see. Makes sense. Got my triangle in the window now finally, thank you. :P

Also, not sure if I should create another question for this or not; but do I even need a EBO/IBO in this piece of code since I'm never actually calling using glDrawElements ?
Edit: Wait I do call glDrawElements... Nevermind, I should probably go to sleep instead lol.