Jump to content
  • Advertisement
Sign in to follow this  
gfl0w92

OpenGL How to draw cuboids by defining offsets correctly?

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

If you intended to correct an error in the post then please contact us.

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!

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!