vbos着色器从OGLES 1.0变为2.0
我花了大约一周的时间学习和使用OGLES 1.0。 我意识到我需要使用2.0来获得我感兴趣的一些功能。 现在我一直在努力将1.0和2.0转换为2.0。 我终于碰到了一个我没有得到错误(我可以找到)并且有一个屏幕渲染的点。 我看到的只是清晰的颜色。 我尝试过改变事情并阅读ALOT来找出我的问题是什么,但我无法找到答案:(所以在这里,我问你为什么我看到的是clearcolor。假设我的解析器是正确的(它正在工作1.0)我的BufferObjects /数组/索引是正确的(他们是为1.0)我的问题是在我发布的代码。请任何任何方向到我需要开始寻找非常感谢,在此先感谢。
(请原谅未使用的变量和方法等)渲染器
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);
}
你一下子咬掉很多东西,如果你没有得到任何东西(没有纹理,没有颜色,只有一个三角形),并且从那里开始建立起来,那么我可以从简单的事情开始。
也就是说,我看到一个错误,因为glBindAttribLocation仅在下次调用glLinkProgram后生效,所以您对glBindAttribLocation的调用没有任何作用(您需要在连接着色器之前绑定位置)。
当你编译/链接着色器时,你应该用glGetShaderiv(GL_COMPILE_STATUS)/ glGetProgramiv(GL_LINK_STATUS)检查结果。 这比仅仅打印信息日志更好地检查成功/失败。
链接地址: http://www.djcxy.com/p/52425.html