Android OpenGL ES - 我无法让 gluLookAt/gluPerspective 工作 [英] Android OpenGL ES - I can't make gluLookAt/gluPerspective work

查看:18
本文介绍了Android OpenGL ES - 我无法让 gluLookAt/gluPerspective 工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下文字有点冗长,因为我想确保有类似问题的人,例如谷歌这个页面可以轻松地跟踪确切的问题及其解决方案.但现在是我的问题:

the following text is a bit lenghty as I wanted to make sure that people with similar problems that e.g. google this page can easily follow the exact problem and its solutions easily. But now on to my issue:

我最近开始在 Android 智能手机上编程 OpenGL ES,但在理解如何使用 gluPerspective 和 gluLookAt 时遇到了问题.我认为我明白他们在做什么,但是我看不到我画的三角形.我在活动的 onDrawFrame 方法中运行以下代码.所述活动使用标有@Override 的onDrawFrame 实现GLSurfaceView.Renderer.我的代码是:

I recently started programming OpenGL ES on an Android smartphone and ran into problems understanding how gluPerspective and gluLookAt are used. I think I understand what they do, however I cannot see the triangle I draw. I run the following code in the onDrawFrame-method of my activity. Said activity implements GLSurfaceView.Renderer with the onDrawFrame marked with @Override. My code is:

gl.glViewport(0, 0, screen_width, screen_height);
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f); //set background to black
gl.glClearDepthf(1.0f); 

gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

gl.glMatrixMode(GL10.GL_PROJECTION); // Change PROJECTION matrix
gl.glLoadIdentity();

float aspect_ratio = (float) screen_width / screen_height;
GLU.gluPerspective(gl, 67, aspect_ratio, 1, 100);

gl.glEnable(GL10.GL_DEPTH_TEST);
gl.glMatrixMode(GL10.GL_MODELVIEW); // Change MODELVIEW matrix
gl.glLoadIdentity();

GLU.gluLookAt(gl, 0, 0, 20, 0, 0, 0, 0, 1, 0);

// make triangle with vertices (-2,0,0), (2,0,0), and (0,.5,0)
float[] vertices = { -2f, 0, 0, 2f, 0, 0, 0, .5f, 0 };
ByteBuffer buffer = ByteBuffer.allocateDirect(vertices.length * 4);
buffer.order(ByteOrder.nativeOrder());
FloatBuffer vertex_buffer = buffer.asFloatBuffer();
for (int i = 0; i < 9; i++) {
    vertex_buffer.put(vertices[i]);
}

gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertex_buffer);
gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisable(GL10.GL_DEPTH_TEST);

接下来的几点是关于我在上述代码中假定知道的相机设置的简短说明.如果我在以下解释中出现错误,请纠正我,因为这可能已经解决了我所有的问题!

The next points are short notes on what I putatively know about camera settings in the above code. PLEASE correct me if I got something wrong in the following explanations, as that might already solve all of my issues!

  1. glMatrixMode(GL10.GL_PROJECTION) 切换到改变相机矩阵(我之后直接加载单位矩阵)
  2. GLU.gluPerspective(gl, 67, aspect_ratio, 1, 100) 设置透视投影,在 y 维度上具有 67 度视角的视锥.使用aspect_ration=(float)width/height"OpenGL 计算 x 维度的视角.此外,在这种情况下,1"是近剪裁平面,而100"是远剪裁平面的值.
  3. 据我所知,在代码中调用 gluLookAt 的位置并不重要,只要在实际绘图之前完成即可(不过我不确定这一点).在本例中,我首先更改为 MODELVIEW 矩阵,加载单位矩阵,然后调用 gluLookAt.3.
  4. "GLU.gluLookAt(gl, 0, 0, 20, 0, 0, 0, 0, 1, 0)" 实际上由三个 3 元素向量组成.第一个(本例中的(0,0,20))设置相机在世界坐标中的位置(即正 Z 轴上的 20 个单位,这意味着它伸出"手机屏幕的正面).第二个向量确定了相机在世界坐标系中观察的点(在本例中是坐标系的原点,即相机沿负 z 轴观察).第三个向量是向上向量,它定义了相机坐标系的 y 轴方向(因此也是 x 轴,通过与从相机位置到它观察点的向量的叉积).

三角形的绘制代码是从一个教程中复制过来的(但我在其他教程中也发现了类似的代码,所以我认为这不是问题).

The triangle drawing code was copied from a tutorial (but I found similar code also in other tutorials, so I assume this is not the problem).

据我所知,相机现在位于 (0,0,20) 上,并朝向坐标系的原点.它的绘图截头体从 z 值 19 开始,一直延伸到 -80(相机位于 z 轴点 20 并沿负 z 轴观察;它的视锥体在 1 和 100 处具有近平面和远平面(两者都看在这种情况下分别沿负 z 轴)).因此,三角形应该是可见的,因为它的顶点位于 z 轴 = 0 平面(即 xy 平面)上的原点周围,该平面显然位于视锥体内.进一步绘制的顶点按逆时针顺序排列;OpenGL 中的默认设置.

As far as I get it, the camera is now positioned on (0,0,20) and looks towards the origin of the coordinate system. Its drawing frustrum starts at z-value 19 and goes as far as -80 (the camera is at z-axis point 20 and looks along the negative z-axis; its viewing frustrum has near and far plane at 1 and 100 (both looking along the negative z-axis in this case), respectively). Therefore, the triangle should be visible, as its vertices are positioned around the origin on the z-axis=0 plane (i.e. the xy-plane), which is clearly within the viewing frustrum. Further the drawn vertices are arranged in counter-clockwise order; the default in OpenGL.

所以在对问题进行了长时间的介绍之后,我的简单问题是:我是不是搞砸了,以至于这段代码没有按照我的预期运行?

So after this long introduction into the problem my simple question is: What did I mess up so that this code does not do what I expect it to?

推荐答案

我在查看更多教程后找到了答案(然后在我之前使用的教程中也注意到了)!感谢大家的回答,他们对诊断 OpenGL 问题非常有帮助,将来肯定会帮助我!

I found the answer after looking at further tutorials (and then also noticed it in the ones I used before)! Thank all of you for your answers, they were very helpful in diagnosing OpenGL problems in general and will for sure help me in the future!

我的问题的解决方案是... 问题不是 glulookat 或 gluperspective,而是在我的情况下 FloatBuffer 的内部位置.在 for(0

The solution to my problem is... that the problem is not glulookat or gluperspective, but in my case the internal position of the FloatBuffer. I did not set the position of variable vertex_buffer to 0 after the for(0 < i < 9) loop. Setting the buffer's position to 0 can be accomplished with at least two commands:

  • vertex_buffer.rewind()
  • vertex_buffer.position(0)

据我所知,他们做同样的事情.我快速测试了位置重置在代码中的位置,在我的示例代码中,我可以在 gl.glEnableClientState(GL10.GL_VERTEX_ARRAY) 之后调用它,但是我必须在 gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertex_buffer) 之前调用它),否则返回初始问题.

To my knowledge they do the same thing. I quickly tested where in the code the position reset belongs and in my example code I could call it after gl.glEnableClientState(GL10.GL_VERTEX_ARRAY), however I had to call it before gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertex_buffer), otherwise the initial problem returned.

我希望这能帮助其他有类似问题的人,或者可能只是缺乏 glulookat 和 gluperspective ;)

I hope this helps other people with similar issues or maybe is just a short of glulookat and gluperspective ;)

再次感谢您的建议!

这篇关于Android OpenGL ES - 我无法让 gluLookAt/gluPerspective 工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆