簡體   English   中英

在iPhone的開放式GL ES1中繪制立方體

[英]Drawing a cube in open GL ES1 for the iphone

您好友好的計算機人員,

我一直在用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++;
    }
}

感謝您的所有幫助。

如果您只需要一個OpenGL ES 1.1多維數據集,我就創建了一個示例應用程序(具有紋理,並允許您用手指旋轉它),您可以在此處獲取代碼。 我在iTunes U上的課程OpenGL ES會話中生成了此示例(此后,我修復了該視頻中看到的破碎的紋理渲染)。

作者在本書中演示了如何用C ++構建通用的3-D引擎,因此他的代碼比我的代碼要復雜得多。 在代碼的這一部分中,他按照對應於coneSlices的多個步驟在0到2 * pi的角度內循環。 您可以用與我的示例應用程序中的頂點相對應的一系列手動頂點添加來替換他的循環,以便繪制一個立方體而不是他的圓錐體。 您還需要刪除他在其他地方繪制圓錐的圓形基座的代碼。

在OpenGLES 1中,您可能會使用glVertexPointer繪制幾何體,並使用glDrawArrays繪制立方體。 請參閱以下教程:

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

OpenGLES是基於C的庫。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM