我正在使用vColor
更改片段着色器的glUniform4f
时出现着色器1282代码问题。我没有花费很多时间,但没有任何线索。请帮忙。
我的程序的目标是为三角形使用不同的颜色,但不能使用glUniform
应用颜色。关于改变glUniform
我得到无效的操作错误。
public class BuildCanvas
{
private final String vertexShaderCode =
"attribute vec4 vPosition;" +
"void main() {" +
" gl_Position = vPosition;" +
"}";
private final String fragmentShaderCode =
" precision mediump float; " +
" uniform vec4 vColor; " +
" void main() { " +
" gl_FragColor = vColor; " +
" } ";
int vertexShader;
int fragmentShader;
int mProgram;
FloatBuffer vertexBuffer;
float[] color;
static final int COORDS_PER_VERTEX = 2;
static final int TRIANGLE_VERTEX_CNT =3;
int initShaders()
{
// prepare shaders and OpenGL program
int vertexShader = MyGLRenderer.loadShader(
GLES20.GL_VERTEX_SHADER, vertexShaderCode);
MyGLRenderer.checkGlError("vertexShaderCode");
int fragmentShader = MyGLRenderer.loadShader(
GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);
MyGLRenderer.checkGlError("fragmentShaderCode");
mProgram = GLES20.glCreateProgram(); // create empty OpenGL Program
MyGLRenderer.checkGlError("glCreateProgram");
GLES20.glAttachShader(mProgram, vertexShader); // add the vertex shader to program
MyGLRenderer.checkGlError("vertexShader");
GLES20.glAttachShader(mProgram, fragmentShader); // add the fragment shader to program
MyGLRenderer.checkGlError("fragmentShader");
GLES20.glLinkProgram(mProgram); // create OpenGL program executables
MyGLRenderer.checkGlError("glLinkProgram");
return 0;
}
int fillData(Shape shape)
{
int vertexCnt = shape.listPoints.size() *COORDS_PER_VERTEX;
drawingType eType = shape.eType;
float[] data ;
int dataIdx =0;
data = new float[ COORDS_PER_VERTEX * shape.listPoints.size()];
//Log.e("OpenGL", "shape.listPoints.size():" + shape.listPoints.size());
for(int idx=0;idx<shape.listPoints.size();idx++)
{
data[dataIdx++] = shape.listPoints.get(idx).xPos;
data[dataIdx++] = shape.listPoints.get(idx).yPos;
}
// initialize vertex byte buffer for shape coordinates
ByteBuffer bb = ByteBuffer.allocateDirect(
// (number of coordinate values * 4 bytes per float)
dataIdx * 4);
// use the device hardware's native byte order
bb.order(ByteOrder.nativeOrder());
// create a floating point buffer from the ByteBuffer
vertexBuffer = bb.asFloatBuffer();
// add the coordinates to the FloatBuffer
vertexBuffer.put(data, 0, dataIdx);
// set the buffer to read the first coordinate
vertexBuffer.position(0);
return 0;
}
int draw()
{
int mPositionHandle;
int mColorHandle;
// get handle to vertex shader's vPosition member
mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");
MyGLRenderer.checkGlError("mPositionHandle");
// Enable a handle to the triangle vertices
GLES20.glEnableVertexAttribArray(mPositionHandle);
// get handle to fragment shader's vColor member
mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor");
MyGLRenderer.checkGlError("mColorHandle");
// Prepare the triangle coordinate data
GLES20.glVertexAttribPointer(
mPositionHandle, COORDS_PER_VERTEX,
GLES20.GL_FLOAT, false,
0, vertexBuffer);
// Set color for drawing the triangle
// GLES20.glUniform4fv(mColorHandle, 1, color, 0);
// MyGLRenderer.checkGlError("mColorHandle");
float rgba[] ={0.0f,1.0f,1.0f,0.0f};
for(int i=0;i<vertexBuffer.capacity()/(TRIANGLE_VERTEX_CNT*COORDS_PER_VERTEX);i++)
{
/********************************************/
/********************************************/
/***********On uncommenting following 2 lines am getting shader error 1282 ***********/
//GLES20.glUniform4f(mColorHandle,rgba[0],rgba[1],rgba[2],rgba[3]);
//MyGLRenderer.checkGlError("mColorHandle");
// Draw the triangle
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, i*TRIANGLE_VERTEX_CNT, TRIANGLE_VERTEX_CNT);
}
// Disable vertex array
GLES20.glDisableVertexAttribArray(mPositionHandle);
//GLES20.glDisableVertexAttribArray(mColorHandle);
return 0;
}
}