2011-06-28 28 views
1

如何將「正常」矩形轉換爲一組頂點OpenGL ES。我不擅長几何,所以我不知道頂點是如何工作的,我希望能夠操縱矩形,而不必通過試驗和錯誤來計算頂點的值。將「正常」矩形轉換爲一組GLes矢量?

基本上,我需要這種結構轉換:

typedef struct __nrect { 
    float width; 
    float height; 
    float depth; 

    /* center */ 
    float x; 
    float y; 
    float z; 
} simple3dRect; 

爲了這樣的事情:

const GLfloat cubeVertices[6][12] = { 
    { 1,-1, 1, -1,-1, 1, 1, 1, 1, -1, 1, 1 }, 
    { 1, 1, 1, 1,-1, 1, 1, 1,-1, 1,-1,-1 }, 
    {-1, 1,-1, -1,-1,-1, -1, 1, 1, -1,-1, 1 }, 
    { 1, 1, 1, -1, 1, 1, 1, 1,-1, -1, 1,-1 }, 
    { 1,-1,-1, -1,-1,-1, 1, 1,-1, -1, 1,-1 }, 
    { 1,-1, 1, -1,-1, 1, 1,-1,-1, -1,-1,-1 }, 
}; 

是否有一個簡單的方法來做到這一點?

+0

normalRect結構沒有足夠的信息來做到這一點 - 是否圍繞(0,0,0)?範圍從(0,0,0) - (寬度,高度,深度)? –

+0

添加東西到結構。 –

+0

您的normalRect看起來像一個音量,而不是矩形。考慮一個立方體有多少個面,以及有多少個頂點定義一個面,現在檢查你從normalRect中給出的信息如何得到每個面(假設這是你所擁有的,因爲它不允許太多)。 – pmr

回答

1

假設所得的立方體是軸對齊和寬度對應於x軸,高度與y軸和深度z軸:

const GLfloat cubeVertices[6][12] = { 
    { x + width/2, y - height/2, z + depth/2, x - width/2, y - height/2, z + depth/2, x + width/2, y + height/2, z + depth/2, x - width/2, y + height/2, z + depth/2 }, 
    { x + width/2, y + height/2, z + depth/2, x + width/2, y - height/2, z + depth/2, x + width/2, y + height/2, z - depth/2, x + width/2, y - height/2, z - depth/2 }, 
    { x - width/2, y + height/2, z - depth/2, x + width/2, y - height/2, z - depth/2, x - width/2, y + height/2, z + depth/2, x - width/2, y - height/2, z + depth/2 }, 
    { x + width/2, y + height/2, z + depth/2, x - width/2, y + height/2, z + depth/2, x + width/2, y + height/2, z - depth/2, x - width/2, y + height/2, z - depth/2 }, 
    { x + width/2, y - height/2, z - depth/2, x - width/2, y - height/2, z - depth/2, x + width/2, y + height/2, z - depth/2, x - width/2, y + height/2, z - depth/2 }, 
    { x + width/2, y - height/2, z + depth/2, x - width/2, y - height/2, z + depth/2, x + width/2, y - height/2, z - depth/2, x - width/2, y - height/2, z - depth/2 }, 
}; 

顯然,這可以簡化/通過預先計算寬度/ 2等值進行優化,這裏爲了清楚起見而將其包括在內。

+0

這不會給立方體兩倍的寬度,高度和深度嗎?我認爲它應該是'寬度/ 2'等等。 – user786653

+0

@ user786653:是的 - 好點。更正... –

+0

我認爲現在任何編譯器都會將多餘的{width,height,depth}/2優化爲預計算。這在AST上很容易實現。 – datenwolf