我试图对三角形的每个顶点进行不同的着色。当我编译代码时,它没有绘制任何东西,所以我使用了glGetError()函数。函数返回0,这意味着没有错误,但它仍然没有任何吸引力。所以我不知道我做错了什么。这是我的GLsurfaceView类和我的绘图类。
@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,0);
draw.drawTriangle(mMVPMatrix,new float[]{ 1,0,0,1f});
Matrix.rotateM(mMVPMatrix,0,x,0,1,0);
Matrix.translateM(mMVPMatrix, 0, 0, 0, 0);
draw.drawTriangle(mMVPMatrix,new float[]{1,1,0,0.5f});
x++;
}
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 short[] indicies={0,1,2};
private ShortBuffer indexBuffer;
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.glVertexAttribPointer(PositionAttrib, 3, GLES20.GL_FLOAT, false, 0, vertexBuffer);
GLES20.glEnableVertexAttribArray(PositionAttrib);
GLES20.glVertexAttribPointer(ColorAttrib, 4, GLES20.GL_FLOAT, false, 0, colorBuffer);
GLES20.glEnableVertexAttribArray(ColorAttrib);
Integer a=GLES20.glGetError();
Log.e("ERROR:",a.toString());
matrixHandle= GLES20.glGetUniformLocation(ShaderProgram,"uMVPMatrix");
GLES20.glUniformMatrix4fv(matrixHandle, 1, false, mvpMatrix, 0);
GLES20.glDrawArrays(GLES20.GL_TRIANGLES,0,vertecies.length/3);
GLES20.glDisableVertexAttribArray(PositionAttrib);
}
}
答案 0 :(得分: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 int createProgram(String vertexSource, String fragmentSource) {
int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexSource);
int pixelShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentSource);
int program = GLES20.glCreateProgram();
if (program != 0) {
GLES20.glAttachShader(program, vertexShader);
checkGlError("glAttachShader");
GLES20.glAttachShader(program, pixelShader);
checkGlError("glAttachShader");
GLES20.glLinkProgram(program);
int[] linkStatus = new int[1];
GLES20.glGetProgramiv(program, GLES20.GL_LINK_STATUS, linkStatus, 0);
if (linkStatus[0] != GLES20.GL_TRUE) {
Log.e(TAG, "Could not link program: ");
Log.e(TAG, GLES20.glGetProgramInfoLog(program));
GLES20.glDeleteProgram(program);
program = 0;
}
}
return program;
}
private int loadShader(int shaderType, String source) {
int shader = GLES20.glCreateShader(shaderType);
if (shader != 0) {
GLES20.glShaderSource(shader, source);
GLES20.glCompileShader(shader);
int[] compiled = new int[1];
GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compiled, 0);
if (compiled[0] == 0) {
Log.e(TAG, "Could not compile shader " + shaderType + ":");
Log.e(TAG, GLES20.glGetShaderInfoLog(shader));
GLES20.glDeleteShader(shader);
shader = 0;
}
}
return shader;
}
请记住,在渲染对象之前需要调用一次