Please see SVN Access for information regarding DokuWiki projects.
Your Donations help keep my Software going!

Simple OpenGL Wall

Hello,

Maniraj S. wrote…

I'm a newbie in openGl and I got you address from the Nehe website.
I have to submit an openGL application in some days and Im facing a big
problem.
I have to draw wall and within those walls I have to draw transpartent
windows so that you can see outside the wall
I have thought of many ways but can think of nothing else other than
breaking the walls into polygons which is very impractical.
Does opengl support something for this ?

Well, breaking up the wall is not so impractical; most hardware level 3d graphics apis break any polygon you give to it into triangles, so essentially feeding the api triangles is usually just as good.

So I assume you're in a situation similar to figure 1, and that straight on it would look like figure 2.

Figure 1Figure 2

To achieve this– and keep in mind order isn't usually important unless you attempt to blend… you first draw the tree with opengl as a textured quad (figure 3), then the wall in front of it.

Figure 3

So to draw the wall, the easiest way is to just break everything into triangles, and to assign all the vertices a letter, which I have done in figure 4.

Figure 4

After you have assigned labels to vertices, construct a list of all triangles that are only part of the wall… A,E,B, then A,H,E, then L,F,E, and so on. This list will become an array of vertices that you will draw with glBegin(GL_TRIANGLES); You could put them in your source file like so:

float verts[][2] =
{
  A, E, B,
  A, H, E,
  L, F, E,
  .
  .
  .
};

Now if your wall originally extends from coordinates (0,0) to (1, 1), for instance, you would assign A as 0,0, C as 1,1, B as 0,1, and D as 1,0… with all other points as somewhere between 0 and 1. (Say E is 20% across, and one unit up, so it's 0.2,1) You can then define these as macros, and put them above your “verts” definition above, like so:

#define A {0.0f, 0.0f}
#define B {0.0f, 1.0f}
#define C {1.0f, 1.0f}
.
.
.
float verts[][2] =...

Notice I'm only using x and y– you can include z if you want but it's not necessary.

Now if you have texture coordinates for your wall, they would essentially be re-assigned in a similar fashion to the vertex coordinates above. You could probably use the same A, B, C in this case (if your texture spans the quad from 0 to 1 like the coordinates do), but that's not generally true– it depends on how you decide to break up a polygon.

So to draw this, use a for loop similar to:

glBegin(GL_TRIANGLES);
for(int i = 0; i < number of letters; i++)
{
  glTexCoord2fv(&verts[i][0]);
  glVertex2fv(&verts[i][0]);
}
glEnd();

Finally, approach the windows in a similar fashion– if you have to draw them with alpha blending, turn on blending, set your options, then draw them in a similar fashion to what's above. (See figure 5.)

Figure 5

It is important that you do blending last (in this case), since you need to blend the window color against the tree or any other background objects, and the background objects need to be there first for this to happen.

This should be good enough to get you about halfway there. The rest is up to you.

Good luck, — Terence J. Grant 11/08/2006 05:56


Personal Tools