I'm learning OpenGL using C++ and Visual Studio, and when I run my program it prompts the display window quickly and then crashes leading me to a break point that says "Unhandled exception at 0x00000000 in OpenGL.exe: 0xC000000005: Access violation.".
This is my code:
#include <iostream>
#include <string>
#include <fstream>
#include <sdl2/SDL.h>
#include <gl/glew.h>
GLuint crearShader (const std :: string & texto, GLenum tipo);
std :: string cargarShader (const std :: string & nombreArchivo);
void comprobarErrorShader (GLuint shader, GLuint flag, bool esPrograma, const std :: string & mensaje);
void display ()
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_Window * ventana = SDL_CreateWindow("OpenGL", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
640, 480, SDL_WINDOW_OPENGL);
SDL_GLContext contexto = SDL_GL_CreateContext(ventana);
GLenum estado = glewInit();
if (estado != GLEW_OK)
{
std :: cerr << "Fallo al inicializar Glew." << std :: endl;
exit(EXIT_FAILURE);
}
GLuint programa = glCreateProgram(); // <-- Problem goes here.
const unsigned int NUM_SHADERS = 2;
GLuint shaders[ NUM_SHADERS ];
shaders[0] = crearShader(cargarShader("./shaderBasico.vs"), GL_VERTEX_SHADER);
shaders[1] = crearShader(cargarShader("./shaderBasico.fs"), GL_FRAGMENT_SHADER);
unsigned int i;
for (i = 0; i < NUM_SHADERS; i++)
{
glAttachShader(programa, shaders[ i ]);
}
glLinkProgram(programa);
comprobarErrorShader(programa, GL_LINK_STATUS, true, "Error: Fallo al vincular el programa: ");
glValidateProgram(programa);
comprobarErrorShader(programa, GL_VALIDATE_STATUS, true, "Error: El programa es invalido: ");
SDL_Event evento;
do
{
glClearColor(0x00, 0x00, 0x00, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(programa);
SDL_GL_SwapWindow(ventana);
SDL_PollEvent(&evento);
}
while (evento.type != SDL_QUIT) ;
for (i = 0; i < NUM_SHADERS; i++)
{
glDetachShader(programa, shaders[ i ]);
glDeleteShader(shaders[ i ]);
}
glDeleteProgram(programa);
SDL_GL_DeleteContext(contexto);
SDL_DestroyWindow(ventana);
SDL_Quit();
}
GLuint crearShader (const std :: string & texto, GLenum tipo)
{
GLuint shader = glCreateShader(tipo);
if (shader == 0)
{
std :: cerr << "Error al crear shader." << std :: endl;
}
const GLchar * shaderSourceStrings[1] = {texto.c_str()};
GLint shaderSourceStringLengths[1] = {texto.length()};
glShaderSource(shader, 1, shaderSourceStrings, shaderSourceStringLengths);
glCompileShader(shader);
comprobarErrorShader(shader, GL_COMPILE_STATUS, true, "Error: Fallo al vincular el programa: ");
return shader;
}
std :: string cargarShader (const std :: string & nombreArchivo)
{
std :: ifstream archivo;
archivo.open(nombreArchivo.c_str());
std :: string salida;
std :: string linea;
if (archivo.is_open())
{
while (archivo.good())
{
getline(archivo, linea);
salida.append(linea + "\n");
}
}
else
{
std :: cerr << "Error, no se pudo cargar un shader." << std :: endl;
}
return salida;
}
void comprobarErrorShader (GLuint shader, GLuint flag, bool esPrograma, const std :: string & mensaje)
{
GLint exito = 0;
GLchar error[1024] = { 0 };
if (esPrograma)
{
glGetProgramiv(shader, flag, &exito);
}
else
{
glGetShaderiv(shader, flag, &exito);
}
if (exito == GL_FALSE)
{
if (esPrograma)
{
glGetProgramInfoLog(shader, sizeof error, NULL, error);
}
else
{
glGetShaderInfoLog(shader, sizeof error, NULL, error);
}
std :: cerr << mensaje << ": '" << error << "'" << std :: endl;
}
}
int main (int argc, char * argv[])
{
display();
return 0;
}
As you can see I initialized glew before creating the program and still giving me errors, any ideas?
glGetString(GL_VERSION)
returns 1.4.0 - Build 8.14.10.2364
Well there you go, glCreateProgram()
wasn't core in OpenGL 1.4.
GLEW most likely set glCreateProgram()
to NULL.
Check if your GL implementation supports ARB_vertex_shader
, ARB_fragment_shader
, and ARB_shader_objects
and use those ARB functions instead.
If those extensions aren't supported then you're SOL on the shader front unless you want to use a software rasterizer like Mesa.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With