0

OpenGL を使用して平面を作成しようとしていますが、コードはある程度機能しますが、一部のインデックスで奇妙な結果が得られます。私のコードをできる限り説明しようと思います。これは、私のコードとオンラインで見つけたものが混在しています。

メイン:

すべてのセットアップはメインで行われるため、関数は必要なすべての値を認識します。

float zoom = 6.0f;
float vertical = 1.2f;
float horizontal = 1.2f;

const int planeWidth = 4;  //columns
const int planeHeight = 2; //rows

const int totalVertices = (planeWidth + 1) * (planeHeight + 1);

//GLfloat* vertices = new GLfloat[totalVertices];
GLfloat vertices[totalVertices] = { 0.0 };

const int indPerRow = planeWidth * 2 + 2;
const int indDegenReq = (planeHeight - 1) * 2;
const int totalIndices = indPerRow * planeWidth + indDegenReq;

//GLuint* indices = new GLuint[totalIndices];
GLuint indices[totalIndices] = { 0 };

GLfloat texCoords[totalVertices] = { 0 };

makePlane(planeWidth, planeHeight, vertices, indices, texCoords);

関数:

最初の for ループは頂点を作成し、2 番目はインデックスを作成します

void makePlane(int width, int height, GLfloat *vertices, GLuint *indices)
{
width++;  //columns
height++; //rows

int size = sizeof(GLfloat);
for (int y = 0; y < height; y++)
{
    int base = y * width;
    for (int x = 0; x < width; x++)
    {
        int index = (base + x) * 2;
        vertices[index]    = (float)x;
        vertices[index +1] = (float)y;
    }
}

int i = 0;
height--;

for (int y = 0; y < height; y++)
{
    int base = y * width;

    for (int x = 0; x < width; x++)
    {
        indices[i++] = base + x;
        indices[i++] = base + width + x; 
    }
    if (y < height - 1)
    {
        indices[i++] = ((y + 1) * width + (width - 1));
        indices[i++] = ((y + 1) * width);
    }   
}
}

結果:

4×2

インデックス 0、5、1、6、2、7、3、8、4、9、9、5、5、10、6、11、7、12、8、13、9、14、0、0、0 , 0, 0, 0, ...} unsigned int[42]

22 個の値を正しく処理し、残りはゼロです。

理由はありますか?

4

2 に答える 2

0

ループは次のようにする必要があると思います(テストされていないことに注意してください:)。基本的には、四角形をループし、三角形のペアのインデックスを出力すると考えてください。

for (int y = 0; y < height; y++)
{
    unsigned int base = y * width;
    unsigned int top = base + width;
    for (int x = 0; x < (width-1); x++)
    {
        indices[i++] = base + x;
        indices[i++] = top + x; 
        indices[i++] = top + x + 1; 

        indices[i++] = top + x + 1;
        indices[i++] = base + x + 1; 
        indices[i++] = base + x; 
    }
}
于 2014-08-09T15:26:11.890 に答える
0

頂点は下から上に作成され、インデックスは上から下に三角形を作成するように順序付けられていることに気付きました。そこで、ループの頂点作成を変更しました。

int index = 0;
for (int y = height; y >= 0; y--)
{

    for (int x = 0; x <= width; x++)
    {
        vertices[index] = ((float)x/4)-0.5;
        vertices[index +1] = ((float)y/4)-0.5;
        vertices[index + 2] = 0.0f;
        index += 3;
    }
}

左上から右下に頂点を作成します。インデックスのループは同じままでした:

int i = 0;
++width;
GLuint indices2[170] = { 0 };

for (int y = 0; y < height; y++)
{
    int base = y * width;

    for (int x = 0; x < width; x++)
    {
        indices[i++] = base + x;
        indices[i++] = base + width + x;
    }
    if (y < height - 1)
    {
        indices[i++] = ((y + 1) * width + (width - 1));
        indices[i++] = ((y + 1) * width);
    }
}
于 2014-08-15T08:49:21.377 に答える