Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

OpenGL: Moving a bezier curve . Please help

I'm new to openGL. I manipulated the following code to draw 3 4-point
bezier curves but i'll like to be able to move any of the control points with the mouse. Please help me explain case WM_LBUTTON_DOWN, BUTTON_UP and MOUSE_MOVE for my little prog.
Thanks in advance.
here the source


#include "math.h"
#include "main.h" // This includes our header file


bool g_bFullScreen = true; // Set full screen as default
HWND g_hWnd; // This is the handle for the window
RECT g_rRect; // This holds the window dimensions
HDC g_hDC; // General HDC - (handle to device context)
HGLRC g_hRC; // General OpenGL_DC - Our Rendering Context for OpenGL
HINSTANCE g_hInstance; // This holds the global hInstance for UnregisterClass() in DeInit()


static int checkMouseX = 0;
static int checkMouseY = 0;
static bool Mouse_on_Point = false;

#define POINT_SIZE = 8.0;
#define MAX_STEPS 50.0f // This is the amount of steps we want to draw the curve

// We create a class that has an X, Y and Z. We will use this point class
// to store our XYZ points of the curve.

struct CVector3 // Remember, we use a C in front to show it's a class
{
public:
float x, y, z; // We just want a float for a X Y and Z.
};

float g_CurrentTime = 0.0f; // This is the current position of the sphere along the curve (0 to 1)

CVector3 g_vStartPoint = {-3, 0, 0}; // This is the starting point of the curve
CVector3 g_vControlPoint1 = {-2, 3, 2}; // This is the first control point of the curve
CVector3 g_vControlPoint2 = { -1, 3, -2}; // This is the second control point of the curve
CVector3 g_vEndPoint = { -1, 1, 0}; // This is the end point of the curve


CVector3 g_2_vStartPoint = {-1, 1, 0}; // This is the starting point of the curve
CVector3 g_2_vControlPoint1 = {0, 3, 2}; // This is the first control point of the curve
CVector3 g_2_vControlPoint2 = { 3, 3, -2}; // This is the second control point of the curve
CVector3 g_2_vEndPoint = { 1, 1, 0};


CVector3 g_3_vStartPoint = {1, 1, 0}; // This is the starting point of the curve
CVector3 g_3_vControlPoint1 = {2, 3, 2}; // This is the first control point of the curve
CVector3 g_3_vControlPoint2 = { 4, 3, -2}; // This is the second control point of the curve
CVector3 g_3_vEndPoint = { 4, 1, 0};


///// This function initializes the window.

void Init(HWND hWnd)
{
g_hWnd = hWnd; // Assign the window handle to a global window handle
GetClientRect(g_hWnd, &g_rRect); // Assign the windows rectangle to a global RECT
InitializeOpenGL(g_rRect.right, g_rRect.bottom); // Init OpenGL with the global rect



glEnable(GL_LIGHT0); // Turn on this light
glEnable(GL_LIGHTING); // Turn lighting on
glEnable(GL_COLOR_MATERIAL); // Since lighting is on, allow glColor*() functions


}

///////////////////////////////// MAIN GAME LOOP \\\\\\\\\\\\\\\\*
/////
///// This function Handles the main game loop
/////
///////////////////////////////// MAIN GAME LOOP \\\\\\\\\\\\\\\\*

WPARAM MainLoop()
{
MSG msg;
bool bRedraw = false;

while(1) // Do our infinate loop
{ // Check if there was a message
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if(msg.message == WM_QUIT) // If the message wasnt to quit
break;
TranslateMessage(&msg); // Find out what the message does
DispatchMessage(&msg); // Execute the message
}
else // if there wasn't a message
{
RenderScene(); // Update the screen every frame
}
}

DeInit(); // Clean up and free all allocated memory

return(msg.wParam); // Return from the program
}


///////////////////////////////// POINT ON CURVE \\\\\\\\\\\\\\\\*
/////
///// This function returns an XYZ point along the curve, depending on t (0 to 1)
/////
///////////////////////////////// POINT ON CURVE \\\\\\\\\\\\\\\\*

CVector3 PointOnCurve(CVector3 p1, CVector3 p2, CVector3 p3, CVector3 p4, float t)
{
float var1, var2, var3;
CVector3 vPoint = {0.0f, 0.0f, 0.0f};


var1 = 1 - t;

// Store the (1 - t)^3 into a variable to cut down computation and create clean code
var2 = var1 * var1 * var1;

// Store the t^3 in a variable to cut down computation and create clean code
var3 = t * t * t;


vPoint.x = var2*p1.x + 3*t*var1*var1*p2.x + 3*t*t*var1*p3.x + var3*p4.x;
vPoint.y = var2*p1.y + 3*t*var1*var1*p2.y + 3*t*t*var1*p3.y + var3*p4.y;
vPoint.z = var2*p1.z + 3*t*var1*var1*p2.z + 3*t*t*var1*p3.z + var3*p4.z;

// Now we should have the point on the curve, so let's return it.
return(vPoint);
}



void bezier()
{
glColor3f(0.0, 1.0, 0.0); // Set the color to Green

CVector3 vPoint = {0.0f, 0.0f, 0.0f}; // Initialize a CVector3 to hold points.


glLineWidth(1.5); // Increase the size of a line for visibility


glBegin(GL_LINE_STRIP); // Start drawing lines


for(float t = 0; t <= (1 + (1.0f / MAX_STEPS)); t += 1.0f / MAX_STEPS)
{

vPoint = PointOnCurve(g_vStartPoint, g_vControlPoint1, g_vControlPoint2, g_vEndPoint, t);

// Draw the current point at distance "t" of the curve.
glVertex3f(vPoint.x, vPoint.y, vPoint.z);
}

glEnd();


glEnable(GL_LIGHTING); // Turn lighting back on



glPointSize(8.0);
glColor3f(0.0, 1.0, 1.0);

glBegin(GL_POINTS);
glVertex2f(g_vStartPoint.x, g_vStartPoint.y);
glVertex2f(g_vControlPoint1.x, g_vControlPoint1.y);
glVertex2f(g_vControlPoint2.x, g_vControlPoint2.y);
glVertex2f(g_vEndPoint.x, g_vEndPoint.y);

glEnd();




glColor3f(1.0, 0.0, 0.0);


glBegin(GL_LINES);
glVertex2f(g_vStartPoint.x, g_vStartPoint.y);
glVertex2f(g_vControlPoint1.x, g_vControlPoint1.y);
glVertex2f(g_vControlPoint2.x, g_vControlPoint2.y);
glVertex2f(g_vEndPoint.x, g_vEndPoint.y);
glEnd();


}


void bezier1()
{
glColor3f(0.0, 1.0, 0.0); // Set the color to Green

CVector3 vPoint = {0.0f, 0.0f, 0.0f}; // Initialize a CVector3 to hold points.


glLineWidth(1.5); // Increase the size of a line for visibility


glBegin(GL_LINE_STRIP); // Start drawing lines


for(float t = 0; t <= (1 + (1.0f / MAX_STEPS)); t += 1.0f / MAX_STEPS)
{

vPoint = PointOnCurve(g_2_vStartPoint, g_2_vControlPoint1, g_2_vControlPoint2, g_2_vEndPoint, t);

// Draw the current point at distance "t" of the curve.
glVertex3f(vPoint.x, vPoint.y, vPoint.z);
}

glEnd();


glEnable(GL_LIGHTING); // Turn lighting back on



glPointSize(8.0);
glColor3f(0.0, 1.0, 1.0);

glBegin(GL_POINTS);
glVertex2f(g_2_vStartPoint.x, g_2_vStartPoint.y);
glVertex2f(g_2_vControlPoint1.x, g_2_vControlPoint1.y);
glVertex2f(g_2_vControlPoint2.x, g_2_vControlPoint2.y);
glVertex2f(g_2_vEndPoint.x, g_2_vEndPoint.y);

glEnd();




glColor3f(1.0, 0.0, 0.0);


glBegin(GL_LINES);
glVertex2f(g_2_vStartPoint.x, g_2_vStartPoint.y);
glVertex2f(g_2_vControlPoint1.x, g_2_vControlPoint1.y);
glVertex2f(g_2_vControlPoint2.x, g_2_vControlPoint2.y);
glVertex2f(g_2_vEndPoint.x, g_2_vEndPoint.y);
glEnd();


}


void bezier2()
{
glColor3f(0.0, 1.0, 0.0); // Set the color to Green

CVector3 vPoint = {0.0f, 0.0f, 0.0f}; // Initialize a CVector3 to hold points.


glLineWidth(1.5); // Increase the size of a line for visibility


glBegin(GL_LINE_STRIP); // Start drawing lines


for(float t = 0; t <= (1 + (1.0f / MAX_STEPS)); t += 1.0f / MAX_STEPS)
{

vPoint = PointOnCurve(g_3_vStartPoint, g_3_vControlPoint1, g_3_vControlPoint2, g_3_vEndPoint, t);

// Draw the current point at distance "t" of the curve.
glVertex3f(vPoint.x, vPoint.y, vPoint.z);
}

glEnd();


glEnable(GL_LIGHTING); // Turn lighting back on



glPointSize(8.0);
glColor3f(0.0, 1.0, 1.0);

glBegin(GL_POINTS);
glVertex2f(g_3_vStartPoint.x, g_3_vStartPoint.y);
glVertex2f(g_3_vControlPoint1.x, g_3_vControlPoint1.y);
glVertex2f(g_3_vControlPoint2.x, g_3_vControlPoint2.y);
glVertex2f(g_3_vEndPoint.x, g_3_vEndPoint.y);

glEnd();




glColor3f(1.0, 0.0, 0.0);


glBegin(GL_LINES);
glVertex2f(g_3_vStartPoint.x, g_3_vStartPoint.y);
glVertex2f(g_3_vControlPoint1.x, g_3_vControlPoint1.y);
glVertex2f(g_3_vControlPoint2.x, g_3_vControlPoint2.y);
glVertex2f(g_3_vEndPoint.x, g_3_vEndPoint.y);
glEnd();


}



void RenderScene()
{


glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
glLoadIdentity(); // Reset The matrix

// Position View Up Vector
gluLookAt(0, 0.5f, 10.0f, 0, 0.5f, 0, 0, 1, 0); // Set up our camera position and view

// Below we disable the lighting so you can clearly see the bezier curve.

glDisable(GL_LIGHTING); // Disable lighting for now

bezier();
//glTranslatef(-1.0, 1.0, 0.0);
bezier1();
bezier2();

SwapBuffers(g_hDC); // Swap the backbuffers to the foreground
}



LRESULT CALLBACK WinProc(HWND hWnd,UINT uMsg, WPARAM wParam, LPARAM lParam)
{
LONG lRet = 0;
PAINTSTRUCT ps;

int mouseX = 0;
int mouseY = 0;

switch (uMsg)
{
case WM_SIZE: // If the window is resized
if(!g_bFullScreen) // Do this only if we are NOT in full screen
{
SizeOpenGLScreen(LOWORD(lParam),HIWORD(lParam));// LoWord=Width, HiWord=Height
GetClientRect(hWnd, &g_rRect); // Get the window rectangle
}
break;

case WM_PAINT: // If we need to repaint the scene
BeginPaint(hWnd, &ps); // Init the paint struct
EndPaint(hWnd, &ps); // EndPaint, Clean up
break;
//Get mouse coordinate when the mouse is pressed down (left button)
case WM_LBUTTONDOWN:
mouseX = (lParam);
mouseY = (lParam);
if ((mouseX = g_vControlPoint1.x) || (mouseY = g_vControlPoint1.y)){
checkMouseX = mouseX - g_vControlPoint1.x;
checkMouseY = mouseY - g_vControlPoint1.y;
Mouse_on_Point = true;
}
break;

case WM_LBUTTONUP:
Mouse_on_Point = false;
break;

case WM_MOUSEMOVE:
mouseX = (lParam);
mouseY = (lParam);
if(Mouse_on_Point)
g_vControlPoint1.x += mouseX;
g_vControlPoint1.y += mouseY;
//bezier();
//SwapBuffers(g_hDC);
break;

case WM_CLOSE: // If the window is being closes
PostQuitMessage(0); // Send a QUIT Message to the window
break;

default: // Return by default
lRet = DefWindowProc (hWnd, uMsg, wParam, lParam);
break;
}

return lRet; // Return by default
}



Sign In or Register to comment.