I made this project for assignment for my Computer Graphics course. This is a program like a 'Paint'. I can draw line, points, polygons as clicking points on the window. And also, I can change the color of them(RGB + Alpha) and size of lines(or points)
This is how my program looks like. On the top there are UI buttons. User clicks the button to change the color, size, type of drawing etc.
Each drawings are saved with struct 'Polygon' which have points(Pts), color information(as a array), type of drawing(will be lines, points, or polygon etc). Variable 'ptsize' means when its' type is points, the size of point. Similary, variable 'linesize' means when its' type is line(line, lineloop, polygon, linestrip), the size of lines.
And here is settings of the world and 'main' function of the C++ project.
This is first part of display callback function 'Render' Here, I activated blending mode and initialized color. And with 'for' loop, I draw all members of a array 'PolyList' using switch sentence following each member's type.
This is second part of 'Render' function, which draw preview of current drawing. So 'Render' function which consists of 2 part is similar with previous project that I posted just before in same category(see this: http://busterworld.tistory.com/12)
So here in the second part, if user is clicked 1st point, program draws a preview of object with points that Polygon struct already have and current mouse position. Then, I drew UI on the top with 'drawPalette()' function.
This is mouse callback function, which will be called when user click with mouse. If button clicked is left button, the point information will be saved in temporary point struct variable. Then, if position of the point is in one of the buttons on top UI, proper variables will be changed following which button is clicked.
And when user didn't clicked any button on UI, the point will be added in temporary Polygon struct 'TmpPolygon'. This will be used when Render function draw preview.
This is end of mouse function. When user clicked right button of mouse, this means user finished drawing one object. So temporary polygon will be added in the PolyList and TmpPolygon will be initialized. And I printed current alpha value as window title.
This is MouseMotion function, which is called when user move mouse cursor. This will save current position of mouse in realtimePt Point struct variable.
For UI, I draw several quads as buttons. This is the code
void drawPalette()
{
glLineWidth(1);
//Draw UI
glColor3f(1, 1, 1);
glBegin(GL_QUADS);
glVertex3f(xmin + 0 * 20, ymax - 20, 1);
glVertex3f(xmin + 40 * 20, ymax - 20, 1);
glVertex3f(xmin + 40 * 20, ymax, 1);
glVertex3f(xmin + 0 * 20, ymax, 1);
//Red Palette
glColor3f(1, 0, 0);
glBegin(GL_QUADS);
glVertex3f(xmin + 0 * 20, ymax - 20, 1);
glVertex3f(xmin + 1 * 20, ymax - 20, 1);
glVertex3f(xmin + 1 * 20, ymax, 1);
glVertex3f(xmin + 0 * 20, ymax, 1);
glEnd();
//Green Palette
glColor3f(0, 1, 0);
glBegin(GL_QUADS);
glVertex3f(xmin + 1 * 20, ymax - 20, 1);
glVertex3f(xmin + 2 * 20, ymax - 20, 1);
glVertex3f(xmin + 2 * 20, ymax, 1);
glVertex3f(xmin + 1 * 20, ymax, 1);
glEnd();
//Blue Palette
glColor3f(0, 0, 1);
glBegin(GL_QUADS);
glVertex3f(xmin + 2 * 20, ymax - 20, 1);
glVertex3f(xmin + 3 * 20, ymax - 20, 1);
glVertex3f(xmin + 3 * 20, ymax, 1);
glVertex3f(xmin + 2 * 20, ymax, 1);
glEnd();
//Yellow Palette
glColor3f(1, 1, 0);
glBegin(GL_QUADS);
glVertex3f(xmin + 3 * 20, ymax - 20, 1);
glVertex3f(xmin + 4 * 20, ymax - 20, 1);
glVertex3f(xmin + 4 * 20, ymax, 1);
glVertex3f(xmin + 3 * 20, ymax, 1);
glEnd();
//Violet Palette
glColor3f(1, 0, 1);
glBegin(GL_QUADS);
glVertex3f(xmin + 4 * 20, ymax - 20, 1);
glVertex3f(xmin + 5 * 20, ymax - 20, 1);
glVertex3f(xmin + 5 * 20, ymax, 1);
glVertex3f(xmin + 4 * 20, ymax, 1);
glEnd();
//Skyblue Palette
glColor3f(0, 1, 1);
glBegin(GL_QUADS);
glVertex3f(xmin + 5 * 20, ymax - 20, 1);
glVertex3f(xmin + 6 * 20, ymax - 20, 1);
glVertex3f(xmin + 6 * 20, ymax, 1);
glVertex3f(xmin + 5 * 20, ymax, 1);
glEnd();
//White Palette
glColor3f(1, 1, 1);
glBegin(GL_QUADS);
glVertex3f(xmin + 6 * 20, ymax - 20, 1);
glVertex3f(xmin + 7 * 20, ymax - 20, 1);
glVertex3f(xmin + 7 * 20, ymax, 1);
glVertex3f(xmin + 6 * 20, ymax, 1);
glEnd();
//Black Palette
glColor3f(0, 0, 0);
glBegin(GL_QUADS);
glVertex3f(xmin + 7 * 20, ymax - 20, 1);
glVertex3f(xmin + 8 * 20, ymax - 20, 1);
glVertex3f(xmin + 8 * 20, ymax, 1);
glVertex3f(xmin + 7 * 20, ymax, 1);
glEnd();
//Alpha Up
glColor3f(1, 1, 1);
glBegin(GL_QUADS);
glVertex3f(xmin + 8 * 20, ymax - 20, 1);
glVertex3f(xmin + 9 * 20, ymax - 20, 1);
glVertex3f(xmin + 9 * 20, ymax, 1);
glVertex3f(xmin + 8 * 20, ymax, 1);
glEnd();
glColor3f(0, 0, 0);
glBegin(GL_LINES);
glVertex3f(xmin + 9 * 20, ymax - 20, 1);
glVertex3f(xmin + 9 * 20, ymax, 1);
glVertex3f(xmin + 8.25 * 20, ymax - 12, 1);
glVertex3f(xmin + 8.5 * 20, ymax - 8, 1);
glVertex3f(xmin + 8.5 * 20, ymax - 8, 1);
glVertex3f(xmin + 8.75 * 20, ymax - 12, 1);
glEnd();
//Alpha Down
glColor3f(1, 1, 1);
glBegin(GL_QUADS);
glVertex3f(xmin + 9 * 20, ymax - 20, 1);
glVertex3f(xmin + 10 * 20, ymax - 20, 1);
glVertex3f(xmin + 10 * 20, ymax, 1);
glVertex3f(xmin + 9 * 20, ymax, 1);
glEnd();
glColor3f(0, 0, 0);
glBegin(GL_LINES);
glVertex3f(xmin + 10 * 20, ymax - 20, 1);
glVertex3f(xmin + 10 * 20, ymax, 1);
glVertex3f(xmin + 9.25 * 20, ymax - 8, 1);
glVertex3f(xmin + 9.5 * 20, ymax - 12, 1);
glVertex3f(xmin + 9.5 * 20, ymax - 12, 1);
glVertex3f(xmin + 9.75 * 20, ymax - 8, 1);
glEnd();
//Draw Circle
glColor3f(1, 1, 1);
glBegin(GL_QUADS);
glVertex3f(xmin + 10 * 20, ymax - 20, 1);
glVertex3f(xmin + 11 * 20, ymax - 20, 1);
glVertex3f(xmin + 11 * 20, ymax, 1);
glVertex3f(xmin + 10 * 20, ymax, 1);
glEnd();
glColor3f(0, 0, 0);
drawCircle(xmin + 10.5 * 20, ymax - 10, 5);
//Draw Polygon
glColor3f(1, 1, 1);
glBegin(GL_QUADS);
glVertex3f(xmin + 11 * 20, ymax - 20, 1);
glVertex3f(xmin + 12 * 20, ymax - 20, 1);
glVertex3f(xmin + 12 * 20, ymax, 1);
glVertex3f(xmin + 11 * 20, ymax, 1);
glEnd();
glColor3f(0, 0, 0);
glBegin(GL_QUADS);
glVertex3f(xmin + 11.25 * 20, ymax - 15, 1);
glVertex3f(xmin + 11.75 * 20, ymax - 15, 1);
glVertex3f(xmin + 11.75 * 20, ymax - 5, 1);
glVertex3f(xmin + 11.25 * 20, ymax - 5, 1);
glEnd();
//Draw Line Loop
glColor3f(1, 1, 1);
glBegin(GL_QUADS);
glVertex3f(xmin + 12 * 20, ymax - 20, 1);
glVertex3f(xmin + 13 * 20, ymax - 20, 1);
glVertex3f(xmin + 13 * 20, ymax, 1);
glVertex3f(xmin + 12 * 20, ymax, 1);
glEnd();
glColor3f(0, 0, 0);
glBegin(GL_LINE_LOOP);
glVertex3f(xmin + 12.25 * 20, ymax - 15, 1);
glVertex3f(xmin + 12.75 * 20, ymax - 15, 1);
glVertex3f(xmin + 12.75 * 20, ymax - 5, 1);
glVertex3f(xmin + 12.25 * 20, ymax - 5, 1);
glEnd();
//Draw Line Strip
glColor3f(1, 1, 1);
glBegin(GL_QUADS);
glVertex3f(xmin + 13 * 20, ymax - 20, 1);
glVertex3f(xmin + 14 * 20, ymax - 20, 1);
glVertex3f(xmin + 14 * 20, ymax, 1);
glVertex3f(xmin + 13 * 20, ymax, 1);
glEnd();
glColor3f(0, 0, 0);
glBegin(GL_LINE_STRIP);
glVertex3f(xmin + 13.75 * 20, ymax - 15, 1);
glVertex3f(xmin + 13.25 * 20, ymax - 15, 1);
glVertex3f(xmin + 13.75 * 20, ymax - 5, 1);
glVertex3f(xmin + 13.25 * 20, ymax - 5, 1);
glEnd();
//Draw Lines
glColor3f(1, 1, 1);
glBegin(GL_QUADS);
glVertex3f(xmin + 14 * 20, ymax - 20, 1);
glVertex3f(xmin + 15 * 20, ymax - 20, 1);
glVertex3f(xmin + 15 * 20, ymax, 1);
glVertex3f(xmin + 14 * 20, ymax, 1);
glEnd();
glColor3f(0, 0, 0);
glLineWidth(0.5);
glBegin(GL_LINES);
glVertex3f(xmin + 14.25 * 20, ymax - 17, 1);
glVertex3f(xmin + 14.75 * 20, ymax - 3, 1);
glEnd();
//Line size Down
glColor3f(1, 1, 1);
glBegin(GL_QUADS);
glVertex3f(xmin + 15 * 20, ymax - 20, 1);
glVertex3f(xmin + 16 * 20, ymax - 20, 1);
glVertex3f(xmin + 16 * 20, ymax, 1);
glVertex3f(xmin + 15 * 20, ymax, 1);
glEnd();
glColor3f(0, 0, 0);
glLineWidth(3);
glBegin(GL_LINES);
glVertex3f(xmin + 15.25 * 20, ymax - 10, 1);
glVertex3f(xmin + 15.75 * 20, ymax - 10, 1);
glEnd();
//Line size Up
glColor3f(1, 1, 1);
glBegin(GL_QUADS);
glVertex3f(xmin + 16 * 20, ymax - 20, 1);
glVertex3f(xmin + 17 * 20, ymax - 20, 1);
glVertex3f(xmin + 17 * 20, ymax, 1);
glVertex3f(xmin + 16 * 20, ymax, 1);
glEnd();
glColor3f(0, 0, 0);
glLineWidth(0.5);
glBegin(GL_LINES);
glVertex3f(xmin + 16.25 * 20, ymax - 10, 1);
glVertex3f(xmin + 16.75 * 20, ymax - 10, 1);
glEnd();
//Draw Point (size Up)
glColor3f(1, 1, 1);
glBegin(GL_QUADS);
glVertex3f(xmin + 17 * 20, ymax - 20, 1);
glVertex3f(xmin + 18 * 20, ymax - 20, 1);
glVertex3f(xmin + 17 * 20, ymax, 1);
glVertex3f(xmin + 18 * 20, ymax, 1);
glEnd();
glColor3f(0, 0, 0);
/*glBegin(GL_LINES);
glVertex3f(xmin + 17 * 20, ymax - 20, 1);
glVertex3f(xmin + 17 * 20, ymax, 1);
glVertex3f(xmin + 16.25 * 20, ymax - 12, 1);
glVertex3f(xmin + 16.5 * 20, ymax - 8, 1);
glVertex3f(xmin + 16.5 * 20, ymax - 8, 1);
glVertex3f(xmin + 16.75 * 20, ymax - 12, 1);
glEnd();*/
glPointSize(4.0);
glBegin(GL_POINTS);
glVertex3f(xmin + 17.5 * 20, ymax - 10, 1);
glEnd();
//Draw Point (size Down)
glColor3f(1, 1, 1);
glBegin(GL_QUADS);
glVertex3f(xmin + 18 * 20, ymax - 20, 1);
glVertex3f(xmin + 19 * 20, ymax - 20, 1);
glVertex3f(xmin + 19 * 20, ymax, 1);
glVertex3f(xmin + 18 * 20, ymax, 1);
glEnd();
glColor3f(0, 0, 0);
/*glBegin(GL_LINES);
glVertex3f(xmin + 18 * 20, ymax - 20, 1);
glVertex3f(xmin + 18 * 20, ymax, 1);
glVertex3f(xmin + 17.25 * 20, ymax - 8, 1);
glVertex3f(xmin + 17.5 * 20, ymax - 12, 1);
glVertex3f(xmin + 17.5 * 20, ymax - 12, 1);
glVertex3f(xmin + 17.75 * 20, ymax - 8, 1);
glEnd();*/
glPointSize(1.0);
glBegin(GL_POINTS);
glVertex3f(xmin + 18.5 * 20, ymax - 10, 1);
glEnd();
//Reset
glColor3f(1, 1, 1);
glBegin(GL_QUADS);
glVertex3f(xmin + 19 * 20, ymax - 20, 1);
glVertex3f(xmin + 20 * 20, ymax - 20, 1);
glVertex3f(xmin + 20 * 20, ymax, 1);
glVertex3f(xmin + 19 * 20, ymax, 1);
glEnd();
glColor3f(0, 0, 0);
glBegin(GL_LINES);
glVertex3f(xmin + 19.3 * 20, ymax - 5, 1);
glVertex3f(xmin + 19.3 * 20, ymax - 17, 1);
glVertex3f(xmin + 19.3 * 20, ymax - 10, 1);
glVertex3f(xmin + 19.8 * 20, ymax - 17, 1);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex3f(xmin + 19.25 * 20, ymax - 5, 1);
glVertex3f(xmin + 19.8 * 20, ymax - 5, 1);
glVertex3f(xmin + 19.8 * 20, ymax - 10, 1);
glVertex3f(xmin + 19.3 * 20, ymax - 10, 1);
glEnd();
}
This is my function that draws a circle. It draws a circle which radius size is r, at (x, y)
'작업물(Works) > ComputerGraphics' 카테고리의 다른 글
빛 정리(Ambient, Diffuse, Specular, Emissive) (0) | 2022.04.19 |
---|---|
[OpenGL]Mouse Callback Function (0) | 2016.07.08 |
[OpenGL]First OpenGL Project (0) | 2016.07.08 |
[OpenGL]First Step to Computer Graphics (0) | 2016.07.08 |