簡體   English   中英

紋理 opengl 立方體

[英]Texturing an opengl cube

我有一個 opengl 立方體,我想對所有 6 個面進行紋理處理。

我需要多個紋理嗎?

這是當前多維數據集的屏幕截圖:

圖片

基本上我不知道如何將紋理包裹在整個立方體周圍......

這是我的 cube.h header 文件,其中定義了 IBO 和 VBO

#pragma once
#include <GL\glew.h>

class cube {
public:
    cube() {
        x = 0;
        y = 0;
        z = 0;
        width = 0;
        vertices = 0;
        indices = 0;
    }
    cube(GLfloat X, GLfloat Y, GLfloat Z, float w) {
        x = X;
        y = Y;
        z = Z;
        width = w;

        vertices = new GLfloat[40];

        //1
        vertices[0] = x;     //x pos
        vertices[1] = y;     //y pos
        vertices[2] = z;     //z pos

        vertices[3] = 0;     //x pos in texture
        vertices[4] = 1;     //y pos in texture
        //2
        vertices[5] = x + width;
        vertices[6] = y;
        vertices[7] = z;

        vertices[8] = 1;
        vertices[9] = 1;
        //3
        vertices[10] = x;
        vertices[11] = y - width;
        vertices[12] = z;

        vertices[13] = 0;
        vertices[14] = 0;
        //4
        vertices[15] = x + width;
        vertices[16] = y - width;
        vertices[17] = z;

        vertices[18] = 1;
        vertices[19] = 0;
        //5
        vertices[20] = x;
        vertices[21] = y;
        vertices[22] = z - width;

        vertices[23] = 0;
        vertices[24] = 1;
        //6
        vertices[25] = x + width;
        vertices[26] = y;
        vertices[27] = z - width;

        vertices[28] = 1;
        vertices[29] = 1;
        //7
        vertices[30] = x;
        vertices[31] = y - width;
        vertices[32] = z - width;

        vertices[33] = 0;
        vertices[34] = 0;
        //8
        vertices[35] = x + width;
        vertices[36] = y - width;
        vertices[37] = z - width;

        vertices[38] = 1;
        vertices[39] = 0;

        //indices
        indices = new unsigned int[36];
        //0
        indices[0] = 0;
        indices[1] = 1;
        indices[2] = 2;
        //1
        indices[3] = 1;
        indices[4] = 2;
        indices[5] = 3;
        //2
        indices[6] = 4;
        indices[7] = 5;
        indices[8] = 6;
        //3
        indices[9] = 5;
        indices[10] = 6;
        indices[11] = 7;
        //4
        indices[12] = 4;
        indices[13] = 0;
        indices[14] = 1;
        //5
        indices[15] = 4;
        indices[16] = 5;
        indices[17] = 1;
        //6
        indices[18] = 6;
        indices[19] = 2;
        indices[20] = 3;
        //7
        indices[21] = 6;
        indices[22] = 7;
        indices[23] = 3;
        //8
        indices[24] = 1;
        indices[25] = 5;
        indices[26] = 3;
        //9
        indices[27] = 5;
        indices[28] = 7;
        indices[29] = 3;
        //10
        indices[30] = 4;
        indices[31] = 0;
        indices[32] = 2;
        //11
        indices[33] = 4;
        indices[34] = 6;
        indices[35] = 2;
    }

    GLfloat* vertices;
    unsigned int* indices;
private:
    GLfloat x;
    GLfloat y;
    GLfloat z;
    float width;
};

所有這些代碼所做的就是為一個立方體 object 設置一個簡單的 VBO 和 IBO/EBO 以供稍后使用。

問題是每個立方體頂點在 3 個面之間共享,其中它的紋理坐標可能不相同。 因此,您要么復制這些頂點(每個頂點具有不同的紋理坐標),要么使用 2 個單獨的索引(一個用於頂點,一個用於紋理)。

復制可能看起來像這樣(使用GL_QUADS原語):

double cube[]=
    {
    // x,   y,   z,  s,  t,
    +1.0,-1.0,-1.0,0.0,1.0,
    -1.0,-1.0,-1.0,1.0,1.0,
    -1.0,+1.0,-1.0,1.0,0.0,
    +1.0,+1.0,-1.0,0.0,0.0,

    -1.0,+1.0,-1.0,0.0,0.0,
    -1.0,-1.0,-1.0,0.0,1.0,
    -1.0,-1.0,+1.0,1.0,1.0,
    -1.0,+1.0,+1.0,1.0,0.0,

    -1.0,-1.0,+1.0,0.0,1.0,
    +1.0,-1.0,+1.0,1.0,1.0,
    +1.0,+1.0,+1.0,1.0,0.0,
    -1.0,+1.0,+1.0,0.0,0.0,

    +1.0,-1.0,-1.0,1.0,1.0,
    +1.0,+1.0,-1.0,1.0,0.0,
    +1.0,+1.0,+1.0,0.0,0.0,
    +1.0,-1.0,+1.0,0.0,1.0,

    +1.0,+1.0,-1.0,0.0,1.0,
    -1.0,+1.0,-1.0,1.0,1.0,
    -1.0,+1.0,+1.0,1.0,0.0,
    +1.0,+1.0,+1.0,0.0,0.0,

    +1.0,-1.0,+1.0,0.0,0.0,
    -1.0,-1.0,+1.0,1.0,0.0,
    -1.0,-1.0,-1.0,1.0,1.0,
    +1.0,-1.0,-1.0,0.0,1.0,
    };

有了這個紋理:

質地

這(對於舊的 GL api 很抱歉,但它更容易測試):

int i,n=sizeof(cube)/(sizeof(cube[0]));
glColor3f(1.0,1.0,1.0);
scr.txrs.bind(txr);
glBegin(GL_QUADS);
for (i=0;i<n;i+=5)
    {
    glTexCoord2dv(cube+i+3);
    glVertex3dv(cube+i+0);
    }
glEnd();
scr.txrs.unbind();

我得到了這個結果:

預習

暫無
暫無

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

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