我有一个简单的函数map_color,用于计算我大学的侵蚀模拟器的顶点颜色。我想将其转换为顶点着色器。对我的第一个着色器来说似乎很简单,但我不知道从哪里开始。我看过几个代码样本似乎很有希望,但过于复杂,我很难遵循。
我应该提一下,我正在使用java / jogl,但我可以查看加载着色器并自行打开它的详细信息。主要是寻找实际着色器代码的帮助。
private void map_color(float height, float color[])
{
float t = (height - COLOR_MIN_HEIGHT) / (COLOR_MAX_HEIGHT - COLOR_MIN_HEIGHT);
// Clamp to range, just in case
if(t < 0.0) t = 0.0f;
if(t > 1.0) t = 1.0f;
// Brown yellow map
if (t < 0.5)
{
color[0] = 0.488f + t * 0.836f;
color[1] = 0.164f + t * 0.953f;
color[2] = 0.094f + t * 0.172f;
}
else
{
color[0] = 0.906f + (t - 0.5f) * 0.180f;
color[1] = 0.640f + (t - 0.5f) * 0.680f;
color[2] = 0.180f + (t - 0.5f) * 0.773f;
}
}
private void drawTerrain()
{
// Need a separate vertex normal for each quad -- this implementation is inefficient
// Should be computed once and stored when grid is loaded
float [] v1 = new float[3];
float [] v2 = new float[3];
float [] norm = new float[3];
float [] color = new float[3];
v1[0] = gridHorizontalScaleFactor / 5.0f; // Scale factor is a hack
v1[1] = 0.0f;
v2[0] = 0.0f;
v2[1] = gridHorizontalScaleFactor / 5.0f; // Ditto on hack
// Get ready for varying material properties
gl.glEnable(GLLightingFunc.GL_COLOR_MATERIAL);
gl.glColorMaterial(GL.GL_FRONT_AND_BACK,
GLLightingFunc.GL_AMBIENT_AND_DIFFUSE);
for(int x = 2; x < latticeSizeX-1; x++)
{
for(int y = 2; y < latticeSizeY-1; y++)
{
gl.glBegin(GL.GL_TRIANGLES);
v1[2] = unsafeTopo.getFloatAt((x+1) * 263 + y) - unsafeTopo.getFloatAt( (x-1) * 263 + y);
v2[2] = unsafeTopo.getFloatAt(x * 263 + (y + 1)) - unsafeTopo.getFloatAt( x * 263 + (y - 1));
cross(norm, v2, v1);
gl.glNormal3fv(norm, 0);
map_color(unsafeTopo.getFloatAt(x * 263 + y), color);
gl.glColor3f(color[0], color[1], color[2]);
gl.glVertex3f(x, y, unsafeTopo.getFloatAt(x * 263 + y));
map_color(unsafeTopo.getFloatAt((x+1) * 263 + y), color);
gl.glColor3f(color[0], color[1], color[2]);
gl.glVertex3f(x+1, y, unsafeTopo.getFloatAt((x+1) * 263 + y));
map_color(unsafeTopo.getFloatAt(x * 263 + (y + 1)), color);
gl.glColor3f(color[0], color[1], color[2]);
gl.glVertex3f(x, y+1, unsafeTopo.getFloatAt(x * 263 + (y + 1)));
v1[2] = unsafeTopo.getFloatAt((x+2) * 263 + y+1) - unsafeTopo.getFloatAt(x * 263 + (y + 1));
v2[2] = unsafeTopo.getFloatAt((x+1) * 263 + y+2) - unsafeTopo.getFloatAt((x+1) * 263 + y);
cross(norm, v2, v1);
map_color(unsafeTopo.getFloatAt((x+1) * 263 + y+1), color);
gl.glColor3f(color[0], color[1], color[2]);
gl.glVertex3f(x+1, y+1, unsafeTopo.getFloatAt((x+1) * 263 + y+1));
map_color(unsafeTopo.getFloatAt(x * 263 + (y + 1)), color);
gl.glColor3f(color[0], color[1], color[2]);
gl.glVertex3f(x, y+1, unsafeTopo.getFloatAt(x * 263 + (y + 1)));
map_color(unsafeTopo.getFloatAt((x+1) * 263 + y), color);
gl.glColor3f(color[0], color[1], color[2]);
gl.glVertex3f(x+1, y, unsafeTopo.getFloatAt((x+1) * 263 + y));
gl.glEnd();
}
}
gl.glDisable(GLLightingFunc.GL_COLOR_MATERIAL);
}