YourfirsttaskistocreateanOpenGLprogramthatdisplaysacolorfulbrickata fixedlocationonthescreenusingthedisplaycallback. Todothis,youwillhaveto...

YourfirsttaskistocreateanOpenGLprogramthatdisplaysacolorfulbrickata fixedlocationonthescreenusingthedisplaycallback.Todothis,youwillhaveto calculatethe(x,y,z)locationsofall8corners ofthebrick,anddefinethe6facesof thebrickusingpolygonswithdifferentcolors. Onceyouhavethisworking,youcancreateamousecallback function thatcaptures the(x,y)locationofthemouse,andredrawsthecolorfulbrickatthatlocation. As youmoveyourmousearoundandclickindifferentlocations,thebrickshouldbe erasedfromthescreenandredrawninthenewlocation.Thekeytothisprocess willbeconvertingfromthe(x,y)windowcoordinatesinto(x,y,z)objectcoordinates fordisplaypurposes. Finally,youneedtocreateamotioncallback functionthatcapturesthe(x,y)window coordinatesofthemouseasyoudragitacrossthescreen.Asthemousemoves,you shouldredrawthebrickatthecorresponding(x,y,z)locationinobjectcoordinates. Fordebuggingpurposes,youmaywanttoprintoutthe(x,y)windowand(x,y,z) objectcoordinatesastheyarechanging.




I have the program that draws the brick so I need the part that uses a mouse to redraw it when you click


#include #include #include #ifdef MAC #include #else #include #endif // Global constants #define MIN_X_VIEW -50 #define MAX_X_VIEW 50 #define MIN_Y_VIEW -50 #define MAX_Y_VIEW 50 #define MIN_Z_VIEW -50 #define MAX_Z_VIEW 50 #define MIN_X_SCREEN 0 #define MAX_X_SCREEN 500 #define MIN_Y_SCREEN 0 #define MAX_Y_SCREEN 500 #define RECT_COUNT 1000 GLenum mode = GL_POLYGON; // Global variables int count = 0; float point[RECT_COUNT][4]; float color[RECT_COUNT][3]; //--------------------------------------- // Init function for OpenGL //--------------------------------------- void init() { glClearColor(0.0, 0.0, 0.0, 1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glOrtho(MIN_X_VIEW, MAX_X_VIEW, MIN_Y_VIEW, MAX_Y_VIEW, MIN_Z_VIEW, MAX_Z_VIEW); glEnable(GL_DEPTH_TEST); // Clear point array for (int i = 0; i < rect_count;="" i++)="" {="" for="" (int="" j="0;" j="">< 4;="" j++)="" point[i][j]="0.0;" for="" (int="" j="0;" j="">< 3;="" j++)="" color[i][j]="0.5;" }="" }="" draw="" bricks="" void="" cube(float="" midx,="" float="" midy,="" float="" midz,="" float="" size)="" {="" define="" 8="" vertices="" float="" ax="midx" -="" size="" ;="" float="" ay="midy" -="" size="" 2;="" float="" az="midz" +="" size="" 2;="" float="" bx="midx" +="" size="" ;="" float="" by="midy" -="" size="" 2;="" float="" bz="midz" +="" size="" 2;="" float="" cx="midx" +="" size="" ;="" float="" cy="midy" +="" size="" 2;="" float="" cz="midz" +="" size="" 2;="" float="" dx="midx" -="" size="" ;="" float="" dy="midy" +="" size="" 2;="" float="" dz="midz" +="" size="" 2;="" float="" ex="midx" -="" size="" ;="" float="" ey="midy" -="" size="" 2;="" float="" ez="midz" -="" size="" 2;="" float="" fx="midx" +="" size="" ;="" float="" fy="midy" -="" size="" 2;="" float="" fz="midz" -="" size="" 2;="" float="" gx="midx" +="" size="" ;="" float="" gy="midy" +="" size="" 2;="" float="" gz="midz" -="" size="" 2;="" float="" hx="midx" -="" size="" ;="" float="" hy="midy" +="" size="" 2;="" float="" hz="midz" -="" size="" 2;="" draw="" 6="" faces="" glbegin(mode);="" glcolor3f(1.0,="" 0.0,="" 0.0);="" glvertex3f(ax,="" ay,="" az);="" glvertex3f(bx,="" by,="" bz);="" glvertex3f(cx,="" cy,="" cz);="" glvertex3f(dx,="" dy,="" dz);="" glend();="" glbegin(mode);="" glcolor3f(4.0,="" 1.0,="" 4.0);="" glvertex3f(ax,="" ay,="" az);="" glvertex3f(dx,="" dy,="" dz);="" glvertex3f(hx,="" hy,="" hz);="" glvertex3f(ex,="" ey,="" ez);="" glend();="" glbegin(mode);="" glcolor3f(0.0,="" 0.0,="" 1.0);="" glvertex3f(ax,="" ay,="" az);="" glvertex3f(ex,="" ey,="" ez);="" glvertex3f(fx,="" fy,="" fz);="" glvertex3f(bx,="" by,="" bz);="" glend();="" glbegin(mode);="" glcolor3f(0.0,="" 1.0,="" 1.0);="" glvertex3f(gx,="" gy,="" gz);="" glvertex3f(fx,="" fy,="" fz);="" glvertex3f(ex,="" ey,="" ez);="" glvertex3f(hx,="" hy,="" hz);="" glend();="" glbegin(mode);="" glcolor3f(1.0,="" 0.0,="" 1.0);="" glvertex3f(gx,="" gy,="" gz);="" glvertex3f(cx,="" cy,="" cz);="" glvertex3f(bx,="" by,="" bz);="" glvertex3f(fx,="" fy,="" fz);="" glend();="" glbegin(mode);="" glcolor3f(1.0,="" 1.0,="" 0.0);="" glvertex3f(gx,="" gy,="" gz);="" glvertex3f(hx,="" hy,="" hz);="" glvertex3f(dx,="" dy,="" dz);="" glvertex3f(cx,="" cy,="" cz);="" glend();="" }="" ---------------------------------------="" mouse="" callback="" for="" opengl="" ---------------------------------------="" void="" mouse(int="" button,="" int="" state,="" int="" x,="" int="" y)="" {="" calculate="" scale="" factors="" float="" x_scale="(MAX_X_VIEW" -="" min_x_view)="" (float)(max_x_screen="" -="" min_x_screen);="" float="" y_scale="(MIN_Y_VIEW" -="" max_y_view)="" (float)(max_y_screen="" -="" min_y_screen);="" handle="" mouse="" down="" if="" (state="=" glut_down)="" {="" point[count][0]="MIN_X_VIEW" +="" (x="" -="" min_x_screen)="" *="" x_scale;="" point[count][1]="MAX_Y_VIEW" +="" (y="" -="" min_y_screen)="" *="" y_scale;="" }="" handle="" mouse="" up="" else="" if="" (state="=" glut_up)="" {="" point[count][2]="MIN_X_VIEW" +="" (x="" -="" min_x_screen)="" *="" x_scale;="" point[count][3]="MAX_Y_VIEW" +="" (y="" -="" min_y_screen)="" *="" y_scale;="" count++;="" glutpostredisplay();="" }="" }="" ---------------------------------------="" motion="" callback="" for="" opengl="" ---------------------------------------="" void="" motion(int="" x,="" int="" y)="" {="" calculate="" scale="" factors="" float="" x_scale="(MAX_X_VIEW" -="" min_x_view)="" (float)(max_x_screen="" -="" min_x_screen);="" float="" y_scale="(MIN_Y_VIEW" -="" max_y_view)="" (float)(max_y_screen="" -="" min_y_screen);="" handle="" mouse="" motion="" point[count][2]="MIN_X_VIEW" +="" (x="" -="" min_x_screen)="" *="" x_scale;="" point[count][3]="MAX_Y_VIEW" +="" (y="" -="" min_y_screen)="" *="" y_scale;="" glutpostredisplay();="" }="" ---------------------------------------="" display="" callback="" for="" opengl="" ---------------------------------------="" void="" display()="" {="" glclear(gl_color_buffer_bit);="" glmatrixmode(gl_modelview);="" glloadidentity();="" draw="" all="" cubes="" for="" (int="" i="count;" i="">=0; i--) if ((point[i][0] != point[i][2]) || (point[i][1] != point[i][3])) cube(point[i][0], point[i][1], point[i][2], point[i][3]); glFlush(); } //--------------------------------------- // Main program //--------------------------------------- int main(int argc, char *argv[]) { glutInit(&argc, argv); glutInitWindowSize(MAX_Y_SCREEN, MAX_X_SCREEN); glutInitWindowPosition(MAX_Y_SCREEN / 2, MAX_X_SCREEN / 2); glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE); glutCreateWindow("Cube"); glutDisplayFunc(display); glutMouseFunc(mouse); glutMotionFunc(motion); init(); glutMainLoop(); return 0; }
Feb 12, 2021
SOLUTION.PDF

Get Answer To This Question

Submit New Assignment

Copy and Paste Your Assignment Here