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

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

问题描述

下面的文字是有点lenghty因为我想确保人们有类似的问题,例如:谷歌这个页面可以轻松方便地按照具体问题及其解决方案。但现在我的问题:

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使用问题。我的认为的我知道他们做了什么,但是我看不到的三角形我画。我运行下面的code在我的活动onDrawFrame法。据说活动实现GLSurfaceView.Renderer与标有@Override的onDrawFrame。我的code是:

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);

接下来的点是什么我假定的了解,在上述code相机设置短期票据。请纠正我,如果我有什么问题在下面的解释,因为这可能已经解决了我所有的问题!

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)设定为67度视角中的y维度的图平截的立体投影。使用aspect_ration =(浮点)的宽度/高度的OpenGL计算在x维度的视角。进一步的1是近裁剪面,而100是远裁剪面的这种情况下的值。
  3. 据我所知不要紧,凡在code gluLookAt被调用,只要它是实际的绘制完成之前,(我不确定这虽然)。在这个例子中,我改变了模型视图矩阵第一,装载单位矩阵,然后gluLookAt被调用。 3。
  4. GLU.gluLookAt(GL,0,0,20,0,0,0,0,1,0)实际上由三个3-要素矢量。初始酮((0,0,20)在本实施例中)设置在相机世界坐标(即,20个单位在正Z轴,这意味着它是伸出的手机的屏幕的前面的位置)。第二矢量确定在世界上点坐标系统的相机着眼于(在这个例子中,坐标系的原点,即相机看起来沿负z轴)。第三矢量是向上矢量定义的y轴的摄像机坐标系(因此也对x轴通过叉积与从摄像机位置到它着眼于该点的矢量)的方向。
  1. glMatrixMode(GL10.GL_PROJECTION) switches to changing the camera matrix (for which I load an identity matrix directly afterwards)
  2. GLU.gluPerspective(gl, 67, aspect_ratio, 1, 100) sets a perspective projection with a view frustrum of 67 degree view angle in the y-dimension. Using "aspect_ration=(float)width/height" OpenGL computes the view angle in the x-dimension. Further the "1" is the near clipping plane, while "100" is the value of the far clipping plane in this case.
  3. As far as I know it does not matter, where in the code gluLookAt is called, as long as it is done before the actual drawing (I'm unsure about this though). In this example I change to the MODELVIEW matrix first, load an identity matrix, and then gluLookAt is called. 3.
  4. "GLU.gluLookAt(gl, 0, 0, 20, 0, 0, 0, 0, 1, 0)" actually consists of three 3-element vectors. The initial one ((0,0,20) in this example) sets the position of the camera in world coordinates (i.e. 20 units on the positive Z-axis, which means it is "sticking out" of the front of the phone's screen). The second vector determines the point in the world coordinate system the camera looks at (in this example the origin of the coordinate system, i.e. the camera looks along the negative z-axis). The third vector is the up-vector that defines the direction of the y-Axis of the cameras coordinate system (and therefore also the x-axis via cross product with the vector from camera position to the point it looks at).

三角绘图code是从教程复制(但我发现了类似的code也在其他教程,所以我想这是没有问题的)。

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.

所以这漫长的引入问题,我简单的问题是后:?我是怎么陷入困境,使该code没有做什么,我希望它

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的内部位置。予后为没有设置可变vertex_buffer的位置为0(0℃,I&10 9)循环。缓冲区的位置设置为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)

据我所知,他们做同样的事情。我赶紧测试,其中在C位置复位$ C $属于和我的例子code 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天全站免登陆