Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 6 of 6

Thread: How create Cube with subdivisions

  1. #1
    Junior Member Newbie
    Join Date
    Mar 2014
    Posts
    10

    How create Cube with subdivisions

    Hello,

    I tryed to find code examples to create cube with subdivisions for each side but with no luck. I change my code so ATM I can do that but it is like create small cube with duplicating which is not good at all. Does anyone have code example to create cube with this input: width, height, length, width segments, height segments, length segments ?

    Thank you.

  2. #2
    Junior Member Newbie
    Join Date
    Mar 2014
    Posts
    10
    I spend hours on Google and looks like code for create Cube with XYZ segments doesn`t exist yet!

  3. #3
    Intern Contributor Brokenmind's Avatar
    Join Date
    Feb 2014
    Location
    Aachen / Germany
    Posts
    71
    You should be able to figure out the basic theory behind it yourself!

    I hope I interpreted your task right because otherwise, the stuff below was for nothing.

    Code untested (I only made sure it doesn't crash), but you should be able to understand what was done and adapt it accordingly if it doesn't work.

    Code :
     
    void drawFace(float x, float y, float z, float normalX, float normalY, float normalZ,
    	float dir1X, float dir1Y, float dir1Z, int dir1num,
    	float dir2X, float dir2Y, float dir2Z, int dir2num)	 
    {
    	// set normal for all to-come vertices first
    	glNormal3f(normalX, normalY, normalZ);
     
    	// increment because of more coordinates
    	dir1num++;
    	dir2num++;
     
    	// allocate space for raster
    	float *vertices = (float*)malloc(dir1num*dir2num*3*sizeof(float));
     
    	// fill raster according to directions specified in header
    	for(int i = 0; i < dir1num; i++)
    	{
    		for(int j = 0; j < dir2num; j++)
    		{
    			vertices[3*(dir2num*i+j)+0] = x + i * dir1X + j * dir2X;
    			vertices[3*(dir2num*i+j)+1] = y + i * dir1Y + j * dir2Y;
    			vertices[3*(dir2num*i+j)+2] = z + i * dir1Z + j * dir2Z;
    		}
    	}
     
    	// draw triangles according to raster
    	for(int i = 0; i < dir1num - 1; i++)
    	{
    		for(int j = 0; j < dir2num - 1; j++)
    		{
    			glVertex3fv(vertices+3*(dir2num*i+j));
    			glVertex3fv(vertices+3*(dir2num*i+j+1));
    			glVertex3fv(vertices+3*((dir2num*i+1)+j+1));
    			glVertex3fv(vertices+3*(dir2num*i+j));
    			glVertex3fv(vertices+3*((dir2num*i+1)+j+1));
    			glVertex3fv(vertices+3*((dir2num*i+1)+j));
    		}
    	}
     
    	// don't forget to free allocated space
    	free(vertices);
    }
     
    void drawCubeSubdivided(float x, float y, float z, 
    	float width, float height, float length, 
    	int widthSegments, int heightSegments, int lengthSegments)
    {
    	glBegin(GL_TRIANGLES);
     
    	// bottom
    	drawFace(x, y, z,
    		0, -1, 0,
    		0, 0, length / lengthSegments, lengthSegments,
    		width / widthSegments, 0, 0, widthSegments
    		);
     
    	// top	
    	drawFace(x, y + height, z,
    		0, 1, 0,
    		width / widthSegments, 0, 0, widthSegments,
    		0, 0, length / lengthSegments, lengthSegments
    		);
     
    	// front
    	drawFace(x, y, z,
    		-1, 0, 0,
    		0, height / heightSegments, 0, heightSegments,
    		0, 0, length / lengthSegments, lengthSegments
    		);
     
    	// back	
    	drawFace(x + width, y, z,
    		1, 0, 0,
    		0, 0, length / lengthSegments, lengthSegments,
    		0, height / heightSegments, 0, heightSegments
    		);
     
    	// left
    	drawFace(x, y, z,
    		0, 0, -1,
    		width / widthSegments, 0, 0, widthSegments,
    		0, height / heightSegments, 0, heightSegments
    		);
     
    	// right
    	drawFace(x, y, z + length,
    		0, 0, 1,
    		0, height / heightSegments, 0, heightSegments,
    		width / widthSegments, 0, 0, widthSegments
    		);
     
    	glEnd();
    }
     
    void runCube()
    {
    	drawCubeSubdivided(1, 1, 1, 1, 1, 1, 100, 100, 100);
    }

  4. #4
    Junior Member Newbie
    Join Date
    Mar 2014
    Posts
    10
    Thank you Brokenmind for code. I tryed to create code myself but it was just copy of one single plane into xxx planes to fit a bigger cube. It works but i think it was not best because it doubles vertexs.
    Thank you.

  5. #5
    Junior Member Newbie
    Join Date
    Mar 2014
    Posts
    10
    Can someone verify Brokenmind`s code for segmented cube? I retyped it to delphi but cube have missing faces and some have wrong vertices.

    Thank you!

    Click image for larger version. 

Name:	cube.png 
Views:	18 
Size:	2.5 KB 
ID:	1241

    This code I use:
    Code :
      procedure DrawFace(const Mesh : TMeshObject;
                         const x, y, z : single;
                         const normalX, normalY, normalZ : single;
                         const dir1X, dir1Y, dir1Z : single; dir1num : integer;
                         const dir2X, dir2Y, dir2Z : single; dir2num : integer);
      var
        FGR : TFGVertexNormalTexIndexList;
        VertexOffset  : Integer;
        NormalOffset  : Integer;
        i, j : integer;
      begin
        VertexOffset := Mesh.Vertices.Count;
        NormalOffset := Mesh.Normals.add(AffineVectorMake(normalX, normalY, normalZ));
     
        For i := 0 to dir1num do
          For j := 0 to dir2num do
          begin
            Mesh.Vertices.Add(AffineVectorMake(x + (i * dir1X) + (j * dir2X),
                                               y + (i * dir1Y) + (j * dir2Y),
                                               z + (i * dir1Z) + (j * dir2Z)));
        end;
     
        FGR := TFGVertexNormalTexIndexList.CreateOwned(Mesh.FaceGroups);
        FGR.Mode := fgmmTriangles;
     
        For i := 0 to dir1num + 1 do
          For j := 0 to dir2num + 1 do
          begin
            FGR.VertexIndices.Add(VertexOffset + (i + j),
                                  VertexOffset + (i + j + 1),
                                  VertexOffset + (i + 1 + j + 1));
     
            FGR.NormalIndices.Add(NormalOffset,NormalOffset,NormalOffset);
          end;
      end;
    Last edited by Spambox; 03-19-2014 at 07:23 AM.

  6. #6
    Junior Member Newbie
    Join Date
    Mar 2014
    Posts
    10
    Hello,

    I try to build one side of cube from vertices but still have problem to build Indices correctly. Can someone help me how build it? I tryed lots of ways but I do not know if Brokenmind code is correct because I got this:

    On the picture you can see example of one side of cube (each side have 3 vertices). I addes them to the mesh. Number inside circle is position of vertices inside mesh. How should I build indices?

    Thank you.

    EDIT: GOT IT! This is a correct Delphi code:
    Code :
        For i := 0 to dir1num - 1 do
          For j := 0 to dir2num - 1 do
          begin
            FGR.VertexIndices.Add(VertexOffset + (i*dir2num) + j + i,
                                  VertexOffset + (i*dir2num) + j + i + 1,
                                  VertexOffset + ((i+1)*dir2num) + j + i + 1);
     
            FGR.VertexIndices.Add(VertexOffset + ((i+1)*dir2num) + j + i + 1,
                                  VertexOffset + (i*dir2num) + j + i + 1,
                                  VertexOffset + ((i+1)*dir2num) + j + i + 2);
     
            FGR.NormalIndices.Add(NormalOffset,NormalOffset,NormalOffset);
            FGR.NormalIndices.Add(NormalOffset,NormalOffset,NormalOffset);
          end;


    Click image for larger version. 

Name:	vertex_pozice.png 
Views:	16 
Size:	4.2 KB 
ID:	1242
    Last edited by Spambox; 03-20-2014 at 05:46 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
  •