Jump to content
  • Advertisement
Sign in to follow this  
mikenovemberoscar

OpenGL GLSL Shaders?

This topic is 2765 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 recently wanted to dive into the realms of shader programming in GLSL on my OpenGL based application.
I borrowed some of [color="#000000"]swiftless' code on GLSL, and this works for simple stuff, like void main(void)
{

gl_FragColor = vec4(1.0,1.0,0.0,1.0);


}


but if I try to copy someone else's shader program (this was one for fog I found on the internet) to test if this really works errors pop up:

[source = cpp] [font="Courier"][Session started at 2011-05-02 11:03:37 +0100.][/font]********** VERTEX SHADER **********

varying float atten;

varying float fogFactor;

varying vec3 lightVec, viewVec;

attribute vec3 tangent;




void main(void)

{

gl_Position = ftransform();

gl_TexCoord[0] = gl_MultiTexCoord0.xy;



vec3 n = normalize(gl_NormalMatrix * gl_Normal);

vec3 t = normalize(gl_NormalMatrix * tangent);

vec3 b = cross(n, t);



vec3 vVertex = vec3(gl_ModelViewMatrix * gl_Vertex);

vec3 vLVec = gl_LightSource[0].position.xyz - vVertex;



atten = 1.0 / (1.0 + 0.00001 * dot(vLVec, vLVec));



vec3 vVec = -vVertex;

viewVec.x = dot(vVec, t);

viewVec.y = dot(vVec, b);

viewVec.z = dot(vVec, n);



lightVec.x = dot(vLVec, t);

lightVec.y = dot(vLVec, b);

lightVec.z = dot(vLVec, n);



const float LOG2 = 1.442695;

gl_FogFragCoord = length(vVertex);

fogFactor = exp2( -gl_Fog.density *

gl_Fog.density *

gl_FogFragCoord *

gl_FogFragCoord *

LOG2 );

fogFactor = clamp(fogFactor, 0.0, 1.0);

}




********** FRAGMENT SHADER **********




varying float atten;

varying float fogFactor;

varying vec3 lightVec, viewVec;

uniform sampler2D normalMap;

uniform sampler2D colorMap;

uniform sampler2D colorMap2;




void main (void)

{

vec3 lVec = normalize(lightVec);

vec3 vVec = normalize(viewVec);



vec4 base = texture2D(colorMap, gl_TexCoord[0]*2.0);

vec3 bump = normalize(texture2D(normalMap,

gl_TexCoord[0]*2.0).xyz*2.0-1.0);

vec4 base2 = texture2D(colorMap2, gl_TexCoord[0]*4.0);




float diffuse = max( dot(lVec, bump), 0.0 );

float specular = pow(clamp(dot(reflect(-vVec, bump), lVec), 0.0, 1.0),

gl_FrontMaterial.shininess );



vec4 vAmbient = gl_FrontLightProduct[0].ambient * base + (base2*0.4);

vec4 vDiffuse = gl_FrontLightProduct[0].diffuse * diffuse * base;

vec4 vSpecular = gl_FrontLightProduct[0].specular * specular;



vec4 finalColor = (vAmbient + vDiffuse + vSpecular) * atten;



gl_FragColor = mix(gl_Fog.color, finalColor, fogFactor );

}




shader 1 (test_vertex_shader.sfvs) compile error: ERROR: 0:9: 'assign' : cannot convert from '2-component vector of float' to '4-component vector of float'




shader 2 (glow.sffs) compile error: ERROR: 0:14: 'texture2D' : no matching overloaded function found

ERROR: 0:14: '=' : cannot convert from 'const float' to '4-component vector of float'

ERROR: 0:16: 'texture2D' : no matching overloaded function found

ERROR: 0:16: 'xyz' : field selection requires structure or vector on left hand side

ERROR: 0:15: '=' : cannot convert from 'const float' to '3-component vector of float'

ERROR: 0:17: 'texture2D' : no matching overloaded function found

ERROR: 0:17: '=' : cannot convert from 'const float'[/source]


Why is this happening? Is GLSL not general - purpose and cross application?
Considering my end goal would be to create a fullscreen 'glow' effect, how could I go about doing this? (I have to save the current screen to a texture then blur, add to the final scene, then render a fullscreen quad?)
Is there a complete reference for GLSL and how to use it in OpenGL?
How does GLSL work with OpenGL, is it a post-processing thing or what?

Thanks


mikey

Share this post


Link to post
Share on other sites
Advertisement
Thanks, but I already had read that particular document, and it still seems too compilcated! I'm not actually sure what exactly a shader is, how does it work, when is it 'applied', how is it applied, etc?

Share this post


Link to post
Share on other sites
GLSL is reasonably general purpose, but it's the same as anything else in computing - garbage in, garbage out. A shader has specifically defined inputs which much be provided by your program's code; if you don't provide them then they may have default or undefined values.

Share this post


Link to post
Share on other sites
GL_TEXTURE_2D

texture2D( texture, texCoord.st )

GL_TEXTURE_2D_ARRAY

texture2DArray( texture, texCoord.stp )

notice the coordinates are vec2 and vec3 respectively
that shader you just posted is full of mistakes :)
i can help you fix them, but youll learn alot from fixinf it yourself!

Share this post


Link to post
Share on other sites
Ah OK thanks, but I can't seem to find a complete reference for GLSL, They are all either constant lists or other stuff. This also leads back to the original question, what can be done in GLSL, what does it do? Should I write a program to play PONG in a quad? Is GLSL ONLY applied to textures or the whole screen?

Share this post


Link to post
Share on other sites
GLSL is the Shader language of OpenGL. Shaders in general let u calculate anything u want on the GPU, of course with an focus on graphic stuff. You can do with them anything u want like filters, Lighting effects, modifying geometry, ray tracing. With them u specify what should be display on your screen. With OpenGL u always render to a RenderTartget, if this target is a Texture or the screen depends what u want to do.

I would suggest u to do some OpenGL 3 tutorials, they will explain all the necessary stuff what u need to know.

Share this post


Link to post
Share on other sites
So I feed geometry, textures, etc to OpenGL, which creates a 2D texture which is then either rendered on the screen or modified using shaders, then rendered on the screen?

( glVertex, glVertex -> texture -> screen) ??
( glVertex, glVertex -> texture -> shader -> screen ) or??

But then how do vertex shaders work?

I'll have a squint at some OpenGL 3 Tutorials, thanks

Share this post


Link to post
Share on other sites
The code was likely written on a relaxed rules video card such as most modern Nvidia cards. The code has type mismatched errors all through. For instance, the texture error that says "ERROR: 0:16: 'texture2D' : no matching overloaded function found " is happening because a texture map in shader programming is a vec4, but the normals are vec3. To fix this particular error : add .xyz at the end of the function ----->vec3 bump = normalize(texture2D(normalMap, gl_TexCoord[0]*2.0).xyz*2.0-1.0).xyz;<----- This feeds only the first three parameters to the vec3 bump variable, eliminating one of your errors.

Share this post


Link to post
Share on other sites
a shader is a method for directly manipulating the (i) vertices of a model, (ii) the individual fragments of the model , as well as the texture map coordinates Once you format a model into the proper form and send it to the video card using openGL calls you can now alter the model on a much more subtle basis. Shader programs alter the lighting characteristics of the model by using lighting formulas which can be reconfigured and re-written so far as your ability and imagination allow. The vertex processor is also capable of moving the vertices of a model individually. For a few quick examples download the CG toolkit from nVidia ->> http://developer.nvidia.com/ The language is mostly the same as glsl but with some setup differences. You should study both cg as well glsl since they are compatible with one another in the same program and all the basic concepts are the same. Also visit http://www.codesampler.com/ , as well as http://nehe.gamedev.net/. Nehe uses mostly fixed function lighting but is full of excellent openGL material. Code sampler has many shader programs. Be sure to check the [font=Verdana, Arial, Helvetica, sans-serif][size=2]User Samples [/font] at the bottom of the openGL section. Look for the following-->> It will help get the point across of shaders[font=Verdana, Arial, Helvetica, sans-serif]

[/font]
[font=Verdana, Arial, Helvetica, sans-serif]//---------------------------------------------------------------//user samples @ codeSampler.com
Bump Mapping with GLSlang (OpenGL - C++) Author: [email="anudhyan@gmail.com"]Anudhyan Boral[/email]
//---------------------------------------------------------------

Follow the next link to see a vertex processor program which creates a wavy effect that is applied to a flat tessellated plane( the thumbnail you're looking for is a green wavy mesh)

http://nehe.gamedev.net/lesson.asp?index=10

It can be used for water, flags,capes or even grass blowing in the wind..

[/font]

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!