Sign in to follow this  
mlt

Input for a Vertex program?

Recommended Posts

I am currently trying to use the vertex program from the Cg Toolkit it looks something like this:
struct vertex_input
{
  float4 position : POSITION;
  float4 normal   : NORMAL;
};

struct vertex_output
{
  // Homogeneous 'clip-space' coordinates.	
  float4 position : POSITION;
  float4 color    : COLOR;
};

vertex_output main(
                     vertex_input IN
                   , uniform float4x4 ModelViewProj ///< Concatenation of the modelview and projection matrices
                   , uniform float4x4 ModelViewIT   ///< The inverse transpose of the modelview matrix.
                   , uniform float4   light_vector	///< Specification of light location.
             )
{
  vertex_output OUT;
  
  OUT.position = mul(ModelViewIT, IN.position);
  
...
...

In another file its loaded and enabled:
m_vertex_program.load_from_file(vertex_program,demo_path + "vp_lighting.cg");
m_vertex_program.set_modelview_matrix();
m_vertex_program.set_modelview_projection_matrix();
m_vertex_program.set_modelview_inverse_transpose();
m_vertex_program.set_float_param("light_vec",light_vec_x,light_vec_y,light_vec_z);

But I don't understand how the struct "vertex_input" to the vertex program is passed. In the main program in the vertex program the parameter IN should be passed from somewhere right?

Share this post


Link to post
Share on other sites
When you render something, it automatically goes into that struct thing.
Of course, in reality, what happens is that when the program is compiled, a special binary gets generated.
float4 position : POSITION and float4 normal : NORMAL are basically input streams.

Share this post


Link to post
Share on other sites
Quote:
Original post by mlt
Ok so there is no way to manually specify arguments to the entry function in a vertex program? They just get passed magically?
There is nothing magic about it:
struct vertex_input
{
float4 position : POSITION;
float4 normal : NORMAL;
};
Here float4 position is bound to the position attribute of each vertex (i.e. the value submitted using glVertex or glVertexPointer), and float4 normal is bound to the normal attribute (glNormal or glNormalPointer).

Share this post


Link to post
Share on other sites
Quote:
Original post by mlt
Is it possible to give an example of how the vertex program is called, supplying all the parameters (including the vertex_in struct)?
I think you need to read up on how shaders work. A shader is not 'called', instead it is bound. In psuedo code it would look something like this:
bind_shader( my_shader )

glBegin(GL_TRIANGLES);

glVertex3f(..)
glNormal3f(...)
...
...

glEnd();

unbind_shader( my_shader )


Now, as long as the shader in bound, it will be executed once for every vertex you specify (using glVertex, glNormal), and each time, the vertex_in struct will be filled out with the current vertex position and normal vector.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this