Din*_*dal 10 android opengl-es
我有一系列OpenGL-ES调用,可以在模拟器(2.0.1)上使用alpha混合正确渲染三角形并对其进行纹理处理.当我在实际设备(Droid 2.0.1)上启动相同的代码时,我得到的只是白色方块.
这告诉我纹理没有加载,但我无法弄清楚它们为什么不加载.我的所有纹理都是带有alpha通道的32位PNG,在res/raw下,所以它们没有按照sdk文档进行优化.
这是我加载纹理的方式:
private void loadGLTexture(GL10 gl, Context context, int reasource_id, int texture_id)
{
//Get the texture from the Android resource directory
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), reasource_id, sBitmapOptions);
//Generate one texture pointer...
gl.glGenTextures(1, textures, texture_id);
//...and bind it to our array
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[texture_id]);
//Create Nearest Filtered Texture
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
//Different possible texture parameters, e.g. GL10.GL_CLAMP_TO_EDGE
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_REPEAT);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT);
//Use the Android GLUtils to specify a two-dimensional texture image from our bitmap
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
//Clean up
bitmap.recycle();
}
Run Code Online (Sandbox Code Playgroud)
这是我渲染纹理的方式:
//Clear
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
//Enable vertex buffer
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
//Push transformation matrix
gl.glPushMatrix();
//Transformation matrices
gl.glTranslatef(x, y, 0.0f);
gl.glScalef(scalefactor, scalefactor, 0.0f);
gl.glColor4f(1.0f,1.0f,1.0f,1.0f);
//Bind the texture
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[textureid]);
//Draw the vertices as triangles
gl.glDrawElements(GL10.GL_TRIANGLES, indices.length, GL10.GL_UNSIGNED_BYTE, indexBuffer);
//Pop the matrix back to where we left it
gl.glPopMatrix();
//Disable the client state before leaving
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
Run Code Online (Sandbox Code Playgroud)
以下是我启用的选项:
gl.glShadeModel(GL10.GL_SMOOTH); //Enable Smooth Shading
gl.glEnable(GL10.GL_DEPTH_TEST); //Enables Depth Testing
gl.glDepthFunc(GL10.GL_LEQUAL); //The Type Of Depth Testing To Do
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_SRC_ALPHA,GL10.GL_ONE_MINUS_SRC_ALPHA);
Run Code Online (Sandbox Code Playgroud)
编辑:我刚刚尝试向BitmapFactory.decodeResource()调用提供BitmapOptions,但这似乎无法解决问题,尽管手动设置相同的preferredconfig,density和targetdensity.
Edit2:根据要求,这是模拟器工作的屏幕截图.底层三角形显示为圆形纹理,透明度正常,因为您可以看到黑色背景.
删除了死的ImageShack链接
下面是机器人使用完全相同的代码执行的操作:
删除了死的ImageShack链接
Edit3:这是我的BitmapOptions,更新了上面的调用,我现在调用了BitmapFactory,结果如下:sBitmapOptions.inPreferredConfig = Bitmap.Config.RGB_565;
sBitmapOptions.inDensity = 160;
sBitmapOptions.inTargetDensity = 160;
sBitmapOptions.inScreenDensity = 160;
sBitmapOptions.inDither = false;
sBitmapOptions.inSampleSize = 1;
sBitmapOptions.inScaled = false;
Run Code Online (Sandbox Code Playgroud)
这是我的顶点,纹理坐标和索引:
/** The initial vertex definition */
private static final float vertices[] = {
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
-1.0f, 1.0f, 0.0f,
1.0f, 1.0f, 0.0f
};
/** The initial texture coordinates (u, v) */
private static final float texture[] = {
//Mapping coordinates for the vertices
0.0f, 1.0f,
1.0f, 1.0f,
0.0f, 0.0f,
1.0f, 0.0f
};
/** The initial indices definition */
private static final byte indices[] = {
//Faces definition
0,1,3, 0,3,2
};
Run Code Online (Sandbox Code Playgroud)
无论如何,一旦将纹理内容加载到OpenGL ES中,它是否会转储?也许我可以将模拟器的加载纹理与实际设备的加载纹理进行比较?
我尝试使用不同的纹理(默认的android图标),再次,它适用于模拟器,但无法在实际手机上呈现.
Edit4:当我进行纹理加载时尝试切换.没运气.尝试使用0到glGenTextures的常量偏移0,没有变化.
有没有我正在使用的模拟器支持实际的手机没有?
编辑5:下面的每个Ryan,我将纹理从200x200调整为256x256,问题未得到解决.
编辑:根据要求,将调用添加到上面的glVertexPointer和glTexCoordPointer.另外,这里是vertexBuffer,textureBuffer和indexBuffer的初始化:
ByteBuffer byteBuf = ByteBuffer.allocateDirect(vertices.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
vertexBuffer = byteBuf.asFloatBuffer();
vertexBuffer.put(vertices);
vertexBuffer.position(0);
byteBuf = ByteBuffer.allocateDirect(texture.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
textureBuffer = byteBuf.asFloatBuffer();
textureBuffer.put(texture);
textureBuffer.position(0);
indexBuffer = ByteBuffer.allocateDirect(indices.length);
indexBuffer.put(indices);
indexBuffer.position(0);
loadGLTextures(gl, this.context);
Run Code Online (Sandbox Code Playgroud)
Nic*_*ood 11
当我从SDK 3移到4时,我确实遇到了这个问题.我的纹理将显示在模拟器上,但不会显示在真实设备上(Motorola Milestone/Droid).
你的所有纹理都必须是2的二进制幂.我的是256x256.然而,纹理不显示的原因是操作系统正在从二进制功率大小缩放它们以显示在Milestone的高密度屏幕上.
解决方案很简单 - 只需将纹理移动到drawable-nodpi目录,操作系统在加载时就不会缩放它们.
我也是 Android OpenGL ES 开发新手。我拥有 Milestone(Droid 的欧元版)。
从我到目前为止所看到的情况来看,在我的里程碑上,从不同的书籍/网站上启动了几个教程应用程序,这款手机处理 OpenGL ES 的方式似乎与迄今为止发布的所有其他 Android 手机不同。
我认为这与设备支持的 OpenGL 扩展有关。
检查此链接以获取支持的扩展列表,我很确定代码专家会找到为什么 droid 以如此奇怪的方式处理 opengl es。
http://www.rbgrn.net/content/345-hands-on-motorola-droid-opengl-es-specs
希望这个对你有帮助
编辑:
我调整了manifest.xml并在使用时注意到
<uses-sdk android:minSdkVersion="6"/>
Run Code Online (Sandbox Code Playgroud)
资源根本没有加载。
使用时
<uses-sdk android:minSdkVersion="3"/>
Run Code Online (Sandbox Code Playgroud)
资源加载正常。
所以我切换回
<uses-sdk android:minSdkVersion="6"/>
Run Code Online (Sandbox Code Playgroud)
并改变了位图的加载方式。
尝试更换:
//Get the texture from the Android resource directory
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), reasource_id, sBitmapOptions);
Run Code Online (Sandbox Code Playgroud)
和 :
InputStream is = context.getResources().openRawResource(your.resource.there);
Bitmap bitmap = null;
try {
bitmap = BitmapFactory.decodeStream(is);
} finally {
//Always clear and close
try {
is.close();
is = null;
} catch (IOException e) {
}
}
Run Code Online (Sandbox Code Playgroud)
使用该代码,纹理在我的里程碑上加载良好,希望这对您有用!
| 归档时间: |
|
| 查看次数: |
6117 次 |
| 最近记录: |