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
\
layout(location = 0) in vec4 position;
\
void main()
\
{
\
gl_Position = position;
\
}";
const char* fragmentSource = "#version 330 core
\
out vec4 color;
\
void main()
\
{
\
color = vec4(1.0, 0.0, 0.0, 1.0);
\
} ";
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.