【发布时间】:2011-04-29 00:54:50
【问题描述】:
我正在尝试通过 NDK 使用 GLES 2.0 进行绘制,除了在 C++ 中创建表面外,什么都没有,但屏幕上什么都没有绘制……我不知道为什么。
我已经尝试/验证了许多不同的东西,但仍然不知所措。
我最后的几个猜测是:
我的纹理现在以某种方式与屏幕兼容:
我像这样加载纹理:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->GetPixels());
但默认的 GLSurfaceView 格式是 RGB_565
我试过这个没有成功:
mGLView.getHolder().setFormat(PixelFormat.RGBA_8888);
还有这个:
glTexImage2D(GL_TEXTURE_2D, 0, GL_UNSIGNED_SHORT_5_6_5, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->GetPixels());
纹理格式是否与问题有关?我没有参考,因为 Android SDK 完全将其抽象出来..
这是我正在使用的着色器:
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); \
}";
经过进一步测试,并移除纹理(仅用红色填充三角形),我可以看到一个扭曲的红色框在每次运行应用程序时闪烁 3 次,然后屏幕变为空白 =/
和正交矩阵:
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);
}
【问题讨论】:
-
从基础开始。你的 glClearColor 是你期望的吗?您的着色器加载时是否没有任何错误?
-
是的。屏幕被正确清除为指定的颜色,并且着色器错误(或缺少)确实打印到控制台。我还验证了我的正交矩阵函数会产生与 glOrtho() 相同的矩阵。
-
实际上,我现在已经把纹理从方程中去掉了,简单地用红色填充三角形,现在,我得到了一些快速扭曲的红色闪烁,但只在前半秒应用程序运行 =/
-
你能贴出计算模型视图和投影矩阵的代码吗?你的绘图循环呢?
标签: c++ android opengl-es drawing android-ndk