PDA

View Full Version : Access violation



paul188
02-03-2016, 12:17 PM
Hello guys i have a problem in opengl, I cant fix...when I compile my little program, I just get:
Access violation executing location 0x00005 in sdl_opengl2.exe. Im writing the code and stuff in visual studio and the setup should work properly.
Please if you have any idea that could fix my problem than id be very thankful
The source code is here:

main.cpp


#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;

#include <GL/glew.h>
#include <SDL.h>
#include "Extras.h"

GLuint program;
GLint attribute_coord2d, attribute_v_color;
GLuint vs;
GLuint fs;
SDL_Event *ev;
GLuint vbo_triangle, vbo_triangle_colors;

bool init_resources(void) {
GLint compile_ok = GL_FALSE, link_ok = GL_FALSE;

GLfloat triangle_vertices[] = {
0.0, 0.8,
-0.8, -0.8,
0.8, -0.8,
};
glGenBuffers(1, &vbo_triangle);
glBindBuffer(GL_ARRAY_BUFFER, vbo_triangle);
glBufferData(GL_ARRAY_BUFFER, sizeof(triangle_vertices), triangle_vertices, GL_STATIC_DRAW);

const char *vs_source =
//"#version 100\n" // OpenGL ES 2.0
"attribute vec2 coord2d;"
"attribute vec3 v_color;"
"varying vec3 f_color;"
"void main(void) {"
"gl_Position = vec4(coord2d, 0.0, 1.0);"
"f_color = v_color;"
"}";

vs=createShader(vs, GL_VERTEX_SHADER,vs_source);


const char *fs_source =
//"#version 100\n" // OpenGL ES 2.0
"void main(void){"
"gl_FragColor=3"
"}";

fs = createShader(fs, GL_FRAGMENT_SHADER, fs_source);

program = createAndLinkProgram(program, vs, fs);

const char* attribute_name = "coord2d";
attribute_coord2d = glGetAttribLocation(program, attribute_name);
if (attribute_coord2d == -1) {
cerr << "Could not bind attribute " << attribute_name << endl;
return false;
}

attribute_name = "v_color";
attribute_v_color = glGetAttribLocation(program, attribute_name);
if (attribute_v_color == -1) {
cerr << "Could not bind attribute " << attribute_name << endl;
return false;
}

return true;
}



void render(SDL_Window* window) {
/* Clear the background as white */
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glUseProgram(program);
glBindBuffer(GL_ARRAY_BUFFER, vbo_triangle);
glEnableVertexAttribArray(attribute_coord2d);
/* Describe our vertices array to OpenGL (it can't guess its format automatically) */
glVertexAttribPointer(
attribute_coord2d, // attribute
2, // number of elements per vertex, here (x,y)
GL_FLOAT, // the type of each element
GL_FALSE, // take our values as-is
0, // no extra data between each position
0 // pointer to the C array
&nbsp;);

/* Push each element in buffer_vertices to the vertex shader */
glEnableVertexAttribArray(attribute_v_color);
glBindBuffer(GL_ARRAY_BUFFER, vbo_triangle_colors);
glVertexAttribPointer(
attribute_v_color, // attribute
3, // number of elements per vertex, here (r,g,b)
GL_FLOAT, // the type of each element
GL_FALSE, // take our values as-is
0, // no extra data between each position
0 // offset of first element
&nbsp;);

glDrawArrays(GL_TRIANGLES, 0, 3);

glDisableVertexAttribArray(attribute_coord2d);
glDisableVertexAttribArray(attribute_v_color);

/* Display the result */
SDL_GL_SwapWindow(window);
}


void free_resources() {
glDeleteProgram(program);
glDeleteBuffers(1, &vbo_triangle);
}

void mainLoop(SDL_Window* window) {
while (true) {
SDL_Event ev;
while (SDL_PollEvent(&ev)) {
if (ev.type == SDL_QUIT)
return;
}
render(window);
}
}

int main(int argc, char* argv[]) {
/* SDL-related initialising functions */
SDL_Init(SDL_INIT_VIDEO);
SDL_Window* window = SDL_CreateWindow("My First Triangle",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
640, 480,
SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL);
SDL_GL_CreateContext(window);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 1);

/* Extension wrangler initialising */
GLenum glew_status = glewInit();
if (glew_status != GLEW_OK) {
cerr << "Error: glewInit: " << glewGetErrorString(glew_status) << endl;
return EXIT_FAILURE;
}

/* When all init functions run without errors,
the program can initialise the resources */
if (!init_resources())
return EXIT_FAILURE;

/* We can display something if everything goes OK */
mainLoop(window);

/* If the program exits in the usual way,
free resources and exit with a success */
free_resources();
return EXIT_SUCCESS;
}


And the extra functions in Extra.h

Extras.h


#pragma once
#include <GL\glew.h>
#include <SDL.h>


GLuint createShader(GLuint shader,GLenum type,const char*source);
GLuint createAndLinkProgram(GLuint program, GLuint shader_vs, GLuint shader_fs);
SDL_Window* BasicInit();


And heres the declaration of everything in Extras.h

Extras.cpp


#include "Extras.h"
#include <GL\glew.h>
#include <iostream>
#include <cstdlib>
#include <SDL.h>

GLint compile_ok = GL_FALSE;
GLint link_ok = GL_FALSE;
SDL_Window*window;

GLuint createShader(GLuint shader,GLenum type, const char* source) {
shader = glCreateShader(type);
glShaderSource(shader, 1, &source, NULL);
glCompileShader(shader);
glGetShaderiv(shader, GL_COMPILE_STATUS, &compile_ok);
if (compile_ok==GL_FALSE) {
std::cerr << "Failed to compile shader : " << shader << std::endl;
return NULL;
}

return shader;

}

GLuint createAndLinkProgram(GLuint program, GLuint shader_vs, GLuint shader_fs) {
program = glCreateProgram();
glAttachShader(program, shader_vs);
glAttachShader(program, shader_fs);
glLinkProgram(program);
glGetProgramiv(program, GL_LINK_STATUS, &link_ok);
if (!link_ok) {
std::cerr << "Failed to link program" << std::endl;
return NULL;
}
return program;
}


Please if anyone can help me...im very lost with opengl now

ohh...and im using an intel sandybridge graphics if this helps.
it uses opengl 3.0

Dark Photon
02-04-2016, 08:36 AM
paul188, this is not a debugging service. Please read the:

* Forum Posting Guidelines (https://www.opengl.org/discussion_boards/showthread.php/176139-Forum-Posting-Guidelines?p=1230021&viewfull=1#post1230021)

When you have specific OpenGL questions, feel free to ask.

Also, in the future, please surround source code with ... or
... blocks as it makes it readable.

mhagain
02-04-2016, 02:19 PM
In addition to what Dark Photon said:

Access violation executing location 0x00005 in sdl_opengl2.exe

This is nothing specifically to do with OpenGL. This is a C/C++ crash, and you just have a NULL pointer somewhere. You're using Visual Studio so you have the best C/C++ debugger available; learn to use it, because it's your friend. Run your program under the debugger and it will halt at the line of code causing this crash.