PDA

View Full Version : Introduce How to use .vert and .frag on OGLES2

feelthat
11-29-2013, 05:27 AM
http://chimera.labs.oreilly.com/books/1234000001814/index.html

.vert and .frag example source code on xcode 4

GLSL work outline
http://www.cse.ohio-state.edu/~whmin/courses/cse5542-2013-spring/13-GLSL.pdf

GLSL commands list
http://mew.cx/glsl_quickref.pdf

transpose and Inverse Matrix
http://www.katjaas.nl/transpose/transpose.html
http://www.mathsisfun.com/algebra/matrix-inverse.html

verctor normalize
http://www.ditutor.com/vec/normalizing_vector.html

dot product
http://www.mathsisfun.com/algebra/vectors-dot-product.html

cross product generate a normal vector

normal vector on a plane to generate normal vector transformation.
http://www.songho.ca/math/plane/plane.html

GL Matrix Transform Description and example execute code
http://www.songho.ca/opengl/gl_transform.html

feelthat
11-29-2013, 07:13 AM

////////////attribute/////////
1 Position dimensionality 2,3,4
2 Normal dimensionality 3
3 Color dimensionality 4
4 Point Size dimensionality 1
5 Texture Coordinate dimensionality 2,3,4
6 Generic Attribute(ES 2.0) dimensionality 1,2,3,4

dimensionality
typedef Vector1<float> vec1;
vec1(1.0);

typedef Vector2<float> vec2;
vec2(1.0,1.0);

typedef Vector3<float> vec3;
vec3(1.0,1.0,1.0);

typedef Vector4<float> vec4;
vec4(1.0,1.0,1.0,1.0);

////////////varying/////////////////
1. a connection point.

2.Vertex Program
varying vec4 pcolor
void main()
{
gl_Position = position;
pcolor = color;
}

3.Fragment Program
varying vec4 pcolor;
void main()
{
gl_FragColor = pcolor;
}

pcolor is varying

////////////////uniform/////////////////////
uniform variables remain the same values for all vertices/fragments
you cannot change their values in .vert
set value in .cpp or .mm

1 modeling matrix
2 viewing matrix
3 projection matrix
4 modelview matrix
5 modelviewprojection matrix

////////////
////////////
///////////example.vert
attribute vec4 Position;
attribute vec4 SourceColor;
varying vec4 DestinationColor;
uniform mat4 Projection;
uniform mat4 Modelview;

void main(void)
{
DestinationColor = SourceColor;
gl_Position = Projection * Modelview * Position;
}

/////////////example.frag
varying lowp vec4 DestinationColor;

void main(void)
{
gl_FragColor = DestinationColor;
}

DestinationColor is a connect point

feelthat
11-29-2013, 10:13 AM
attribute vec3 inVertexPosition;
attribute vec3 inVertexNormal;
attribute vec4 inVertexColor;
attribute vec2 inTexCoord0;

attribute name define in sBuiltInVertexAttributeNames of EVertexAttributes.h
const char* const sBuiltInVertexAttributeNames[] =
{
"inVertexPosition",
"inVertexNormal",
"inVertexColor",
"inTexCoord0",
"inTexCoord1",
"inVertexTangent",
"inVertexBinormal",
0
};

//! Enumeration of EVertexAttributes.h for all vertex attibutes there are
enum E_VERTEX_ATTRIBUTES
{
EVA_POSITION = 0,
EVA_NORMAL,
EVA_COLOR,
EVA_TCOORD0,
EVA_TCOORD1,
EVA_TANGENT,
EVA_BINORMAL,
EVA_COUNT
};

void COGLES2MaterialRenderer::init(s32& outMaterialTypeNr,
for ( size_t i = 0; i < EVA_COUNT; ++i )
glBindAttribLocation( Program, i, sBuiltInVertexAttributeNames[i]);

//glVertexAttribPointer use EVA_POSITION~EVA_BINORMAL
//example
void COGLES2Driver::drawVertexPrimitiveList2d3d(const void* vertices,
u32 vertexCount,
const void* indexList,
u32 primitiveCount,
E_VERTEX_TYPE vType,
scene::E_PRIMITIVE_TYPE pType,
E_INDEX_TYPE iType,
bool threed)
{
glVertexAttribPointer(EVA_POSITION, (threed ? 3 : 2), GL_FLOAT, false, sizeof(S3DVertex), &(static_cast<const S3DVertex*>(vertices))[0].Pos);
}

///////////////////////////////////////////////////////////////////////

uniform mat4 mWorldViewProj;
uniform mat4 mInvWorld;
uniform mat4 mTransWorld;
uniform vec3 mLightPos;
uniform vec4 mLightColor;

OnSetConstants(video::IMaterialRendererServices* services, s32 userData)
{
//get uniform ID

//set uniform matrix
}

feelthat
12-02-2013, 08:49 AM
///////////For OGLES 1.1, this changes to the following:
glMatrixMode(GL_PROJECTION);
glFrustumf(-1.6f, 1.6, -2.4, 2.4, 5, 10);
glMatrixMode(GL_MODELVIEW);
glTranslatef(0, 0, -7);

//////////For OGLES 2.0, this changes to the following:
glUseProgram(m_simpleProgram);

GLint projectionUniform = glGetUniformLocation(m_simpleProgram, "Projection");
mat4 projectionMatrix = mat4::Frustum(-1.6f, 1.6, -2.4, 2.4, 5, 10);
glUniformMatrix4fv(projectionUniform, 1, 0, projectionMatrix.Pointer());

/////////////////////mat4::Frustum()/////////////////
static Matrix4<T> Frustum(T left, T right, T bottom, T top, T near, T far)
{
T a = 2 * near / (right - left);
T b = 2 * near / (top - bottom);
T c = (right + left) / (right - left);
T d = (top + bottom) / (top - bottom);
T e = - (far + near) / (far - near);
T f = -2 * far * near / (far - near);
Matrix4 m;
m.x.x = a; m.x.y = 0; m.x.z = 0; m.x.w = 0;
m.y.x = 0; m.y.y = b; m.y.z = 0; m.y.w = 0;
m.z.x = c; m.z.y = d; m.z.z = e; m.z.w = -1;
m.w.x = 0; m.w.y = 0; m.w.z = f; m.w.w = 1;
return m;
}

////////////////////simple.vert////////////////
attribute vec4 Position;
attribute vec4 SourceColor;
varying vec4 DestinationColor;
uniform mat4 Projection;
uniform mat4 Modelview;

void main(void)
{
DestinationColor = SourceColor;
gl_Position = Projection * Modelview * Position;
}

feelthat
12-04-2013, 12:24 PM

First of all, there is no real difference between a "World Matrix" and a "View Matrix", they are both transformation matrices and the distinction is somewhat arbitrary. Some systems even combine the two (OpenGL simply has a "ModelView" matrix).

Traditionally the "world matrix" is used to move individual models from "model space" to "world space". Then the "view matrix" is used to move all the models from world space into their relative positions in front of the camera (which, in effect, "moves the camera"). And finally the "Projection Matrix" converts the 3D positions into their 2D positions on the screen (generally with a perspective projection). Because they are matrices, they can be multiplied together into a single matrix that can transform points in a single step.

///////////multiplies order//////////////
mat4 modelview = cameraviewmatrix * modelmatrix;
mat4 modelviewproj = projectionmatrix * modelview;
gl_Position = modelview * gl_Vertex;

So in fixed function terms, the ModelView matrix is calculated as:

MV = V * M

and ModelViewProjection is:

MVP = P * V * M

Therefore every vertex sent to the Fixed function or a shader is transformed by:

gl_Position = V * M * Vertex;