【问题标题】:OpenGLES10: Apply Projection & Camera ViewOpenGLES10:应用投影和相机视图
【发布时间】:2011-10-16 14:42:17
【问题描述】:

我需要一点帮助: android 开发者,教程:OpenGLES10。 a link

在我输入投影和相机视图的代码之前,第一个三角形一切正常。这应该调整 OpenGLES Square 视图以匹配手机的屏幕,因此对象保持在比例中。 作为一个新手观看,代码看起来很好,我已经检查了参考文件,没有缺少参数或类似的东西。但现在我迷路了..!看不出有什么问题。 如果应用了投影和相机代码,则没有三角形,而是应用程序。正在运行并显示带有背景颜色的视图。

这是我的代码:

    package notme.helloopengles10;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLSurfaceView;
import android.opengl.GLU;

public class HelloOpenGLES10Renderer implements GLSurfaceView.Renderer {

// Set the background frame color
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    gl.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);

    // initialize the triangle vertex array
    initShapes();
    //enable use of vertex arrays
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
}

public void onDrawFrame(GL10 gl) {
    // Redraw background color  
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

 /* // set GL_MODELVIEW transformation mode (If outline from here to after GLU.gluLookAt() - it works when also outlines further down i code!
    gl.glMatrixMode(GL10.GL_MODELVIEW);
    gl.glLoadIdentity();  // reset Matrix to its default state

    // when using GL_MODELVIEW, you must set the view point
    GLU.gluLookAt(gl, 0, 0, -5, 0f, 0f, 0f, 0f, 1.0f, 0.0f);  */

    //Draw Triangel
    gl.glColor4f(0.63671875f, 0.76953125f, 0.22265625f, 0.0f);
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, triangleVB);
    gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);
}

// Redraw on orientation changes // adjust for screen size ratio
public void onSurfaceChanged(GL10 gl, int width, int height) {
    gl.glViewport(0, 0, width, height);

    // Make adjustments  for screen ratio 
 /*(If outline from here to after gl.Frumstumf() - it works!
    float ratio = (float) width / height;
    gl.glMatrixMode(GL10.GL_PROJECTION);  // set matrix to projection mode
    gl.glLoadIdentity();                  // reset the matrix to its default state
    gl.glFrustumf(-ratio, ratio, -1, 1, 3, 7);        // apply the projection   */

}

/*
 * Draw a shape, a triangle. first add new member variable to contain
* the vertices of a triangle
*/
 private FloatBuffer triangleVB;

//Create a method, initShaoe(), which populate the members variable
 private void initShapes(){
     //create a array 
     float triangleCoords[] = {
             // X, Y, Z
             -0.5f, -0.25f, 0,
             0.5f, -0.25f, 0,
             0.0f, 0,559016994f, 0
     };
 // initialize vertex Buffer for triangle
     ByteBuffer vbb= ByteBuffer.allocateDirect(
             //(# of coordinates values * 4 bytes per float)
             triangleCoords.length * 4 );
     vbb.order(ByteOrder.nativeOrder()); // use device hardware's native byte order
     triangleVB = vbb.asFloatBuffer(); //create floating point buffer from the ByteBuffer
     triangleVB.put(triangleCoords);  // add coordinates to the FloatBuffer
     triangleVB.position(0);  // set the buffer to read the first coordinate
 }


} // end

希望有人能告诉我,哪里出了问题?

开发工具:Eclipse。

【问题讨论】:

  • 欢迎来到 StackOverflow,希望您阅读FAQ
  • 我对 Android 开发者页面上提供的培训材料的质量感到非常失望。很多代码都丢失了,通常不可能简单地按照示例进行......

标签: android opengl-es


【解决方案1】:

我在本教程中遇到了同样的问题,当我在 Triangle 类中的 vertex shader code 中更改乘法顺序时,它得到了解决。因此,不要使用uMVPMatrix * vPosition,而是将其替换为vPosition * uMVPMatrix。我猜这是因为vPosition 是一个行向量。

【讨论】:

  • 谢谢。我有同样的错误。在开发文档中,顺序是错误的,甚至有一个注释,代码不会使用错误的乘数顺序。
【解决方案2】:

代码看起来很合理(如果您取消注释此刻被注释掉的部分)。您的矩阵修改代码非常正确,所有转换都应用于正确的矩阵。

但此时您正在从点 (0,0,-5) 到点 (0,0,0),因此沿着 +z 轴。但由于默认的 OpenGL 视图沿 -z 轴查看,因此您实际上将视图围绕 y 轴旋转 180 度。虽然这绝对没有问题,但您现在看到了三角形的背面。那么,您是否启用了背面剔除并且该背面只是被优化掉了?只需尝试通过调用glDisable(GL_CULL_FACE) 禁用背面剔除,或将gluLookAt 调用中的-5 更改为5,以便您沿-z 轴查看。

您也可以尝试使用gluPerspective(45, ratio, 3, 7) 而不是glFrustum 调用,但是您对glFrustum 的参数看起来很合理。当然,请记住,这两个调用都创建了一个透视图,更远的对象变得更小,就像在现实中一样。如果您确实想要一个平行/正交视图(屏幕上的大小与深度无关),您应该将 glFrustum 替换为 glOrtho,尽管参数可以保持不变。

【讨论】:

  • 嘿克里斯蒂安。谢谢您的帮助。我终于明白了。你用 pihntlook 为我指明了正确的方向。但是在 Glu.LookAt(); 中将 -5 更改为 5;没有帮助,也没有改变 glFrusstumf();到 glOrthof();。但这确实让我想知道为什么 glFrumstuf()-ratio, ratio, -1, 1, 3 7);有一个负数!我认为投影设置为-1很奇怪,所以我改变了它,现在屏幕上显示了三角形。
  • @RAJensen 但就像我说的,您的矩阵函数及其参数实际上是正确的。你有什么改变?
  • 只有 gl.glFrustumf(gl,-ratio, ratio, -1 , 3, 7);到 gl.glFrustumf(gl,-ratio, ratio, 1 , 3, 7);负面的
  • @RAJensen 所以你的意思是glFrustumf(-ratio, ratio, 1, 1 , 3, 7)?这不应该工作。它应该是glFrustumf(-ratio, ratio, -1, 1 , 3, 7),就像在您的代码示例中一样。如果这使它起作用,则错误必须在其他地方。也许您可以在代码中的某些点检查glGetError 的返回。
  • 但它使它工作! :D 是的,你说得对,仍然有问题,因为它只适用于 Emulator5554,但不能在上传到 HTC Wildfire S 时使用。如果教程代码中有错误,学习新东西并不容易! :D
【解决方案3】:

您对 gluLookAt 的调用会破坏您的模型视图矩阵。您应该在投影矩阵处于活动状态时调用此函数。

http://www.opengl.org/sdk/docs/man/xhtml/gluLookAt.xml

这段代码为我显示了三角形:

public void onDrawFrame(GL10 gl) {
    // Redraw background color  
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

    gl.glMatrixMode(GL10.GL_PROJECTION);
    gl.glLoadIdentity();
    // when using GL_MODELVIEW, you must set the view point
    GLU.gluLookAt(gl, 0, 0, -5, 0f, 0f, 0f, 0f, 1.0f, 0.0f);

 // set GL_MODELVIEW transformation mode (If outline from here to after GLU.gluLookAt() - it works when also outlines further down i code!
    gl.glMatrixMode(GL10.GL_MODELVIEW);
    gl.glLoadIdentity();  // reset Matrix to its default state

    //Draw Triangel
    gl.glColor4f(0.63671875f, 0.76953125f, 0.22265625f, 0.0f);
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, triangleVB);
    gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);
}

【讨论】:

  • 嘿谢尔盖。是的,它也可以在这里使用,但是 Matrix 不能扩展到设备屏幕。所以 Matrix 仍然作为默认的正方形大小应用,所以三角形的比例不适合设备屏幕大小。结果和之前输入屏幕比例校正代码一样。
  • 错误答案和常见的误解。 gluLookAt 代表相机/view 变换,因此属于modelview 矩阵(通常作为glLoadIdentity 之后的第一个变换)。您的示例实际上并没有设置投影变换(因此使用身份正交投影)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-21
  • 1970-01-01
  • 2016-06-26
相关资源
最近更新 更多