0

3d grid it is cube consists from a lot of poligons Something similar to this enter image description here

Now I have a 2d grid enter image description here Code for 2d grid

 int slices = 10;
    for(int j=0; j<=slices; ++j) 
    {
        for(int i=0; i<=slices; ++i) 
        {
            float x = (float)i/(float)slices;
            float y = 0;
            float z = (float)j/(float)slices;
            vertices.push_back(QVector3D(x, y, z));
        }
    }

    for(int j=0; j<slices; ++j) 
    {
        for(int i=0; i<slices; ++i) 
        {

            int row1 =  j * (slices+1);
            int row2 = (j+1) * (slices+1);
            indices.push_back(row1+i); indices.push_back(row1+i+1); indices.push_back(row1+i+1); indices.push_back(row2+i+1);
            indices.push_back(row2+i+1); indices.push_back(row2+i); indices.push_back(row2+i); indices.push_back(row1+i);
        }
    }

Red lines it is what I need to draw enter image description here

  • What primitive mode do you use, `GL_QUADS`? – Alexey S. Larionov Mar 29 '22 at 08:44
  • @AlexeyLarionov For 2d grid? Gl Lines, but i need to use GLTriangles, because this 3d grid will be filled – Андрей Петров Mar 29 '22 at 08:46
  • You can use GL_TRIANGLE_STRIP, and with [this answer](https://softwareengineering.stackexchange.com/a/399624) you'll generate a 2D grid filled with triangles. You can always render the triangles as lines if you switch the [polygon mode](https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glPolygonMode.xhtml)(no need to change VBO at all). And to make a cube. Well you could simply render 6 2D grids. Or you could pass in a shader a rotation matrix, to render the same 2D grid with 6 rotations (thus making a cube) – Alexey S. Larionov Mar 29 '22 at 08:54
  • @AlexeyLarionov It is should looks like a huge cube consist from many small cubes, I have cube right now, but if I draw a lot of cubes it is a lot of request to videocard – Андрей Петров Mar 29 '22 at 08:55
  • @AlexeyLarionov This 6 grids will not to connected to each other, there is no information about connection in y axes – Андрей Петров Mar 29 '22 at 08:57
  • Well in your pictures you didn't mention you want to draw the cube's internals too. In that case you can generate a single VBO with 10x10x10 evenly spaced vertices, and then connect them. I assume it's possible with either GL_QUAD_STRIP or GL_TRIANGLE_STRIP, which would be more efficient, but GL_TRIANGLES should be ok too unless your cube is crazy huge (e.g. cube 100x100x100 would result in 12 million triangles, which is a lot). I not sure if you want to optimize it though, it can become very complex (e.g. in Minecraft unseen vertices just don't exist, and are only as you mine deeper) – Alexey S. Larionov Mar 29 '22 at 09:14
  • So, my question is, how to do it? Just, every vertex should have his own color later, I need to save all vertices in one buffer? Can I do it with ebo – Андрей Петров Mar 29 '22 at 11:15
  • "I need to save all vertices in one buffer" - Yes, unless you really want a big cube, then some clever manual optimizations would be necessary. About colors, you could store all colors in a texture array, and in shader calculate the vertex coordinates in this texture, and paint it with with the corresponding color. EBO is something you inevitably need to use, because you want to connect the vertices in triangles. You just put 3 indices of some triangle in it. Put 12*3 indices to make a cube, to make adjacent cubes you can skip some sides, because they're already present in previous cubes – Alexey S. Larionov Mar 29 '22 at 11:22
  • why not do a shader that will convert single QUAD covering your screen into grid with desired shading/coloring/filling ... ? also see [How to draw dynamic 2D grid that adjusts according to camera zoom: OpenGL](https://stackoverflow.com/a/62444198/2521214) – Spektre Mar 29 '22 at 17:30

0 Answers0