LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-13-2011, 01:05 PM   #1
fcdev
Member
 
Registered: Sep 2005
Posts: 47

Rep: Reputation: 15
Post extern variables in GLSL


I'm currently writing an OpenGL application with a lot of shaders in it, and amongst those shaders, most of them use the same lighting algorithm. I read in the OpenGL Shading Language that you can link multiple shaders of the same type (ie: multiple fragment shaders) into a single shader program, unfortunately, I can't find anywhere for those fragment shaders to share global variables.

I'm using Opengl 2.0 at the moment, and due to the target platform, moving to OpenGL 3 or above is not currently an option.

What I have is a lighting shader containing two functions ... initLighting and applyLighting. InitLighting initializes the lighting variables, and applyLighting will add a single light source to the lighting variables. Then I have the main function which applies the texturing.

Since the lighting functions are used in almost every shader, I want them to be compiled into their own lighting shader, and linked with the main function (as opposed to what I'm doing now which is including the source code for the lighting into every fragment shader, which is causing really long processing times for compiling shaders). The problem is that I can't find any references on how to share the global lighting variables between the lighting shader and the one that applies texturing.

Here's some simplified pseudocode of what I'm trying to do

Lighting Fragment Shader
Code:
vec4 lightDiffuse, lightSpecular;

void applyLighting(int lightID)
{ ...
  lightDiffuse += diffuseCalculation;
  lightSpecular += specularCalculation;
}

void initLighting(void)
{ lightDiffuse=vec4(0,0,0,0);
  lightSpecular=vec4(0,0,0,0);
  applyLighting(0);                // Add light source 0
  applyLighting(1);                // Add light source 1
}
Texturing Fragment Shader
Code:
void initLighting(void);
extern vec4 lightDiffuse, lightSpecular;  // This is what I'm trying to get to work

void main(void)
{ initLighting();
  lightDiffuse *= texture2D(tex0, gl_TexCoord[0].xy);
  lightSpecular *= texture2D(tex1, gl_TexCoord[1].xy);
  gl_FragColor = lightDiffuse + lightSpecular;
}
Any help would be greatly appreciated.
 
Old 11-13-2011, 01:19 PM   #2
fcdev
Member
 
Registered: Sep 2005
Posts: 47

Original Poster
Rep: Reputation: 15
I found the problem, apparently the problem was a bug in my linking code which prevented the lighting shader being linked in, which prevented my experiments from working correctly.
If anyone else is interested in the solution, inside the texturing fragment shader, leave out the 'extern' token, and just declare the variables again. Apparently they're automatically shared between the two fragment shaders.
 
Old 11-14-2011, 07:59 AM   #3
resetreset
Senior Member
 
Registered: Mar 2008
Location: Cyberspace
Distribution: Dynebolic, Ubuntu 10.10
Posts: 1,340

Rep: Reputation: 62
Hey,
I'm writing not because of anything to do with your problem (I couldn't follow the discussion), but I just want to know - how do you do OpenGL programming in Linux? If I have a Ubuntu box with the proprietary Nvidia driver, is that enough? Or do I need to download Mesa, the Open Source stuff?

By the way, what are you writing? A game?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: OpenGL 4.2 Specification Published With GLSL 4.20 LXer Syndicated Linux News 0 08-08-2011 04:00 PM
GLSL Shader Dev IDE needed Simon Bridge Linux - Software 6 06-08-2009 10:04 PM
LXer: OpenGL 3.0, GLSL 1.30 Released LXer Syndicated Linux News 0 08-11-2008 11:20 PM
Linux 2.5.69 extern global variables odomae Linux - Newbie 1 06-24-2003 04:03 PM
extern variables in c++ abi_sh Programming 1 08-11-2002 01:42 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:10 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration