Jump to content
  • Advertisement
Sign in to follow this  
alejandro

OpenGL [GLSL] Problem with Simple Shading Example

This topic is 2678 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I just got the 3rd Ed of the Orange book (learning GLSL) and I have some problems running the first example :(. The code is as follows:

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

Share this post


Link to post
Share on other sites
Advertisement
Never thought of that :blink: so I did a check with OpenGL Extensions Viewer 4.02 and I got the following report (for a complete report)


Renderer: GeForce GT 240/PCI/SSE2
Vendor: NVIDIA Corporation
Memory: 1024 MB
Version: 3.3.0
Shading language version: 3.30 NVIDIA via Cg compiler
...
Extension verification:
GL_APPLE_vertex_array_object was not found, but has the entry point glBindFragDataLocationIndexed
...
GL_VERSION_3_0 was not found, but has the entry point glBindFragDataLocation
[/quote]

I look at the API for the glBindFragDataLocation and it appears that's supported on the GLSL version that I have, so I don't know what's really happening :(

Any suggestions?

Share this post


Link to post
Share on other sites
Your card may support it, but maybe it was missed by whatever extension linker you are using. Are you using GLEW? Can you make sure that it is loading glBindFragDataLocation?

Maybe you can try using glBindFragDataLocationExt instead.

Share this post


Link to post
Share on other sites

Are you using GLEW?

Yes, glew 1.6


Can you make sure that it is loading glBindFragDataLocation?

I'm not sure how can I do that, but if I go to the definition on VS 2010 I found the header definition of the function


Maybe you can try using glBindFragDataLocationExt instead.

It worked, at least no compile errors, though I don't see anything :(

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!