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()從緩衝區記錄一些顏色值。 –