Step 1:
准备Vertex Data:
(1). vertex count 有多少个数据? (2). 具体的vertex data, (3)这些数据的大小(in byte)
例如:
const GLint coordinateLineVertexCount = 6;
const GLint coordinateLineVertexSize = sizeof(dgp::vec3f) * 2 * coordinateLineVertexCount;
dgp::vec3f coordinateLineVertexData[] = {
// vertex position // vertex color
dgp::vec3f(0, 0, 0), dgp::vec3f(1, 0, 0),
dgp::vec3f(fLength, 0, 0), dgp::vec3f(1, 0, 0),
dgp::vec3f(0, 0, 0), dgp::vec3f(0, 1, 0),
dgp::vec3f(0, fLength, 0), dgp::vec3f(0, 1, 0),
dgp::vec3f(0, 0, 0), dgp::vec3f(0, 0, 1),
dgp::vec3f(0, 0, fLength), dgp::vec3f(0, 0, 1)
};
注意每一个vertex data包含了两部分, position and color.Step 2:
生成一个buffer object放这些vertex data.
Step 3:
生成一个shader program,
Step 4:
当真正需要render的时候, 往shader program的相应的location里面写入对应的数据.Step 4:
例如: projection matrix, model view matrix, the vertex data (position, normal, color) ....
然后指定画成什么primitive形式。
相对于glColor来给出顶点的颜色, 在shader里面的做法 我用了下面三种最简单的(每一种对应一个shader program):
例如图中的Cube, 用Blinn Phong lighting,fragment shader中计算color.
例如图中的Grid,all the vertices are the same color, they are same color in fragment too, 所以我在fragment shader中用了一个uniform vec3 fragmentColor;程序中指定这个颜色,黑色,就ok了。
例如图中的coordinate system line,一个红线,一个绿色的线,一个蓝色的线。就需要每一个vertex 给出 color 信息. vertex shader 中:
in vec3 position;
in vec3 color; // 之后取他们的location并写入准备好的data.
例如图中的Cube, 用Blinn Phong lighting,fragment shader中计算color.