gpt4 book ai didi

android - GL Wallpaper 示例仅在模拟器中显示绿屏,但它在设备中正常工作

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:47:13 30 4
gpt4 key购买 nike

运行 OpenGL 应用程序是否需要任何特殊的模拟器设置?

我已经将“GPU 仿真”属性设置为"is"。

我正在尝试运行 Android 示例动态壁纸,使用从 this link 找到的示例源,所需的输出是一个旋转的三角形。

经过一些努力,我让应用程序运行起来,但它没有在模拟器中绘制任何东西,但是当我在设备中测试时它可以工作,但在模拟器中它仍然只是显示一个绿屏,我在 Google groups here 中找到了关于它的讨论.我试图按照其中的说明设置视口(viewport)。但它仍然没有显示任何结果,表面上我已经添加了这一行

gl.glViewport(0, 0, width, height);

这是设置视口(viewport)的正确方法吗?

这是我的渲染类,

 public class MyRenderer implements GLWallpaperService.Renderer {
GLTriangle mTriangle;

public void onDrawFrame(GL10 gl) {


gl.glClearColor(0.2f, 0.4f, 0.2f, 1f);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

gl.glMatrixMode(GL10.GL_MODELVIEW);
autoRotate(gl);
gl.glColor4f(.2f, 0f, .5f, 1f);

mTriangle.draw(gl);
}

public void onSurfaceChanged(GL10 gl, int width, int height) {

gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
GLU.gluPerspective(gl, 60f, (float)width/(float)height, 1f, 100f);

gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glTranslatef(0, 0, -5);
}

public void onSurfaceCreated(GL10 gl, EGLConfig config) {
mTriangle = new GLTriangle();



gl.glClearDepthf(1f);
gl.glEnable(GL10.GL_DEPTH_TEST);
gl.glDepthFunc(GL10.GL_LEQUAL);
}

/**
* Called when the engine is destroyed. Do any necessary clean up because
* at this point your renderer instance is now done for.
*/
public void release() {

}

private void autoRotate(GL10 gl) {
gl.glRotatef(1, 0, 1, 0);
gl.glRotatef(0.5f, 1, 0, 0);
}
}

Herse 是 GLTriangle 类

import java.nio.FloatBuffer;
import java.nio.ShortBuffer;

import javax.microedition.khronos.opengles.GL10;

public class GLTriangle {
private FloatBuffer _vertexBuffer;
private final int _nrOfVertices = 3;

private ShortBuffer _indexBuffer;

public GLTriangle() {
init();
}

private void init() {
// We use ByteBuffer.allocateDirect() to get memory outside of
// the normal, garbage collected heap. I think this is done
// because the buffer is subject to native I/O.
// See http://download.oracle.com/javase/1.4.2/docs/api/java/nio/ByteBuffer.html#direct

// 3 is the number of coordinates to each vertex.
_vertexBuffer = BufferFactory.createFloatBuffer(_nrOfVertices * 3);

_indexBuffer = BufferFactory.createShortBuffer(_nrOfVertices);

// Coordinates for the vertexes of the triangle.
float[] coords = {
-1f, -1f, 0f, // (x1, y1, z1)
1f, -1f, 0f, // (x2, y2, z2)
0f, 1f, 0f // (x3, y3, z3)
};

short[] _indicesArray = {0, 1, 2};

_vertexBuffer.put(coords);
_indexBuffer.put(_indicesArray);

_vertexBuffer.position(0);
_indexBuffer.position(0);
}

public void draw(GL10 gl) {
// 3 coordinates in each vertex
// 0 is the space between each vertex. They are densely packed
// in the array, so the value is 0
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, getVertexBuffer());

// Draw the primitives, in this case, triangles.
gl.glDrawElements(GL10.GL_TRIANGLES, _nrOfVertices, GL10.GL_UNSIGNED_SHORT, _indexBuffer);
}

private FloatBuffer getVertexBuffer() {
return _vertexBuffer;
}
}

这里出了什么问题?有没有更好的Open GL动态壁纸示例代码?

最佳答案

我终于找到了..

我需要做的就是添加

gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

onSurfaceCreated 方法以及代码行

gl.glViewport(0, 0, width, height);

MyRenderer类的onSurfaceChanged方法中

我在 stack itself 中发现了类似的问题[但是对我有用的解决方案没有被标记为正确:( ]

关于android - GL Wallpaper 示例仅在模拟器中显示绿屏,但它在设备中正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12651887/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com