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:


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


vs:
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;
}

fs:
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"

glewInit( );
std::cout << "GLSL OK!" << std::endl;
else{
std::cout << "GLSL NOT OK.." << std::endl;
return;
}

}

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: http://www.lighthouse3d.com/opengl/glsl/index.php?oglexample1 http://www.swiftless.com/tutorials/glsl/1_setup/1_setup.html#DL 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 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?

Pseudocode:
glGetShader(shaderID, GL_COMPILE_STATUS, error); // tells you if there is an errorglGetShader(shaderID, GL_INFO_LOG_LENGTH, length); // length of log, needed for reading it backglGetShaderInfoLog(shaderID, length, infoLog); // read back info log

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

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 on other sites
This is just a random suggestion, but try drawing a triangle yourself instead of using gluSphere, maybe it's unsetting some of your global state.

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);  ((PFNGLCOMPILESHADERPROC)wglGetProcAddress("glCompileShader"))(s);  ((PFNGLATTACHSHADERPROC)wglGetProcAddress("glAttachShader")) (p,s);  s = ((PFNGLCREATESHADERPROC)wglGetProcAddress("glCreateShader"))(GL_FRAGMENT_SHADER);   ((PFNGLSHADERSOURCEPROC)wglGetProcAddress("glShaderSource")) (s, 1, &fsh, NULL);  ((PFNGLCOMPILESHADERPROC)wglGetProcAddress("glCompileShader"))(s);  ((PFNGLATTACHSHADERPROC)wglGetProcAddress("glAttachShader")) (p,s);     ((PFNGLLINKPROGRAMPROC)wglGetProcAddress("glLinkProgram"))(p);  ((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).

thanks

p

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.

Create an account

Register a new account

• Partner Spotlight

• Forum Statistics

• Total Topics
627658
• Total Posts
2978474
• Similar Content

• Both functions are available since 3.0, and I'm currently using glMapBuffer(), which works fine.
But, I was wondering if anyone has experienced advantage in using glMapBufferRange(), which allows to specify the range of the mapped buffer. Could this be only a safety measure or does it improve performance?
Note: I'm not asking about glBufferSubData()/glBufferData. Those two are irrelevant in this case.
• By xhcao
Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness.
• By cebugdev
hi guys,
are there any books, link online or any other resources that discusses on how to build special effects such as magic, lightning, etc. in OpenGL? i mean, yeah most of them are using particles but im looking for resources specifically on how to manipulate the particles to look like an effect that can be use for games,. i did fire particle before, and I want to learn how to do the other 'magic' as well.
Like are there one book or link(cant find in google) that atleast featured how to make different particle effects in OpenGL (or DirectX)? If there is no one stop shop for it, maybe ill just look for some tips on how to make a particle engine that is flexible enough to enable me to design different effects/magic
let me know if you guys have recommendations.
• By dud3
How do we rotate the camera around x axis 360 degrees, without having the strange effect as in my video below?
Mine behaves exactly the same way spherical coordinates would, I'm using euler angles.
Tried googling, but couldn't find a proper answer, guessing I don't know what exactly to google for, googled 'rotate 360 around x axis', got no proper answers.

References:
Code: https://pastebin.com/Hcshj3FQ
The video shows the difference between blender and my rotation:

• By Defend
I've had a Google around for this but haven't yet found some solid advice. There is a lot of "it depends", but I'm not sure on what.
My question is what's a good rule of thumb to follow when it comes to creating/using VBOs & VAOs? As in, when should I use multiple or when should I not? My understanding so far is that if I need a new VBO, then I need a new VAO. So when it comes to rendering multiple objects I can either:
* make lots of VAO/VBO pairs and flip through them to render different objects, or
* make one big VBO and jump around its memory to render different objects.
I also understand that if I need to render objects with different vertex attributes, then a new VAO is necessary in this case.
If that "it depends" really is quite variable, what's best for a beginner with OpenGL, assuming that better approaches can be learnt later with better understanding?

• 10
• 12
• 22
• 13
• 33