Access violation

Hello,

I have started using environment mapping in OpenGL.
But this kind of error occurs: Unhandled exception at 0x00000000 in EnvMap.exe: 0xC0000005: Access violation.

It showa that the error is in glsl lib ..in:
GLuint create_program(const char* vs, const char* fs)
{
GLuint prg = glCreateProgram();
if (vs)
{
GLuint vh = create_shader(GL_VERTEX_SHADER, vs);
glAttachShader(prg, vh);
}
if (fs)
{
GLuint fh = create_shader(GL_FRAGMENT_SHADER, fs);
glAttachShader(prg, fh);
}
glLinkProgram(prg);
int linked;
glGetProgramiv(prg, GL_LINK_STATUS, &linked);
if (linked == GL_FALSE)
{
int len;
glGetProgramiv(prg, GL_INFO_LOG_LENGTH, &len);
char* log = (char*)malloc(len);
glGetProgramInfoLog(prg, len, NULL, log);
printf(log); free(log); exit(-1);
}
return prg;
}


Here is my code:


#include "glsl_util.hpp"
#include <assert.h>

#include <GL/glut.h>

#include "Graphics/TrackBall.h"

using namespace CGLA;
using namespace GFX;

static TrackBall* ball = NULL;
static int width, height;

static GLuint cubemap;
static GLuint background;
static GLuint glass;
static GLuint normalmap;

void display() {

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// Set up modelview transformation
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
ball->do_spin();
ball->set_gl_modelview();

//get camera position
Vec3f eye, at, up;
ball->get_view_param(eye, at, up);

//draw skybox
//set texture unit 0 to use our cubemap
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_CUBE_MAP, cubemap);
//get location of samplerCube for the background
GLint locBk1 = glGetUniformLocation(background, "background");
assert(locBk1 != -1);
//activate the program
glUseProgram(background);
//tell OpenGL that samplerCube“background” should read from texture unit 0
glUniform1i(locBk1, 0);
//draw!

glutSolidCube(20.);

//draw object
glUseProgram(glass);
//grab location camera uniform
GLint locCam = glGetUniformLocation(glass, "camera");
assert(locCam != -1);
//enable glass program
glUseProgram(glass);
//pass the eye coordinates to the camera uniform
glUniform3f(locCam, eye[0], eye[1], eye[2]);


//glutSolidTeapot(1.f);
glutSolidSphere(1.f, 32, 32);
//glutSolidTorus(0.5, 1., 32, 32);

glUseProgram(0);
glutSwapBuffers();

check_gl_error();
}


int old_state=GLUT_UP;
void mouse(int button, int state, int x, int y)
{
if (old_state==GLUT_UP && state==GLUT_DOWN)
{
if (button==GLUT_LEFT_BUTTON)
ball->grab_ball(ROTATE_ACTION,Vec2i(x,y));
else if (button==GLUT_MIDDLE_BUTTON)
ball->grab_ball(ZOOM_ACTION,Vec2i(x,y));
else if (button==GLUT_RIGHT_BUTTON)
ball->grab_ball(PAN_ACTION,Vec2i(x,y));
}
if (old_state==GLUT_DOWN && state==GLUT_UP)
ball->release_ball();
old_state=state;
}

void motion(int x, int y)
{
if (old_state==GLUT_DOWN)
ball->roll_ball(Vec2i(x,y));
}

void keyboard(unsigned char key, int x, int y)
{
switch(key)
{
case '\033': exit(0); break;
}
}

void animate(int x)
{
glutPostRedisplay();
glutTimerFunc(10, animate, 0);
}

void reshape(int w, int h)
{
width = w;
height = h;

glViewport(0, 0, width, height);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(65.f, float(width)/height, 0.1, 50.);
glMatrixMode(GL_MODELVIEW);

delete ball;
ball = new TrackBall(Vec3f(0), 5.f, 1.f, width, height);
}

void init(void)
{
glClearColor(1,0,0,0);
glEnable(GL_DEPTH_TEST);

//create cube map
glEnable(GL_TEXTURE_CUBE_MAP);

const char* cube_fn[] = { "textures/cm_left.ppm",
"textures/cm_right.ppm",
"textures/cm_top.ppm",
"textures/cm_bottom.ppm",
"textures/cm_back.ppm",
"textures/cm_front.ppm" };

glGenTextures(1, &cubemap);
glBindTexture(GL_TEXTURE_CUBE_MAP, cubemap);
for (int i=0; i<6; ++i)
{
int width, height;
void* data = load_ppm(cube_fn[i], width, height);
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i,
0, //level
GL_RGB8, //internal format
width, //width
height, //height
0, //border
GL_RGB, //format
GL_UNSIGNED_BYTE, //type
data); //pixel data
free(data);
}

glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);


//TODO

//create normalmap
const char* normalmap_fn = "textures/normalmap.ppm";
glGenTextures(1, &normalmap);
glBindTexture(GL_TEXTURE_2D, normalmap);
int width, height;
void* data = load_ppm(normalmap_fn, width, height);
glTexImage2D(GL_TEXTURE_2D,
0, //level
GL_RGB8, //internal format
width, //width
height, //height
0, //border
GL_RGB, //format
GL_UNSIGNED_BYTE, //type
data); //pixel data
free(data);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
//TODO

//create shaders
background = create_program("background.vert", "background.frag");
glass = create_program("glass.vert", "glass.frag");

check_gl_error();
}

int main(int argc, char** argv)
{
glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE|GLUT_DEPTH);
glutInitWindowSize(768, 768);
glutInit(&argc, argv);
glutCreateWindow("02561-10: Environment Mapping and Bump Mapping");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutTimerFunc(10, animate, 0);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutReshapeFunc(reshape);

glewInit();

init();

glutMainLoop();
return 0;
}

Thank you! :)
Topic archived. No new replies allowed.