Main Page | Report this Page
Computers Forum Index  »  Computer - Graphics (Algorithms)  »  3d to 2d vertex shader not working ?...
Page 1 of 1    

3d to 2d vertex shader not working ?...

Author Message
Skybuck Flying...
Posted: Fri Sep 18, 2009 5:16 am
Guest
// vertex shader
function VertexProgramString : AnsiString;
begin
result :=
'float4x4 WorldViewProj : WorldViewProjection;' + ConstNewLine +
'float4 MainVS(float3 pos : POSITION) : POSITION{' + ConstNewLine +
' float3 test;' + ConstNewLine +
' test = pos;' + ConstNewLine +
' test.x = 0;' + ConstNewLine +
' test.y = 0;' + ConstNewLine +
' test.z = 0;' + ConstNewLine +
//' test.y = test.y + cos(test.x/100)*60 + sin(test.z/100)*50;' +
ConstNewLine +
' return mul(WorldViewProj, float4(test.xyz, 1.0));' + ConstNewLine +
'}';
end;

// pixel shader
function FragmentProgramString : AnsiString;
begin
result :=
'float3 MainFS( float2 Position : TEXCOORD0, float3 Color : COLOR ) :
COLOR' + ConstNewLine +
'{' + ConstNewLine +
' float3 vOutput;' + ConstNewLine +
'' + ConstNewLine +
' vOutput.r = 1;' + ConstNewLine +
' vOutput.g = 0;' + ConstNewLine +
' vOutput.b = 0;' + ConstNewLine +
'' + ConstNewLine +
' return vOutput;' + ConstNewLine +
'}';
end;

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0-0.5, mOpenGL.Width-0.5, mOpenGL.Height-0.5, 0.0 - 0.5);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(0, 0, mOpenGL.Width, mOpenGL.Height );

glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
glLoadIdentity;

cgGLEnableProfile(CG_PROFILE_GLSLF);
cgGLBindProgram(mCG_VertexProgramHandle);
cgGLBindProgram(mCG_FragmentProgramHandle);

CreateVertexPoints( mOpenGL.Width, mOpenGL.Height );


The fragment shader is working... all verteces are red.

The vertex shader does not seem to work...

Any idea what's wrong ?

Bye,
Skybuck.
 
Skybuck Flying...
Posted: Fri Sep 18, 2009 5:16 am
Guest
Maybe vertex creation is the problem ?

Maybe verteces must be stuff into vertex buffer first ?

procedure CreateVertexPoints( ParaWidth : integer; ParaHeight : integer );
var
vX : integer;
vY : integer;
// vZ : integer;
begin
glBegin(GL_POINTS);

// vZ := -500;
for vY := 0 to ParaHeight-1 do
begin
for vX := 0 to ParaWidth-1 do
begin
// glVertex3f( vX, vY, 0 {-2 + Random(5)} );
// glVertex3f( 0.5 + vX, 0.5 + vY, 0 {-2 + Random(5)} );
// vZ := vZ + 1;
// glVertex3f( vX, vY, 1 + Random(2) );
glVertex3f( vX, vY, 0 );
end;
end;
glEnd;
end;

No idea...

Bye,
Skybuck.
 
Skybuck Flying...
Posted: Fri Sep 18, 2009 5:16 am
Guest
Well the problem must be with my code somewhere...

Since I just modified the NeHe lesson47a (saved as lesson47b).

And after screwing around with the code.. GL_POINTS does seem to work for
vertex shaders...

So I will have to port that code to my code and see if it's then gonna work
! ;)

Bye,
Skybuck.
 
Wolfgang Draxinger...
Posted: Fri Sep 18, 2009 1:15 pm
Guest
Skybuck Flying wrote:

Quote:
The fragment shader is working... all verteces are red.
The vertex shader does not seem to work...
Any idea what's wrong ?

Just out of curiosity: Why are you using Cg and not GLSL?

BTW: You shouldn't hardcode the shader code into the program binary, but
instead load them from external files instead. This makes things A LOT
easier. A change in the shader then doesn't require a full recompilation of
the program.


Wolfgang
 
Skybuck Flying...
Posted: Fri Sep 18, 2009 6:37 pm
Guest
In NeHe the code does work but I had to change the vertex coordinates.

They seem to go from -1.0 to 1.0

Anybody know why and if it can be changed to go from 0,0 to 640,480 without
having to do a *Width, *Height ?

procedure CreateVertexPoints( ParaWidth : integer; ParaHeight : integer );
var
vX : integer;
vY : integer;
begin
glBegin(GL_POINTS);

for vY := 0 to ParaHeight-1 do
begin
for vX := 0 to ParaWidth-1 do
begin
glVertex3f( (-320 + vX)/320, (-240 + vY)/240, 0 );
end;
end;
glEnd;
end;

Bye,
Skybuck.
 
Skybuck Flying...
Posted: Sat Sep 19, 2009 12:12 am
Guest
The problem seems to be with a TopenGL component which I didn't write... so
it's pretty much out of my control...

For some reason it just doesn't work for the vertex shader... strange.

Bye,
Skybuck.
 
Skybuck Flying...
Posted: Sat Sep 19, 2009 12:24 am
Guest
Well tried that... doesn't seem to be it...
 
Skybuck Flying...
Posted: Sat Sep 19, 2009 12:26 am
Guest
I was hoping to use the control to maybe some day do some graphics... with
vertex shader support I am not sure if that's possible because the texture
coordinates or positions need to be set...

Anyway I could now switch to other source code without actually graphics and
then work with arrays... actually I could then draw those arrays to the
screen so this would be pretty good as well.

Bye,
Skybuck.
 
Skybuck Flying...
Posted: Sun Sep 20, 2009 12:08 am
Guest
According to FX Composer 2.5 it seems my vertex shader was flawed...
actually the code was ripped from the internet I think and then modified.

The problem seemed to be with the "mul" parameters... these needed to be
swapped.

Now it seems to draw alright:

At least in FX Composer 2.5... (z and y would need to be swapped though)

Here are my basic vertex and pixel shader examples ! ;)

Now I try them out to see if that helps in the opengl delphi test program...

// *** BEGIN OF EXAMPLE ***

/*

% Description of my shader.
% Second line of description for my shader.

keywords: material classic

date: YYMMDD

*/

// unused for now:
texture ColorTexture <
string ResourceName = "default_color.dds";
string UIName = "Diffuse Texture";
string ResourceType = "2D";
Quote:
;

//float4x4 WorldViewProj : WorldViewProjection;

// Vertex shader input structure
struct VertexShaderInput
{
// basic inputs
float4 mPosition : POSITION;
float2 mTexture : TEXCOORD0;

// extra inputs, go here
float3 mExample : COLOR1;

};


// Vertex shader output structure
struct VertexShaderOutput
{
// basic outputs
float4 mPosition : POSITION;
float2 mTexture : TEXCOORD0;
};


// Global variables
float4x4 GlobalModelViewProjection : WorldViewProjection;


// Name: Simple Vertex Shader
// Type: Vertex shader
// Desc: Vertex transformation and texture coord set to vertex position
//
VertexShaderOutput VertexShaderMain( in VertexShaderInput ParaInput )
{
VertexShaderOutput vOut; // create an output vertex local variable

// ParaInput.mPosition.z = sin(ParaInput.mPosition.x*3.14/3 +
ParaInput.mPosition.y*3.14/5 + ParaInput.mExample.x)*3;

vOut.mPosition = mul(GlobalModelViewProjection, ParaInput.mPosition);
//apply vertex transformation

vOut.mTexture.x = ParaInput.mPosition.x;
vOut.mTexture.y = ParaInput.mPosition.z;

// vOut.mTexture = ParaInput.mTexture; //copy original texcoords
return vOut; //return output vertex
}


//float4 mainVS(float3 pos : POSITION) : POSITION{
// return mul(WorldViewProj, float4(pos.xyz, 1.0));
//}

//float4 mainPS() : COLOR {
//
// return float4(1.0, 1.0, 1.0, 1.0);
//}


// Pixel shader input structure
struct TPixelShaderInput
{
// basic inputs
float2 mPosition : TEXCOORD0;
float2 mColor : COLOR;

// extra inputs, go here
};


// Pixel shader output structure
struct TPixelShaderOutput
{
// basic outputs
// maybe must be float4 ?
float3 mColor : COLOR;
};


// Global variables would go here


// Name: Simple Pixel Shader
// Type: Pixel shader
// Desc: Set pixel color based on input position

// basic pixel shader
TPixelShaderOutput PixelShaderMain( in TPixelShaderInput ParaInput ) : COLOR
{
TPixelShaderOutput vOutput;

vOutput.mColor.r = 0.5 + ParaInput.mPosition.x/2;
vOutput.mColor.g = 0.5 + ParaInput.mPosition.y/2;
vOutput.mColor.b = 0;

return vOutput;
}


technique technique0 {
pass p0 {
CullFaceEnable = false;
VertexProgram = compile vp40 VertexShaderMain();
FragmentProgram = compile fp40 PixelShaderMain();
}
}

// *** END OF EXAMPLE ***

Bye,
Skybuck.
 
Skybuck Flying...
Posted: Sun Sep 20, 2009 12:15 am
Guest
No TopenGL still not working with vertex shader:

////////////////////////////////////////////////////////////////////////////////
// TOpenGLv2 component
//
// Component that allows to deal with the OpenGL library
//
// by Alan GARNY
// gry at (no spam) physiol.ox.ac.uk
// http://pc-heartbreak.physiol.ox.ac.uk/
//
// © Copyright 1998-99
////////////////////////////////////////////////////////////////////////////////
// Date of Creation: 15/10/98 (based on the BCB version of TOpenGLv2)
//
// Modifications: (model: [<initials>, dd/mm/yy] <what has been done>)
// [GRY, 06/11/98] Made the method "GetOpenGLBmp" public to allow the
programmer
// to get a "TBitmap*" of the rendered scene. The user has
to
// release the "handle" to the bitmap of the rendered scene,
we
// then have a "ReleaseOpenGLBmp" method (like for "GetDC"
and
// "ReleaseDC")
// [GRY, 08/03/99] Fixed a problem of memory leak in the "Print" method
// [GRY, 23/10/99] Removed the backward compatibility in order to make
things
// easier for the future
// [GRY, 23/10/99] Added the OnKeyDown, OnKeyPress, OnKeyUp, OnMouseWheel,
// OnMouseWheelDown and OnMouseWheelUp events (suggested to
us
// by Nimai C. Malle, nimai_malle at (no spam) yahoo.com)
////////////////////////////////////////////////////////////////////////////////

Wacky ?!

Bye,
Skybuck.
 
 
Page 1 of 1    
All times are GMT
The time now is Thu Nov 26, 2009 7:11 pm