Results 1 to 2 of 2

Thread: How to make a rectangular grid of triangles?

  1. #1
    Newbie Newbie
    Join Date
    Jul 2016
    Posts
    1

    How to make a rectangular grid of triangles?

    I'm using OpenGL, GLFW and GLEW to create a simulation of water waves for a school project. I'm using a textured grid and will manipulate said grid in the vertex shader.

    I tried to create the grid by adapting the GeometryGenerator class in Frank Luna's book "Introduction to 3D Game Programming". I created the function but it gives me an odd scalene triangle instead of the grid.
    I have attempted multiple of my own fixes and forums to no success.

    Can anyone suggest a fix or a different function which would work better?

    Here are the relevant snippets of code:

    Code :
    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);
            }
        }
    }

    Also passing the data to the VAO.

    Code :
    #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(vertex) * grid.Vertices.size(), &grid.Vertices, GL_STATIC_DRAW);
     
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, planeEBO);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(index) * grid.Indices.size(), &grid.Indices, GL_STATIC_DRAW);
     
     
        glEnableVertexAttribArray(0);
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), (GLvoid*)0);
        glEnableVertexAttribArray(1);
        glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
        glBindVertexArray(0);
     
        // Load textures
        GLuint floorTexture = loadTexture("C://path to.../brushwater.png");
        #pragma endregion

    Here is the rendering code:

    Code :
    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, 3*grid.Indices.size());
            glBindVertexArray(0);               
     
     
            // Swap the buffers
            glfwSwapBuffers(window);
        }

    My outputs now are textured scalene triangles.
    Thanks in advanced for reading it though, and thanks for your help!

  2. #2
    Senior Member Regular Contributor
    Join Date
    May 2016
    Posts
    477
    there is NO WAY to render lines of a grid if you use:
    glDrawArrays(GL_TRIANGLES, ...);
    because GL_TRIANGLES ONLY generates triangles, if you pass only 2 points, it will rener nothing

    you have to use GL_LINES (or GL_LINE_LOOP) as rendering mode in the draw call if you want to render lines


    ps: shouldnt it be
    Code :
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertex) * grid.Vertices.size(), &grid.Vertices.data(), GL_STATIC_DRAW);
    rather than
    Code :
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertex) * grid.Vertices.size(), &grid.Vertices, GL_STATIC_DRAW);
    ?

Similar Threads

  1. Help Coverting Glew Grid to Glad Grid.
    By Josheir in forum OpenGL: Basic Coding
    Replies: 10
    Last Post: 02-03-2019, 09:45 AM
  2. Trying to make a grid of Squares
    By avpai1992 in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 10-19-2015, 12:52 AM
  3. How to make 3ds max looking-like grid?
    By iceworld in forum OpenGL: Basic Coding
    Replies: 6
    Last Post: 09-09-2009, 04:24 AM
  4. non rectangular scissor, how ??
    By haust in forum OpenGL: Advanced Coding
    Replies: 2
    Last Post: 03-01-2002, 08:14 AM
  5. How do I make texture which is not a rectangular
    By songeb in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 02-19-2002, 08:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean