how to assign a color in horde3d ?

External libraries (GTK, GSL, SDL, Allegro, OpenGL, etc) questions.
Post Reply
fifoul
Posts: 15
Joined: Oct 17, 2005 13:20
Location: France

how to assign a color in horde3d ?

Post by fifoul »

I tested the horde 3D library and it works well, but I don't know how to assign a color to an object, i have try this code to Sets manually a shader uniform of a Material resource but it doesn't work :


h3dSetMaterialUniform(my_Material,"matDiffuseCol",1,0,0,1) ' try to pass a red color inside matDiffuseCol in the shader

dim as TH3DNode cubes(MAX_CUBES-1)
for i as integer =0 to MAX_CUBES-1
cubes(i)=h3dAddModelNode(H3DRootNode,"model",my_Geometry)
h3dAddMeshNode(cubes(i),"mesh",my_Material,0,36*3,0,3)
next


sample code issue of model.shader:

// Uniforms
float4 matDiffuseCol <
// string desc_abc = "abc: diffuse color";
// string desc_d = "d: alpha for opacity";
> = {1.0, 1.0, 1.0, 1.0};

Code: Select all



const as double pi=4*atn(1)
const as double deuxpi=pi*2
const as double pisur2=pi/2
const as double troispisur2=3*pisur2
const as double pisur4=pi/4
const as double pisur180=pi/180
const as double pisur360=pi/360

type vec3f
 as single x,y,z
end type

type vec3s
 as short x,y,z
end type

const size_mem=100000*3

redim as string text_line(size_mem)
redim shared as vec3f vertex(size_mem)
redim shared as vec3s normal(size_mem)
redim shared as single col(size_mem)
redim shared as ulong index(size_mem)

dim shared as integer face_index_max
dim shared as integer vertex_index_max


'-------------------------------------------------------------------------------

#include once "fbgfx.bi"
#include once "horde3d.bi"
#include once "load_ply.bi"

#ifndef NULL
 #define NULL cptr(any ptr,0)
#endif

'-------------------------------------------------------------------------------

dim as integer screen_size_h,screen_size_v
'screeninfo screen_size_h,screen_size_v
screen_size_h=1920
screen_size_v=1080
screenres screen_size_h,screen_size_v,32,0,fb.GFX_OPENGL
h3dInit() 


'-------------------------------------------------------------------------------

load_ply("cubes.ply")

var renderPipeline = h3dAddPipeline("pipelines/forward.pipeline.xml")
var my_Material    = h3dAddMaterial("models/cube/cube.material.xml")
h3dutLoadResourcesFromDisk(exepath()+"/Content")

var camNode=h3dAddCameraNode(H3DRootNode,"Camera",RenderPipeline)
h3dSetNodeParamI(camNode,H3DCamera_ViewportWidthI,screen_size_h)
h3dSetNodeParamI(camNode,H3DCamera_ViewportHeightI,screen_size_v)
h3dResizePipelineBuffers(RenderPipeline,screen_size_h,screen_size_v)
h3dSetupCameraView(camNode,60,screen_size_h/screen_size_v,0.1,1000)

var lightNode=h3dAddLightNode(H3DRootNode,"Light1",0,"LIGHTING","SHADOWMAP")
h3dSetNodeParamI(lightNode,H3DLight_ShadowMapCountI,1)
h3dSetNodeParamF(lightNode,H3DLight_RadiusF,0,180)
h3dSetNodeParamF(lightNode,H3DLight_FovF,0,180)
h3dSetNodeParamF(lightNode,H3DLight_ColorF3,0,1)                                ' red
h3dSetNodeParamF(lightNode,H3DLight_ColorF3,1,1)                                ' green
h3dSetNodeParamF(lightNode,H3DLight_ColorF3,2,1)                                ' blue
h3dSetNodeParamF(lightNode,H3DLight_ShadowMapBiasF,0,0.01)

dim as TH3DNode meshNode
var my_geometry=h3dutCreateGeometryRes("forme a",ubound(vertex)+1,ubound(index)+1,@vertex(0).x,@index(0),@normal(0).x,NULL,NULL,NULL,NULL)
 
const as integer MAX_CUBES=50*50
dim as TH3DNode cubes(MAX_CUBES-1)
for i as integer =0 to MAX_CUBES-1
 cubes(i)=h3dAddModelNode(H3DRootNode,"model",my_Geometry)
 h3dAddMeshNode(cubes(i),"mesh",my_Material,0,36*3,0,3)
next

' set cube position
for i as integer =0 to MAX_CUBES-1
 var z=(i\50)-50/2
 var x=(i mod 50)-50/2
 h3dSetNodeTransform(cubes(i),x*2,-3,z*2,0,10,0,1.5,.4,0.8)
next
h3dSetNodeTransform(cubes(5),2,3,5,0,0,0,4,1,4)
h3dSetNodeTransform(cubes(6),-5,3,3,0,0,0,2,1,2)


'-------------------------------------------------------------------------------

type single_xyz
 x as single
 y as single
 z as single
end type

dim shared as single_xyz eye_pos,eye_rot,light_pos,light_rot
dim shared as integer angle_vision
dim shared as single cam_decal_step

dim as integer frames
dim as integer xmouse,ymouse,molette,mousebouton
dim as integer xmouse_old,ymouse_old,molette_old,xmouse_dif,ymouse_dif
dim as string key

cam_decal_step=.25
angle_vision=60
eye_pos.y=5
eye_pos.z=20
light_pos.z=8
light_pos.y=16
light_rot.x=-60


'h3dSetOption(H3DOptions_DebugViewMode,1)
'h3dSetOption(H3DOptions_WireframeMode,1)
h3dSetOption(H3DOptions_TrilinearFiltering,0)
h3dSetOption(H3DOptions_LoadTextures,0)
h3dSetOption(H3DOptions_TexCompression,0)
h3dSetOption(H3DOptions_FastAnimation,0)
h3dSetOption(H3DOptions_ShadowMapSize,2048)

do:

 GETMOUSE xmouse,ymouse,molette,mousebouton
 
 if mousebouton=1 or mousebouton=2 then
  xmouse_dif=xmouse-screen_size_h/2
  ymouse_dif=ymouse-screen_size_v/2
  eye_rot.y-=xmouse_dif/100
  eye_rot.x-=ymouse_dif/100
 end if
 
 if mousebouton=3 then
  xmouse_dif=xmouse-screen_size_h/2
  ymouse_dif=ymouse-screen_size_v/2
  'light_rot.x-=ymouse_dif/100
 end if
 
 if xmouse>-1 then xmouse_old=xmouse
 if ymouse>-1 then ymouse_old=ymouse
   
 if molette<>-1 then
  if molette<molette_old then
   angle_vision+=2:if angle_vision>120 then angle_vision=120
   molette_old=molette
  end if 
  if molette>molette_old then
   angle_vision-=2:if angle_vision<2 then angle_vision=2
   molette_old=molette
  end if 
 end if
 
 key=inkey
 if key=Chr(27) then goto fin
 
 if key=Chr(255)&"H" or mousebouton=1 then                                      ' fleche haut
  if mousebouton=3 then
   light_pos.z-=cos(eye_rot.y*pisur180)*cam_decal_step
   light_pos.x-=sin(eye_rot.y*pisur180)*cam_decal_step
  else    
   eye_pos.z-=cos(eye_rot.y*pisur180)*cam_decal_step
   eye_pos.x-=sin(eye_rot.y*pisur180)*cam_decal_step
  end if
 end if 
 
 if key=Chr(255)&"P" then                                                       ' fleche bas
  if mousebouton=3 then
   light_pos.z+=cos(eye_rot.y*pisur180)*cam_decal_step
   light_pos.x+=sin(eye_rot.y*pisur180)*cam_decal_step   
  else    
   eye_pos.z+=cos(eye_rot.y*pisur180)*cam_decal_step
   eye_pos.x+=sin(eye_rot.y*pisur180)*cam_decal_step
  end if
 end if

 if key=Chr(255)&"K" then                                                       ' fleche gauche
  if mousebouton=3 then
   light_pos.z-=cos(eye_rot.y*pisur180+pisur2)*cam_decal_step
   light_pos.x-=sin(eye_rot.y*pisur180+pisur2)*cam_decal_step  
  else    
   eye_pos.z-=cos(eye_rot.y*pisur180+pisur2)*cam_decal_step
   eye_pos.x-=sin(eye_rot.y*pisur180+pisur2)*cam_decal_step
  end if
 end if
 
 if key=Chr(255)&"M" then                                                       ' fleche droite
  if mousebouton=3 then
   light_pos.z+=cos(eye_rot.y*pisur180+pisur2)*cam_decal_step
   light_pos.x+=sin(eye_rot.y*pisur180+pisur2)*cam_decal_step  
  else    
   eye_pos.z+=cos(eye_rot.y*pisur180+pisur2)*cam_decal_step
   eye_pos.x+=sin(eye_rot.y*pisur180+pisur2)*cam_decal_step
  end if
 end if

 if key=Chr(255)&"I" then                                                       ' fleche page up
  if mousebouton=3 then
   light_pos.y+=cam_decal_step   
  else    
   eye_pos.y+=cam_decal_step
  end if 
 end if 
  
 if key=Chr(255)&"Q"  then                                                      ' fleche page down
  if mousebouton=3 then
   light_pos.y-=cam_decal_step   
  else    
   eye_pos.y-=cam_decal_step
  end if 
 end if 
 
 h3dSetupCameraView(camNode,angle_vision,screen_size_h/screen_size_v,0.1,1000)
 h3dSetNodeTransform(camNode,eye_pos.x,eye_pos.y,eye_pos.z,eye_rot.x,eye_rot.y,eye_rot.z,1,1,1)
 h3dSetNodeTransform(lightNode,light_pos.x,light_pos.y,light_pos.z,light_rot.x,light_rot.y,light_rot.z,1,1,1)
 h3dRender(camNode)
 h3dFinalizeFrame()
 flip
 
loop


'-------------------------------------------------------------------------------

fin:

h3dRelease()


'-------------------------------------------------------------------------------


D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: how to assign a color in horde3d ?

Post by D.J.Peters »

If you you know the resource id and element id you can try something like this:

Code: Select all

' set uniform red,green,blue,alpha
h3dSetResParamF(resID, H3DMatRes_UniformElem, elemIdx, H3DMatRes_UnifValueF4, 0, r)
h3dSetResParamF(resID, H3DMatRes_UniformElem, elemIdx, H3DMatRes_UnifValueF4, 1, g)
h3dSetResParamF(resID, H3DMatRes_UniformElem, elemIdx, H3DMatRes_UnifValueF4, 2, b)
h3dSetResParamF(resID, H3DMatRes_UniformElem, elemIdx, H3DMatRes_UnifValueF4, 3, a)
fifoul
Posts: 15
Joined: Oct 17, 2005 13:20
Location: France

Re: how to assign a color in horde3d ?

Post by fifoul »

Thanks D.J.Peters,

I will try to find the ressource_id and element_id by myself, I am novice.

Here you can find the link to the full test program : https://transfert.free.fr/U0i0vbx

this variable is located inside the file named TEST LIB HORDE3D\Content\shaders\model.shader
The name of the variable is : 'matDiffuseCol'
Post Reply