Zoe Veale Lab 2 Draw2 part 1: I edited the glutinitwindowsize() function tom change the size of my screen window. int main(int argc, char** argv) glutinit(&argc, argv); //initialize toolkit glutinitdisplaymode (GLUT_SINGLE GLUT_RGB ); //set display mode: single bufferring RGB model glutinitwindowsize(200, 300); //set window size on screen glutinitwindowposition( 200, 200 ); //set window position on screen glutcreatewindow(argv[0]); //open screen window // glutcreatewindow( "CS 420 Draw" ); //open screen window init(); glutdisplayfunc (display); //points to display function glutmainloop(); //go into perpetual loop return 0; by modifying the glutinitwindowsize() function you can change the screen size to whatever you please.
Part 2: Similarly, by editing glutinitwindowposition() parameters you can move where the screen renders at, glutinitwindowposition(0,0) being the top left of the world window, but glutinitwindowposition(300, 350) being 300 pixels to the right (xaxis) and 350 pixels down (yaxis). int main(int argc, char** argv) glutinit(&argc, argv); //initialize toolkit glutinitdisplaymode (GLUT_SINGLE GLUT_RGB ); //set display mode: single bufferring RGB model glutinitwindowsize(500, 500); //set window size on screen glutinitwindowposition( 300, 350 ); //set window position on screen glutcreatewindow(argv[0]); //open screen window // glutcreatewindow( "CS 420 Draw" ); //open screen window init(); glutdisplayfunc (display); //points to display function glutmainloop(); //go into perpetual loop return 0; Further editing of the glutinitwindowpostion() function will change the position of the screen, even to move it off the world window entirely if the parameters are to high or in the negatives.
Part 3: Another function that can be edited for the users needs is gluortho2d(), it takes in two parameters that edit the display screen s (in the screen window) x and y axis. For example, gluortho2d(500, 500), means 500 pixels on the x-axis and 500 pixels o the y-axis. But, the bottom left of the screen window is considered to be (0,0). if you increase these values over 500 you will the the graphics that have been render to be shifted. void init( void ) glclearcolor( 1.0, 1.0, 1.0, 0.0 ); //get white background color glcolor3f( 0.0f, 1.0f, 0.0f ); //set drawing color glpointsize( 8.0 ); //a dot is 4x4 glmatrixmode( GL_PROJECTION ); glloadidentity(); //replace current matrix with identity matrix gluortho2d( 0.0, 1000.0, 0.0, 1000.0 );
part 4: continuing our editing of functions, in the top of our display function if we add, glviewport( ), it takes in 4 parameters that allows us to change where the rest of the display function is render. For example, adding: glviewport(150,200,250,100) shows: we basically make a window within our window we are rendering all of our primitives to. void display( void ) glviewport( 150, 200, 250, 100); //add a view port glclear( GL_COLOR_BUFFER_BIT ); //clear screen /* rest of code... */
Bresenham part: As discussed in class, I was able to implement the bresenham formula into my code thanks to the provided code. I edited it to make a Line() and Circle() function that each take in 4 parameters so I could draw more than one easily. void Draw() int x0, y0, xn, yn; int xcenter,ycenter,r; x0 = 0; y0 = 0; xn = 250; yn = 250; xcenter = 250; ycenter = 250; r = 20; glclear(gl_color_buffer_bit); //clear screen DrawLine(x0, y0, xn, yn); DrawCircle(xCenter, ycenter, r);
glflush(); void DrawLine(int x0, int y0, int xn, int yn) int x, y; int dx, dy, //deltas pk, //decision parameter k; //looping variable glcolor3f( 1,0, 0); setpixel(x0, y0); //plot first point // difference between starting and ending points dx = xn - x0; dy = yn - y0; pk = 2 * dy - dx; x = x0; y = y0; for ( k = 0; k < dx-1; ++k ) if ( pk < 0 ) pk = pk + 2 * dy; else pk = pk + 2*dy - 2*dx; ++y; ++x; setpixel( x, y ); //calculate next pk //next pixel: (x+1, y ) //next pixel: (x+1, y+1) //calculate next pk void DrawCircle(int xcenter, int ycenter, int r) int x, y; x=0; y=r; int d = 3/2 - r; // = 1 - r glcolor3f( 1,0, 0); while(x<=y) setpixel(xcenter+x,ycenter+y); setpixel(xcenter+y,ycenter+x); //find other points by symmetry setpixel(xcenter-x,ycenter+y); setpixel(xcenter+y,ycenter-x); setpixel(xcenter-x,ycenter-y);
setpixel(xcenter-y,ycenter-x); setpixel(xcenter+x,ycenter-y); setpixel(xcenter-y,ycenter+x); if (d<0) d += (2*x)+3; else d += (2*(x-y))+5; y -= 1; x++; I could also iterated through a loop while editing the parameters to make some interesting designs.
Summary: This lab helped me understand several of the functions that opengl and GLUT rely on to make the the library easy to use and understand. GlutInitWindowSize(), glutinitwindowposition(), gluortho2d(), and glviewport() are all powerful functions that are very simple in nature. Going over the Bresenham code following the lecture helped cement my understanding of how a pixel is filled in on the screen. And led to some interesting patters beign displayed on my screen window. After finishing every part of this lab and demonstrating my understanding of the code I believe I have earned full points on this lab.