0

这篇文章中了解到,我使用该函数绘制了一个立方体,glDrawElements而不是使用该函数绘制由大量顶点列表中指定的三角形组成的立方体glDrawArrays(GL_TRIANGLES, 0, 6)

我使用该函数成功地对两个三角形进行纹理glDrawArrays化以创建一个带纹理的正方形,但是当我在我的新glDrawElements方法上尝试相同的方法时,我似乎无法让它工作......

这就是我现在绘制立方体的方式(包括我在纹理方面的尝试):

void SkyBox::constructGeometry(Shader* myShader)
{
    verts[0] = -dimX;   verts[ 1] = -dimY;  verts[ 2] = -dimZ; //0
    verts[3] = -dimX;   verts[ 4] =  dimY;  verts[ 5] = -dimZ; //1
    verts[6] =  dimX;   verts[ 7] =  dimY;  verts[ 8] = -dimZ; //2
    verts[9] =  dimX;   verts[10] = -dimY;  verts[11] = -dimZ; //3

    verts[12] = -dimX;   verts[13] = -dimY;  verts[14] = dimZ; //4
    verts[15] = -dimX;   verts[16] =  dimY;  verts[17] = dimZ; //5
    verts[18] =  dimX;   verts[19] =  dimY;  verts[20] = dimZ; //6
    verts[21] =  dimX;   verts[22] = -dimY;  verts[23] = dimZ; //7

    cols[0] = 0.0;   cols[ 1] = 0.0;  cols[ 2] = 0.0;
    cols[3] = 0.0;   cols[ 4] = 1.0;  cols[ 5] = 0.0;
    cols[6] = 0.0;   cols[ 7] = 0.0;  cols[ 8] = 1.0;
    cols[9] = 1.0;   cols[10] = 1.0;  cols[11] = 1.0;

    cols[12] = 1.0;   cols[13] = 0.0;  cols[14] = 0.0;
    cols[15] = 0.0;   cols[16] = 1.0;  cols[17] = 0.0;
    cols[18] = 0.0;   cols[19] = 0.0;  cols[20] = 1.0;
    cols[21] = 1.0;   cols[22] = 1.0;  cols[23] = 0.0;

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

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

    glGenVertexArrays(2, &m_vaoID[0]);
    glBindVertexArray(m_vaoID[0]);
    glGenBuffers(3, m_vboID);

    GLint vertexLocation= glGetAttribLocation(myShader->handle(), "in_Position");
    GLint colorLocation= glGetAttribLocation(myShader->handle(), "in_Color");
    GLint texCoordLocation = glGetAttribLocation(myShader->handle(), "in_TexCoord");
    glUniform1i(glGetUniformLocation(myShader->handle(), "DiffuseMap"), 0);

    glBindBuffer(GL_ARRAY_BUFFER, m_vboID[0]);
    glBufferData(GL_ARRAY_BUFFER, numOfVerts2 *3*sizeof(GLfloat), verts, GL_STATIC_DRAW);
    glVertexAttribPointer(vertexLocation, 3, GL_FLOAT, GL_FALSE, 0, 0); 
    glEnableVertexAttribArray(vertexLocation);

    glBindBuffer(GL_ARRAY_BUFFER, m_vboID[1]);
    glBufferData(GL_ARRAY_BUFFER, numOfVerts2 *3*sizeof(GLfloat), cols, GL_STATIC_DRAW);
    glVertexAttribPointer(colorLocation, 3, GL_FLOAT, GL_FALSE, 0, 0); 
    glEnableVertexAttribArray(colorLocation);

    glBindBuffer(GL_ARRAY_BUFFER, m_vboID[2]);
    glBufferData(GL_ARRAY_BUFFER, numOfTexs *sizeof(GLfloat), tex, GL_STATIC_DRAW);
    glVertexAttribPointer(texCoordLocation, 2, GL_FLOAT, GL_FALSE, 0,0);
    glEnableVertexAttribArray(texCoordLocation);

    glBindBuffer(GL_ARRAY_BUFFER, 0);

    glGenBuffers(1, &ibo);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, numOfTris2 * 3 * sizeof(unsigned int), tris, GL_STATIC_DRAW);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

    glEnableVertexAttribArray(0);

    glBindVertexArray(0);
}

以及我如何渲染它:

void SkyBox::render(Shader* myShader)           //shader expected
{
    glUseProgram(myShader->handle());       //shader
    glBindTexture(GL_TEXTURE_2D, texName);      //blending needed to use alpha channel
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    //draw objects
    glBindVertexArray(m_vaoID[0]);              // select VAO
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);     //element arrays to draw indices from an array
    glDrawElements(GL_TRIANGLES, numOfTris2 *3, GL_UNSIGNED_INT, 0);

    glDisable(GL_BLEND);
    glUseProgram(0);
    glBindVertexArray(0);

}

我整个晚上都在盯着它看,所以我可能错过了一些非常愚蠢的东西,但我就是看不出我哪里出错了......

4

0 回答 0