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;
// 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]);
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;
}
}