GLBasic forum

Codesnippets => 3D-snippets => Topic started by: Hemlos on 2009-Jul-01

Title: GLSL - The OpenGL Shader Language: for 2d and 3d SFX
Post by: Hemlos on 2009-Jul-01
I made a new Shader, for increasing the brightness of an object by 50%.
I will add more in this thread later on.


Example for using GLSL:
Code (Load the shader in head of program) Select

X_LOADSHADER(0, "Bright.vert", "Bright.frag")

-
Code (2d renderings) Select

X_SETSHADER 0;
DRAWSPRITE
X_SETSHADER -1

-
Code (3d renderings) Select

X_SETSHADER 0;
X_DRAWOBJ
X_SETSHADER -1


-Hemlos

EDIT: This post is updated with the proper shader.
===============================================================
EDIT: COLOR SHADER BELOW:
I made a color shader, it adds color to your texture in the gpu.
This can also be used as a "brightness".
To use as brightness, increase R G B the same amount.
Remember, this is additive, so if your texture is already bright...itll push the color closer to white.

This will add some red:
Code (RGB(128,0,0)) Select

X_COLORSHADER( 0.5, 0.0, 0.0 )


The colors in GLSL ranges from 0.0 to 1.0 which is the equivilant of 0x00 to 0xff


Put the shader files with the exe, and use this function to declare and set the color.
You call this function right before X_SETTEXTURE.

Code (glbasic) Select

X_COLORSHADER( 0.5, 0.0, 0.0 )
X_SETTEXTURE Texture0 ,-1
X_DRAWOBJ Object0, 0
X_SETSHADER -1



You can reduce cycles by preloading the shader instead of putting it in the function.

Code (Preset to shader #11) Select

FUNCTION X_COLORSHADER: R,G,B //RGB Addition

STATIC Initialize, Test
IF Initialize<>TRUE
Initialize=TRUE
Test=X_LOADSHADER(11,"Color.vert","Color.frag")
ENDIF

X_SETSHADER 11
X_PUTSHADER "R",R
X_PUTSHADER "G",G
X_PUTSHADER "B",B

RETURN Test

ENDFUNCTION


edit July 2012:
I reupload the colorshader.rar, it wasnt correct.



[attachment deleted by admin]
Title: Re: GLSL - The OpenGL Shader Language: for 2d and 3d SFX
Post by: Kitty Hello on 2009-Jul-02
the internal bump mapping and cel shading use different techniques. No shaders, thus compatible with older hardware.
Title: Re: GLSL - The OpenGL Shader Language: for 2d and 3d SFX
Post by: Hemlos on 2009-Jul-02
Ok cool good to know, Gernot..

And Thanks for the info...

Ok here is the "Shiny" shader...

Scalar Variables in shader are adjustable with the settings at top of BumpLight.frag:
You can adjust Ambient, Specular, And Diffuse
Also you can adjust the size of the texture.

Test: If you set the Specular to 2.0 and the TextureScale to 0.1 - 0.5 you can make a GLASS ball for instance.

It makes it "shiny".... You must use both inputs of X_SETTEXTURE 3d bumpmap system.
Here are the files:

[attachment deleted by admin]
Title: Re: GLSL - The OpenGL Shader Language: for 2d and 3d SFX
Post by: Kitty Hello on 2009-Jul-02
// GLBasic exports 2 "uniform sampler2D" variables if
// you set X_SETTEXTURE a,b after X_SETSHADER!
// TextureID1 = a
// TextureID2 = b
uniform sampler2D TextureID1;
uniform sampler2D TextureID2;
Title: Re: GLSL - The OpenGL Shader Language: for 2d and 3d SFX
Post by: Hemlos on 2009-Oct-08
Hi again,

I am learning more about GLSL, couple questions please.

Is there anything else GLBasic exports to shaders?

What  OpenGL and GLSL version is GLBasic using?
Title: Re: GLSL - The OpenGL Shader Language: for 2d and 3d SFX
Post by: Kitty Hello on 2009-Oct-09
The version depends on your drivers (2.x) and no, I'm not exporting anything else, but you can manually export variables with uhm... there's a command ;)
Title: Re: GLSL - The OpenGL Shader Language: for 2d and 3d SFX
Post by: Hemlos on 2009-Oct-09
Aye uniform


All the projection matrix, texture coordinate matrix, and all those other matrixs....are these normal in the sense that my shaders will read them as intended by the language?

Also, all the stuff that a frag shader exports into the standard matrixs....are these compatible with GLBasic?

Are you aware of anything the language wont do, which is actually intended to be possible with the shader language?
Title: Re: GLSL - The OpenGL Shader Language: for 2d and 3d SFX
Post by: Kitty Hello on 2009-Oct-09
no, there should not be any limit.
Title: Re: GLSL - The OpenGL Shader Language: for 2d and 3d SFX
Post by: Hemlos on 2009-Oct-09
How do i get this info?
glGetString(GL_SHADING_LANGUAGE_VERSION)
Title: Re: GLSL - The OpenGL Shader Language: for 2d and 3d SFX
Post by: Kitty Hello on 2009-Oct-09
Code (glbasic) Select

CONSTANT GL_SHADING_LANGUAGE_VERSION = 35724

PRINT glGetString$(GL_SHADING_LANGUAGE_VERSION), 0,0
SHOWSCREEN
MOUSEWAIT



FUNCTION foo:



ENDFUNCTION


INLINE
extern "C" const unsigned char* __stdcall glGetString( int name );
ENDINLINE


FUNCTION glGetString$: id%
INLINE
return DGStr((const char*)glGetString(id));
ENDINLINE
ENDFUNCTION


Constants are here:
http://www.khronos.org/registry/gles/api/2.0/gl2.h
Title: Re: GLSL - The OpenGL Shader Language: for 2d and 3d SFX
Post by: Hemlos on 2009-Oct-10
Awesome,

1.2 on nvidia, using Cg compiler.

Things that make me go "Hmmmmmm"



[attachment deleted by admin]
Title: Re: GLSL - The OpenGL Shader Language: for 2d and 3d SFX
Post by: Hemlos on 2009-Oct-11
3 questions:

1.
Can i use x_putshader multiple times before calling the X_DRAWOBJ?

2.
Also, does x_shader need to be set before the x_make3d?
Or after move scale and rotate?
I ask because I realize you noted to put it before the x_settexture
And theres a bunch of matrix it needs to read for model and views.

3.
Can multiple shaders be applied in a sequence?
This language seems to be oriented to doing stuff this way.

apply matrices
apply shader.glsl 1
apply shader.glsl  2
apply shader.glsl  3, etc
draw object

The way i understand it in OpenGL, each new apply of a shader affects all the previously pipelined shader information before drawing an object.
An example of this is RenderMonkey program.
This program applies multple shaders to an object to create AMAZING effect (whichi cant seem to replicate)

Oh ya thanks for the getsetring command....i added a version info program showing all extensions and internal versions of opengl for any 3d card.
link: http://www.glbasic.com/forum/index.php?topic=3631.0

Title: Re: GLSL - The OpenGL Shader Language: for 2d and 3d SFX
Post by: Kitty Hello on 2009-Oct-12
1. Yes of course.
2. it doesn't matter where you put shader variables, just before x_settexture. I can't remember why, though.
3. uhm. Can you apply several shaders? I don't think that's possible (in OpenGL).
Title: Re: GLSL - The OpenGL Shader Language: for 2d and 3d SFX
Post by: Hemlos on 2009-Oct-12
This is a screenshot of AMD's render monkey.
It is a program that processes shaders like an IDE and view port.

Here you can see that several shaders are being applied to achieve this effect "GL OOZE Effect"..looks awesome, and it works with glsl 1.20 apparently.

To make this effect with shaders, it uses 3 shader.vert and .frags in one group, and it needs a second shader set which contains 6 more shaders.

[attachment deleted by admin]
Title: Re: GLSL - The OpenGL Shader Language: for 2d and 3d SFX
Post by: Kitty Hello on 2009-Oct-12
They must render the model twice with different shaders then. There's no GLSL extension function hidden that you can't use with GLBasic.
Title: Re: GLSL - The OpenGL Shader Language: for 2d and 3d SFX
Post by: Hemlos on 2009-Oct-12
Quote from: Kitty Hello on 2009-Oct-12
They must render the model twice with different shaders then. There's no GLSL extension function hidden that you can't use with GLBasic.

Rendered to backbuffer, then more shaders are reapplied, thats how shaders work.

The first pass.....has 3 shaders
The second has 6 shaders.
Title: Re: GLSL - The OpenGL Shader Language: for 2d and 3d SFX
Post by: Kitty Hello on 2009-Oct-12
OK, then you must do that manually. But you can.
Title: Re: GLSL - The OpenGL Shader Language: for 2d and 3d SFX
Post by: Hemlos on 2009-Oct-12
Do Like this?

X_SETSHADER 1
X_SETSHADER 2
X_SETSHADER 3
Title: Re: GLSL - The OpenGL Shader Language: for 2d and 3d SFX
Post by: Kitty Hello on 2009-Oct-13
no. That will exchange the shaders. You only can set one shader per rendering.
X_SETSHADER 1
X_DRAWOBJ
X_SETSHADER 2
X_DRAWOBJ ...

(maybe) I have no idea what RenderMonkey really does.
Title: Re: GLSL - The OpenGL Shader Language: for 2d and 3d SFX
Post by: Hemlos on 2009-Oct-13
Ahh, ya that wont work correctly, youll get the surfaces z-fighting.

I have been reading the opengl glsl specifications.

The language links multiple programs (verts and frags).

Get the rendermonkey from AMD.com, its awesome!
Youll certainly be amazed at what opengl is capable of now.
Try the fur project, and the ooze project.....tons of eye candy.
Title: Re: GLSL - The OpenGL Shader Language: for 2d and 3d SFX
Post by: Kitty Hello on 2009-Oct-13
I can't find _any_ command that enables multiple. Or is it just linking a bunch of "object" files, that produce one shader then?
You only can have _one_ main() for a shader and only one shader per pixel.
Title: Re: GLSL - The OpenGL Shader Language: for 2d and 3d SFX
Post by: Hemlos on 2009-Oct-13
hmmm i checked it out ....

youre right, it is only passing 1 vert and 1 frag.

i was confused by the way the program works, sorry for troubling you.
Title: Re: GLSL - The OpenGL Shader Language: for 2d and 3d SFX
Post by: Hemlos on 2009-Oct-14
Here is my current project, as i wait patiently for point sprites :whistle:


First off, I have successfully created a 2d animated background using a shader.
Therefore i am moving on to animation in 3d.

The effect im attempting to achieve is going to have a "flowing fur" effect.

Here is a screenshot showing this in action:




[attachment deleted by admin]
Title: Re: GLSL - The OpenGL Shader Language: for 2d and 3d SFX
Post by: Hemlos on 2009-Oct-14

Im trying to load a  "3d texture", it is named NoiseVolume.dds
This format .dds is not glbasic, and it isnt .3dstudio.
I have no clue, i cant find this extension on the search engines.

a 3d texture:
.dds
8bit L8
and from what i understand,
it is 128x128x128 dimensions, and it has 8 mip map layers.

This file is needs to be loaded using uniform sampler3D filename

The shader needs to read this file in the frag program.
I dont need to render it to the screen, i dont even think it is a regular 3d object, it seems to be "special"

How do i load the sampler3d? x_putshader "sampler3D", filename$   ? ?

Do you think this why the shader fails? Am i not loading it correctly?
Title: Re: GLSL - The OpenGL Shader Language: for 2d and 3d SFX
Post by: Kitty Hello on 2009-Oct-14
 :S
I have no idea about 3D textures. Really.
Title: Re: GLSL - The OpenGL Shader Language: for 2d and 3d SFX
Post by: Hemlos on 2009-Oct-14
LOL  :'(


x_putshader "sampler3D", filename$   ? ?

Integer ID ? String$ ID ?

What is the input type?

I need to know what to pass through to load it.
Title: Re: GLSL - The OpenGL Shader Language: for 2d and 3d SFX
Post by: Kitty Hello on 2009-Oct-14
I only support "uniform float".
You can't load a shader file within a shader, can you?
Title: Re: GLSL - The OpenGL Shader Language: for 2d and 3d SFX
Post by: Hemlos on 2009-Oct-14
No it must be a numeric value, i assume an integer, but somehow GLBasic needs to tell the shader what to load.
Its like loading a texture with sampler2d i think.
TextureID1 and TextureID2 i assume are integers, and they are pointing to some file.
I think we need Texture3d1 or something..

These all get loaded the same way i think:

sampler1D
sampler2D
sampler3D
samplerCube
sampler1DShadow
sampler2DShadow

I think we need a naming convention of constants and pointers to files loaded with opengl here:

GLB_sampler1D
GLB_sampler2Da
GLB_sampler2Db
GLB_sampler3D
GLB_samplerCube
GLB_sampler1DShadow
GLB_sampler2DShadow

Quote4.1.7 Samplers
Sampler types (e.g. sampler2D) are effectively opaque handles to textures. They are used with the builtin
texture functions (described in Section 8.7 “Texture Lookup Functions” ) to specify which texture to
access. They can only be declared as function parameters or uniform variables (see Section 4.3.5
“Uniform” ). Except for array indexing, structure field selection, and parentheses, samplers are not
allowed to be operands in expressions. Samplers cannot be treated as l-values; hence cannot be used as
out or inout function parameters, nor can they be assigned into. As uniforms, they are initialized only
with the OpenGL API; they cannot be declared with an initializer in a shader. As function parameters,
only samplers may be passed to samplers of matching type. This enables consistency checking between
shader texture accesses and OpenGL texture state before a shader is run.
Title: Re: GLSL - The OpenGL Shader Language: for 2d and 3d SFX
Post by: Hemlos on 2009-Dec-15
Sorry it took so long guys...but i finally got the bright shader working proper.
It is the download in the first message of this thread.