如何在openGL中创建一个矩形网格(三角形)?

时间:2016-07-02 11:38:38

标签: c++ opengl glfw glew

我正在使用OpenGL,GLFW和GLEW为学校项目创建水波模拟。我正在使用纹理网格,并将在顶点着色器中操作所述网格。

我尝试通过在Frank Luna的书“3D游戏编程简介”中调整GeometryGenerator类来创建网格。我创建了这个函数,但它给了我一个奇怪的斜角三角形而不是网格。

任何人都可以提出一个更好的修复或不同的功能吗?

以下是代码的相关摘要:

struct vertex
    {
        GLfloat x, y, z;
        GLfloat u, v;
    };

struct index
{
    GLuint a, b, c;
};

struct MeshData
{
    std::vector<vertex> Vertices;
    std::vector<index> Indices;
    //std::vector<GLuint> Indices;
};
void CreateGrid(float width, float depth, int m, int n, MeshData& meshData)
{
    int vertexCount = m*n;
    int faceCount   = (m-1)*(n-1)*2;
    // Create the vertices.

    float halfWidth = 0.5f*width;
    float halfDepth = 0.5f*depth;

    float dx = width / (n-1);
    float dz = depth / (m-1);

    float du = 1.0f / (n-1);
    float dv = 1.0f / (m-1);


    meshData.Vertices.resize(vertexCount);
    for(GLfloat i = 0.0f; i < m; ++i)
    {
        GLfloat z = halfDepth - i*dz;
        for(GLfloat j = 0.0f; j < n; ++j)
        {
            //code
            GLfloat x = -halfWidth + j*dx;

            meshData.Vertices[i*n+j].x = x;
            meshData.Vertices[i*n+j].y = 0.0f;
            meshData.Vertices[i*n+j].z = z;

            meshData.Vertices[i*n+j].u = j*du;
            meshData.Vertices[i*n+j].v = i*dv;

            //vertex temp = {x, 0.0f, z, j*du, i*dv};
            //meshData.Vertices.push_back(temp);
        }
    }
    //indices
    meshData.Indices.resize(faceCount);

    GLuint offset = 0;
    for(GLuint i = 0; i < m-1; ++i)
    {
        for(GLuint j = 0; j < n-1; ++j)
        {               
            //offset = x * m;
            index temp = 
            {
                i*n+j,
                i*n+j+1,
                (i+1)*n+j
            };

            index temp_2 = 
            {
                (i+1)*n+j,
                i*n+j+1,
                (i+1)*n+j+1
            };

            meshData.Indices.push_back(temp);
            meshData.Indices.push_back(temp_2);
        }
    }
}

同时将数据传递给VAO。

#pragma region "object_initialization"
    // Set the object data (buffers, vertex attributes)

    MeshData grid;

    CreateGrid(30, 20, 2.0f, 3.0f, grid);

    // Setup plane VAO
    GLuint planeVAO, planeVBO, planeEBO;
    glGenVertexArrays(1, &planeVAO);

    glGenBuffers(1, &planeVBO);
    glGenBuffers(1, &planeEBO);
    glBindVertexArray(planeVAO);

    glBindBuffer(GL_ARRAY_BUFFER, planeVBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(grid.Vertices) * grid.Vertices.size(), &grid.Vertices, GL_STATIC_DRAW);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, planeEBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(grid.Indices) * grid.Indices.size(), &grid.Indices, GL_STATIC_DRAW);


    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)0);
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
    glBindVertexArray(0);

    // Load textures
    GLuint floorTexture = loadTexture("C://path to.../brushwater.png");
    #pragma endregion

以下是请求的呈现代码:

while(!glfwWindowShouldClose(window))
    {
        // Set frame time
        GLfloat currentFrame = glfwGetTime();
        deltaTime = currentFrame - lastFrame;
        lastFrame = currentFrame;

        // Check and call events
        glfwPollEvents();
        Do_Movement();

        // Clear the colorbuffer
        glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);    

        // Draw objects
        shader.Use(); 
        glm::mat4 model;
        glm::mat4 view = camera.GetViewMatrix();
        glm::mat4 projection = glm::perspective(camera.Zoom, (float)screenWidth/(float)screenHeight, 0.1f, 100.0f);
        glUniformMatrix4fv(glGetUniformLocation(shader.Program, "view"), 1, GL_FALSE, glm::value_ptr(view));
        glUniformMatrix4fv(glGetUniformLocation(shader.Program, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
        // Floor
        glBindVertexArray(planeVAO);
        glBindTexture(GL_TEXTURE_2D, floorTexture);
        model = glm::mat4();
        glUniformMatrix4fv(glGetUniformLocation(shader.Program, "model"), 1, GL_FALSE, glm::value_ptr(model));
        glDrawArrays(GL_TRIANGLES, 0, 6);
        glBindVertexArray(0);               


        // Swap the buffers
        glfwSwapBuffers(window);
    }

着色器:

顶点着色器

#version 430 core
layout (location = 0) in vec3 position;
layout (location = 1) in vec2 texCoords;

out vec2 TexCoords;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

void main()
{
    gl_Position = projection * view * model * vec4(position, 1.0f);
    TexCoords = texCoords;
}

片段着色器

#version 430 core
in vec2 TexCoords;

out vec4 color;

uniform sampler2D texture1;

void main()
{             
    color = texture(texture1, TexCoords);
}

我的输出是纹理斜角三角形或只是空白背景。 非常感谢您阅读它,感谢您的帮助!

0 个答案:

没有答案