iphone - 在 openGL ES1 中为 iphone 绘制立方体

标签 iphone c++ opengl-es 3d

你好友好的计算机人,

我一直在通过这本书学习 openGL iPhone 3D programming from O'Reilly .下面我从文本中发布了一个示例,该示例显示了如何绘制圆锥体。我仍在努力思考它,这有点困难,因为我对 C++ 不是很熟悉。

无论如何,我想做的是画一个立方体。任何人都可以建议将以下代码替换为可以绘制简单立方体的代码的最佳方法吗?

const float coneRadius = 0.5f;
const float coneHeight = 1.866f;
const int coneSlices = 40;

{
    // Allocate space for the cone vertices.
    m_cone.resize((coneSlices + 1) * 2);

    // Initialize the vertices of the triangle strip.
    vector<Vertex>::iterator vertex = m_cone.begin();
    const float dtheta = TwoPi / coneSlices;
    for (float theta = 0; vertex != m_cone.end(); theta += dtheta) {

        // Grayscale gradient
        float brightness = abs(sin(theta));
        vec4 color(brightness, brightness, brightness, 1);

        // Apex vertex
        vertex->Position = vec3(0, 1, 0);
        vertex->Color = color;
        vertex++;

        // Rim vertex
        vertex->Position.x = coneRadius * cos(theta);
        vertex->Position.y = 1 - coneHeight; 
        vertex->Position.z = coneRadius * sin(theta);
        vertex->Color = color;
        vertex++;
    }
}

感谢大家的帮助。

最佳答案

如果您只想要一个 OpenGL ES 1.1 立方体,我创建了这样一个示例应用程序(具有纹理并允许您用手指旋转它),您可以获取 here 的代码.我在 iTunes U 上为我的类(class)的 OpenGL ES session 生成了这个示例(我已经修复了您在该类(class)视频中看到的损坏的纹理渲染)。

作者在书中演示了如何用 C++ 构建通用 3-D 引擎,因此他的代码比我的复杂一些。在这部分代码中,他在与 coneSlices 相对应的多个步骤中循环从 0 到 2 * pi 的角度。您可以将他的循环替换为一系列与我在示例应用程序中的顶点相对应的手动顶点添加,以便绘制一个立方体而不是他的圆锥体。您还需要删除他在其他地方用于绘制圆锥体圆形底部的代码。

关于iphone - 在 openGL ES1 中为 iphone 绘制立方体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4100715/

相关文章:

android - 无法在 VMWare 中运行 Genymotion

Android - 为什么我收到致命信号?

iphone - uiwebview 和 mobile safari 的区别

opengl - 将 VBO 传递给具有不同布局的着色器

iphone - 如何在 xcode4.3 中获取和修改显示屏幕大小

c++ - 对 shared_ptr 的引用的引用计数

c++ - 如何访问派生类中定义的函数而不是多态性中的基类

c++ - char *name 和 char* name 有什么区别?

iphone - 平板电脑/移动设备上的数字内容被删除/隐藏

android - 如何在应用程序启动时触发方法? (安卓)