Sign in to follow this  
rm2000

OpenGL Beginning to work with GLSL

Recommended Posts

I want to begin working with GLSL to create object shaders. I started at Lighthouse3D Tutorials, downloading the GLSL ARB Extensions Syntax VC code, which compiles fine. http://www.lighthouse3d.com/opengl/glsl/index.php?minimal There are two lines: vs = textFileRead("minimal.vert"); fs = textFileRead("minimal.frag"); which appears to attach the vertex and fragment shaders to the rendered model in the Visual Studio project. As I began testing, I downloaded TyphoonLabs Shader Designer and opened, modified a basic lighting shader. I noticed the Shader Designer also had several other predesigned shader projects, with vert and frag files. However, if I just change the text file parsing to read the sample files (i.e. vs = textFileRead("stripes.vert");) it doesn't work -- the model stays a single, black entity. The Shader Designer syntax looks pretty straight-forward, but how can I get this implemented into a Visual Studio OpenGL project?

Share this post


Link to post
Share on other sites
There's something you should take into account when programming with GLSL, for example the function glShaderSource takes as input the actual shader code as a C-string (i.e. null terminated).

Perhaps the textFileRead() function in Lighthouse3D tutorial is reading the file in some different way.

Share this post


Link to post
Share on other sites
I'd use ATI's RenderMonkey rather than Shader Designer - much better - got me started on GLSL in a few hours :)

Share this post


Link to post
Share on other sites
Thank you for your help so far.

I downloaded RenderMonkey, put a model in and added some effects. I then pulled up the vert and frag code windows, copied and pasted the code into .frag and .vert files and tried again. Still no luck. I tried opening the lighthouse code and using these files and the teapot still shows up all black, and I thought it would have my texture, lighting effects in RenderMonkey.

I understand how to load 3ds files, models, etc. using OpenGL and I think I understand how these editors work. But I am missing the connection between the two, it seems.

and didn't know if anyone want to take a look. I'm either (a) not exporting the shader information correctly or (b) not understanding how lighthouse loads a shader and applies it to a model. Can't seem to find any RenderMonkey GLSL tutorial anywhere...

[Edited by - rm2000 on March 12, 2007 11:24:42 AM]

Share this post


Link to post
Share on other sites
You are not passing the "uniform" and "varying" variables to the shader.

I'm pretty sure lighthouse3D explains what they are and how to use them.

Share this post


Link to post
Share on other sites
Thank you GamerYZ that seemed to be the problem.

An additional question if I may, I'm trying to get this to compile (now) using Code:Blocks (MINGW) instead of Visual Studio. I keep getting linker errors:

i.e.

undefined reference to '_imp____glewCreateShaderObjectARB'
undefined reference to '_imp____glewCreateShaderObjectARB'
undefined reference to '_imp____glewCreateShaderObjectARB'
undefined reference to '_imp____glewShaderSourceARB'

...etc...etc

I have included glew.h and glut.h into the file as an include and I have set my link libraries to:

libopengl32.a
libwinmm.a
libglu32.a
libglut.a
libglu32.a
glew32s.lib
glew32.lib

glew32.dll has been copied to %SYSROOT% and to the codeblocks lib folder

(I use some of the other libraries for other parts of my code). Am I missing anything else?

Thanks again

Share this post


Link to post
Share on other sites
Are you initializing glew and making sure glew knows where the functions are (if i remember correctly the glew documentation goes into detail on getting entry points to the correct functions);

Share this post


Link to post
Share on other sites
Everything, code-wise, seems to be okay. The same project builds fine under Visual Studio. I did notice the glew lib extensions are .lib where the ones for codeblocks are .a.

Share this post


Link to post
Share on other sites
Setting up the extensions isn't at all hard to do manually - I'd try it yourself rather than using GLEW, and then you know exactly what's going on rather than relying on the GLEW 'magic'.

Share this post


Link to post
Share on other sites
Maybe I'll try giving that a shot. Any good sites for reference? I was able to finally get it to build by attaching the glew32.dll file (not the library). It doesn't make a whole lot of sense to me why the dll worked.

If I may continue the conversation further, do I need to use GLUT to use these extensions? I was able to set up a window using GLUT and such, but if I draw a Window and use just OpenGL I get the no GLSL support on the following:

glewInit();

if (GLEW_ARB_vertex_shader && GLEW_ARB_fragment_shader)

Much thanks.

Share this post


Link to post
Share on other sites
Heh, most sites say to use GLEW or similar so they can concentrate on the GLSL more. In summary what I do is (windows code):



#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include "glext.h" // Make sure its a later edition header.
...

// Shader extensions: (upper case defs are in glext.h)
PFNGLCREATESHADEROBJECTARBPROC glCreateShaderObjectARB = 0;
PFNGLSHADERSOURCEARBPROC glShaderSourceARB = 0;
// so on for all required fns...

void CheckGlExtensions()
{
char* supportedExtensionsString = (char *) glGetString(GL_EXTENSIONS);

if (strstr(supportedExtensionsString, "GL_ARB_shader_objects"))
{
if (strstr(supportedExtensionsString, "GL_ARB_shading_language_100"))
{
LOG("GL_ARB_shading_language_100 supported");

glCreateShaderObjectARB = (PFNGLCREATESHADEROBJECTARBPROC)wglGetProcAddress("glCreateShaderObjectARB");
glShaderSourceARB = (PFNGLSHADERSOURCEARBPROC)wglGetProcAddress("glShaderSourceARB");
// so on for all required fns...
}
}
}

...

// Now the ARB functions are ready for use.






GLUT is a separate wrapper framework for quick use of OpenGL fixed pipeline stuff (not shaders last time I looked at it).

Ed: Looks like GLEW might depend on GLUT though...

[Edited by - Aph3x on March 14, 2007 8:18:08 AM]

Share this post


Link to post
Share on other sites
Thanks.

How does your line:

glCreateShaderObjectARB = (PFNGLCREATESHADEROBJECTARBPROC)wglGetProcAddress("glCreateShaderObjectARB");
glShaderSourceARB = (PFNGLSHADERSOURCEARBPROC)wglGetProcAddress("glShaderSourceARB");

compare to:

v = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
f = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
f2 = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);

would this code also go in the latter if block in your code:

const char * vv = vs;
const char * ff = fs;

glShaderSourceARB(v, 1, &vv,NULL);
glShaderSourceARB(f, 1, &ff,NULL);

free(vs);free(fs);

glCompileShaderARB(v);
glCompileShaderARB(f);

p = glCreateProgramObjectARB();
glAttachObjectARB(p,v);
glAttachObjectARB(p,f);

glLinkProgramARB(p);

glUseProgramObjectARB(p);


I have a procedure (which I pretty much modified from lighthouse during my testing phase) which is called setshaders and I call it when the program loads. I want to apply it to a model or primitive that I've already loaded into my code (that much I have done). I'm also not so sure your code will work with mingw..?

Share this post


Link to post
Share on other sites
My lines there are just setting up the function pointers to the ARB extensions. This is what things like GLEW do for you.
Your code there seems to be actually *using* the functions to create a shader object.
You have to compile and create a shader program, then 'use' it before rendering the object in question. When you use the program, the programmable pipeline kicks in for the consecutively submitted geometry. Don;t forget to UseProgram(0) when finished (return to fixed pipeline).

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