我一直在关注本教程:http://developer.android.com/training/graphics/opengl/index.html,并且一切正常。目前我有一个缓冲区用于顶点位置,一个缓冲区用于绘制顶点的顺序。我想在每个顶点添加一个颜色,但是当我这样做时,我在GLES20.glUseProgram()中得到错误1282。
任何帮助都会很棒,因为我有点像GL菜鸟。
这是我的着色器代码:
static public final String vertexShaderCode =
"uniform mat4 uMVPMatrix;" +
"attribute vec4 aPosition;" +
"attribute vec4 aColor;" +
"varying vec4 vColor;" +
"void main() {" +
" gl_Position = uMVPMatrix * aPosition;" +
" vColor = aColor;" +
"}";
static public final String fragmentShaderCode =
"varying vec4 vColor;" +
"void main() {" +
" gl_FragColor = vColor;" +
"}";
我加载的内容:
int shaderV = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER);
GLES20.glShaderSource(shaderV, vertexShaderCode);
GLES20.glCompileShader(shaderV);
int shaderF = GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER);
GLES20.glShaderSource(shaderF, fragmentShaderCode);
GLES20.glCompileShader(shaderF);
我设置了这样的程序:
int vertexShader = ShaderInfo.GetVertexShader();
int fragmentShader = ShaderInfo.GetFragmentShader();
mProgram = GLES20.glCreateProgram(); // create empty OpenGL ES Program
MyGLRenderer.checkGlError("glCreateProgram");
GLES20.glAttachShader(mProgram, vertexShader); // add the vertex shader to program
MyGLRenderer.checkGlError("glAttachShader");
GLES20.glAttachShader(mProgram, fragmentShader); // add the fragment shader to program
MyGLRenderer.checkGlError("glAttachShader");
GLES20.glLinkProgram(mProgram); // creates OpenGL ES program executables
MyGLRenderer.checkGlError("glLinkProgram");
在每个时间步/循环/迭代/我称之为:
GLES20.glClearColor(1.0f, 0.0f, 1.0f, 1.0f);
MyGLRenderer.checkGlError("glClearColor");
// Redraw background color
//GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
MyGLRenderer.checkGlError("glClear");
// Set the camera position (View matrix)
Matrix.setLookAtM(mViewMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
MyGLRenderer.checkGlError("setLookAtM");
// Calculate the projection and view transformation
Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mViewMatrix, 0);
MyGLRenderer.checkGlError("multiplyMM");
// Draw shape
rect.Draw(mMVPMatrix);
其中rect.Draw();由下列人员给出:
MyGLRenderer.checkGlError("BEFOREglUseProgram");
GLES20.glUseProgram(mProgram);
MyGLRenderer.checkGlError("glUseProgram");
// get handle to vertex shader's vPosition member
mPositionHandle = GLES20.glGetAttribLocation(mProgram, "aPosition");
MyGLRenderer.checkGlError("glGetAttribLocationPosition");
// Enable a handle to the triangle vertices
GLES20.glEnableVertexAttribArray(mPositionHandle);
// Prepare the triangle coordinate data
GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, vertexStride, vertexBuffer);
// get handle to fragment shader's aColor member
mColorHandle = GLES20.glGetUniformLocation(mProgram, "aColor");
// Set color for drawing the triangle
GLES20.glUniform4fv(mColorHandle, 1, color, 0);
// get handle to shape's transformation matrix
mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
MyGLRenderer.checkGlError("glGetUniformLocation");
// Apply the projection and view transformation
GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0);
MyGLRenderer.checkGlError("glUniformMatrix4fv");
GLES20.glDrawElements(
GLES20.GL_TRIANGLES, drawOrder.length,
GLES20.GL_UNSIGNED_SHORT, drawListBuffer);
// Disable vertex array
GLES20.glDisableVertexAttribArray(mPositionHandle);
最后,我像这样创建缓冲区:
private short drawOrder[] = { };
private void SetDrawOrder()
{
if(drawOrder.length != elements.size()*3)
drawOrder = new short[elements.size()*3];
for(int i = 0; i< elements.size(); i++)
{
drawOrder[i*3 + 0] = (short)elements.get(i).nodes.get(0).indexInMesh;
drawOrder[i*3 + 1] = (short)elements.get(i).nodes.get(1).indexInMesh;
drawOrder[i*3 + 2] = (short)elements.get(i).nodes.get(2).indexInMesh;
}
// initialize byte buffer for the draw list
ByteBuffer dlb = ByteBuffer.allocateDirect(
// (# of coordinate values * 2 bytes per short)
drawOrder.length * 2);
dlb.order(ByteOrder.nativeOrder());
drawListBuffer = dlb.asShortBuffer();
drawListBuffer.put(drawOrder);
drawListBuffer.position(0);
}
private void AddNodesToArray()
{
// initialize vertex byte buffer for shape coordinates
ByteBuffer bb = ByteBuffer.allocateDirect(
// (number of coordinate values * 4 bytes per float)
vertexCount * COORDS_PER_VERTEX * 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
for(int i = 0; i< nodes.size(); i++)
vertexBuffer.put(nodes.get(i).position);
// set the buffer to read the first coordinate
vertexBuffer.position(0);
}
private void AddColoursToArray()
{
// initialize vertex byte buffer for shape coordinates
ByteBuffer bb = ByteBuffer.allocateDirect(
// (number of coordinate values * 4 bytes per float)
vertexCount * 4 * 4);
// use the device hardware's native byte order
bb.order(ByteOrder.nativeOrder());
// create a floating point buffer from the ByteBuffer
colourBuffer = bb.asFloatBuffer();
// add the coordinates to the FloatBuffer
for(int i = 0; i< nodes.size(); i++)
colourBuffer.put(nodes.get(i).colour);
// set the buffer to read the first coordinate
colourBuffer.position(0);
}
答案 0 :(得分:0)
此代码
// get handle to fragment shader's aColor member
mColorHandle = GLES20.glGetUniformLocation(mProgram, "aColor");
// Set color for drawing the triangle
GLES20.glUniform4fv(mColorHandle, 1, color, 0);
无法运作。 aColor
不是着色器中的统一,而是顶点属性。您永远不会为属性设置顶点属性指针。