Skip to main content
gamedev.net
🔒 Locked

shaders language

Started by penetrator 0 replies 350+ views
Original Post
penetrator
penetrator
After studying ARB papers, i have the idea that they explain everything and nothing at the same time. I''m stubborn i know, but i can''t figure out how you can write a fragment program using instructions which are not shown in any tutorial. Do you have an idea, for example, on how to distort texture coordinates or vertices using those ADD,MUL,TEX commands ? There is any tutorial that decently explain how they should be used ? Or that explain how to pass parameters from the main opengl pipe to a fragment or vertex program ?
Ostsol
Ostsol
If you look at some of the diagrams of the graphics pipeline, you''ll notice that the programmable pipelines (vertex and fragment shaders) replace corresponging fixed function pipelines. The way the data gets sent in does not at all change, though. A vertex shader receives the same data, for example, that the old-style transform and lighting engine would.

Lets look at three types of registers available in vertex shaders:

Input Registers
Output Registers
General Purpose Registers

The basic input registers in ARB_vertex_program are all part of a vertex structure named "vertex". There are elements in this structure for each of the vertex attributes. "position", "normal", "color", and "texcoord[n]" are some of the most basic ones. Accessing an input register is as simple as accessing an element of a structure in C++. "vertex.position" accesses the vertex position, for example.

Not exactly a register, there are also input parameters that contain data such as matrices, lighting parameters, and such. The matrices are under the "state" structure, which contains a "matrix" structure, which in turn contains the specific matrices. "state.matrix.mvp", for example, contains the combined modelview-projectin matrix that you may multiply with vertex positions to get their final, transformed positions.

Output registers in ARB_vertex_program all are under the "output" structure. Each one corresponds to a vertex attribute: "position", "color", "normal", texcoord[n]", etc. Once you''re done manipulating the data, passing it into the output registers will send it off to the next stage in the rendering pipeline.

General purpose registers in ARB_vertex_program are just temporary data storage units. The actual manipulation of data will be done using these registers.

In a vertex program you can also create aliases of registers in order to shorten names or make them more meaningful to you. For input registers this is accomplished via the "ATTRIB" keyword. For parameters this is accomplished via the "PARAM" keyword. For output registers this is accomplished via the "OUTPUT" keyword. I''ll demonstrate each in a commented example (the pound symbol, #, is the comment character, BTW):

!!ARBvp1.0

# declare aliases for the input registers
ATTRIB iPos = vertex.position;
ATTRIB iColour = vertex.color;
ATTRIB iTexCoord0 = vertex.texcoord[0];

# declare an alias for the MVP matrix
PARAM iMVP[4] = {state.matrix.mvp};

# declare aliases for the output registers
OUTPUT oPos = result.position;
OUTPUT oColour = result.color;
OUTPUT oTexCoord0 = result.texcoord[0];

# multiply the vertex position by the MVP matrix
# and output it to the output register
DP4 oPos.x, iMVP[0], iPos;
DP4 oPos.y, iMVP[1], iPos;
DP4 oPos.z, iMVP[2], iPos;
DP4 oPos.w, iMVP[3], iPos;

# move other data to the output registers
MOV oColour, iColour;
MOV oTexCoord0, iTexCoord0;

# end the program
END


Once you understand one shader, the other isn''t very hard to learn. The real difficult part isn''t the syntax, but understanding the various equations (such as lighting) for manipulating data.
-Ostsol

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.