在适用于 iPhone 的 open GL ES1 中绘制立方体
大家好,友好的计算机人员,
我一直在通过 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++;
}
}
感谢您的所有帮助。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想要的只是 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.在 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.