在适用于 iPhone 的 open GL ES1 中绘制立方体

发布于 2024-10-01 01:32:34 字数 1225 浏览 0 评论 0 原文

大家好,友好的计算机人员,

我一直在通过 O'Reilly 的 iPhone 3D 编程一书学习 openGL。下面我发布了一个文本示例,展示了如何绘制圆锥体。我仍在尝试解决它,这有点困难,因为我对 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++;
    }
}

感谢您的所有帮助。

Hello friendly computer people,

I've been studying openGL with the book iPhone 3D programming from O'Reilly. Below I've posted an example from the text which shows how to draw a cone. I'm still trying to wrap my head around it which is a bit difficult since I'm not super familiar with C++.

Anyway, what I would like to do is draw a cube. Could anyone suggest the best way to replace the following code with one that would draw a simple cube?

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++;
    }
}

Thanks for all the help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

妄断弥空 2024-10-08 01:32:34

如果您想要的只是 OpenGL ES 1.1 立方体,我创建了这样一个示例应用程序(具有纹理并允许您使用手指旋转它),您可以获取 此处。我在 iTunes U(此后我修复了您在该课程视频中看到的损坏的纹理渲染)。

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

If all you want is an OpenGL ES 1.1 cube, I created such a sample application (that has texture and lets you rotate it using your finger) that you can grab the code for here. I generated this sample for the OpenGL ES session of my course on iTunes U (I've since fixed the broken texture rendering you see in that class video).

The author is demonstrating how to build a generic 3-D engine in C++ in the book, so his code is a little more involved than mine. In this part of the code, he's looping through an angle from 0 to 2 * pi in a number of steps corresponding to coneSlices. You could replace his loop with a series of manual vertex additions corresponding to the vertices I have in my sample application in order to draw a cube instead of his cone. You'd also need to remove the code he has elsewhere for drawing the circular base of the cone.

清晨说晚安 2024-10-08 01:32:34

在 OpenGLES 1 中,您可能会使用 glVertexPointer 提交几何图形并使用 glDrawArrays 绘制立方体来绘制立方体。请参阅这些教程:

http://iphonedevelopment .blogspot.com/2009/05/opengl-es-from-ground-up-table-of.html

OpenGLES 是一个基于 C 的库。

In OpenGLES 1 you would probably draw a cub using glVertexPointer to submit geometry and glDrawArrays to draw the cube. See these tutorials:

http://iphonedevelopment.blogspot.com/2009/05/opengl-es-from-ground-up-table-of.html

OpenGLES is a C based library.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文