Question
How do i modify this code so it moves (translates) multiple verticies instead of just one. (OPENGL)(C++). E.G., i want to move this object: dcPt
How do i modify this code so it moves (translates) multiple verticies instead of just one. (OPENGL)(C++).
E.G., i want to move this object:
dcPt vertices1[5]; vertices1[0].x = 32; vertices1[0].y = 40; vertices1[1].x = 92; vertices1[1].y = 28; vertices1[2].x = 128; vertices1[2].y = 100; vertices1[3].x = 68; vertices1[3].y = 76; vertices1[4].x = 12; vertices1[4].y = 124;
Here is the code:
#include "Display_4.h" #include "glut.h" #include #include #include "dcPt.h"
// For accessing OpenCV ... #include #include #include "opencv2/imgproc/imgproc.hpp" #include
using namespace cv; using namespace std; ///////////////////////
extern void winReshapeFcn(GLint newWidth, GLint newHeight); extern GLsizei winWidth, winHeight; extern void drawWorldFrame(void);
Display_4::Display_4(void) { }
Display_4::~Display_4(void) { }
void Display_4::Draw(int *viewing) { //// Section 1 /////////////////////////////////////////////// //// Do not change/remove any function in Section 1. //////// glClear (GL_COLOR_BUFFER_BIT); // Clear display window. glClear (GL_DEPTH_BUFFER_BIT); // Clear the depth (z) buffer glDisable (GL_DEPTH_TEST); // Disable the depth test glDisable(GL_LIGHTING); // Disable lighting glutSetWindowTitle("Display 4"); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); //////////////////////////////////////////////////////////////
//// Section 2 /////////////////////////////////////////////// //// You may select a display method: *viewing = 2, 3 or 4. // //// Do not change/remove anything else in Section 2. //////// *viewing = 2; // Decide the display method: 2 = 2D display; 3 = 3D orthogonal parrallel projection; 4 = 3D perspective projection; winReshapeFcn(winWidth, winHeight); // Show the display/projection now. //////////////////////////////////////////////////////////////
//// Section 3 /////////////////////////////////////////////// /*******************************/ // Place your drawing code below. // Note: // For 3D display: call the following function after the projection parameters are set. // drawWorldFrame(); // Draw the World coordinate frame based on the current projection setting // For 2D display: call this function below. /*******************************/ drawWorldFrame(); // Draw the World coordinate frame based on the current projection setting
// Set drawing color (R, G, B) to black. glColor3ub(0, 0, 0);
// Translate a line segment { double r11, r12, tx, r21, r22, ty, x, y, x1, y1; int count;
// A line between two points dcPt vertices1[2]; vertices1[0].x = 32; vertices1[0].y = 40; vertices1[1].x = 92; vertices1[1].y = 28;
// Translate the line to a new location dcPt vertices11[2]; r11 = 1.0; r12 = 0.0; r21 = 0.0; r22 = 1.0; double xFinal = 200, yFinal = 200; //Final location of the line int m, step, totalSteps = 80; // Total steps for the movement
for (step = 0; step < totalSteps; step++) { tx = step * xFinal / totalSteps; ty = step * yFinal / totalSteps;
for (count = 0; count < 2; count++) { x = (double)vertices1[count].x; y = (double)vertices1[count].y; transform(r11, r12, tx, r21, r22, ty, x, y, &x1, &y1); vertices11[count].x = (int)x1; vertices11[count].y = (int)y1; }
glClearColor(1.0, 1.0, 1.0, 1.0); // Clear the background and set it to white glClear(GL_COLOR_BUFFER_BIT); // Show the background colour
drawWorldFrame(); // Draw the World coordinate frame based on the current projection setting
// Show the line at a new location glColor3ub(0, 0, 0); // Set drawing color (R, G, B) to black. glBegin(GL_LINES); glVertex2i(vertices11[0].x, vertices11[0].y); glVertex2i(vertices11[1].x, vertices11[1].y); glEnd();
glFlush(); // Show the result for (int m = 0; m < 5000000; m++) int wait = 0; // Wait for sometime ... } }
//// Section 4 /////////////////////////////////////////////// //// Do not delete the following function //////////////////// glFlush ( ); // Show the result now ////////////////////////////////////////////////////////////// }
/* A generic function for 2D transformation. Original point = (x, y); Transformed point = (x1, y1). For example, for translation with amounts tx = 10 and ty = 20, the following code may be used. double r11 = 1.0, r12 = 0.0, tx = 10.0; double r21 = 0.0, r22 = 1.0, ty = 20.0; double x = 30.0, y = 40.0; // Original point = (30.0, 40.0) (as an example). double x1, y1; // The transformed point will be stored in (x1, y1). transform(r11, r12, tx, r21, r22, ty, x, y, &x1, &y1); // Calculate the transformed point and store it in (x1, y1). */ void Display_4::transform(double r11, double r12, double tx, double r21, double r22, double ty, double x, double y, double * x1, double * y1) { /* [ x1 ] [ r11 r12 tx ] [ x ] | y1 | = | r21 r22 ty | | y | [ 1 ] [ 0 0 1 ] [ 1 ] */ double xx = r11 * x + r12 * y + tx; double yy = r21 * x + r22 * y + ty; *x1 = xx; *y1 = yy; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started