I made OpenGL Project that draws lines. User click two points in the window, and line is drawed with them. Also, when user click 1st point, I drew line in real time that connect 1st point and current mouse position. So, user can see the line which will be made before clicking 2nd point.


This is 'main' function of the project.




I made 'line' struct, and array of lines. line struct contains 4 integer variables that represent 2 points (x1, y1), (x2, y2) and 3 float variables that represent RGB color of the line



In display callback function 'Render()', I drew all lines in the array lines that user created before, and then if user clicked 1st point of new line(when variable draw is true), draw a line connecting that point(tmp[0], tmp[1]) and current mouse position(tmp[2], tmp[3])



In mouse callback function "MouseFunc" which means user clicked certain position for point,


when the clicked point is 1st point of the line,




set line's RGB color for random value, and variable 'draw' as true. And save the coordinate of the point in array 'tmp[0]', 'tmp[1]'.


BTW when the clicked point is 2nd point of the line,



set 'draw' variable as false and save all information about temporary line into new member of array 'lines'





And for information of mouse position in real time, I used glutPassiveMotionFunc callback function 'MouseMotion'. In this function, I set coordinates of mouse position into tmp[2], tmp[3] and call Render function. So in 'render' function, Computer will draw a line with mouse cursor after 1st point of new line is clicked.



+ Recent posts