PDA

View Full Version : Draw a border around a 3d subwindow



David Doria
09-02-2008, 02:11 PM
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

ZbuffeR
09-02-2008, 02:17 PM
You can change the projection multiple times while drawing, before swpbuffers.

T101
09-03-2008, 03:32 AM
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

David Doria
09-03-2008, 07:42 AM
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

Rosario Leonardi
09-03-2008, 07:47 AM
// if i change the projection here, then it will change the view, no?
You will change the view for the object you have to draw, but the object you have drawn are already in the framebuffer.

David Doria
09-06-2008, 03:06 PM
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

dletozeun
09-07-2008, 06:22 AM
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.

David Doria
09-07-2008, 09:24 AM
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

dletozeun
09-07-2008, 10:59 AM
glViewport take position and size in pixels and you set a 1 by 1 window size, I think it is a bit small.

David Doria
09-07-2008, 12:17 PM
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?

dletozeun
09-07-2008, 01:17 PM
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.

David Doria
09-07-2008, 01:25 PM
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

zed
09-07-2008, 02:11 PM
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