# OpenGL How to draw cuboids by defining offsets correctly?

This topic is 1067 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Dear community,

I am currently facing the following problem:

* I want to draw multiple cuboids representing items to be packed in a container and one cuboid representing the container. I have an algorithm that packs the container items and outputs the boxes with their dimensions (and x,y,z offsets)

I have the following input data for example:

* Container dimensions (Width 10, Length 10, Height 10)
* Box1: [Width=2, Length=2, Height=2] Position=[X=5, Y=4, Z=0]]
* Box2: [Width=4, Length=4, Height=4] Position=[X=5, Y=0, Z=0]]
* Box3: [Width=5, Length=5, Height=5] Position=[X=0, Y=0, Z=0]]

The solution should be something like this:

* Use OpenGL BeginMode Quads to draw the cuboids.
* Start at X=0, Y=0, Z=0 and draw the container cuboid with its width, height and length.
* Start at X=BoxPositionX, Y=BoxPositionY, Z=BoxPositionZ to draw each of the boxes cuboids.

My problems are:

* How can I tell OpenGL that if I use my drawCuboid function for example with Width=10,Height=10 and Length=10 that it should start with X=0,Y=0,Z=0 and draw to X=Width,Y=Height,Z=Length and not go into the -X,-Y and -Z coordinates?
* How can I apply that X-Shift,Y-Shift and Z-Shift correctly?

To draw my cuboids I use the following code (attention: I use the OpenTK OpenGL C# Wrapper so please dont try to compile this with OpenGL libraries):

...
// Use "Wireframe mode"
GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill);

// Precalc
length = (float)length / (float)650f;
height = (float)height / (float)650f;
width = (float)width / (float)650f;

//Side 1
GL.Vertex3(width, height, -length);
GL.Vertex3(-width, height, -length);
GL.Vertex3(-width, height, length);
GL.Vertex3(width, height, length);
GL.End();

// Side 2
GL.Vertex3(width, -height, length);
GL.Vertex3(-width, -height, length);
GL.Vertex3(-width, -height, -length);
GL.Vertex3(width, -height, -length);

// Side 3

GL.Vertex3(width, height, length);
GL.Vertex3(-width, height, length);
GL.Vertex3(-width, -height, length);
GL.Vertex3(width, -height, length);

// Side 4

GL.Vertex3(width, -height, -length);
GL.Vertex3(-width, -height, -length);
GL.Vertex3(-width, height, -length);
GL.Vertex3(width, height, -length);

// Side 5

GL.Vertex3(-width, height, length);
GL.Vertex3(-width, height, -length);
GL.Vertex3(-width, -height, -length);
GL.Vertex3(-width, -height, length);

// Side 6

GL.Vertex3(width, height, -length);
GL.Vertex3(width, height, length);
GL.Vertex3(width, -height, length);
GL.Vertex3(width, -height, -length);
...

Any help would be greatly appreciated!

1. 1
2. 2
Rutin
20
3. 3
khawk
17
4. 4
A4L
14
5. 5

• 12
• 16
• 26
• 10
• 11
• ### Forum Statistics

• Total Topics
633756
• Total Posts
3013710
×