PDA

View Full Version : Mouse Drawing



Thahir
10-14-2009, 05:47 AM
Hi friends,

I am new to opengl , can any please tell to how to draw a line using mouse in particular plane (xy, yz , zx).

Thnxs.

punky
10-14-2009, 08:50 AM
on first mouseclick get point1(Mousex,Mousey,z) on 2nd mouseclick get point2(Mousex,Mousey,z);

than paint the line if 3d mouseclick
with glBegin(GL_LINES);

punky
10-14-2009, 09:35 AM
this is the thing how to calculate your mouse(coordinates) into opengl mouse coordinates


void Calc3DMouse(void){
GLfloat nh=nHeight/2;
GLfloat nw=nWidth/2;
Mouse.yGL=-(Mouse.y-nh-nHeight)/10;
Mouse.xGL=(Mouse.x-nw-nWidth)/10;
}

if u move then on your winow u get the opengl coodrinates istead of the window form coordinates

Thahir
10-15-2009, 02:22 AM
hi punky,

i think u not understand my post. What i am asking, i want to get the points of x, y, z while clicking. But the mouse is 2d co-ordinate and screen is 3d co-ordinate. Then how we convert the mouse co-ordinate in to 3d co-ordinate...

Thnxs in advance.

Thahir
10-15-2009, 02:27 AM
hi punky,

i think u not understand my post. What i am asking, i want to get the points of x, y, z while clicking. But the mouse is 2d co-ordinate and screen is 3d co-ordinate. Then how we convert the mouse co-ordinate in to 3d co-ordinate...

Thnxs in advance.

rakesh_thp
10-15-2009, 03:07 AM
i guess the screen what u r referring is the opengl canvas right.??? In this case you got to use gluunproject function.. This function will convert ur windows 2d coordniates to opengl 3d coordinates..

Search for converting windows coordinates to opengl coordinates using gluunproject() in your favorite search engine.. Good luck..

punky
10-16-2009, 01:30 AM
i dont know what u mean with 3d mouse coordinates it can always only be 2d, how the mouse should detect 3d inputs, u can only use the mouse wheel for 3d input but the input is still 2d
u need a 3rd sensor on the mouse to detect 3d inputs, from physical to virtual space..????????

Thahir
10-17-2009, 01:22 AM
hi punky,

i need to draw a line on opengl using mouse(like mspaint). But the mouse is 2d co-ordinate and how can i pass the 2d argumnents to opengl ?.

punky
10-18-2009, 03:07 AM
i dont know what u mean ... sorry
the mouse coodridantes u get for example from WinProc are the windows form coordinates they are different to opengl, and i dont know your general c++ windows skills so if u can tell me how you get the mouse coordinates .. it would help

punky
10-18-2009, 04:14 AM
as i do i make a Mouse Structure, wich keeps all important Infos about the mouse:



typedef struct {
GLfloat x; // the windows Form Coordinates
GLfloat y;// the windows Form Coordinates
GLfloat xGL; // this will get the opengl Coordinates x
GLfloat yGL; //this will get the opengl Coordinates y
GLfloat Clicks; // counts how many clicks done until we reset this counter
bool lbuttonDown; // if mouse button is down, left one
bool rbuttonDown; // the other
}mouse;
mouse Mouse;




u should define, nWidth nHeight global:


int nWidth,nHeight;



in the MAinWndProc of your window




LRESULT CALLBACK MainWndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
{

RECT rect;
LONG lRet = 1;
PAINTSTRUCT ps;

switch( message )
{
case WM_CREATE:
{

nWidth = LOWORD(lParam); // get the Window Width and Height, we define it globally so whe can use them for opengl Coordinates
nHeight = HIWORD(lParam);
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 45.0, (GLdouble)nWidth / (GLdouble)nHeight, 0.1, 100.0);


glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
break;
}

case WM_LBUTTONUP:
{
Mouse.lbuttonDown=false; // set Mouse button as down false
break;
}
case WM_LBUTTONDOWN:
{
Mouse.lbuttonDown=true; // mouse button is pressed so true
break;
}



case WM_MOUSEMOVE:
{
Mouse.x =(short)LOWORD(lParam); // gets the windows coordinates, if mouse moves in the opengl window
Mouse.y= (short)HIWORD(lParam); // gets the windows coordinates something like >0 is always a positive number
break;
}
}

Mouse.x and Mouse.y is the Windows coordinates!



void CalcOpenGLMouse(void){
GLfloat nh=nHeight/2; // get the middle of windows coordinates x
GLfloat nw=nWidth/2; // get the middle of windows coordinates y
Mouse.yGL=(Mouse.y-nh-nHeight)/10; // calculate the Mouse Coordinates to OpenGL x, and y
Mouse.xGL=(Mouse.x-nw-nWidth)/10;
}


Mouse.yGL and Mouse.xGL are now the true Corrdinates fo OpenGL!!

now we have everything we find the OpenGL x,y to move our mouse in Opengl Space, we know now when the Mouse Button is Pressed and when not
so we can do a function wich is placed into the OpenGL render function

Now we need a Line Structure because we wanna keep this line all time renderd:
the Structur for the Line we wanna paint can look like this:


typedef struct tagCustomLine
{
GLfloat x[50]; // lets make space for 50 lines
GLfloat y[50]; // fo y 50
GLfloat Z[50]; // we make Z static, to get the Z value for the line all time is more difficult,
GLfloat Index; // line Index
}CustomLine;

to get the Mouse Z coordiante we need to calculate it form the actual lookat coordinates, because we need to know if we
should inout z,y or z,x ?? its difficult because it depends on how we look at the matrix... i wanna show you
just 2d input.



CalcOpenGLMouse(); // Get The OpenGL Mouse Coordinates
if(Mouse.lbuttonDown==true){
Mouse.Clicks=+1; /// add a click count to our Mouse counter
}

if (Mouse.Clicks==1){
//we start painting , or better we get point one for the line, u can also put a Mouse is Drawing indicator here, Like PaintMode==true or something
tagCustomLine.Index=+1; // index +1
CustomLine.x[tagCustomLine.Index]=Mouse.xGL;
CustomLine.y[tagCustomLine.Index]=Mouse.yGL;
CustomLine.z[tagCustomLine.Index]=0; // lets make z 0, static

}
if (Mouse.Clicks==2){
//we get point two for the line
tagCustomLine.Index=+1; // index +1
CustomLine.x[tagCustomLine.Index]=Mouse.xGL;
CustomLine.y[tagCustomLine.Index]=Mouse.yGL;
CustomLine.z[tagCustomLine.Index]=0; // lets make z 0, static
// now we reset the Mouse.Click counter so we can paint another line
Mouse.Clicks=0;
}

now lets render the line i make a line DrawFunction,the function should called in the RenderFunction


void PaintLine(GLfloat x,GLfloat y,GLfloat z){
//may we should pop Matrix
// always a pair from the CustomLine STruct defines a full line!
for (int i=0;i<tagCustomLine.Index+1;i++){
glBegin(GL_LINES);
glVertex3f(CustomLine.x[i],CustomLine.y[i],CustomLine.z[i]); // 1st Point
// now add 1 to i for the 2nd point to draw
i++;
glVertex3f(CustomLine.x[i],CustomLine.y[i],CustomLine.z[i]); // 2nd Point

glEnd();

}


if u wanna delete a line out u need to delete the line at index and reorder your struct to have no holes in the struct
like index 1,2,3 exits u delete 2
now u need to give index 3 the index 2 , the x,y array must move down one in CustomLine x and y...

NOTE:
this is not a full developed Mouse Painting code just to see how it could work
(i just wrote it out of my brain)

Thahir
10-19-2009, 12:14 AM
hi punky,

thanks for ur help....but i get the article from Nehe

http://nehe.gamedev.net/data/articles/article.asp?article=13

Thahir
10-19-2009, 12:15 AM
hi punky,

thanks for ur help....but i get the article from Nehe

http://nehe.gamedev.net/data/articles/article.asp?article=13