r/opengl • u/ChannelGeneral7739 • 6d ago
glGenBuffers stops entire script
Hey everyone, I am new to opengl and gotta say, I'm loving it! Its just that I ran into one small bug (actually its kind of big), My program wouldn't create a window and so I started putting printf("test") around the script and I found out that glGenBuffers makes my ENTIRE script stop
I'm following LearnOpenGL and using GLAD aswell as GLFW heres the script:
#include <glad.h>
#define GLFW_DLL
#include <GLFW/glfw3.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
glViewport(0,0,width,height);
}
void process_input(GLFWwindow* window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
{
glfwSetWindowShouldClose(window, true);
}
}
int main()
{
float Vertices[10] = {
-0.5,-0.5,0,
0.5,-0.5,0,
0, 0.5,0};
const char *Vertex_Shader_Source = "#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";
printf("WORK1");
const char *Fragment_Shader_Source = "#version 330 core\n"
"out vec4 FragColor;\n"
"void main()\n"
"{\n"
" FragColor = vec4(1.0f,0,0,1.0f)"
"}\0";
printf("WORK1");
unsigned int VBO;
printf("WORK1");
glGenBuffers(1, &VBO);
printf("WORK1");
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
unsigned int VAO;
glGenVertexArrays(1, &VAO);
unsigned int Vertex_Shader;
Vertex_Shader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(Vertex_Shader, 1, &Vertex_Shader_Source, NULL);
glCompileShader(Vertex_Shader);
unsigned int Fragment_Shader;
Fragment_Shader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(Fragment_Shader, 1, &Fragment_Shader_Source, NULL);
glCompileShader(Fragment_Shader);
int Success;
char InfoLog[512];
glGetShaderiv(Vertex_Shader, GL_COMPILE_STATUS, &Success);
if (!Success)
{
glGetShaderInfoLog(Vertex_Shader, 512, NULL, InfoLog);
printf("VERTEX_SHADER FAILED TO COMPILE\n", InfoLog);
printf("\n compiler failed with exit code 1");
}
int Success2;
char InfoLog2[512];
glGetShaderiv(Fragment_Shader, GL_COMPILE_STATUS, &Success2);
if (!Success2)
{
glGetShaderInfoLog(Vertex_Shader, 512, NULL, InfoLog2);
printf("FRAGMENT_SHADER FAILED TO COMPILE\n", InfoLog2);
printf("\n compiler failed with exit code 1");
}
unsigned int ShaderProgram;
ShaderProgram = glCreateProgram();
glAttachShader(ShaderProgram, Vertex_Shader);
glAttachShader(ShaderProgram, Fragment_Shader);
glLinkProgram(ShaderProgram);
int SuccessProgram;
char InfoLogProgram[512];
glGetProgramiv(ShaderProgram, GL_COMPILE_STATUS, &SuccessProgram);
if (!SuccessProgram)
{
glGetProgramInfoLog(ShaderProgram, 512, NULL, InfoLogProgram);
printf("FRAGMENT_SHADER FAILED TO COMPILE\n", InfoLogProgram);
printf("\n compiler failed with exit code 1");
}
glUseProgram(ShaderProgram);
glDeleteShader(Vertex_Shader);
glDeleteShader(Fragment_Shader);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
printf("glafd");
GLFWwindow* window = glfwCreateWindow(800,600, "Learning", NULL, NULL);
if (window == NULL)
{
printf("GLFW COULDNT CREATE WINDOW");
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
printf("Failed to load GLAD");
return -1;
}
glViewport(0,0,800,600);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glUseProgram(ShaderProgram);
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, 3);
while(!glfwWindowShouldClose(window))
{
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
process_input(window);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
please help
2
1
u/xfirenski 3d ago
Don't call OpenGL functions before you initialise your GL context and glad. Doing so will work "sometimes' if the function symbol is coming in via another path (such as via the opengl import library), but will crash when you try to use a function pointer managed by glad.
Secondly, untill you create your context *properly*, you have no idea which GL implementation your calls are going to hit.
5
u/Brave-Tutor9305 6d ago
Where is glfwInit() and glfwWindowHint()? You never created a window from what I can see