Horde3D
http://horde3d.org/forums/

Changing color
http://horde3d.org/forums/viewtopic.php?f=2&t=1480
Page 1 of 1

Author:  mikel [ 01.04.2011, 16:23 ]
Post subject:  Changing color

Hi.
Can I change color of renderable objects and mix it with texture (like glColor3f) ?
Somehow modifying objects material? Or model.shader?

Author:  vikingcode [ 02.04.2011, 03:31 ]
Post subject:  Re: Changing color

mikel wrote:
Can I change color of renderable objects and mix it with texture (like glColor3f) ?
Somehow modifying objects material? Or model.shader?

Yes but it's not straightforward, you will need to understand Horde3D. At the moment, geometry does not have vertex colors.

You will need to setup the vertex colors in the static geometry stream and setup the shader to retrieve the vertex colors.
The jointIndices in H3DGeoRes::GeoVertStaticStream will need to be used. This data is normally used for skinning as far as I understand.

You will also need to create a model node, mesh node and material resource to render the geometry. There is a lot to understand about Horde3D and particularly about shaders and pipelines.

Setting the geometry vertex colors
The variables posData, indData, and texData are std::vector types; vertexCount and indexCount are self explanatory.

Code:
// Create the geometry resource
H3DRes geometryResource = h3dutCreateGeometryRes( "GeometryName", vertexCount, indexCount, &posData[0], &indData[0], 0x0, 0x0, 0x0, &texData[0], 0x0 );

// Add the vertex colors to the geometry resource in RGBA format, use the jointIndices in the static stream.
float* vertexStaticStream = static_cast<float*>( h3dMapResStream( geometryResource, H3DGeoRes::GeometryElem, 0, H3DGeoRes::GeoVertStaticStream, false, true ) );
for ( int i=0; i < vertexCount; ++i )
{
   vertexStaticStream += 2;   // skip u1,v1
   *vertexStaticStream++ = 1.0f;
   *vertexStaticStream++ = 1.0f;
   *vertexStaticStream++ = 1.0f;
   *vertexStaticStream++ = 1.0f;
   vertexStaticStream += 6;   // skip joint weights and u2,v2
   ++sourceVertices;
}
h3dUnmapResStream( geometryResource );


Shader
Here is an example of a shader that mixes texture color with vertex color. The uniform projection matrix is orthogonal.
The vertex color is stored in the jointIndices of the H3DGeoRes::GeoVertTanStream.
Code:
[[FX]]

// Samplers
sampler2D albedoMap;

// Contexts
context TRANSLUCENT
{
   VertexShader = compile GLSL VS_GENERAL;
   PixelShader = compile GLSL FS_AMBIENT;

   ZWriteEnable = false;
   BlendMode = Blend;
}

[[VS_GENERAL]]

#include "shaders/utilityLib/vertCommon.glsl"

uniform mat4 projMat;
attribute vec3 vertPos;

attribute vec4 joints;      // vertex color (r,g,b,a)
varying vec4 vColor;

#ifdef _F01_Textured
   attribute vec2 texCoords0;
   varying vec2 vTexCoords;
#endif

void main( void )
{
   #ifdef _F01_Textured
      vTexCoords = vec2( texCoords0.s, -texCoords0.t );
   #endif

   // The joints stream is used to store vertex color
   vColor = vec4( joints.r, joints.g, joints.b, joints.a );

   // Calculate world space position
   vec4 pos = calcWorldPos( vec4( vertPos.x, vertPos.y, vertPos.z, 1.0 ) );

   // Convert the projected position to a pixel coordinate of the window
   gl_Position = projMat * pos;
}

[[FS_AMBIENT]]

#ifdef _F01_Textured
   uniform sampler2D albedoMap;
   varying vec2 vTexCoords;
#endif
varying vec4 vColor;

void main( void )
{
   #ifdef _F01_Textured
      gl_FragColor = texture2D( albedoMap, vTexCoords ) * vColor;
   #else   
      gl_FragColor = vColor;
   #endif
}


Material Resource Creation
The variable textureRes is a Horde3D texture resource that has been previously loaded.
The file location "shaders/example.shader" is the name of the shader file shown above.

Code:
// Create the material resource
H3DRes materialRes = h3dAddResource( H3DResTypes::Material, "MaterialName" , 0 );

// Setup the XML string to represent the material data
std::string materialData = std::string( "<Material>\n" );
materialData.append( "<Shader source=\"shaders/example.shader\"/>\n" ); // shader source
if ( textureRes )
{
   std::string textureName = h3dGetResName( textureRes );
   materialData.append( "<ShaderFlag name=\"_F01_Textured\"/>\n" );   // shader flags
   materialData.append( "<Sampler name=\"albedoMap\" map=\"" );      // sampler name and attributes
   materialData.append( textureName.c_str() );               // name of the texture resource to set as the <map> value
   materialData.append( "\" allowCompression=\"false\" mipmaps=\"false\" />\n" );
}
materialData.append( "</Material>\n" );

// Load the material resource from the material data string
const bool loaded = h3dLoadResource( materialRes, materialData.c_str(), materialData.length() );
if ( !loaded )
{
   (void)h3dRemoveResource( materialRes );
   materialRes = 0x0;
}

Author:  mikel [ 03.04.2011, 07:54 ]
Post subject:  Re: Changing color

Thanks, Ill make my static objects change colors with this (even this is quite 'hackish'). But good and
informative reply :)

I think Horde needs support custom uniforms in xml/materials, I hope someday
there is.. I read old forum posts and 2008 someone made Knight demo work
with his modifications so many colors made with xml attributes, unfortunately
he does not apply the patch (or I didnt see it anywhere).

Author:  Rauy [ 03.04.2011, 12:49 ]
Post subject:  Re: Changing color

If you do not need per-vertex colors, you do not have to use this "hackish" solution, as Horde supports custom uniforms in materials, that's what material are all about. You just need to write a new shader or modify the existing one and make a material that uses this shader and exposes the per-object color as a float4 uniform.

Author:  vikingcode [ 03.04.2011, 19:41 ]
Post subject:  Re: Changing color

mikel wrote:
I think Horde needs support custom uniforms in xml/materials

It does.
Rauy wrote:
If you do not need per-vertex colors, you do not have to use this "hackish" solution, as Horde supports custom uniforms in materials


I assumed you needed to change more than one color per object. Must be because that is what I was working on :roll:

Regards,
vikingcode

Author:  mikel [ 05.04.2011, 09:50 ]
Post subject:  Re: Changing color

Well, I just needed to change object's color, not single vertex color (atm). But now I finally got uniforms to work, had problems with them so thought that h3d does not support own uniform variables.

What I finally did, I added <Uniform name="color" a="0" b="1" c="0" /> to .material.xml file, then
added 'float4 color={0,0,0,0};' in beginning of the model.shader file, and 'uniform vec4 color;' to
the right context in model.shader file and modified gl_FragColor line, so it multiplies with 'color' too.

Page 1 of 1 All times are UTC + 1 hour
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/