Draw a border around a 3d subwindow

I would like to have a border around a subwindow. If I use ortho2D for the subwindow, then it’s simple to draw a rectangle around the window (it’s just a 2d rectangle). But if I use a projective subwindow, then it would be pretty hard to figure out which rectangle to draw. Is there anyway to say “for this object, forget about the current matrix stack and just draw directly to these pixels”? (writing directly to the frame buffer, i guess?)



You can change the projection multiple times while drawing, before swpbuffers.

Since that’s perhaps a bit short, here’s what you would do:

  1. clear the backbuffer (assuming you’re using a doublebuffered window, which you should)
  2. set your projection matrix for the 3D stuff, using depth test and depth write etc
  3. render the scene
  4. disable depth test and depth write, set your orthogonal projection
  5. draw your 2D stuff (the borders)
  6. call swapbuffers and you’re done

I don’t really follow.

I have the window setup using:

TopViewID = glutCreateSubWindow(MainID, 700, 0, 300, 200);
	gluPerspective(50, 3./2., 1, 100);
	gluLookAt(0, 10, 0, 0, 0, 0, 1, 0, 0);

so in the display function


// if i change the projection here, then it will change the view, no?

	//draw the outline	
	DrawRectangle(300, 200, 0, 0);

What I’m saying is that I want to leave the view however it is, and just draw a rectangle around it.

How do you specify which buffer to draw to - and why do you need two buffers to do this?



You will change the view for the object you have to draw, but the object you have drawn are already in the framebuffer.

ah of course.

now the question is, how do I get the coords of the edge of the current view?

If I set the view with glLookAt or gluPerspective, then I don’t know what the border of the window is currently corresponding to, so I don’t know how to draw a rectangle there!



Set the position and size of your subwindow with glViewport and then draw into it setting perspective projection for your 3D stuff and 2D projection for 2D stuff.

before window transformation, coordinates are normalized coordinates and completly independant of your viewport position and size. This is you that set the rasterization area.

From my understanding of the documentation, this should draw a rectangle around my window no matter what orientation the view currently is in:

glViewport(0, 0, 1, 1);
glVertex3f(.2, .2, 0);
glVertex3f(.8, .2, 0);
glVertex3f(.8, .8, 0);
glVertex3f(.2, .8, 0);

However, it doesn’t draw anything. What have I done wrong?


glViewport take position and size in pixels and you set a 1 by 1 window size, I think it is a bit small.

gluPerspective(50, 3./2., 1, 20);
gluLookAt(0, 0, 10, 0, 0, 0, 0, 1, 0);

double w = 300, h = 200, x = 0, y = 0;
glVertex3f(x+1, y+1, 0);
glVertex3f(x+1, y+h-1, 0);
glVertex3f(x+w-1, y+h-1, 0);
glVertex3f(x+w-1, y+1, 0);

What I get is the bottom left corner of a rectangle drawn in the top right corner of the window.

	//gluLookAt(0, 0, 10, 0, 0, 0, 0, 1, 0);//if i use this one, it draws a rectangle
	gluLookAt(0, 10, 10, 0, 0, 0, 0, 1, 0);//if i use this one, it skews the rectangle by the perspective, just as I thought it would

double w = 300, h = 200, x = 0, y = 0;
glVertex3f(-x/2+1, -y/2+1, 0);
glVertex3f(x/2-1, -y/2+1, 0);
glVertex3f(x/2-1, y/2-1, 0);
glVertex3f(-x/2+1, y/2-1, 0);

This does not accomplish the goal of trying to draw a rectangle around the window no matter what the perspective is. Any other suggestions?

when you draw your rectangle, you have to set orthographic projection and not perspective one. In the latter case You will obtain anything but what you want.

In the second code snippet this works because you don’t set any projection matrix and the default one is the identity matrix.

but it doesn’t matter what projection I set, if I simply draw a rectangle with vertices (-w/2, -h/2), (w/2, -h/2), (w/2, h/2), (-w/2, h/2) then it will only be a rectangle if I look at it directly down the z axis, right? My understanding is that these vertices are in world coordinates, so it is “placed in the world” independent of the viewing transformation, right?

We’ve gone back and forth several times - would it be possible to provide a small snippet that shows where I have gone wrong?


at dletozeun saix u need something like

gluPerspective(50, 3./2., 1, 20);
gluLookAt(0, 0, 10, 0, 0, 0, 0, 1, 0);

draw 3d scene

setup the model view (perhaps the identity matrix)

draw border