vbos shaders going from OGLES 1.0 to 2.0
I spent about a week learning and using OGLES 1.0. I realized I needed to use 2.0 for some features that I found appealing. I have been working on converting from 1.0 to 2.0 for a couple days now. I have finally hit a point where I am not getting errors (That I can find) and have a screen rendering. All I see is the clearcolor though. I have tried changing things around and reading ALOT to figure out what my problem is but I just can't find the answer :( So here I am asking you why all I see is the clearcolor. Assuming my parser is correct (it was working flawlessly for 1.0) my BufferObjects/arrays/indices are correct(they were for 1.0) my problems is in the code I am posting. Please, any ANY direction to where I need to start looking is very much appreciated. Thanks in advance.
(please excuse unused variables and methods etc) renderer
public class TestRenderer implements Renderer {
private static final String TAG = TestRenderer.class.getSimpleName();
Cube tester;
Parser parser;
Context context;
private int mProgram;
private int muMVPMatrixHandle;
private float[] mMVPMatrix = new float[16];
private float[] mMMatrix = new float[16];
private float[] mVMatrix = new float[16];
private float[] mProjMatrix = new float[16];
private String vertexShaderCode = "attribute vec4 a_Position; "
+ "attribute vec3 a_Normal; " + "attribute vec2 a_Textcoords; "
+ "varying vec2 v_Textcoords;" + "uniform mat4 uMVPMatrix; "
+ "attribute vec4 vPosition; " + "void main(){ "
+ "v_Textcoords = a_Textcoords;"
+ " gl_Position = uMVPMatrix * vPosition; " +
"} ";
private String fragmentShaderCode = "precision mediump float; "
+ "varying vec2 v_Textcoords;" + "uniform sampler2D u_Texture; "
+ "void main(){ "
+ " gl_FragColor = texture2D(u_Texture, v_Textcoords); " + "} ";
TestRenderer(Context context) {
this.context = context;
parser = new Parser(context);
parser.parse("Turret2.obj");
tester = new Cube(parser.v, parser.f, parser.vt, parser.vtPointer,
parser.vn, parser.vnPointer, 1);
}
public static void checkGLError(String msg) {
int e = GLES20.glGetError();
if (e != GLES20.GL_NO_ERROR) {
Log.d(TAG, "GLES20 ERROR: " + msg + " " + e);
Log.d(TAG, errString(e));
}
}
public static String errString(int ec) {
switch (ec) {
case GLES20.GL_NO_ERROR:
return "No error has been recorded.";
case GLES20.GL_INVALID_ENUM:
return "An unacceptable value is specified for an enumerated argument.";
case GLES20.GL_INVALID_VALUE:
return "A numeric argument is out of range.";
case GLES20.GL_INVALID_OPERATION:
return "The specified operation is not allowed in the current state.";
case GLES20.GL_INVALID_FRAMEBUFFER_OPERATION:
return "The command is trying to render to or read from the framebuffer"
+ " while the currently bound framebuffer is not framebuffer complete (i.e."
+ " the return value from glCheckFramebufferStatus is not"
+ " GL_FRAMEBUFFER_COMPLETE).";
case GLES20.GL_OUT_OF_MEMORY:
return "There is not enough memory left to execute the command."
+ " The state of the GL is undefined, except for the state"
+ " of the error flags, after this error is recorded.";
default:
return "UNKNOW ERROR";
}
}
public void onDrawFrame(GL10 unused) {
// TODO Auto-generated method stub
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mVMatrix, 0);
GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, mMVPMatrix, 0);
tester.draw(mProgram);
checkGLError("onDrawFrame 0");
}
public void onSurfaceChanged(GL10 unused, int width, int height) {
GLES20.glViewport(0, 0, width, height);
float ratio = (float) width / height;
Matrix.frustumM(mProjMatrix, 0, -ratio, ratio, -1, 1, .001f, 100);
muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
Matrix.setLookAtM(mVMatrix, 0, 0, 0, 0, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
}
public void onSurfaceCreated(GL10 unused, EGLConfig config) {
GLES20.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
mProgram = GLES20.glCreateProgram();
checkGLError("onSurfaceCreated 3");
int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
checkGLError("onSurfaceCreated 1");
int fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER,
fragmentShaderCode);
GLES20.glAttachShader(mProgram, vertexShader); // add the vertex shader
// to program
checkGLError("onSurfaceCreated 5");
GLES20.glAttachShader(mProgram, fragmentShader); // add the fragment
// shader to program
checkGLError("onSurfaceCreated 2");
GLES20.glLinkProgram(mProgram);
checkGLError("onSurfaceCreated 7");
Log.d(TAG,
"link program true/false 1 = "
+ GLES20.glGetProgramInfoLog(mProgram));
GLES20.glUseProgram(mProgram);
checkGLError("onDrawFrame 2");
checkGLError("onSurfaceCreated 8");
Bitmap bmp = BitmapFactory.decodeResource(context.getResources(),
R.drawable.turretbottom);
tester.loadTextures(context, bmp);
}
private int loadShader(int type, String shaderCode) {
int shader = GLES20.glCreateShader(type);
GLES20.glShaderSource(shader, shaderCode);
GLES20.glCompileShader(shader);
Log.d(TAG, "Shader info log = " + GLES20.glGetShaderInfoLog(shader));
return shader;
}
}
public void draw(int program) {
GLES20.glGenBuffers(4, vboIds, 0);
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vboIds[0]);
GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, verts.length, vertBuff,
GLES20.GL_STATIC_DRAW);
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vboIds[1]);
GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, textVerts.length, textBuff,
GLES20.GL_STATIC_DRAW);
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vboIds[2]);
GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, normPoints.length,
normBuff, GLES20.GL_STATIC_DRAW);
GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, vboIds[3]);
GLES20.glBufferData(GLES20.GL_ELEMENT_ARRAY_BUFFER, 2 * indexa.length,
faceBuff, GLES20.GL_STATIC_DRAW);
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vboIds[0]);
GLES20.glEnableVertexAttribArray(0);
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vboIds[1]);
GLES20.glEnableVertexAttribArray(1);
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vboIds[2]);
GLES20.glEnableVertexAttribArray(2);
GLES20.glVertexAttribPointer(0, 3, GLES20.GL_FLOAT, false, 0, 0);
GLES20.glVertexAttribPointer(1, 2, GLES20.GL_FLOAT, false, 0, 0);
GLES20.glVertexAttribPointer(2, 3, GLES20.GL_FLOAT, false, 0, 0);
GLES20.glBindAttribLocation(program, 0, "a_Position");
GLES20.glBindAttribLocation(program, 1, "a_Textcoords");
GLES20.glBindAttribLocation(program, 2, "a_Normal");
GLES20.glDrawElements(GLES20.GL_TRIANGLES, indexa.length,
GLES20.GL_UNSIGNED_SHORT, 0);
GLES20.glDeleteBuffers(4, vboIds, 0);
}
You're biting off a lot at once, I might start with something simpler if you don't get anywhere (no textures, no colors, just single triangle) and build up from there when you get that working.
That said, I do see one error, in that glBindAttribLocation only takes effect after the next call to glLinkProgram, so your calls to glBindAttribLocation aren't doing anything (you need to bind location before linking shader).
Also when you compile/link a shader, you should be checking the result with glGetShaderiv(GL_COMPILE_STATUS)/glGetProgramiv(GL_LINK_STATUS). That's a better check for success/failure than just printing the info log.
链接地址: http://www.djcxy.com/p/52426.html