我有 4 维顶点(X,Y,A,B),我想将其绘制为 6 个单独的 2D 图(XxY,XxA,XxB,YxA,...)
我的顶点定义如下:
GLint data[MAX_N_POINT][4];
我可以通过以下方式绘制二维图的第一个 (X,Y):
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(4, GL_INT, 4*sizeof(GLint), &data[0][0]);
glDrawArrays(GL_POINTS, 0, MAX_N_POINT-1);
glDisableclientState(GL_VERTEX_ARRAY);
为了绘制其他 2D 图(XxA、AxB 等...),我决定使用着色器来调整顶点的 X、Y、Z、W 尺寸,具体取决于我要绘制的尺寸。:
uniform int axes;
void main()
{
vec2 point;
if (axes == 0) {
point = gl_Vertex.xy;
} else if (axes == 1) {
point = gl_Vertex.xz;
} else if (axes == 2) {
point = gl_Vertex.xw;
} else if (axes == 3) {
point = gl_Vertex.yz;
} else if (axes == 4) {
point = gl_Vertex.yw;
} else if (axes == 5) {
point = gl_Vertex.zw;
}
gl_Position = gl_ModelViewProjectionMatrix * vec4(point.xy, 0.0, 1.0);
gl_FrontColor = gl_Color;
gl_BackColor = gl_Color;
}
我已成功加载、编译、将着色器添加到程序并链接程序。
现在我有了着色器程序,它不清楚如何使用该程序,这会影响我的顶点数组的绘制方式。我尝试了以下方法,但它似乎没有任何效果,因为它绘制的东西与没有着色器的情况完全相同:
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(4, GL_INT, 4*sizeof(GLint), &data1[0][0]);
//New shader code here
glUseProgram(shaderProg);
int plotAxes = rand()%4;
GLint axes = glGetUniformLocation(shaderProg, "axes");
glUniform1i(axes, plotAxes);
glDrawArrays(GL_POINTS, 0, MAX_N_POINT-1);
glDisableClientState(GL_VERTEX_ARRAY);
glUseProgram(0);
是否有一些我遗漏或无法正确理解的基本内容?
编辑 2:我根据 Christian 的建议更新了着色器代码。我还验证了阴影加载时没有错误,但是如果我在调用 glUseProgram 后检查 OpenGl 错误,我会收到OpenGL Error: invalid operation
错误消息。
编辑 3:这是最终的着色器:
uniform int axes;
void main()
{
vec4 point;
if (axes == 0) {
point = gl_Vertex;
} else if (axes == 1) {
point = gl_Vertex.xzyw;
} else if (axes == 2) {
point = gl_Vertex.xwzy;
} else if (axes == 3) {
point = gl_Vertex.yzxw;
} else if (axes == 4) {
point = gl_Vertex.ywxz;
} else if (axes == 5) {
point = gl_Vertex.zwxy;
}
point.z = 0.0;
point.w = 1.0;
// eliminate w point
gl_Position = gl_ModelViewProjectionMatrix * point;
gl_FrontColor = gl_Color;
gl_BackColor = gl_Color;
}