hi,
due the fact,that im a very greenhorn to shaders (im learning shader programming since few hours), im coming along with a very noobish question. but all my efforts ended in nowhere.
so here my question, how to pass a dynamic value like time into a shader, or how to set a time value there?
i want just to test and to learn how shaders works. i know my try are completly wrong, its nonsense to use a endless loop in a shader for getting this value. a shader loops by it selfs, but i have no idea how to bring a dynamic value into a shader. of course i could animate a object in my application and pass this value for. but im looking for something like a time value.
thanks in forward for any solution.
//Cg
//
void vshader(
uniform float4x4 mat_modelproj,
in float4 vtx_position: POSITION,
in float2 vtx_texcoord : TEXCOORD,
out float4 l_position: POSITION,
out float4 l_pos)
{
//endless loop
float time = 1.0;
int x = 0;
while (x < time) {
time++;
l_pos=vtx_position;
l_position.x=vtx_texcoord.x*.2-.2+sin(time);
l_position.y=vtx_texcoord.y*.2;
l_position.z=0.0;
}
}
void fshader(
out float4 o_color: COLOR)
{
o_color=float4(1,0,0,0);
}
import time
thetime = time.time()
node.setShaderInput('mytime', thetime)
For that one you would need to have a task running to update with the new time each frame.
In your shader, the name you give it in Panda is prefixed with a “k_”:
void fshader(
in float2 l_texcoord0: TEXCOORD0,
in uniform sampler2D tex_0: TEXUNIT0,
in uniform float4 k_mycolour,
in uniform float k_mytime,
out float4 o_color : COLOR)
{
o_color = k_mycolour * k_mytime;
}
The Panda commands apply these values per-object, but I believe they trickle down to child objects; this way you could do setShaderInput on the top of a model or the whole scene to set the value on everything. You can even pass textures to the shader with this method.
sorry for interrupting again! but i dont understand so many things about shaders. is there anywhere a good manual or tutorial for beginners which discribes the vertex shaders?
what im doing now wrong? i only wanna store a named geom vert array, like -> vtx_(geom) and going to multiply a modelprojetion 4x4 matrix with my float4 model. but nothing gets dispayed. whats the best way for storing a def. vert array? i have to manipulate more then just one objekt and i wanna do this just in one shader.
thanks for help.
here is my test for touching a definite object:
//Cg
//
void vshader(
uniform float4x4 mat_modelproj,
in float4 vtx_Geo: POSITION,
out float4 l_position: POSITION)
{
l_position = mul(mat_modelproj,vtx_Geo);
}
void fshader(
in uniform float4 k_Time,
out float4 o_color: COLOR)
{
o_color=float4(1,0,0,0);
}
neither this is working:
void vshader(
uniform float4x4 mat_modelproj,
in uniform float4x4 k_Geo, // also not with a POSITION def.
out float4 l_position: POSITION)
{
l_position = mul(mat_modelproj,k_Geo);
}
void fshader(
in uniform float4 k_Time,
out float4 o_color: COLOR)
{
o_color=float4(k_Time[0]/2,0,0,0);
}
so i have to uniform if im doing a straigh vert input, but if im using the same column just by using the name i have only to varying? hmm, how makes this sense?
i dont have a vertex column named geo. i just used here a name which is related to.
Uniform variables are the same for the whole geom. Varying variables differ per vertex. In this case, you are referencing a vertex column named Geo, which doesn’t exist according to you, and therefore it doesn’t work. Did you mean to use “vtx_position” instead?
//Cg
//
void vshader (
uniform float4x4 mat_modelproj,
in float4 vtx_needle : POSITION,
out float4 l_my : POSITION,
out float4 l_position: POSITION
)
{
for (int x=0;x<10;x++) {
vtx_position = vtx_Plane;
l_position = mul(mat_modelproj, vtx_needle[x]);
l_my = l_position;
l_my.x = (valueX);
l_my.y = (valueY);
l_my.z = (valueZ);
}
}
or something like this:
//Cg
//
void vshader (
uniform float4x4 mat_modelproj,
in float4 vtx_needle : POSITION,
out float4 l_my : POSITION,
out float4 l_position: POSITION
)
{
int id =0;
//if its not possible to touch a named object array
for (int x=0;x<10;x++) {
//looping over 4 verts
for (xx= 0;xx<4;xx++) {
vert[id].x = (valueX);
vert[id].y = (valueY);
vert[id].z = (valueZ);
id++;
}
}
}
As for your first shader, you have two outputs bound to POSITION. You can only have one output to represent the clip-space position.
Secondly, why the for loop that does the same thing anyways? A vertex shader is executed per vertex.
As for your second shader, it makes not much sense, as a vertex shader is executed for one single vertex. Maybe you mean to use a geometry shader there, that can loop over the vertices of a triangle (or other primitive kind).