to move an object on mouse move inside OpenGL window using windows forms application in vc++.net

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi
I have an opengl window inside a form in VC++.net windows form application.
I have created an object in opengl class and able to display it in the opengl window.
Now , i want to move the object in the opengl window on mouse move.
I have written the code for moving an object in the opengl class.
I am calling this method from Form->mouse move event.
Im able to move the object only when i click the mouse in form.
But my task is to move the object when i click the object.
My piece of code for your reference:
The OpenGL class:
public ref class COpenGL: public System::Windows::Forms::NativeWindow<br/>
{<br/>
public:<br/>
COpenGL(System::Windows::Forms::Form ^ parentForm, GLsizei iWidth, GLsizei iHeight)<br/>
{<br/>
CreateParams^ cp = gcnew CreateParams;<br/>
<br/>
// Set the position on the form<br/>
cp->X = 0;<br/>
cp->Y = 0;<br/>
cp->Height = iHeight;<br/>
cp->Width = iWidth;<br/>
<br/>
// Specify the form as the parent.<br/>
cp->Parent = parentForm->Handle;<br/>
<br/>
// Create as a child of the specified parent and make OpenGL compliant (no clipping)<br/>
cp->Style = WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;<br/>
<br/>
// Create the actual window<br/>
this->CreateHandle(cp);<br/>
<br/>
m_hDC = GetDC((HWND)this->Handle.ToPointer());<br/>
<br/>
if(m_hDC)<br/>
{<br/>
MySetPixelFormat(m_hDC);<br/>
ReSizeGLScene(iWidth, iHeight);<br/>
InitGL();<br/>
<br/>
}<br/>
<br/>
/*rtri = 0.0f;<br/>
rquad = 0.0f;*/<br/>
}<br/>
<br/>
<br/>
System::Void Render(System::Void)<br/>
{<br/>
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear screen and depth buffer<br/>
<br/>
glLoadIdentity(); // Reset the
current modelview matrix<br/>
glTranslatef(1.5f,0.0f,-7.0f);
<br/>
<br/>
gluLookAt(<br/>
0.0f, 0.0f, 3.0f,<br/>
0.0f, 0.0f, 0.0f,<br/>
0.0f, 1.0f, 0.0f);<br/>
<br/>
glRotatef(xrot, 1.0f, 0.0f, 0.0f);<br/>
glRotatef(yrot, 0.0f, 1.0f, 0.0f);// Move right 1.5 units and into the screen 7.0<br/>
////glRotatef(rquad,1.0f,1.0f,1.0f); // Rotate the quad on the x axis
<br/>
glBegin(GL_QUADS); // Draw a quad<br/>
glColor3f(0.0f,1.0f,0.0f); // Set The color to green<br/>
//glColor3f(1.0f,0.0f,0.0f);<br/>
glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right of the quad (top)<br/>
glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left of the quad (top)<br/>
glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom left of the quad (top)<br/>
glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom right of the quad (top)<br/>
glColor3f(1.0f,0.5f,0.0f); // Set The color to orange<br/>
//glColor3f(1.0f,0.0f,0.0f);<br/>
glVertex3f( 1.0f,-1.0f, 1.0f); // Top Right of the quad (bottom)<br/>
glVertex3f(-1.0f,-1.0f, 1.0f); // Top Left of the quad (bottom)<br/>
glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom left of the quad (bottom)<br/>
glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom right of the quad (bottom)<br/>
glColor3f(1.0f,0.0f,0.0f); // Set The color to red<br/>
glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right of the quad (front)<br/>
glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left of the quad (front)<br/>
glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom left of the quad (front)<br/>
glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom right of the quad (front)<br/>
glColor3f(1.0f,1.0f,0.0f); // Set The color to yellow<br/>
//glColor3f(1.0f,0.0f,0.0f);<br/>
glVertex3f( 1.0f,-1.0f,-1.0f); // Top Right of the quad (back)<br/>
glVertex3f(-1.0f,-1.0f,-1.0f); // Top Left of the quad (back)<br/>
glVertex3f(-1.0f, 1.0f,-1.0f); // Bottom left of the quad (back)<br/>
glVertex3f( 1.0f, 1.0f,-1.0f); // Bottom right of the quad (back)<br/>
glColor3f(0.0f,0.0f,1.0f); // Set The color to blue<br/>
//glColor3f(1.0f,0.0f,0.0f);<br/>
glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right of the quad (left)<br/>
glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left of the quad (left)<br/>
glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom left of the quad (left)<br/>
glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom right of the quad (left)<br/>
glColor3f(1.0f,0.0f,1.0f); // Set The color to violet<br/>
//glColor3f(1.0f,0.0f,0.0f);<br/>
glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right of the quad (right)<br/>
glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left of the quad (right)<br/>
glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom left of the quad (right)<br/>
glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom right of the quad (right)<br/>
glEnd();<br/>
<br/>
<br/>
<br/>
}<br/>
<br/>
System::Void idle()<br/>
{<br/>
<br/>
<br/>
<br/>
POINT mouse; // Stores The X And Y Coords For The Current Mouse Position<br/>
GetCursorPos(&mouse); // Gets The Current Cursor Coordinates (Mouse Coordinates)<br/>
<br/>
ScreenToClient((HWND)this->Handle.ToPointer(), &mouse);<br/>
<br/>
GLfloat winX, winY; // Holds Our X, Y and Z Coordinates<br/>
if(mouse.x<=640 && mouse.y<480)<br/>
{<br/>
winX = (float)mouse.x; // Holds The Mouse X Coordinate<br/>
winY = (float)mouse.y; // Holds The Mouse Y Coordinate<br/>
<br/>
yrot = winX - xdiff;<br/>
xrot = winY - ydiff;<br/>
}<br/>
<br/>
gluLookAt(<br/>
0.0f, 0.0f, 3.0f,<br/>
0.0f, 0.0f, 0.0f,<br/>
0.0f, 1.0f, 0.0f);<br/>
glRotatef(xrot, 1.0f, 0.0f, 0.0f);<br/>
glRotatef(yrot, 0.0f, 1.0f, 0.0f);<br/>
<br/>
}
Im trying to call this idle method from Form1->Form1_MouseMove Event....

can anyone help me in this regard please....


Thanks





View the full article
 
Back
Top