0
在我的Android应用与OpenGL ES,我想用颜色画大行FloatBuffers将所有内容填充,与颜色一起协调,以获得更好的性能。我成功地在正确的坐标上绘制了线条,但颜色仍然是黑色的。我从http://www.learnopengles.com/android-lesson-one-getting-started开始。唯一的区别(我可以看到)是,他们使用三角形,我使用线条。所以我的FloatBuffers格式(X,Y,Z,R,G,B,A,X,Y,Z,R,G,B,A,X,Y,Z,R,G,B,A ,. ......所以每一行都只有一种颜色,所以颜色重复自己的起始点和终点(14个浮点数中有4个是多余的,我不在意) 这是相关代码我的渲染器类:颜色无法与大FloatBuffer与格式化X,Y,Z,R,G,B,A,X,Y,Z,R,
private void drawStuff()
{
int mPositionDataSize = 3;
int mColorDataSize = 4;
int mColorOffset = 3 ;
mPositionHandle = GLES20.glGetAttribLocation(mProgramHandle, "a_Position");
mColorHandle = GLES20.glGetUniformLocation(mProgramHandle, "a_Color");
mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgramHandle, "u_MVPMatrix");
//1. vertex positions:
thisBuffer.getVertexFloatBuffers().position(0);
GLES20.glVertexAttribPointer(mPositionHandle, mPositionDataSize,
GLES20.GL_FLOAT, false,
28, thisBuffer.getVertexFloatBuffers());
GLES20.glEnableVertexAttribArray(mPositionHandle);
//2. colors:
thisBuffer.getVertexFloatBuffers().position(mColorOffset);
GLES20.glVertexAttribPointer(mColorHandle, mColorDataSize,
GLES20.GL_FLOAT, false,
28, thisBuffer.getVertexFloatBuffers()) ;
GLES20.glEnableVertexAttribArray(mColorHandle);
// 3. Apply the projection and view transformation
GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, thisMVPmatrix, 0);
// 4. Draw the lines
GLES20.glDrawArrays(GLES20.GL_LINES, 0, thisBuffer.getNrOfVerteces());
}
的想法,任何人
编辑:我着色器定义,在我onSurfaceCreated方法编译:
final String vertexShaderPerLayer =
"uniform mat4 u_MVPMatrix; \n" // A constant representing the combined model/view/projection matrix.
+ "attribute vec4 a_Position; \n" // Per-vertex position information we will pass in.
+ "attribute vec4 a_Color; \n" // Per-vertex color information we will pass in.
+ "varying vec4 v_Color; \n" // This will be passed into the fragment shader.
+ "void main() \n" // The entry point for our vertex shader.
+ "{ \n"
+ " v_Color = a_Color; \n" // Pass the color through to the fragment shader.
// It will be interpolated across the triangle.
+ " gl_Position = u_MVPMatrix \n" // gl_Position is a special variable used to store the final position.
+ " * a_Position; \n" // Multiply the vertex by the matrix to get the final point in
+ "} \n"; // normalized screen coordinates.
int vertexShaderHandle = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER);
// Pass in the shader source.
GLES20.glShaderSource(vertexShaderHandle, vertexShaderPerLayer);
// Compile the shader.
GLES20.glCompileShader(vertexShaderHandle);
// Get the compilation status.
final int[] compileStatus = new int[1];
GLES20.glGetShaderiv(vertexShaderHandle, GLES20.GL_COMPILE_STATUS, compileStatus, 0);
// FRAGMENTSHADER
final String fragmentShaderPerLayer =
"precision mediump float; \n" // Set the default precision to medium. We don't need as high of a
// precision in the fragment shader.
+ "varying vec4 v_Color; \n" // This is the color from the vertex shader interpolated across the
// triangle per fragment.
+ "void main() \n" // The entry point for our fragment shader.
+ "{ \n"
+ " gl_FragColor = v_Color; \n" // Pass the color directly through the pipeline.
+ "} \n";
int fragmentShaderHandlePerLayer = GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER);
// Pass in the shader source.
GLES20.glShaderSource(fragmentShaderHandlePerLayer, fragmentShaderPerLayer);
// Compile the shader.
GLES20.glCompileShader(fragmentShaderHandlePerLayer);
// PROGRAM STUFF
mProgramHandle = GLES20.glCreateProgram();
// Bind the vertex shader to the program.
GLES20.glAttachShader(mProgramHandle, vertexShaderHandle);
// Bind the fragment shader to the program.
GLES20.glAttachShader(mProgramHandle, fragmentShaderHandlePerLayer);
// Bind attributes
GLES20.glBindAttribLocation(mProgramHandle, 0, "a_Position");
GLES20.glBindAttribLocation(mProgramHandle, 1, "a_Color");
// Link the two shaders together into a program.
GLES20.glLinkProgram(mProgramHandle);
这一切看起来不错。 Shader问题也许?只发布顶点和片段着色器的相关着色器代码。 –
我编辑了我的帖子并添加了顶点和片段着色器。感谢您的关注,我被卡住了。 – blubbiedevis
仍然所有看起来不错。尝试指出问题:首先尝试在顶点着色器v_Color = vec4(1.0 ...)中设置手动颜色,然后查看是否可以在形状上看到正确的输出颜色。如果你的问题似乎出现在你的着色器收到的数据中。在这种情况下,尝试在将位置设置为mColorOffset后,通过访问thisBuffer.getVertexFloatBuffers()从缓冲区记录一些颜色值。 –