simpleShaderExample.cpp
...
int installBrickShaders(const GLchar *brickVertex, const GLchar *brickFragment)
{
GLuint brickVS, brickFS, brickProg; // handles to objects
GLint vertCompiled, fragCompiled;
GLint linked;
// Create a vertex shader object and a fragment shader object
brickVS = glCreateShader(GL_VERTEX_SHADER);
brickFS = glCreateShader(GL_FRAGMENT_SHADER);
// load source code strings into shaders
glShaderSource(brickVS, 1, &brickVertex, NULL);
glShaderSource(brickFS, 1, &brickFragment, NULL);
// Coompile the brick vertex shader and print out
// the compiler log file
glCompileShader(brickVS);
printOpenGLError();
glGetShaderiv(brickVS, GL_COMPILE_STATUS, &vertCompiled);
printShaderInfoLog(brickVS);
// Compile the brick fragment shader and print out
// the compiler log file
glCompileShader(brickFS);
printOpenGLError();
glGetShaderiv(brickFS, GL_COMPILE_STATUS, &fragCompiled);
printShaderInfoLog(brickFS);
if(!vertCompiled || !fragCompiled)
return 0;
// Create a program object and attach the two compiled shaders
brickProg = glCreateProgram();
glAttachShader(brickProg, brickVS);
glAttachShader(brickProg, brickFS);
// Bind user-defined in variable to vertex attribute
glBindAttribLocation(brickProg, 1, "MCvertex");
glBindAttribLocation(brickProg, 2, "MCnormal");
// Bind user-defined out variable to data buffer
glBindFragDataLocation(brickProg, 0, "FragColor");
// Link the program object and print out the info log
glLinkProgram(brickProg);
printOpenGLError();
glGetProgramiv(brickProg, GL_LINK_STATUS, &linked);
printProgramInfoLog(brickProg);
// Install program object as part of current state
glUseProgram(brickProg);
// Set up initial uniform values
glUniform3f(getUniLoc(brickProg, "BrickColor"), 1.0, .3, .2);
glUniform3f(getUniLoc(brickProg, "MortarColor"), .85, .86, .84);
glUniform2f(getUniLoc(brickProg, "BrickSize"), .3, .15);
glUniform2f(getUniLoc(brickProg, "BrickPct"), .90 , .85);
glUniform3f(getUniLoc(brickProg, "LightPosition"), .0, .0, 4.0);
}
...
void main(int argc, char **argv)
{
int success = 0;
int gl_major, gl_minor;
GLchar *VertexShaderSource, *FragmentShaderSource;
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize( 500, 500);
window = glutCreateWindow( "3Dlabs Brick Shader" );
glutIdleFunc( play );
glutDisplayFunc( display );
glutKeyboardFunc( key );
glutReshapeFunc( reshape );
glutMotionFunc( motion );
glutMouseFunc( mouse );
glutSpecialFunc( special );
glutTimerFunc(TIMER_FREQUENCY_MILLIS, timer, 0);
// Initialize the "OpenGL Extension Wrangler" library
glewInit();
// Make sure that OpenGL 2.0 is supported by the driver
getGlVersion(&gl_major, &gl_minor);
printf("GL_VERSION major=%d minor=%d\n", gl_major, gl_minor);
if(gl_major < 2)
{
printf("GL_VERSION major=%d minor=%d\n", gl_major, gl_minor);
printf("Support for OpenGL 2.0 is required for this demo... exiting\n");
exit(1);
}
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
NextClearColor();
key('?', 0, 0); // display help
readShaderSource("shader", &VertexShaderSource, &FragmentShaderSource);
success = installBrickShaders(VertexShaderSource, FragmentShaderSource);
if(success)
glutMainLoop();
}
shader.vert
#version 140
in vec4 MCvertex;
in vec3 MCnormal;
uniform mat4 MVMatrix;
uniform mat4 MVPMatrix;
uniform mat3 NormalMatrix;
uniform vec3 LightPosition;
const float SpecularContribution = .3;
const float DiffuseContribution = 1. - SpecularContribution;
out float LightIntensity;
out vec2 MCposition;
void main()
{
vec3 ecPosition = vec3(MVMatrix * MCvertex);
vec3 tnorm = normalize(NormalMatrix * MCnormal);
vec3 lightVec = normalize(LightPosition - ecPosition);
vec3 reflectVec = reflect(-lightVec, tnorm);
vec3 viewVec = normalize(-ecPosition);
float diffuse = max(dot(lightVec, tnorm), 0.);
float spec = 0.;
if(diffuse > 0.)
{
spec = max(dot(reflectVec,viewVec), 0.);
spec = pow(spec, 16.);
}
LightIntensity = DiffuseContribution * diffuse +
SpecularContribution * spec;
MCposition = MCvertex.xy;
gl_Position = MVPMatrix * MCvertex;
}
shader.frag
#version 140
uniform vec3 BrickColor, MortarColor;
uniform vec2 BrickSize;
uniform vec2 BrickPct;
in vec2 MCposition;
in float LightIntensity;
out vec4 FragColor;
void main()
{
vec3 color;
vec2 position, useBrick;
position = MCposition / BrickSize;
if (fract(position.y * .5) > .5)
position.x += .5;
position = fract(position);
useBrick = step(position, BrickPct);
color = mix(MortarColor, BrickColor, useBrick.x * useBrick.y);
color *= LightIntensity;
FragColor = vec4(color, 1.);
}
When the program reaches simpleShaderExample.cpp::::glBindFragDataLocation(brickProg, 0, "FragColor"); it gets an Unhandled Exception
Unhandled exception at 0x00000000 in emptyExample.exe: 0xC0000005: Access violation.
I have looked through google and I can't find any solution hope you can help me on what to do because I'm badly stuck in this error.
Thank you very much for your help






