has anyone ever tried this? it seems to me that most people use OpenGL for games but I am designing an interior design type application (so that people can explore their own designs in 3D). I have a 2D design screen where I want the user to be able to draw box like shapes which will represent rooms. I know how to detect mouse clicks but I dont know how to draw a line on a screen depending on where the user drags the mouse. I suppose this is similar to Microsoft Word or Paint and other such applications, but, of course, I only want straight lines and boxes, no curves.

I dont know where to start, any ideas?!

thanks a lot

Gareth James
<><

ps I am using C++ in case you wanted to know

Gavin

04-16-2002, 02:28 AM

OnMouseDown
line start
OnMouseUp
line end

Add line to set

glBegin(GL_LINES)
draw_my_line_set
glEnd();

billy

04-16-2002, 03:01 AM

You don't need OpenGL to draw 2d lines and boxes. However, if you want to transform this into 3d shapes OpenGL is useful for visualisation.

dfanuk

04-16-2002, 04:49 AM

Gavin

yeh, that's a great idea and it's what I am going to do I think. If someone clicks the mouse button I'll then get the POINT of this click. How can I use the x and y values of the point in a GL_LINES list of vertices? An example list of vertices is like this right?

So, what I am trying to say is, how do I turn the point coordinates into something that can go in the glVertex2d command.
These values will also depend on where I have translated in the scene don't they?

I hope you can see what I am getting at!

thanks
Gareth
<><

Furrage

04-16-2002, 07:25 AM

gluUnproject(). This converts a point in windows co-ordinates to OpenGL co-ordinates. Since you're using glVertex2*() set winz to 0. This will project the point to 0 z in OpenGL which will match the glVertex2*() call.

john

04-16-2002, 03:45 PM

why does every man and his dog like gluUnProject()??!?! It's not some magical device that solves everything (though I hear that gluMedicalResearch(GL_CANCER_VACCINE) will be released in opengl 2.0).

If you want to plot lines in 2D, then you do NOT need to use gluUnproject. (If you want to plot lines in 3D, then you probably also don't need it.)

the trick --
set your camera to ortho projection with some "useful" coordinate system. maybe thats 0,0 - w,h in pixel coordinates, or maybe its 0,0 - 1,1 or mayeb -1,-1 .. 1,1. its whaetever makes you happy; its not relevent.

get your mouse coordinate in x,y. you know how big the screen is in pixels, so you can compute the ratio the x,y is within that screen. Since you know the ratio of the mouse to pixels in the window, you can linearly compute the ratio in camera coordinates.

easy.

cheers,
John

Gavin

04-17-2002, 01:12 AM

Yes use glortho and set it up per pixel sort of idea (as above) so all your 2d stuff is directly related. Assuming you are just wanting to do a simple 2d drawing app.

dfanuk

04-17-2002, 01:48 AM

john, gavin

yes! that sounds good!

ok so in my ReSizeGLScene (which resizes and initializes the GL window) I have, amongst others, these two lines:

glViewport(0, 0, width, height); // Reset the Current Viewport

glMatrixMode(GL_PROJECTION); //Select the Projection Matrix

from what you said, do I take it that I only want to have an ortho projection when I am doing this business with the lines? I hope this is right because I imagine I'm going to have to change a lot of stuff in my code to change complete to an ortho projection.

ie can I change the projection matrix I am using on the fly? and if so, how do I do that.

sorry for my newbie-ness but I really am still finding my feet it seems.

Gareth
<><

Gavin

04-17-2002, 01:54 AM

Hmmm, are you drawing other 3d stuff as well?

JML

04-17-2002, 02:00 AM

ie can I change the projection matrix I am using on the fly? and if so, how do I do that.

sorry for my newbie-ness but I really am still finding my feet it seems.

You can change the projection you use whenever you want, even several times within drawing a single frame. Just issue an

when you need it. If you do not want to disturb the 'overall' projection matrix (the one used in the rest of your application, issue a glPushMatrix() before modifying the projection matrix and a glPopMatrix() once you're done with it (again in GL_PROJECTION matrix mode!).
Do keep in mind that usually there is only room for 2 projection matrices on the stack!

It is quite common to mix ortho projections (console display, GUI display, window-dressing like crosshairs etc.) with perspective projections (drawing 3d objects in a scene).

HTH

Jean-Marc.

dfanuk

04-17-2002, 02:04 AM

oh excellent.

yes I am doing other 3D stuff as well. The part I am working on now is for allowing the user to draw 2D things but I am going to use the points (which I will store in a linked list/array) to not only display the lines they've drawn but also to later build a 3D scene out of them.

Gareth
<><

[This message has been edited by dfanuk (edited 04-17-2002).]

JML

04-17-2002, 02:10 AM

By the way: changing the projection matrix will keep the previously rendered frame buffer intact, including values written to the depth buffer. However, those depth values are 'remapped' to the new projection matrix.

First: no offense, but try to get a copy of the redbook. It explains in detail what the purpose of all the gl functions are and shows their use via examples. You should also checkout some of the excellent tutorials on the net (nehe.gamedev.net being one of them).

Second: clip planes are planes in 3d space that split the space in two parts: a part that gets clipped away (i.e. fragments on that side are not draw) and a part that gets drawn.
The ortho projection sets up 6 planes that effectively reduce the infinite world to a orthogonal box. Only fragments drawn inside the box turn up on your screen, all else is thrown away.
The perspective projection does a similar job, only now the planes are not all parallel/perpendicular to each other, but they are part of a piramid that is missing its top:

+----+
/ \
/ \
+----------+
(and that in 3D)

HTH

Jean-Marc.

dfanuk

04-17-2002, 02:40 AM

ok thanks Jean-Marc. Don't worry, I'm not offended!

I have many of the nehe tutorials and they have been a great help. I just haven't fully understood the perspective stuff. your description helps a lot. did you know that there is a copy of the red book on the net? I shall use that for help as well.

thanks again

Gareth
<><

Gavin

04-17-2002, 03:12 AM

Well depends where you want the origin to be, but they should be relevant to the window width and height that you are using in the resize func. so you could use
0, width, 0, height

or

-height/2, -width/2, height/2, width/2

see...

dfanuk

04-17-2002, 03:38 AM

Gavin

if I used (0, width, 0 height) then that would mean that I am setting the origin to the bottom left of the screen yeh?
would it be easier for me to set it to the top left, ie to make converting my pixel coordinates to window coordinates easier, as pixel origin is top left.

In the red book it says this:

For the special case of projecting a two-dimensional image onto a two-dimensional screen, use the Utility Library routine gluOrtho2D(). This routine is identical to the three-dimensional version, glOrtho(), except that all the z coordinates for objects in the scene are assumed to lie between -1.0 and 1.0. If you're drawing two-dimensional objects using the two-dimensional vertex commands, all the z coordinates are zero; thus, none of the objects are clipped because of their z values. void gluOrtho2D(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top);

isn't this more what I need? I am projecting a 2D image.

from what you've all said, am I right in saying that if I switch to an ortho projection whilst in DrawGLScene, everything else that has already been drawn will not be altered? I just want to be clear on that

Gareth
<><

JML

04-17-2002, 04:36 AM

Originally posted by dfanuk:
from what you've all said, am I right in saying that if I switch to an ortho projection whilst in DrawGLScene, everything else that has already been drawn will not be altered? I just want to be clear on that

What has already been drawn will not be altered by changing the projection for subsequent draws.

It will of course be affected by what you draw next http://www.opengl.org/discussion_boards/ubb/smile.gif

Jean-Marc.

dfanuk

04-17-2002, 06:07 AM

ok, this is what I have got at the moment (I've been working on my linked list)

//LOOP THROUGH THE ARRAY AND
//USE glortho WITH THE MOUSE
//COORDINATES AND SCREEN SIZE IN PIXELS

glPopMatrix();

what I am unsure of at the moment is:
a) whether the values I have put in the gluOrtho2D proc are correct, and
b) what kind of values I need to change the pixel coordinates returned by GetCursorPos to

any help would be much appreciated!

thanks
Gareth
<><

Gavin

04-17-2002, 06:34 AM

It looks ok. Do a printf when you click the mouse and output what you arte getting for the x/y of the mouse and see if it is what you think it should be. Then try drawing a point form this click.

int x_pos = 50, y_pos = 50;

and in your draw draw this point, you should see it at 50, 50. Then in your mouse click func do....
x_pos = mouse_x..... and so on, just make em global for now. to check everything works...

oh if the width of a window is 600 the left will be 0 and right will be 599!!!

dfanuk

04-18-2002, 02:11 AM

At the moment when I put the code that I gave in my last post into my DrawGLScene code and then run the program, when I open the appropriate screen nothing is drawn at all. so I have a feeling that something is going wrong with that code!

Gareth

dfanuk

04-20-2002, 03:39 AM

this problem has half been solved now, I no longer get a blank screen, hurray!

But I don't know what I need to put in the glVertext2d function, ie I'm not sure what a typical value should be. can u check the code and see what you think might be wrong please? and give me an example as to what should go in the glvertex2d function.

I have many of the nehe tutorials and they have been a great help. I just haven't fully understood the perspective stuff. your description helps a lot. did you know that there is a copy of the red book on the net? I shall use that for help as well.

thanks again

Gareth
<><

Hi!
I wanted to know about the copy of the red book on the net...can you write the URL?
Thanks, Libby

Gavin

04-23-2002, 02:57 AM

Give 2d coords that fall within those that you supplied when you set the projection matrix with ortho2D. If you set this up to be the same as the window size they will 1:1 ration with pixel coords. So just use the mouse values directly.

jimmi

04-23-2002, 03:14 AM

Originally posted by libby:

Hi!
I wanted to know about the copy of the red book on the net...can you write the URL?
Thanks, Libby