我试图在每个椎体上使用不同的颜色,但是什么时候 我运行程序它没有绘制任何东西.GLES20.getError();函数返回INVALID_VALUE错误,但我不知道它可能在哪里。任何人都可以帮我解决错误,请?
import android.opengl.GLES20;
import android.util.Log;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.sql.SQLOutput;
import javax.microedition.khronos.opengles.GL10;
public class glDraw {
private float[] vertecies={
0.0f,2.0f,0.0f,
1.0f,0.0f,0.0f,
-1.0f,0.0f,0.0f
};
private float[] colors={
1.0f,0.0f,0.0f,0.5f,
0.0f,1.0f,0.0f,0.5f,
0.0f,0.0f,1.0f,0.5f
};
private FloatBuffer vertexBuffer;
private FloatBuffer colorBuffer;
private final String vertexShaderCode=
"uniform mat4 uMVPMatrix;"+
"attribute vec4 vPosition;"+
"attribute vec4 a_Color"+
"varying vec4 v_Color"+
"void main(){"+
" v_Color=a_Color;"+
" gl_Position = uMVPMatrix * vPosition;"+
" gl_PointSize=10.0;"+
"}";
private final String fragmentShaderCode=
"precision mediump float;"+
"varying vec4 v_Color;"+
"void main(){"+
" gl_FragColor=v_Color;"+
"}";
private int ShaderProgram;
public static int glLoadShader(int Type,String shaderCode){
int shader=GLES20.glCreateShader(Type);
GLES20.glShaderSource(shader, shaderCode);
GLES20.glCompileShader(shader);
return shader;
}
private int matrixHandle;
public glDraw(){
ByteBuffer vbb= ByteBuffer.allocateDirect(vertecies.length*4);
vbb.order(ByteOrder.nativeOrder());
vertexBuffer=vbb.asFloatBuffer();
vertexBuffer.put(vertecies);
vertexBuffer.position(0);
ByteBuffer cbb= ByteBuffer.allocateDirect(colors.length * 4);
vbb.order(ByteOrder.nativeOrder());
colorBuffer=cbb.asFloatBuffer();
colorBuffer.put(colors);
colorBuffer.position(0);
int vertexShader=glLoadShader(GLES20.GL_VERTEX_SHADER,vertexShaderCode);
int fragmentShader=glLoadShader(GLES20.GL_FRAGMENT_SHADER,fragmentShaderCode);
ShaderProgram=GLES20.glCreateProgram();
GLES20.glAttachShader(ShaderProgram, vertexShader);
GLES20.glAttachShader(ShaderProgram, fragmentShader);
GLES20.glLinkProgram(ShaderProgram);
}
public void drawTriangle(float[] mvpMatrix,float[] color){
GLES20.glUseProgram(ShaderProgram);
int PositionAttrib = GLES20.glGetAttribLocation(ShaderProgram, "vPosition");
int ColorAttrib = GLES20.glGetAttribLocation(ShaderProgram,"a_Color");
GLES20.glEnableVertexAttribArray(PositionAttrib);
GLES20.glEnableVertexAttribArray(ColorAttrib);
GLES20.glVertexAttribPointer(PositionAttrib, 3, GLES20.GL_FLOAT, false, 0, vertexBuffer);
GLES20.glVertexAttribPointer(ColorAttrib, 4, GLES20.GL_FLOAT, false, 0, colorBuffer);
matrixHandle= GLES20.glGetUniformLocation(ShaderProgram,"uMVPMatrix");
GLES20.glUniformMatrix4fv(matrixHandle, 1, false, mvpMatrix, 0);
Integer a=GLES20.glGetError();
Log.e("szar",a.toString());
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertecies.length / 3);
GLES20.glDisableVertexAttribArray(PositionAttrib);
}
}
而且这是我的Renderer课程:
import android.opengl.Matrix;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
/**
* Created by Lacko on 2015.07.29..
*/
public class GLRenderer implements GLSurfaceView.Renderer {
private final float[] mMVPMatrix = new float[16];
private final float[] mProjectionMatrix = new float[16];
private final float[] mViewMatrix = new float[16];
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
GLES20.glClearColor(0f,0,0,0);
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
GLES20.glViewport(0, 0, width, height);
float ratio=(float) width/(float)height;
Matrix.frustumM(mProjectionMatrix, 0, -ratio, ratio, -1, 1, 0.1f, 500);
}
public int x=0;
@Override
public void onDrawFrame(GL10 gl) {
Matrix.setLookAtM(mViewMatrix, 0, 0, 0, -1, 0, 1.78f, 0, 0, 1, 0);
Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mViewMatrix, 0);
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT|GLES20.GL_DEPTH_BUFFER_BIT);
GLES20.glEnable(GLES20.GL_BLEND);
GLES20.glEnable(GLES20.GL_DEPTH_TEST);
GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
GLES20.glEnable(GLES20.GL_TEXTURE_2D);
glDraw draw =new glDraw();
Matrix.translateM(mMVPMatrix,0,0,0,-3);
draw.drawTriangle(mMVPMatrix,new float[]{ 1,0,0,1f});
Matrix.rotateM(mMVPMatrix,0,x,0,1,0);
Matrix.translateM(mMVPMatrix, 0, 0, 0, -1);
draw.drawTriangle(mMVPMatrix,new float[]{1,1,0,0.5f});
x++;
}
}
答案 0 :(得分:0)
我对JAVA OpenGL了解不多,但我想让你检查两件事。
<强>更新强>
一般系统不需要1。
如果您不需要更改渲染视图的大小,则不需要2,但我建议无论如何都要调用它。