PDA

View Full Version : Drawing a hole in something

krump89
03-17-2008, 06:41 AM
Hi
I've just started playing around with openGL lately and I'm still learning the basics. What I would like to know is, how to draw a hole in something.

For example say I draw a simple colored box, how can I draw a hole in the middle of it that can be seen through. I've been looking around but haven't found any functions or anything for doing this.

mickw0040817
03-17-2008, 07:07 AM
Yeah I don't know of any functions or tools for doing that either but I'm still a noob too so there prob is some simple method for it.
I don't know what/how exactly you plan to use it but one basic way I can think of that might help you do whatever you need to do is to draw the whole first just using GL_LINES.

With your example of a square:
Draw the hole first - lets say a small square. (using GL_LINES)
Then make up the outside square using a number of small rectangles or whatever shapes you need.

Heres some example code:

void drawShape(){

//the hole in the middle starts here
glBegin(GL_LINES);
glColor3f(0,0,0);
glVertex2i(250,250);
glVertex2i(300,250);
glEnd();
glBegin(GL_LINES);
glVertex2i(300,250);
glVertex2i(300,300);
glEnd();
glBegin(GL_LINES);
glVertex2i(300,300);
glVertex2i(250,300);
glEnd();
glBegin(GL_LINES);
glVertex2i(250,300);
glVertex2i(250,250);
glEnd();

//The outside shapes start here
glColor3f(0,0,1);
glVertex3f(150,150,0);
glVertex3f(400,150,0);
glVertex3f(400,250,0);
glVertex3f(150,250,0);
glEnd();
glVertex3f(150,300,0);
glVertex3f(400,300,0);
glVertex3f(400,400,0);
glVertex3f(150,400,0);
glEnd();
glVertex3f(150,250,0);
glVertex3f(250,250,0);
glVertex3f(250,300,0);
glVertex3f(150,300,0);
glEnd();
glVertex3f(300,250,0);
glVertex3f(400,250,0);
glVertex3f(400,300,0);
glVertex3f(300,300,0);
glEnd();
glFlush();
}

http://users.tpg.com.au/mjulius8//insideSquare.jpg

http://users.tpg.com.au/mjulius8//largerSquare.jpg

Once you know the coordinates of the hole you wouldn't even need to draw its outline just know where to place the outside shapes around it.

I hope this can help you out but it's pretty basic. If not there's some other threads about similar sort of things i've noticed around here somewhere. They seem a little more complicated though but maybe you can look into them for ideas.

good luck
Michael

EvilOne
03-17-2008, 11:24 AM
Use an existing CSG library like http://www.opencsg.org/

krump89
03-18-2008, 05:45 AM
Thanks for the replies I don't know why I didn't think of trying something like that. It should work for the basic stuff I'm doing.
I think I'll check out the CSG library though since that sounds like it would be the proper way to do things and would be good to know.