Jump to content

  • Log In with Google      Sign In   
  • Create Account

akaitora

Member Since 05 Sep 2008
Offline Last Active Apr 27 2014 10:28 PM

Topics I've Started

CullMode Render State In CG Shading Language

12 March 2014 - 10:59 PM

 In CG, you can set the cull mode render state in an fx like so

pass p0
{
	CullFaceEnable = bool(true);
	CullMode = int(Back);

	VertexShader = compile glslv myvs( __model, __modelViewProjection, __deltaTime, edge );
	PixelShader = compile glslf myps(decalSampler);
} 

However the CullMode render state is only support for OpenGL and not DirectX.  

 

http://http.developer.nvidia.com/Cg/CullMode.html

 

Is there some render state I can use to set the Culling mode when rendering in DirectX in a CG shader?  Thanks!

 

 


Can you do string compares in Shaders?

18 January 2014 - 01:55 PM

I am using CG and am passing a string into my shader as a uniform parameter.  I would like to do a string compare in my vertex shader.  Is that possible?  Is so, how?  Thanks!


Weird Issue With CG Runtime

29 December 2013 - 04:02 PM

Hey guys!  I am integrating the CG Runtime into my game engine but I am running into a strange issue.

My engine only supports loading cgfx.  I wrote the following shader as a test

struct VS_INPUT
{
      float4 position  : POSITION;
      float4 normal    : NORMAL;
};
 
struct VS_OUTPUT
{
      float4 color   : COLOR0;
      float4 position  : POSITION;
};
 
VS_OUTPUT myvs( uniform float4x4 ModelViewProj, const VS_INPUT vin)
{
        VS_OUTPUT vout;
        float4 position = mul(ModelViewProj, vin.position) * 5.0;
        vout.position = position;
        vout.color = float4(1,0,0,1);
 
        return vout;
}
 
float4x4 mvp    :       WorldViewProjection;
 
technique main
{
        pass p0
        {
                Zenable = false;
                ZWriteEnable = false;
 
                VertexProgram = compile vs_1_1 myvs( mvp );
        }
}

When running, I can visually see that the ZEnable state is disabled but my 3d model is not showing up as red.  The vertex program that I wrote should have shaded my model red.  I am doing the following to render my model

 

cgSetPassState( this->pass );
 
// Send Verts and normals to directx
mpDev->SetFVF(D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE | D3DFVF_TEX1);
mpDev->DrawPrimitiveUP(D3DPT_TRIANGLELIST, 1, t, sizeof(CUSTOMVERT));
 
cgResetPassState( this->pass );
 

 

 

Will the cgSetPassState also invoke the VertexProgram or is there something else I need to do to invoke it?  Thanks!


Not sure what is causing this Visual Artifact

02 September 2013 - 01:06 PM

I am currently learning MelScript and am writing an obj exporter.  I able able to export all of the geometry however when I import my model into Unity, I see stange black shading all over my model.  The only thing I can think of is that I am exporting the normals incorrectly.  What does it look like to you guys?

 

Robot.png

 

The following is my MelScript code


<pre>
global proc int GetFaceCountInObject( string $obj )
{ 
   int $iFaceCount[] = `polyEvaluate -f $obj`; return $iFaceCount[0];
} 

global proc int GetVertexCountInObject( string $obj )
{ 
   int $iVertexCount[] = `polyEvaluate -v $obj`; 
   return $iVertexCount[0];
} 

global proc vector[] GetVerticiesInObject( string $obj )
{ 
   select -r $obj; 
   changeSelectMode -object; 
   Triangulate; 
   int $iVertCount = GetVertexCountInObject( $obj ); 
   vector $v[]; 
   int $vi = 0; 
   for( $i=0; $i < $iVertCount; ++$i ) 
   { 
          float $vert[] = `getAttr ($obj+".vt["+$i+"]")`; 
          $v[$vi] = <<$vert[0], $vert[1], $vert[2]>>; 
          ++$vi; 
   } 

   return $v;
} 

global proc vector[] GetUVSInObjects( string $obj )
{
   select -r $obj;
   changeSelectMode - object;
   Triangulate;
   int $iNumUVs = GetVertexCountInObject( $obj );
   vector $uv[];
   int $uvi = 0;
   for( $i = 0; $i < $iNumUVs; ++$i )
   {
      float $vert[] = `getAttr ($obj+".uv["+$i+"]")`; 
      $uv[$uvi] = <<$vert[0], $vert[1], 0>>; 
      ++$uvi; 
   }

   return $uv;
}

global proc vector[] GetNormalsInObject( string $obj )
{ 
   select -r $obj; 
   changeSelectMode -object; 
   Triangulate; 
   int $iNumFaces = GetFaceCountInObject( $obj ); 
   vector $n[]; 
   int $ni = 0; 
   for( $i = 0; $i < $iNumFaces; ++$i ) 
   { 
      select -r ($obj+".f["+$i+"]"); 
      string $normalInfo[] = `polyInfo -fn`; 
      string $split[]; tokenize $normalInfo[0] $split; 
      vector $normal = << (float)$split[2], (float)$split[3], (float)$split[4] >>; 
      $normal = unit($normal); 
      $n[$ni] = $normal; 
      ++$ni; 
   } 

   return $n;
}

// Each vector contains the indicies into its verticies.
// IE. <<1,3,2>> means vert 1, vert 3, vert 2.
global proc vector[] GetFacesInObject( string $obj )
{ 
   select -r $obj; 
   changeSelectMode -object; 
   Triangulate; 
   int $iNumFaces = GetFaceCountInObject( $obj ); 
   vector $f[]; int $fi = 0; 
   select -r $obj; 
   for( $i = 0; $i < $iNumFaces; ++$i ) 
   { 
      select -r ($obj+".f["+$i+"]"); 
      string $vinds[] = `polyInfo -fv`; 
      string $split[]; tokenize $vinds[0] $split; 
      int $v1 = ((int)$split[2])+1; 
      int $v2 = ((int)$split[3])+1; 
      int $v3 = ((int)$split[4])+1; 

      $f[$fi] = << $v1, $v2, $v3 >>; ++$fi; 
   }
   return $f;
}  

global proc matrix GetWorldMatrixOfObject( string $obj )
{  
   string $data[] = `listRelatives -p $obj`;
   string $transform = $data[0]; 
   float $f[] = `xform -q -ws -m $transform`;
   matrix $m[4][4] = << $f[0], $f[1],   $f[2],    $f[3]; 
                        $f[4], $f[5],   $f[6],    $f[7]; 
                        $f[8], $f[9],   $f[10],   $f[11]; 
                        $f[12], $f[13], $f[14],   $f[15] >>;
 
   return $m; 
} 

global proc vector TransformPoint( vector $p, matrix $m )
{ 
   float $f[]; 
   $f[0] = $p.x * $m[0][0] + $p.y * $m[1][0] + $p.z * $m[2][0] + $m[3][0]; 
   $f[1] = $p.x * $m[0][1] + $p.y * $m[1][1] + $p.z * $m[2][1] + $m[3][1]; 
   $f[2] = $p.x * $m[0][2] + $p.y * $m[1][2] + $p.z * $m[2][2] + $m[3][2]; 

   vector $v = << $f[0], $f[1], $f[2] >>; 

   return $v;
} 

global proc vector GetWorldPositionOfObject( string $obj )
{ 
   float $f[] = `getAttr ($obj+".parentMatrix")`; 
   vector $vPos = << $f[12], $f[13], $f[14] >>; 
   return $vPos;
} 

proc ExportScene(string $fileName)
{ 
   string $nodes[] = `ls -type "mesh"`; 
   $f = `fopen $fileName "w"`; 
   int $iTotalVerts = 0; 
   int $iTotalFaces = 0; 

   for($n in $nodes) 
   { 
      $iTotalVerts += GetVertexCountInObject( $n ); 
      $iTotalFaces += GetFaceCountInObject( $n ); 
   } 

   fprint $f ("# Vertex Count: " + $iTotalVerts + "\n"); 
   fprint $f ("# Face Count: " + $iTotalFaces + "\n\n"); 
   vector $verts[]; 
   vector $normals[];
   vector $faces[]; 
   $vi = 0; 
   $ni = 0;
   $uvi = 0; 

   // Export all vertex information. 
   fprint $f ("# Vertex Information\n"); 
   for ( $obj in $nodes ) 
   { 
      matrix $matWorld[4][4] = GetWorldMatrixOfObject( $obj ); 
      vector $localVerts[] = GetVerticiesInObject( $obj ); 

      for( $i = 0; $i < size($localVerts); ++$i ) 
      { 
        vector $v = TransformPoint( $localVerts[$i], $matWorld );
        $verts[$i] = $v; 
        fprint $f ("v " + $v.x + " " + $v.y + " " + $v.z + "\n"); 
      } 
   } 

   fprint $f ("\n"); fprint $f ("# Normal Information\n"); 
   for ( $obj in $nodes ) 
   {
      matrix $matWorld[4][4] = GetWorldMatrixOfObject( $obj );  
      vector $localNormals[] = GetNormalsInObject( $obj );
      for( $i = 0; $i < size($localNormals); ++$i ) 
      { 
         //vector $normal = $localNormals[$i];
         vector $normal = TransformPoint( $localNormals[$i], $matWorld ); 
         $normals[$i] = $normal; 
         fprint $f ("n " + $normal.x + " " + $normal.y + " " + $normal.z + "\n"); 
      } 
   } 

   fprint $f ("\n"); fprint $f ("# UV Information\n"); 
   for ( $obj in $nodes ) 
   { 
      vector $localUVs[] = GetUVSInObjects( $obj ); 
      for( $i = 0; $i < size($localUVs); ++$i ) 
      { 
         vector $uv = $localUVs[$i]; 
         $uvs[$i] = $uv; 
         fprint $f ("vt " + $uv.x + " " + $uv.y + " " + $uv.z + "\n"); 
      } 
   }

   fprint $f ("\n"); 
   fprint $f ("# Face Information\n"); 
   int $iVC = 0; 
   int $iNC = 0;
   int $nbase = 0; 
   int $vbase = 0; 
   for ( $obj in $nodes ) 
   { 
      vector $localVerts[] = GetVerticiesInObject( $obj ); 
      vector $localNormals[] = GetNormalsInObject( $obj ); 
      vector $faces[] = GetFacesInObject( $obj ); 
      for( $i = 0; $i < size($faces); ++$i ) 
      { 
         vector $fv = $faces[$i]; 
         vector $fvf = << $fv.x + $vbase, $fv.y + $vbase, $fv.z + $vbase >>; 
         vector $nvf = << $fv.x + $nbase, $fv.y + $nbase, $fv.z + $nbase >>;
         int $uv = 1;
         fprint $f ("f " + $fvf.x + "\/" +$uv+ "\/" + $nvf.x + " " + $fvf.y + "\/" +$uv+ "\/" + $nvf.y + " " + $fvf.z + "\/"+$uv+"\/" + $nvf.z + "\n"); 
         ++$iVC; 
         ++$iNC;
      } 

      $nbase += size($localNormals); 
      $vbase += size($localVerts); 
   } 

   fclose $f; 

   select -cl;
} 

proc int onSave( string $filename, string $type )
{ 
   ExportScene($filename); 
   return true;
} 

string $result[] = `fileDialog2 -fileFilter "*.OBJ (*.obj)" -dialogStyle 2 -fileMode 0 -okc "Export"`;

if ( size($result) > 0 )
{
   onSave( $result[0], "");
}

</pre>


Extracting an object's forward vector from their rotation quaternion

14 June 2012 - 10:41 AM

So the way I am currently trying to achieve this is like so

public void GetForwardVector( Quaternion quatRot )
{
Quaternion quatInverse = GetQuaternionInverse( quatRot ); // Quaternion Conjugate / Quaternion Magnitude
Vector3 vecFwd = quatInverse * Vector3.Forward;

return vecFwd;
}

However this seems to give me a reflected forward vector. Multiplying by -Vector3.Forward doesn't seem to give me what I want either. Any ideas? Thanks!

PARTNERS