I am trying to Draw with GLES 2.0 through the NDK, doing everything except creating the surface in C++, but nothing is drawing on the screen...And I can't figure out why.
I have tried/verified many different things and am still at a loss.
my last few guesses are these:
my textures are somehow now compatible with the screen:
I load textures like this:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->GetPixels());
but the default GLSurfaceView format is RGB_565
I tried this with no success:
mGLView.getHolder().setFormat(PixelFormat.RGBA_8888);
and also this:
glTexImage2D(GL_TEXTURE_2D, 0, GL_UNSIGNED_SHORT_5_6_5, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->GetPixels());
could texture format have anything to do with the problem? I have no reference because the Android SDK abstracts this away completely..
this is the shader I am using:
const char *vertexShader = "\
uniform mat4 uMtxProjection; \
uniform mat4 uMtxModelView; \
attribute vec2 aPosition; \
attribute vec2 aTexCoord; \
varying vec2 vTexCoord; \
void main() \
{ \
gl_Position = uMtxProjection * (uMtxModelView * vec4(aPosition.x, aPosition.y, 0.0, 1.0)); \
vTexCoord = aTexCoord; \
}";
const char *fragmentShader = "\
precision mediump float; \
varying vec2 vTexCoord; \
uniform sampler2D sTexture; \
void main() \
{ \
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); \
}";
After further testing, and removing the texture(filling triangle with only red) I can see a distorted red box flash exactly 3 times each time the app is run, then the 开发者_如何学JAVAscreen goes blank =/
and the ortho matrix:
Mat4 Ortho2D(float left, float right, float bottom, T top, float zNear, float zFar)
{
float dx = right - left;
float dy = top - bottom;
float dz = zFar - zNear;
// avoid division by zero
float tx = (dx != 0) ? -(right + left) / dx : 0;
float ty = (dy != 0) ? -(top + bottom) / dy : 0;
float tz = (dz != 0) ? -(zFar + zNear) / dz : 0;
return Mat4(2.0f / dx, 0, 0, tx,
0, 2.0f / dy, 0, ty,
0, 0, -2.0f / dz, tz,
0, 0, 0, 1);
}
Actually, I'm not sure what was going on exactly, but something I changed fixed the problem =/ I tried going back over the code to re-break it, but couldn't..
but for reference, I can't guarantee what was broken, but I can say for sure, that the problem was neither the texture, nor the shader. Even though the default surface view is set to RGB_565, using the following line creates a texture just fine in NDK, with working alpha.
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->GetPixels());
The projection matrix that I was using was also fine, because I copied it directly from the openGL specs.
I noticed while working that the call glEnable(TEXTURE_2D) was causing an error (invalid enum) because it pertains to the fixed function pipeline of opengl, and I was using GLES2.0... and I was copying some of my old code from a fixed function implementation GLES1. So all I can think of was that I was causing gl to crash somehow by with the old code =/
精彩评论