Sign in to follow this  

OpenGL Setting up GLSL vertex and fragment shaders

Recommended Posts

Hi! I just tried to setup glsl but ran in some problems with it. Or running my vertex shader is actually the problem. It seems that fragment shader works but vertex shader is complitely skipped. I draw my object like this:


  GLUquadric* sphere = gluNewQuadric();

  glColor3f( 0.6f, 0.2f, 0.2f );
  gluSphere(sphere, 2.f, 12, 12);

varying float diffuse_value; // using this in fs

void main() {			
	diffuse_value = 1.0; // assign it to 1.0 so that theres no color change
	gl_FrontColor = gl_Color; 
	gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
varying float diffuse_value;

void main() {
        // diffuse value should be 1.0 now, but I get complitely black object.
	gl_FragColor = gl_Color * diffuse_value; 
So I should be able to adjust the color via diffuse_value variable, but it has no effect, no matter what I assign it to.. it get as zero to fragment shader. heres my shader init code:
#include "glew.h"
#include "gl.h"
#include "glu.h"
void shader_init(){

	glewInit( );
	if( GLEW_ARB_vertex_shader && GLEW_ARB_fragment_shader )
		std::cout << "GLSL OK!" << std::endl;
		std::cout << "GLSL NOT OK.." << std::endl;
        const char * vsText = readFile( "myshader.vert" );
	const char * fsText = readFile( "myshader.frag" );
	shader_vs = glCreateShaderObjectARB( GL_VERTEX_SHADER_ARB );
	shader_fs = glCreateShaderObjectARB( GL_FRAGMENT_SHADER_ARB );

	glShaderSourceARB( shader_vs, 1, &vsText, NULL );
	glShaderSourceARB( shader_fs, 1, &fsText, NULL );

	glCompileShaderARB( shader_vs );
	glCompileShaderARB( shader_fs );

	shader_p = glCreateProgramObjectARB( );

	glAttachObjectARB( shader_p, shader_vs );
	glAttachObjectARB( shader_p, shader_fs );

	glLinkProgramARB( shader_p );
	glUseProgramObjectARB( shader_p );
I also checked with debugger that shader files are readed in alright. Any Idea what might cause this? What libs do you use, you prefer using shaders with glut? Because the only difference I find between my project and tutorial projects is that they use glut and I dont.. I'm following these tutorials: NOTE: Few days ago I tried cg shader and it worked ok, so my gfx-card should be ok. Theres just something I am missing here. thanks Patrick

Share this post

Link to post
Share on other sites
I don't see anything obviously wrong with it, though I only have basic familiarity with GLSL. Have you tried reading back the info log from the compiler?


glGetShader(shaderID, GL_COMPILE_STATUS, error); // tells you if there is an error

glGetShader(shaderID, GL_INFO_LOG_LENGTH, length); // length of log, needed for reading it back
glGetShaderInfoLog(shaderID, length, infoLog); // read back info log

Also glGetProgram() and glGetProgramInfoLog() do the same for the program itself, so do that one also.

Share this post

Link to post
Share on other sites
Thanks lightbringer,

I tried using glGetShader, and glGetProgram. I checked for compile status and it was ok for both. I also made by purpose a small error in vertex shader program to check that I get the error from glGetShader and yes, I got it. So at least it is compiled. I just do not understand why the vertex shader seems to be complitely ignored..

Share this post

Link to post
Share on other sites
I tried drawing different kinds of objects, single triangles, quads, everything, but no help..

But, I actually got it working.. don't know why but I use this code snippet instead and it worked out good:

static void setShaders(){
GLuint p,s;
p = ((PFNGLCREATEPROGRAMPROC)wglGetProcAddress("glCreateProgram"))();
s = ((PFNGLCREATESHADERPROC)(wglGetProcAddress("glCreateShader")))(GL_VERTEX_SHADER);
((PFNGLSHADERSOURCEPROC)wglGetProcAddress("glShaderSource")) (s, 1, &vsh, NULL);
((PFNGLATTACHSHADERPROC)wglGetProcAddress("glAttachShader")) (p,s);

s = ((PFNGLCREATESHADERPROC)wglGetProcAddress("glCreateShader"))(GL_FRAGMENT_SHADER);
((PFNGLSHADERSOURCEPROC)wglGetProcAddress("glShaderSource")) (s, 1, &fsh, NULL);
((PFNGLATTACHSHADERPROC)wglGetProcAddress("glAttachShader")) (p,s);

((PFNGLUSEPROGRAMPROC) wglGetProcAddress("glUseProgram"))(p);

Its the same code but packed in a small size, I copied it from here for testing purposes:

In this solution I am not using glew at all, I am doing the nasty setuping myself, using only the functions I need.
It seems that glew does(or doesnt do) something it should. Maybe it really needs glut to cooperate with? But I'd rather not use glut since it's horribly outdated nowadays.

Using GL extensions(new functionality) properly isn't the most well documented area, if anyone has suggestions for good sources of information for the topic in question, please tell. (Books, tutorial pages, everything appreciated).



Share this post

Link to post
Share on other sites
I've never had to use GLUT or anything, but then LWJGL spoils me with wrapper classes like GL11, GL20, GL30... not like you C++ on Windows guys who have to struggle with having only 1.1 headers. :D to be honest I find what happened in your case pretty weird, but hey, if it works now, no need to fix it (for now). You'll be rewriting those shaders soon enough anyway, if you plan to support GLSL 1.5.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this