rotating a user-defined shape

Hello,

This is my first attempt at using OpenGL.

Is it possible to define a shape by cross-section, and then rotate that, in the same way I have seen simple cubes rotated?

In short: Yes. Can you elaborate? It may be up to you to code a function to turn a cross-section into a “shape”, but then you certainly can glRotate() it.

There will be only a few standard shapes that I would require, and these are based on standard structural shapes produced by steel mills.

I had wanted to post a bitmap image of a sample, but I don’t see how to do that on this board.

These cross-sections are pretty basic and would resemble an ‘H’, ‘I’, ‘T’, ‘0’, etc.

Also, in re-reading my original post, I see that I didn’t make it clear that I would require a 3D image to be generated from this “cross-section”.

Is this also possible?

your post still eludes me.
you have an image (2D representation) of the cross section and you want to extrapolate a 3D model representation of this cross-section?

there is a bit to be said if that’s what your goal is, so I’ll hold off on that.

but, as endash said, after you’ve got a ‘shape’ defined by vertices, rotating it is as easy as glRotatef().

Originally posted by Aeluned:
your post still eludes me.
you have an image (2D representation) of the cross section and you want to extrapolate a 3D model representation of this cross-section?

Yes.

I was wanting to somehow mathematically define this “cross-section”, have the “length” as a variable that can be adjusted at run-time, and have OpenGL do the rest.

The image below is the closest I could find to what I want to do.

The light green member is a “W” section that I would like to be able to generate and rotate.

http://www.nyacad.com/SS07TypConnWallBearing.htm

That looks like an “I” beam to me :stuck_out_tongue:

Regardless…unfortunately as far as I know your task is not a trivial one. What you want to do is in the domain of computer vision and image processing. Techniques such as edge detection would be needed to extract the meaningful data in the image. The image you linked above is exceptionally tough because it contains perspective distortions (it’s seen at an angle) and so determining what’s going on is that much more difficult for a computer.

If all images were shown face on with a known color for the background it would simplify things a bit. You could more readily determine the type of cross section and at that point you can just assign a depth value to the object.

If at all possible, a data source specifying dimensions for the cross sections and the cross section type “I”, “U”, etc should be used. These are much more meaningful in building a computer model and then the task would be trivial. Can this be made available to you?

Originally posted by Aeluned:
That looks like an “I” beam to me :stuck_out_tongue:

Yes I agree :smiley:
“I” beams have narrower flanges however, and although they used to be referred to as such, they are now called “S” beams - go figure :rolleyes:
The posted image is in fact a “W” beam for Wide Flange.

The image I linked is only to show what I was tring to generate; a simple 3D of the beam based on the cross-section which I was hoping to define “mathematically”. I wasn’t planning on using bitmaps, if I didn’t have to.

If at all possible, a data source specifying dimensions for the cross sections and the cross section type “I”, “U”, etc should be used. These are much more meaningful in building a computer model and then the task would be trivial. Can this be made available to you?
I in fact have tables that define the dimensions of every steel section produced.

These give, for the “W” shape for example, the width and thickness of the flange (horizontal component) and the height and thickness of the web (vertical component).

I was trying to start off very simplistic, and just see if I could generate just one 3D image (with arbitrary dimensions) and rotate that by moving the mouse.

Trouble is I don’t know where to start, as all examples I find (and understand), use basic shapes(ie cubes, pyramids, etc.)

A push in the right direction, as to how to start, would be greatly appreciated.

Ah, I see. Good.

The first thing to do is start with one of those examples you saw with a cube. You could grab this code from NeHe that creates an OpenGL window and sets up a perspective projection (setting up an OpenGL window is tedious)
http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=05.

Next, you need to come up with a mapping from the beam dimensions (specified in cm,inches,feet,or whatever the units may be) in real-world measurements to your worldspace coordinate system measurements. This part will involve some trial and error and will probably cause you some grief. For example, try adjusting the vertices of the cube in the application given some real world dimensions to turn it into the ‘flange’ part of the beam.

In its simplest form, you’ll notice that the “W” beam you showed me before is made up of basically 3 cuboids, so it’s a matter of drawing these 3 cuboids. In actuality though, the flange doesn’t meet the web at a 90 degree angle, it curves…let’s hold off on that because it makes things a bit more complicated.

as far as rotations are concerned:
there are many ways to do it. For simplicities sake, I’ll mention the simplest way. This way doesn’t give the most realistic ‘feel’ for rotation though.

declare 3 floats: rotationX,rotationY,rotationZ
all initially 0.

Assuming you know how to track how much the mouse has moved between mouse move messages (if you don’t know how, i can explain)

each time the mouse moves, if the left mouse button is down track delta_X, delta_Y which is the amount the mouse has moved since the last mouse move.

if deltaX is negative decrement rotationY by some value - let’s say 5 degrees (I’ve arbitrarily selected horizontal mouse movement for rotation about Y-axis - this is the standard though).
if deltaX is positive increment it.

rotation about X axis is analogous, swapping in horizontal mouse motion.

for Z rotation, you can check if the CTRL key is down as well as the left mouse button, if it is then rotate in Z.

then in your drawing code:

 
glLoadIdentity();
glRotate(rotationX,1,0,0);
glRotate(rotationY,0,1,0);
glRotate(rotationZ,0,0,1);
//draw your beam.
 

hope this stuff helps.

OK thanks for your help thus far. :smiley:

Now that I know it can be done, I will sit down and try to better understand the samples I had previously found, and the new one you have provided a link for.

I will probably be back with more questions as I get further into this (as I stated before, this will be my first go at using OpenGL).

I managed to create one cuboid like so:

	glBegin(GL_QUADS);
		
		//top
		glColor3f(0.0f,1.0f,0.0f);						// green
		glVertex3f( 1.0f, 1.0f,-1.0f);					// Top Right
		glVertex3f(-1.0f, 1.0f,-1.0f);					// Top Left
		glVertex3f(-1.0f, 1.0f, 1.0f);					// Bottom Left
		glVertex3f( 1.0f, 1.0f, 1.0f);					// Bottom Right
            .
            .
            .
            <5 more sides of this cuboid>

      glEnd();

And was successful in rotating that shape.

Now I have to do this 2 more times (ie 2 more cuboids) to represent my “I” shape?

Before I get to deep into this, I want to make sure I am going in the right direction.

Good so far?

I think you’re on the right track.

OK.

Defining the three front faces that make up the cross-section is relatively easy.

I then have 3 rectangles X 6 faces per cube = 18 sides (faces) to define.

I just wanted to make sure there was no automatic way that OpenGL could generate all other faces of the cuboid since this cross-section was the same (continuous) from front to back. This obviously then would be much less work, with less chance of making errors.

P.S. This is kind of exciting! :smiley:

nope. no automatic face generation, you have to tough it out :slight_smile:

OK :slight_smile:

Well I have a beam drawn :smiley:

Looks pretty good, except that I have each face a different color. This is good for debugging as I can see each face clearly and watch what it does… I’ll try to improve this later.

Rotation works, but not exactly as it should.

I’m sure it was working properly initially, but now it rotates in a giant arc. I think it might have something to do with me adding “zoom out” code. I had to do this because my beam is so big, it completely fills the window and the shape can not be seen.

Perhaps an explanation will help me in understanding why it is rotating as though it is “orbiting” around a distant point, rather than about it’s own axis. Again, I am confused because I didn’t change the rotation code, only added “zoom out” code.

Rotation is always done around the center axis (0, 0, 0) coordinates. You propably translated our beam away from the world center instead of using the glScale function !?!

The automatic way you were looking for would be a function you have to write yourself. This is called an extrusion in most CAD applications.
Setup your vertices of the cross section in an array and write a function which generates QUADS between those vertices and the same set of vertices with a different z value (if the cross section is defined in the xy plane)

Given this function you would only need to supply the cross section and the length of the extrusion and the 3D faces would be created automatically. The next step would be to close the front and back of the beam…

Have fun

This is happening because of the translation you’ve added to the modelview matrix. The origin of your object’s local coordinate system changes after this translation (i.e. it is no longer at the same point as the world’s origin). You need to translate the object back to the world origin, perform the rotation and then translate it back…this isn’t necessary to achieve what you’re trying to do; use glScale to scale the object up or down, like this:

//scales the object to 50% of its original size
glRotate();
glScalef(.5,.5,.5);
//draw object.

def and Aeluned:

glScalef()

I wasn’t aware of this function, so what I did was “zoom out” at startup and this explains why my face-on view appears as an isometric (fades to a vanishing point).

I will correct with the glScalef function.

def:
I understand the term “extrusion” as it applies in a manufacturing sense, so I underdstand exactly what you are getting at.

Do you know of any posted web samples that demonstrates this technique that you could refer me to?

all:
I think I should step back and familiarize myself with some OpenGL terms (modelview matrix, world origin, local coordinate system, etc) and concepts; I don’t really understand these.

Is there a tutorial somewhere that explains this well somewhere (outside of code examples)?

I have looked a NeHe’s site, but I get lost in all the code.

Using OpenGL is not trivial! :eek: (I might be in way over my head) :frowning:

This is a good read:
http://www.opengl.org/documentation/red_book_1.0/

The OpenGL “Redbook” looks like exactly what I need.

Thanks! :slight_smile: