Horde3D

Next-Generation Graphics Engine
It is currently 08.05.2024, 11:09

All times are UTC + 1 hour




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Shader woes
PostPosted: 10.05.2011, 20:15 
Offline

Joined: 08.03.2011, 18:29
Posts: 17
I am trying to work my way through a few GLSL tutorials, but I have a hard time applying what I learned to Horde3D.

1. Why do the shaders that come with Horde all use things like "uniform mat4 viewProjMat;" instead of "gl_ModelViewProjectionMatrix" which I find referenced more often? Is this what was meant by "Replaced fixed-function modelview and projection matrices by custom uniforms (viewMat, viewMatInv, projMat, viewProjMat)." in the changelog for Beta5? Is there documentation for this?

2. For example this should be a very simple Vertex+Fragment shader to billboard and texturize an object. It works fine in the tool I use (OpenGL Shader Builder) and shows no error in the Horde3d_Log.html - yet it is invisible. What did I do wrong? How can I find that out on my own?
Code:
[[FX]]
sampler2D albedoMap;

context AMBIENT {
   VertexShader = compile GLSL VS_GENERAL;
   PixelShader = compile GLSL FS_GENERAL;
}

[[VS_GENERAL]]
void main(void) {
   gl_FrontColor = gl_Color;
   gl_TexCoord[0] = gl_MultiTexCoord0;
   //gl_Position = ftransform();
   // Billboard the model instead
   gl_Position = gl_ModelViewProjectionMatrix*vec4(
      gl_Vertex.y*vec3(   gl_ModelViewProjectionMatrix[0][0],
                  gl_ModelViewProjectionMatrix[1][0],
                   gl_ModelViewProjectionMatrix[2][0]) +
      gl_Vertex.x*vec3(   gl_ModelViewProjectionMatrix[0][1],
                  gl_ModelViewProjectionMatrix[1][1],
                  gl_ModelViewProjectionMatrix[2][1]),
      1.0);
}

[[FS_GENERAL]]
uniform sampler2D albedoMap;

void main(void) {
   gl_FragColor = gl_Color * texture2D(albedoMap, gl_TexCoord[0].xy);
}


3. What is the intended meaning of the contexts ATTRIBPASS, AMBIENT etc.? ATTRIBPASS doesn't even appear in the forward shader.

Any clues are greatly appreciated. :)


Top
 Profile  
Reply with quote  
 Post subject: Re: Shader woes
PostPosted: 10.05.2011, 21:23 
Offline

Joined: 11.09.2010, 20:21
Posts: 44
Location: Germany
You just answered your second question with your first. As Horde3D does not use the builtin gl_ModelView... but its own ones, you need to use these.

To the first: All these user-defined uniforms and attributes are just a bit more forward compatible, as they removed all the deprecated stuff (including builtin uniforms and attributes) from the core in GL3/4 (although with GL2 it would still be fine, but this way Horde is more future ready).

The ATTRIBPASS is there for rendering the models attributes into the G-buffer in the deferred pipeline. Consult a tutorial on deferred rendering for more insight.


Top
 Profile  
Reply with quote  
 Post subject: Re: Shader woes
PostPosted: 11.05.2011, 07:19 
Offline

Joined: 15.02.2009, 02:13
Posts: 161
Location: Sydney Australia
Hi polygoff

Try this:
Code:
[[FX]]
sampler2D albedoMap;

context AMBIENT {
   VertexShader = compile GLSL VS_GENERAL;
   PixelShader = compile GLSL FS_GENERAL;
}

[[VS_GENERAL]]
uniform mat4 viewProjMat;
attribute vec3 vertPos;
attribute vec2 texCoords0;
attribute vec4 weights; //No gl_Color linked to the shader with the default .geo format, need to encode color into weights or some other attribute
varying vec4 outColor; // giving the fragment program the weights attribute (gl_Color), I assume gl_FrontColor is just a varying???
varying vec2 texCoords;

void main(void) {
   outColor = weights;
   texCoords = texCoords0;
   //gl_Position = ftransform();
   // Billboard the model instead
   vertPos = viewProjMat*vec4(
      vertPos.y*vec3(   viewProjMat[0][0],
                  viewProjMat[1][0],
                   viewProjMat[2][0]) +
      vertPos.x*vec3(   viewProjMat[0][1],
                  viewProjMat[1][1],
                  viewProjMat[2][1]),
      1.0);
}

[[FS_GENERAL]]
uniform sampler2D albedoMap;
varying vec2 texCoords;
varying vec4 outColor;

void main(void) {
   gl_FragColor = outColor * texture2D(albedoMap, texCoords.xy);
}


For now I'd just make outColor some kind of constant like vec4( 1, 1, 1, 1 ) because horde doesn't have per-vertex colors exposed by default for geo objects.

Hope this somewhat helps! OpenGL ES 2.0 doesn't have these fixed uniforms/attributes so it's probably best to learn this approach as well if you want to target shaders towards ES 2.0.

_________________
-Alex
Website


Top
 Profile  
Reply with quote  
 Post subject: Re: Shader woes
PostPosted: 11.05.2011, 07:36 
Offline

Joined: 08.03.2011, 18:29
Posts: 17
Rauy wrote:
You just answered your second question with your first. As Horde3D does not use the builtin gl_ModelView... but its own ones, you need to use these.

Thanks - that was it. Got my billboard shader to work (even though it is not perfect). For some reason though I had to interchange the z- and the x-axis between OpenGL and Horde3D.
Code:

[[FX]]
sampler2D albedoMap;

context AMBIENT {
   VertexShader = compile GLSL VS_GENERAL;
   PixelShader = compile GLSL FS_GENERAL;
}


[[VS_GENERAL]]

uniform mat4 viewProjMat; // gl_ModelViewProjectionMatrix
uniform vec3 viewerPos; // ?
attribute vec2 texCoords0; // gl_MultiTexCoord0
attribute vec3 vertPos; // gl_Vertex
varying vec4 pos; // ?
varying vec4 vsPos; // ?
varying vec2 texCoords; // gl_TexCoord[0]
uniform mat4 viewMat;
uniform mat4 worldMat;
uniform   mat3 worldNormalMat;

void main(void) {
   // GL2: gl_TexCoord[0] = gl_MultiTexCoord0;
   texCoords = texCoords0;
   // GL2: gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
   pos = worldMat * vec4(vertPos, 1.0);
   vsPos = viewMat * pos;
   //gl_Position = viewProjMat * pos;

   // Billboard the model instead
   gl_Position = viewProjMat*vec4(
      pos.z*vec3(   viewProjMat[0][0],
            viewProjMat[1][0],
             viewProjMat[2][0]) +
      pos.y*vec3(   viewProjMat[0][1],
            viewProjMat[1][1],
            viewProjMat[2][1]),
      1.0);
}

[[FS_GENERAL]]
uniform sampler2D albedoMap;
varying vec2 texCoords;

void main(void) {
   vec3 newCoords = vec3( texCoords, 0 );
   newCoords.t *= -1.0; // HACK flip the texture orientation
   vec4 albedo = texture2D( albedoMap, newCoords.st );

   gl_FragColor.rgb = albedo.rgb;
}



Rauy wrote:
To the first: All these user-defined uniforms and attributes are just a bit more forward compatible, as they removed all the deprecated stuff (including builtin uniforms and attributes) from the core in GL3/4 (although with GL2 it would still be fine, but this way Horde is more future ready).

Aha! :) But a google search for e.g. "viewProjMat glsl" only returns 7 hits and no hints about OpenGL3/4 - so the naming is Horde3D arbitrary? But why is it different from the old names? And why is "gl_FragColor" still intact?
I wish there was a translation table for these names in the documentation so I can apply my new shaders to Horde3D. The only shader sample on the whole net that works with Horde3D is "model.shader" - and that is actually 5 shaders in one and has few comments. :/

Rauy wrote:
The ATTRIBPASS is there for rendering the models attributes into the G-buffer in the deferred pipeline. Consult a tutorial on deferred rendering for more insight.

Again, a search for ATTRIBPASS returned only Horde3D related sourcecode. I guess I just wish there were more examples that came with Horde3D so I could understand better what was going on.


[Edit]
Thanks a lot for the help MistaED! I saw your post when I clicked "submit". I am a slow writer. ;)
Your version is nicer than mine and your comments help. Thank you!


Top
 Profile  
Reply with quote  
 Post subject: Re: Shader woes
PostPosted: 12.05.2011, 00:24 
Offline

Joined: 11.09.2010, 20:21
Posts: 44
Location: Germany
All these shaders are Horde specific. You will allways have to adapt shaders to your own application. And the ATTRIBPASS, well GLSL does not even know about meterial contexts, it is all a Horde3D invention. The only valid GLSL shader code is, well the shader code (after the [[...]] parts, but not the [[FX]] part). And yes, the names of the uniforms, like viewMatrix... are all Horde specific, like I already said (it wouldn't really make any sense to just rename these variables). I also explained to you, why Horde does not use the old names, like gl_ModelView.... Horde does not have to do this, as it's still targeted at GL2, but it is done to be more future ready. The old variables are still there and can be read, but Horde does not fill them with meaningful values.

Some builtin variables are still existent, as they have special semantics and need to be treated specially in the pipeline (like the gl_Position varying). The gl_FragColor can also be substituted by a user-defined variable, but I think this needs GL3 hardware.

You can look into the Horde documentation (the one with the version you are using, not neccessarily the one from the main webpage) to gain some more understanding of Horde3D. There you will also find a list of the uniforms that Horde always sets and their meanings.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 8 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group