我正在尝试制作一个围绕任何轴旋转的6色立方体(每个面一种颜色)。轮换不是问题,但我想到的是最后绘制的面是被渲染的面。
例如:正面为红色,背面为绿色。当立方体开始旋转时,正如我在“glDrawArrays”(创建绿色面)之前所谓的“glDrawArrays”(创建红色面),每当面部一个在另一个上面时(由于旋转)我总是看到绿色的脸,当我真的应该看到红色,绿色,红色,绿色等。
我该如何解决这个问题?我试图在最后几天找到信息,但没有找到解决方案。
代码如下。
public class MainActivity extends Activity {
GLSurfaceView myGLSurfaceView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myGLSurfaceView = new GLSurfaceView(this);
myGLSurfaceView.setEGLContextClientVersion(2);
myGLSurfaceView.setRenderer(new RendererClass(this));
setContentView(myGLSurfaceView);
runnable.run();
}
// Test 2
static float variable2 = 0;
Handler myHandler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
myHandler.postDelayed(runnable, 20);
variable2 += 1;
}
};
}
和
public class RendererClass implements Renderer {
int shaderProgram;
int variableToSayWhereTheVertexShaderShouldFindInformation;
int variableToSayWhereTheFragmentShaderShouldFindInformation;
int variableToSayWhereTheMatrixShouldFindInformation;
int BYTES_PER_FLOAT = 4;
FloatBuffer vertexData;
float[] a_Result = new float[16];
public RendererClass(Context context){
float[] myVertices = {
// Front face
0.0f, 0.0f, -1.0f,
-0.5f, -0.5f, -1.0f,
0.5f, -0.5f, -1.0f,
0.5f, 0.5f, -1.0f,
-0.5f, 0.5f, -1.0f,
-0.5f, -0.5f, -1.0f,
// Back face
0.0f, 0.0f, -2.0f,
-0.5f, -0.5f, -2.0f,
0.5f, -0.5f, -2.0f,
0.5f, 0.5f, -2.0f,
-0.5f, 0.5f, -2.0f,
-0.5f, -0.5f, -2.0f
};
ByteBuffer intermediateBuffer = ByteBuffer.allocateDirect(myVertices.length*BYTES_PER_FLOAT);
intermediateBuffer.order(ByteOrder.nativeOrder());
vertexData = intermediateBuffer.asFloatBuffer();
vertexData.put(myVertices);
}
@Override
public void onSurfaceCreated(GL10 arg0, EGLConfig config) {
GLES20.glClearColor(0.0f, 0.5f, 1.0f, 1.0f);
String vertexShaderCode = "attribute vec4 myVertexPositionInsideOpenGL;"
+ "uniform mat4 a_Result;"
+ "void main(){"
+ "gl_Position = a_Result*myVertexPositionInsideOpenGL;"
+ "}";
String fragmentShaderCode = "precision mediump float;"
+ "uniform vec4 myColorsInsideOpenGL;"
+ "void main(){"
+ "gl_FragColor = myColorsInsideOpenGL;"
+ "}";
int vertexShaderID = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER);
int fragmentShaderID = GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER);
GLES20.glShaderSource(vertexShaderID, vertexShaderCode);
GLES20.glShaderSource(fragmentShaderID, fragmentShaderCode);
GLES20.glCompileShader(vertexShaderID);
GLES20.glCompileShader(fragmentShaderID);
shaderProgram = GLES20.glCreateProgram();
GLES20.glAttachShader(shaderProgram, vertexShaderID);
GLES20.glAttachShader(shaderProgram, fragmentShaderID);
GLES20.glLinkProgram(shaderProgram);
GLES20.glUseProgram(shaderProgram);
variableToSayWhereTheVertexShaderShouldFindInformation = GLES20.glGetAttribLocation(shaderProgram, "myVertexPositionInsideOpenGL");
variableToSayWhereTheFragmentShaderShouldFindInformation = GLES20.glGetUniformLocation(shaderProgram, "myColorsInsideOpenGL");
variableToSayWhereTheMatrixShouldFindInformation = GLES20.glGetUniformLocation(shaderProgram, "a_Result");
GLES20.glEnableVertexAttribArray(variableToSayWhereTheVertexShaderShouldFindInformation);
vertexData.position(0);
GLES20.glVertexAttribPointer(variableToSayWhereTheVertexShaderShouldFindInformation, 3, GL_FLOAT, false, 0, vertexData);
}
@Override
public void onSurfaceChanged(GL10 arg0, int width, int height) {
Matrix.orthoM(a_Result, 0, -4, 4, -4, 4, 4, -4);
Matrix.rotateM(a_Result, 0, MainActivity.variable2, 1, 0, 0);
}
GL10 a;
@Override
public void onDrawFrame(GL10 glUnused) {
onSurfaceChanged(a, 1, 1);
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
GLES20.glUniformMatrix4fv(variableToSayWhereTheMatrixShouldFindInformation, 1, false, a_Result, 0);
glUniform4f(variableToSayWhereTheFragmentShaderShouldFindInformation, 1.0f, 0.0f, 0.0f, 1.0f);
glDrawArrays(GL_TRIANGLE_FAN, 0, 6);
glUniform4f(variableToSayWhereTheFragmentShaderShouldFindInformation, 0.0f, 0.0f, 1.0f, 1.0f);
glDrawArrays(GL_TRIANGLE_FAN, 6, 6);
}
}