2010-11-10 66 views
0

我一直很喜歡用OpenGL玩和遵循一些例子等iPhone的OpenGL:構形立方體問題

我的問題是紋理的立方體。大多數側面紋理都很好(正面,背面,左側),但正確的是混亂。

我的立方體

// cube 
static const GLfloat cubeVertices[] = { 
    -5.0f, 15.0f, 5.0f, 
    5.0f, 15.0f, 5.0f, 
    5.0f,0.0f, 5.0f, 
    -5.0f,0.0f, 5.0f, 
    -5.0f, 15.0f,-5.0f, 
    5.0f, 15.0f,-5.0f, 
    5.0f,0.0f,-5.0f, 
    -5.0f,0.0f,-5.0f 
}; 

static const GLubyte cubeNumberOfIndices = 36; 

const GLubyte cubeVertexFaces[] = { 
    0, 1, 5, // Half of top face 
    0, 5, 4, // Other half of top face 
    4, 6, 5, // Half of front face 
    4, 6, 7, // Other half of front face 
    0, 1, 2, // Half of back face 
    0, 3, 2, // Other half of back face 
    1, 2, 5, // Half of right face 
    2, 5, 6, // Other half of right face 
    0, 3, 4, // Half of left face 
    7, 4, 3, // Other half of left face 
    3, 6, 2, // Half of bottom face 
    6, 7, 3, // Other half of bottom face 
}; 

我的紋理貼圖

const GLshort squareTextureCoords2[] = { 
    0, 5, // top left 
    0, 0, // bottom left 
    15, 0, //bottom right 
    15, 5 //top right 
}; 

我立方碼

//tell GL about our texture 
    glBindTexture(GL_TEXTURE_2D, 1); 
    glTexCoordPointer(2, GL_SHORT, 0, squareTextureCoords2); 

    glVertexPointer(3, GL_FLOAT, 0, cubeVertices); 

    for(int i = 0; i < cubeNumberOfIndices; i += 3) { 
     //glColor4ub(cubeFaceColors[colorIndex], cubeFaceColors[colorIndex+1], cubeFaceColors[colorIndex+2], cubeFaceColors[colorIndex+3]); 
     int face = (i/3.0); 
     if (face % 2 != 0.0) { 
     } 
     glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_BYTE, &cubeVertexFaces[i]); 
} 

所以,正如我所說的,一切都呈現但是立方體的一側(看不到頂和底部所以不擔心)紋理變得奇怪

由於

UPDATE **

我現在嘗試了這些COORDS和紋理在每一側上沒有出現由前向後的紋理它,所以側面是像從紋理的另一個邊緣線。我必須接近破解這個

const GLfloat squareTextureCoords3[] = { 
    // Front face 
    0, 5,  // top left 
    0, 0,  // bottom left 
    5, 0,  // bottom right 
    5, 5,  // top right 

    // Top face 
    0, 5,  // top left 
    0, 0,  // bottom left 
    5, 0,  // bottom right 
    5, 5,  // top right 

    // Rear face 
    0, 5,  // top left 
    0, 0,  // bottom left 
    5, 0,  // bottom right 
    5, 5,  // top right 

    // Bottom face 
    0, 5,  // top left 
    0, 0,  // bottom left 
    5, 0,  // bottom right 
    5, 5,  // top right 

    // Left face 
    5, 5,  // top left 
    0, 5,  // bottom left 
    0, 0,  // bottom right 
    5, 0,  // top right 

    // Right face 
    0, 5,  // top left 
    0, 0,  // bottom left 
    5, 0,  // bottom right 
    5, 5,  // top right 
}; 

回答

3

首先,描述八個頂點,你需要八個紋理座標。您提供的緩衝區只包含四個緩衝區,所以行爲在技術上是未定義的(並且可能取決於您的編譯器在數組之後放入內存中的內容)。

將一個常量作爲紋理名稱傳遞給glBindTexture也不是真的有效。你應該先通過任何glGenTextures給你回來。

即使這些問題得到解決,當您考慮這些問題時,如果您堅持只有八個頂點,邊緣面上的紋理座標將不會非常有用?這意味着您必須爲每個連接的面都有意義的每個頂點找到紋理座標。嘗試在紙上畫出草圖 - 我不確定這是可能的。爲了避免這種情況,您需要提供具有不同紋理座標的重複頂點位置。

編輯:例如幾何立方體,每個面都有一個完全獨特的組頂點(直接在這裏,未經測試的類型,使點):

// this is unchanged from your original 
static const GLfloat squareTextureCoords3[] = { 
    // Front face 
    0, 5,  // top left 
    0, 0,  // bottom left 
    5, 0,  // bottom right 
    5, 5,  // top right 

    // Top face 
    0, 5,  // top left 
    0, 0,  // bottom left 
    5, 0,  // bottom right 
    5, 5,  // top right 

    // Rear face 
    0, 5,  // top left 
    0, 0,  // bottom left 
    5, 0,  // bottom right 
    5, 5,  // top right 

    // Bottom face 
    0, 5,  // top left 
    0, 0,  // bottom left 
    5, 0,  // bottom right 
    5, 5,  // top right 

    // Left face 
    5, 5,  // top left 
    0, 5,  // bottom left 
    0, 0,  // bottom right 
    5, 0,  // top right 

    // Right face 
    0, 5,  // top left 
    0, 0,  // bottom left 
    5, 0,  // bottom right 
    5, 5,  // top right 
}; 

// this is changed, to match the tex coord list 
static const GLfloat cubeVertices[] = { 
    // front face 
    -5.0f, 15.0f, 5.0f, 
    5.0f, 15.0f, 5.0f, 
    5.0f, 0.0f, 5.0f, 
    -5.0f, 0.0f, 5.0f, 

    // top face 
    -5.0f, 15.0f, 5.0f, 
    5.0f, 15.0f, 5.0f, 
    5.0f, 15.0f,-5.0f, 
    -5.0f, 15.0f,-5.0f,   

    // rear face 
    -5.0f, 15.0f,-5.0f, 
    5.0f, 15.0f,-5.0f, 
    5.0f,0.0f,-5.0f, 
    -5.0f,0.0f,-5.0f 

    // bottom face 
    -5.0f, 0.0f, 5.0f, 
    5.0f, 0.0f, 5.0f, 
    5.0f, 0.0f,-5.0f, 
    -5.0f, 0.0f,-5.0f, 

    // left face 
    -5.0f, 0.0f, 5.0f, 
    -5.0f, 0.0f,-5.0f, 
    -5.0f, 15.0f,-5.0f,   
    -5.0f, 15.0f, 5.0f, 

    // right face 
    5.0f, 0.0f, 5.0f, 
    5.0f, 0.0f,-5.0f, 
    5.0f, 15.0f,-5.0f,   
    5.0f, 15.0f, 5.0f, 
}; 

// this is changed also, to match the new arrays above 
const GLubyte cubeVertexFaces[] = { 
    4, 5, 6, // Half of top face 
    4, 6, 7, // Other half of top face 
    0, 1, 2, // Half of front face 
    0, 2, 3, // Other half of front face 
    8, 9, 10, // Half of back face 
    8, 10, 11, // Other half of back face 
    20, 21, 22, // Half of right face 
    20, 22, 23, // Other half of right face 
    16, 17, 18, // Half of left face 
    16, 18, 19, // Other half of left face 
    12, 13, 14, // Half of bottom face 
    12, 14, 15, // Other half of bottom face 
}; 

其他代碼保持不變。關鍵是在OpenGL中,您不能單獨索引頂點位置和紋理座標。忽略顏色,法線等的可能性,OpenGL中頂點的描述是一個具有一個紋理座標的位置。

+0

你好,感謝您的回覆。我正在使用書籍等例子來創建我的代碼。那麼你將如何去構造一個立方體紋理。我真的很陌生,除了這個血腥的紋理之外,我得到了一切正常的工作(更多的代碼) – Burf2000 2010-11-11 08:55:05

+0

作爲第一步,總共使用24個頂點 - 每個面各有四個頂點,每個頂點都有自己的一組紋理座標。因此,請保留最近添加的squareTextureCoords3,但展開cubeVertices數組並調整cubeVertexFaces。我現在不得不從我的辦公桌衝下來,但如果問題仍然存在,我會在今晚稍後提供一個例子。 – Tommy 2010-11-11 18:18:00

0

嘗試

const GLubyte cubeVertexFaces[] = { 
... 
    1, 5, 2, // Half of right face 
    6, 2, 5, // Other half of right face 
... 
}; 
+0

沒有工作,我認爲它與紋理座標有關的問題 – Burf2000 2010-11-11 08:53:23