Shader problem

Return to C++ coding using Panda3D

Shader problem

Postby Chrys » Tue Sep 27, 2011 1:33 pm

hello,

I have setup some geometry like this

Code: Select all
   PT(GeomVertexArrayFormat) array = new GeomVertexArrayFormat();
   array->add_column(InternalName::make("vertex"), 3, Geom::NT_float32, Geom::C_point);
   array->add_column(InternalName::make("normal"), 3, Geom::NT_float32, Geom::C_point);
   array->add_column(InternalName::make("alphaUV"), 2, Geom::NT_float32, Geom::C_texcoord);
   array->add_column(InternalName::make("colorUV"), 2, Geom::NT_float32, Geom::C_texcoord);

   PT(GeomVertexFormat )unregistered_format = new GeomVertexFormat();
   unregistered_format->add_array(array);

   CPT(GeomVertexFormat) format = GeomVertexFormat::register_format(unregistered_format);

   PT(GeomVertexData) vData = new GeomVertexData("myFormat", format, Geom::UH_dynamic);
   GeomVertexRewriter vertices = GeomVertexRewriter(vData, "vertex");
   GeomVertexRewriter normals = GeomVertexRewriter(vData, "normal");
   GeomVertexRewriter alphaUV = GeomVertexRewriter(vData, "alphaUV");
   GeomVertexRewriter colorUV = GeomVertexRewriter(vData, "colorUV");


I have filled in all the data and I can see the geometry rendering on screen.

I have then loaded a texture like this

Code: Select all
   PT(Texture) pDirt = TexturePool::load_texture("dirt.dds");
myGeometryNode.set_texture(pDirt);


I load my shader like this

Code: Select all
   CPT(Shader) myGeometryShader;

   myGeometryShader = ShaderPool::load_shader("myShader.cg");
myGeometryNode.set_shader(myGeometryShader);


here the implementation of my shader

Code: Select all
//Cg

void vshader(
    uniform float4x4 mat_modelproj,
    in float4 vtx_position : POSITION,
    in float3 vtx_normal : NORMAL,
   in float2 vtx_texcoord0: TEXCOORD0,
   in float2 vtx_texcoord1: TEXCOORD1,
   out float2 l_texcoord0: TEXCOORD0,
   out float2 l_texcoord1: TEXCOORD1,
    out float4 l_position : POSITION)
{
    l_position = mul(mat_modelproj, vtx_position);
    l_texcoord0 = vtx_texcoord0;
    l_texcoord1 = vtx_texcoord1;
}

void fshader(
   uniform sampler2D tex_0 : TEXUNIT0,
   in float2 l_texcoord0: TEXCOORD0,
   in float2 l_texcoord1: TEXCOORD1,
    out float4 o_color : COLOR)
{   
    float4 c0 = tex2D(tex_0, l_texcoord1);   // dirt
   
    o_color = c0;
}


with this my model is rendered but instead of the "dirt" texture I see the whole model in a dark blue color.

Anyone knows what I am doing wrong ?
:?:

chrys
Chrys
 
Posts: 210
Joined: Tue Nov 02, 2010 8:29 am
Location: Montreal - Canada

Postby Chrys » Tue Sep 27, 2011 1:51 pm

I think I just found the problem after reading again through the manual

I have changed the input parameters from

Code: Select all
   in float2 vtx_texcoord0: TEXCOORD0,
   in float2 vtx_texcoord1: TEXCOORD1,


to this
Code: Select all
   in float2 vtx_alphaUV: TEXCOORD0,
   in float2 vtx_colorUV: TEXCOORD1,


in the vertex shader function.

reason is I believe because have defined my own vertex format and in particular

Code: Select all
   array->add_column(InternalName::make("alphaUV"), 2, Geom::NT_float32, Geom::C_texcoord);
    array->add_column(InternalName::make("colorUV"), 2, Geom::NT_float32, Geom::C_texcoord);
 


this isn't very clear from the manual explanation to be honest, and the shader tutorials are not really documented at all a part the first 3 tutorials.

see
http://www.panda3d.org/manual/index.php/Cg_Shader_Tutorial

and
http://www.panda3d.org/manual/index.php/List_of_Possible_Cg_Shader_Inputs

Chrys
Chrys
 
Posts: 210
Joined: Tue Nov 02, 2010 8:29 am
Location: Montreal - Canada

Postby drwr » Tue Sep 27, 2011 1:52 pm

I'm not sure whether this is your only problem, but you haven't chosen a good name for your texture coordinates. Panda uses the column name to identify the semantic meaning of each column, so the name is important.

Instead of InternalName::make("alphaUV"), use InternalName::get_texcoord_name("alphaUV"), and similarly for colorUV.

But I'm not sure whether you also might need to associate the texture coordinate names with the TextureStage when you use a shader.

David
drwr
 
Posts: 11285
Joined: Fri Feb 13, 2004 12:42 pm
Location: Glendale, CA

Postby Chrys » Tue Sep 27, 2011 3:04 pm

this
Code: Select all
   array->add_column(InternalName::get_texcoord_name("alphaUV"), 2, Geom::NT_float32, Geom::C_texcoord);
   array->add_column(InternalName::get_texcoord_name("colorUV"), 2, Geom::NT_float32, Geom::C_texcoord);


now causes the output console to spam

Assertion failed: has_column() at line 656 of c:\panda3d\panda3d\pandadebug\incl
ude\geomVertexWriter.I


why are those names not chosen very well ?

and what did you mean with

But I'm not sure whether you also might need to associate the texture coordinate names with the TextureStage when you use a shader.
Chrys
 
Posts: 210
Joined: Tue Nov 02, 2010 8:29 am
Location: Montreal - Canada

Postby drwr » Tue Sep 27, 2011 3:59 pm

Your first solution is a better solution than mine. Adapt the shader to follow your own naming convention.

My solution is based on getting the fixed-function rendering to support your texture coordinates, which isn't necessary if you plan on using the shader. (The error message, though, is because you also have to change the GeomVertexRewriter to use the new column name you have created.)

David
drwr
 
Posts: 11285
Joined: Fri Feb 13, 2004 12:42 pm
Location: Glendale, CA

Postby Chrys » Tue Sep 27, 2011 5:15 pm

I've managed to achieve what I wanted for the moment with my shader but when I switched to use DirectX instead of OpenGL I got some error messages showing.

Image

any idea what that means ? or how to fix it ?
Chrys
 
Posts: 210
Joined: Tue Nov 02, 2010 8:29 am
Location: Montreal - Canada

Postby drwr » Wed Sep 28, 2011 1:46 pm

Our DirectX shader support has historically lagged behind our OpenGL shader support. But another community member, zhao, provided some patches a few months ago that I had failed to integrate until now, which might address this issue. When the next buildbot release is ready (presumably tomorrow morning, unless there's a build issue) you're welcome to download the latest and see if it works for you.

David
drwr
 
Posts: 11285
Joined: Fri Feb 13, 2004 12:42 pm
Location: Glendale, CA

Postby teedee » Wed Sep 28, 2011 8:28 pm

Many thankyous! With this patch the majority of my shaders work properly in DX9 now.
teedee
 
Posts: 784
Joined: Tue May 12, 2009 11:33 pm
Location: Kepler-22b


Return to C++ coding using Panda3D

Who is online

Users browsing this forum: No registered users and 0 guests