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?)

Thanks,

Dave

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);
	glutSetWindow(TopViewID);
	glutDisplayFunc(DisplayTopView);
	gluPerspective(50, 3./2., 1, 100);
	gluLookAt(0, 10, 0, 0, 0, 0, 1, 0, 0);

so in the display function


	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	DrawScene();
	
// if i change the projection here, then it will change the view, no?

	//draw the outline	
	SetColor('w');
	DrawRectangle(300, 200, 0, 0);
		
	glutSwapBuffers();

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?

Thanks,

Dave

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!

Thanks,

Dave

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);
glColor3f(1,1,1);
glBegin(GL_LINE_LOOP);
glVertex3f(.2, .2, 0);
glVertex3f(.8, .2, 0);
glVertex3f(.8, .8, 0);
glVertex3f(.2, .8, 0);
glEnd();


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

Dave

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);

glViewport(0,0,300,200);
double w = 300, h = 200, x = 0, y = 0;
glBegin(GL_LINE_LOOP);
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);
glEnd();

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

glViewport(0,0,300,200);
double w = 300, h = 200, x = 0, y = 0;
glBegin(GL_LINE_LOOP);
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);
glEnd();

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?

Thanks,
Dave

at dletozeun saix u need something like

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

draw 3d scene

GL_PROJECTION
glOrtho(…)
GL_MODELVIEW
setup the model view (perhaps the identity matrix)

draw border