PDA

View Full Version : How create Cube with subdivisions

Spambox
03-15-2014, 10:12 AM
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.

Spambox
03-16-2014, 01:11 AM
I spend hours on Google and looks like code for create Cube with XYZ segments doesn`t exist yet! :(

Brokenmind
03-16-2014, 12:59 PM
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.

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

Spambox
03-16-2014, 08:33 PM
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.

Spambox
03-19-2014, 06:36 AM
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!

1241

This code I use:

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;

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.FaceG roups);
FGR.Mode := fgmmTriangles;

For i := 0 to dir1num + 1 do
For j := 0 to dir2num + 1 do
begin
VertexOffset + (i + j + 1),
VertexOffset + (i + 1 + j + 1));

end;
end;

Spambox
03-20-2014, 03:12 AM
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:

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);