Android OpenGL 只渲染一半屏幕
我遵循了一个 opengl 应用程序的示例,但我不知道为什么会发生这种情况...
我有一个 GLSurfaceView 及其相应的渲染器绘制一个三角形。但是,我并没有在屏幕上显示整个视图,而是只有上半部分,而且正如您在图片中看到的那样,它也是重复的。我使用的是 Nexus One
xml:
<?xml version="1.0" encoding="utf-8"?>
<com.forgottenprojects.geoturista.DrawingLayer
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawing"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
该活动在清单中具有以下内容:
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
DrawingLayer 类:
public class DrawingLayer extends GLSurfaceView {
private TriangleRenderer renderer;
private Context context;
public DrawingLayer(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public DrawingLayer(Context context) {
super(context);
init(context);
}
private void init(Context c)
{
this.context = c;
setEGLConfigChooser(false);
this.renderer = new TriangleRenderer(c);
setRenderer(renderer);
setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
getHolder().setFormat(PixelFormat.TRANSLUCENT);
}
}
三角形渲染器(大部分来自 Apress Pro Android 2 书)
public class TriangleRenderer implements Renderer {
private final static int VERTS = 3;
private FloatBuffer mFVertexBuffer;
private ShortBuffer mIndexBuffer;
TriangleRenderer(Context context)
{
ByteBuffer vbb = ByteBuffer.allocateDirect(VERTS * 3 * 4);
vbb.order(ByteOrder.nativeOrder());
mFVertexBuffer = vbb.asFloatBuffer();
ByteBuffer ibb = ByteBuffer.allocateDirect(VERTS * 2);
ibb.order(ByteOrder.nativeOrder());
mIndexBuffer = ibb.asShortBuffer();
float[] coords = {
-0.5f, -0.5f, 0, // (x1,y1,z1)
0.5f, -0.5f, 0,
0.0f, 0.5f, 0
};
for (int i = 0; i < VERTS; i++) {
for(int j = 0; j < 3; j++) {
mFVertexBuffer.put(coords[i*3+j]);
}
}
short[] myIndecesArray = {0,1,2};
for (int i=0;i<3;i++)
{
mIndexBuffer.put(myIndecesArray[i]);
}
mFVertexBuffer.position(0);
mIndexBuffer.position(0);
}
protected void draw(GL10 gl)
{
gl.glColor4f(1.0f, 0, 0, 0.5f);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mFVertexBuffer);
gl.glDrawElements(GL10.GL_TRIANGLES, VERTS,
GL10.GL_UNSIGNED_SHORT, mIndexBuffer);
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glDisable(GL10.GL_DITHER);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
gl.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glEnable(GL10.GL_DEPTH_TEST);
}
@Override
public void onDrawFrame(GL10 gl) {
gl.glDisable(GL10.GL_DITHER);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glClearColor(1.0f, 0.5f, 0.7f, 1.0f);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
GLU.gluLookAt(gl, 0, 0, -5, 0f, 0f, 0f, 0f, 1f, 0f);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
draw(gl);
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL10.GL_PROJECTION);
float ratio = (float)width / (float)height;
gl.glLoadIdentity();
gl.glFrustumf(-ratio, ratio, -1, 1, 3, 7);
}
}
提前致谢!
I have follow an example of an opengl application and I don't know why is this thing happening...
I have a GLSurfaceView with it's corresponding renderer drawing a triangle. But, instead of getting the whole view on screen, I have just the upper half, and it's also duplicated as you can see on the picture. I'm using a Nexus One
the xml:
<?xml version="1.0" encoding="utf-8"?>
<com.forgottenprojects.geoturista.DrawingLayer
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawing"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
The activity has this in the manifest:
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
The DrawingLayer class:
public class DrawingLayer extends GLSurfaceView {
private TriangleRenderer renderer;
private Context context;
public DrawingLayer(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public DrawingLayer(Context context) {
super(context);
init(context);
}
private void init(Context c)
{
this.context = c;
setEGLConfigChooser(false);
this.renderer = new TriangleRenderer(c);
setRenderer(renderer);
setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
getHolder().setFormat(PixelFormat.TRANSLUCENT);
}
}
The triangle renderer (most of it from Apress Pro Android 2 book)
public class TriangleRenderer implements Renderer {
private final static int VERTS = 3;
private FloatBuffer mFVertexBuffer;
private ShortBuffer mIndexBuffer;
TriangleRenderer(Context context)
{
ByteBuffer vbb = ByteBuffer.allocateDirect(VERTS * 3 * 4);
vbb.order(ByteOrder.nativeOrder());
mFVertexBuffer = vbb.asFloatBuffer();
ByteBuffer ibb = ByteBuffer.allocateDirect(VERTS * 2);
ibb.order(ByteOrder.nativeOrder());
mIndexBuffer = ibb.asShortBuffer();
float[] coords = {
-0.5f, -0.5f, 0, // (x1,y1,z1)
0.5f, -0.5f, 0,
0.0f, 0.5f, 0
};
for (int i = 0; i < VERTS; i++) {
for(int j = 0; j < 3; j++) {
mFVertexBuffer.put(coords[i*3+j]);
}
}
short[] myIndecesArray = {0,1,2};
for (int i=0;i<3;i++)
{
mIndexBuffer.put(myIndecesArray[i]);
}
mFVertexBuffer.position(0);
mIndexBuffer.position(0);
}
protected void draw(GL10 gl)
{
gl.glColor4f(1.0f, 0, 0, 0.5f);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mFVertexBuffer);
gl.glDrawElements(GL10.GL_TRIANGLES, VERTS,
GL10.GL_UNSIGNED_SHORT, mIndexBuffer);
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glDisable(GL10.GL_DITHER);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
gl.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glEnable(GL10.GL_DEPTH_TEST);
}
@Override
public void onDrawFrame(GL10 gl) {
gl.glDisable(GL10.GL_DITHER);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glClearColor(1.0f, 0.5f, 0.7f, 1.0f);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
GLU.gluLookAt(gl, 0, 0, -5, 0f, 0f, 0f, 0f, 1f, 0f);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
draw(gl);
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL10.GL_PROJECTION);
float ratio = (float)width / (float)height;
gl.glLoadIdentity();
gl.glFrustumf(-ratio, ratio, -1, 1, 3, 7);
}
}
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它可以工作
,但我不知道如何在其后面应用透明度(因为还有另一个带有相机视图的表面视图)
It worked with
but I don't know how to apply transparency behind it (because there is another surfaceview with a camera view)
看来您的 EGL 显示配置错误。
Looks like your EGL display configuration is wrong.
我想对皮埃尔·安托万的回应添加评论,但我还没有适当的声誉。
正如他提到的,问题在于 EGL 显示配置错误。
当我遇到这个问题时,我遇到的 EGL 显示配置问题是,当迭代配置列表时,我在列表中选择了比我应该选择的更靠下的一个。
在我的具体示例(运行 Android 2.3 的索尼 Xperia X10)中,我选择了一个 EGL 配置,将
EGL_SAMPLES
设置为 4,并将EGL_SAMPLE_BUFFERS
设置为 1。配置代码来选择列表中的“较早”配置,其中EGL_SAMPLES
设置为 1,EGL_SAMPLE_BUFFERS
设置为 0,我的半黑屏幕消失了,并且得到了正确的渲染。I would add a comment to Pierre-Antoine's response, but I don't have the proper level of reputation yet.
As he mentions, the issue is that the EGL display configuration is wrong.
When I encountered this problem, the EGL display config issue that I ran into is that when iterating through the list of configs, I choose one further down in the list than I should have.
In my specific example (a Sony Xperia X10 running Android 2.3), I was choosing an EGL config that had
EGL_SAMPLES
set to 4 andEGL_SAMPLE_BUFFERS
set to 1. When I changed my config code to choose an "earlier" config int he list withEGL_SAMPLES
set to 1 andEGL_SAMPLE_BUFFERS
set to 0, my half-black screen went away and I got a proper render.