Introduction to OpenGL: Part 2
Introduction to OpenGL: Part 2 A more complex example recursive refinement
Introduction to OpenGL: Part 2 A more complex example recursive refinement Can OpenGL draw continuous curves?
Introduction to OpenGL: Part 2 A more complex example recursive refinement Can OpenGL draw continuous curves? Yes, by segmenting the curve into a large number of straight-line segments.
Introduction to OpenGL: Part 2 A more complex example recursive refinement Can OpenGL draw continuous curves? Yes, by segmenting the curve into a large number of straight-line segments. The following code draws a simple outline of a square, using 4 lines. The square is centered at the origin, and has a radius of 1. (It is a crude approximation to a circle.)
Introduction to OpenGL: Part 2 A more complex example recursive refinement Can OpenGL draw continuous curves? Yes, by segmenting the curve into a large number of straight-line segments. The following code draws a simple outline of a square, using 4 lines. The square is centered at the origin, and has a radius of 1. (It is a crude approximation to a circle.) The program then recursively finds the center of each of the lines, and normalizes the distance from the origin to 1, making successively better approximations to a square.
Introduction to OpenGL: Part 2 A more complex example recursive refinement Can OpenGL draw continuous curves? Yes, by segmenting the curve into a large number of straight-line segments. The following code draws a simple outline of a square, using 4 lines. The square is centered at the origin, and has a radius of 1. (It is a crude approximation to a circle.) The program then recursively finds the center of each of the lines, and normalizes the distance from the origin to 1, making successively better approximations to a square. The program also parses a simple command line argument (an integer) which it uses to set the level of recursion.
Introduction to OpenGL: Part 2 A more complex example recursive refinement Can OpenGL draw continuous curves? Yes, by segmenting the curve into a large number of straight-line segments. The following code draws a simple outline of a square, using 4 lines. The square is centered at the origin, and has a radius of 1. (It is a crude approximation to a circle.) The program then recursively finds the center of each of the lines, and normalizes the distance from the origin to 1, making successively better approximations to a square. The program also parses a simple command line argument (an integer) which it uses to set the level of recursion. (The C language has I/O functions which are quite different from JAVA or C++.)
Introduction to OpenGL: Part 2 A more complex example recursive refinement Can OpenGL draw continuous curves? Yes, by segmenting the curve into a large number of straight-line segments. The following code draws a simple outline of a square, using 4 lines. The square is centered at the origin, and has a radius of 1. (It is a crude approximation to a circle.) The program then recursively finds the center of each of the lines, and normalizes the distance from the origin to 1, making successively better approximations to a square. The program also parses a simple command line argument (an integer) which it uses to set the level of recursion. (The C language has I/O functions which are quite different from JAVA or C++.) 1 1
/* recursive_circle.c * This is a simple, introductory OpenGL program. * It draws a circle on a white background by recursively * subdividing the sides of a square and renormalizing * the distance from the origin. */ #include <stdio.h> #include <stdlib.h> #include <math.h> #include <GL/glut.h> int n=0; \\ the number of levels of recursion
/* recursive_circle.c * This is a simple, introductory OpenGL program. * It draws a circle on a white background by recursively * subdividing the sides of a square and renormalizing * the distance from the origin. */ #include <stdio.h> #include <stdlib.h> #include <math.h> #include <GL/glut.h> int n=0; \\ the number of levels of recursion void init (void) { /* select clearing color as white */ glclearcolor (1.0, 1.0, 1.0, 0.0); /* initialize viewing values */ glmatrixmode(gl_projection); glloadidentity(); glortho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); } 2
// the recursive function void drawside(float x0,float y0,float x1,float y1,int n) { float xm, ym, norm; if (n <= 0) { // the stopping condition glbegin(gl_lines); // drawing only done here glvertex3f(x0,y0,0.0); glvertex3f(x1,y1,0.0); glend(); }
// the recursive function void drawside(float x0,float y0,float x1,float y1,int n) { float xm, ym, norm; if (n <= 0) { glbegin(gl_lines); glvertex3f(x0,y0,0.0); glvertex3f(x1,y1,0.0); glend(); } else{ n--; xm = 0.5*(x0 + x1); ym = 0.5*(y0 + y1); // the stopping condition // drawing only done here // the recursion // decriment counter // calculate midpoints
// the recursive function void drawside(float x0,float y0,float x1,float y1,int n) { float xm, ym, norm; if (n <= 0) { glbegin(gl_lines); glvertex3f(x0,y0,0.0); glvertex3f(x1,y1,0.0); glend(); } else{ n--; xm = 0.5*(x0 + x1); ym = 0.5*(y0 + y1); norm = sqrt(xm*xm +ym*ym); xm = xm/norm; ym = ym/norm; // the stopping condition // drawing only done here // the recursion // decriment counter // calculate midpoints // normalize line
// the recursive function void drawside(float x0,float y0,float x1,float y1,int n) { float xm, ym, norm; if (n <= 0) { glbegin(gl_lines); glvertex3f(x0,y0,0.0); glvertex3f(x1,y1,0.0); glend(); } else{ } n--; xm = 0.5*(x0 + x1); ym = 0.5*(y0 + y1); norm = sqrt(xm*xm +ym*ym); xm = xm/norm; ym = ym/norm; drawside(x0,y0,xm,ym,n); drawside(xm,ym,x1,y1,n); } // the stopping condition // drawing only done here // the recursion // decriment counter // calculate midpoints // normalize line 3
void display(void) { \\ the new display function
void display(void) \\ the new display function { float x0, y0; /* clear all pixels */ glclear (GL_COLOR_BUFFER_BIT); /* * set initial sides of square with unit "radius" * centered at (0.0, 0.0, 0.0). */ glcolor3f (1.0, 0.0, 0.0); x0 = 1.0/sqrt(2.0); y0 = x0;
void display(void) { float x0, y0; /* clear all pixels */ /* glclear (GL_COLOR_BUFFER_BIT); \\ the new display function * set initial sides of square with unit "radius" * centered at (0.0, 0.0, 0.0). */ glcolor3f (1.0, 0.0, 0.0); x0 = 1.0/sqrt(2.0); y0 = x0; drawside(x0,y0,-x0,y0,n);
void display(void) { float x0, y0; /* clear all pixels */ /* glclear (GL_COLOR_BUFFER_BIT); \\ the new display function * set initial sides of square with unit "radius" * centered at (0.0, 0.0, 0.0). */ glcolor3f (1.0, 0.0, 0.0); x0 = 1.0/sqrt(2.0); y0 = x0; drawside(x0,y0,-x0,y0,n); drawside(-x0,y0,-x0,-y0,n);
void display(void) { float x0, y0; /* clear all pixels */ /* glclear (GL_COLOR_BUFFER_BIT); \\ the new display function * set initial sides of square with unit "radius" * centered at (0.0, 0.0, 0.0). */ glcolor3f (1.0, 0.0, 0.0); x0 = 1.0/sqrt(2.0); y0 = x0; drawside(x0,y0,-x0,y0,n); drawside(-x0,y0,-x0,-y0,n); drawside(-x0,-y0,x0,-y0,n);
void display(void) { float x0, y0; /* clear all pixels */ /* glclear (GL_COLOR_BUFFER_BIT); \\ the new display function * set initial sides of square with unit "radius" * centered at (0.0, 0.0, 0.0). */ glcolor3f (1.0, 0.0, 0.0); x0 = 1.0/sqrt(2.0); y0 = x0; drawside(x0,y0,-x0,y0,n); drawside(-x0,y0,-x0,-y0,n); drawside(-x0,-y0,x0,-y0,n); drawside(x0,-y0,x0,y0,n);
void display(void) { float x0, y0; /* clear all pixels */ /* glclear (GL_COLOR_BUFFER_BIT); \\ the new display function * set initial sides of square with unit "radius" * centered at (0.0, 0.0, 0.0). */ glcolor3f (1.0, 0.0, 0.0); x0 = 1.0/sqrt(2.0); y0 = x0; drawside(x0,y0,-x0,y0,n); drawside(-x0,y0,-x0,-y0,n); drawside(-x0,-y0,x0,-y0,n); drawside(x0,-y0,x0,y0,n); } glflush (); 4
int main(int argc, char** argv) /* * Declare initial window size, position, and * display mode (single buffer and RGBA). * Open window with "circle" * in its title bar. * Call initialization routines. * Register callback function to display graphics. * Enter main loop and process events. */ {
int main(int argc, char** argv) /* * Declare initial window size, position, and * display mode (single buffer and RGBA). * Open window with "circle" * in its title bar. * Call initialization routines. * Register callback function to display graphics. * Enter main loop and process events. */ { if (argc == 2) sscanf(argv[1],"%i",&n);
int main(int argc, char** argv) /* * Declare initial window size, position, and * display mode (single buffer and RGBA). * Open window with "circle" * in its title bar. * Call initialization routines. * Register callback function to display graphics. * Enter main loop and process events. */ { if (argc == 2) sscanf(argv[1],"%i",&n); } glutinit(&argc, argv); glutinitdisplaymode (GLUT_SINGLE GLUT_RGB); glutinitwindowsize (400, 400); glutinitwindowposition (100, 100); glutcreatewindow ("circle"); init (); glutdisplayfunc(display); glutmainloop(); return 0; /* ANSI C requires main to return int. */ 5
Consider the following line of code: if (argc == 2) sscanf(argv[1],"%i",&n); The function sscanf(...) is one of the C input functions. There are several, which do formatted input.
Consider the following line of code: if (argc == 2) sscanf(argv[1],"%i",&n); The function sscanf(...) is one of the C input functions. There are several, which do formatted input. sscanf takes input from a character string and reads it according to the specified format.
Consider the following line of code: if (argc == 2) sscanf(argv[1],"%i",&n); The function sscanf(...) is one of the C input functions. There are several, which do formatted input. sscanf takes input from a character string and reads it according to the specified format. argv[1] is the character string corresponding to the first command line argument.
Consider the following line of code: if (argc == 2) sscanf(argv[1],"%i",&n); The function sscanf(...) is one of the C input functions. There are several, which do formatted input. sscanf takes input from a character string and reads it according to the specified format. argv[1] is the character string corresponding to the first command line argument. The constant character string "%i" is the format string, telling the function that the input string represents an integer.
Consider the following line of code: if (argc == 2) sscanf(argv[1],"%i",&n); The function sscanf(...) is one of the C input functions. There are several, which do formatted input. sscanf takes input from a character string and reads it according to the specified format. argv[1] is the character string corresponding to the first command line argument. The constant character string "%i" is the format string, telling the function that the input string represents an integer. The third argument, &n, is a pointer to the integer variable n which is the value of the integer which was read.
Consider the following line of code: if (argc == 2) sscanf(argv[1],"%i",&n); The function sscanf(...) is one of the C input functions. There are several, which do formatted input. sscanf takes input from a character string and reads it according to the specified format. argv[1] is the character string corresponding to the first command line argument. The constant character string "%i" is the format string, telling the function that the input string represents an integer. The third argument, &n, is a pointer to the integer variable n which is the value of the integer which was read. The man page for scanf gives descriptions of sscanf and related I/O functions. 6
Simple animation
Simple animation Animation is achieved by redrawing the screen repeatedly.
Simple animation Animation is achieved by redrawing the screen repeatedly. The GLUT interface to OpenGL has features to assist in animation. One is to double buffer the display memory.
Simple animation Animation is achieved by redrawing the screen repeatedly. The GLUT interface to OpenGL has features to assist in animation. One is to double buffer the display memory. With double buffering there are two separate framebuffers. One buffer is displayed while drawing operations are applied to the other buffer. After all drawing is complete, the buffers are simply swapped.
Simple animation Animation is achieved by redrawing the screen repeatedly. The GLUT interface to OpenGL has features to assist in animation. One is to double buffer the display memory. With double buffering there are two separate framebuffers. One buffer is displayed while drawing operations are applied to the other buffer. After all drawing is complete, the buffers are simply swapped. Without double buffering the contents of the framebuffer can be changed while being copied to the screen. This can cause serious artefacts.
Simple animation Animation is achieved by redrawing the screen repeatedly. The GLUT interface to OpenGL has features to assist in animation. One is to double buffer the display memory. With double buffering there are two separate framebuffers. One buffer is displayed while drawing operations are applied to the other buffer. After all drawing is complete, the buffers are simply swapped. Without double buffering the contents of the framebuffer can be changed while being copied to the screen. This can cause serious artefacts. Another animation-supporting feature of GLUT is glutpostdisplay a function which posts an event, causing the display to be redrawn.
Simple animation Animation is achieved by redrawing the screen repeatedly. The GLUT interface to OpenGL has features to assist in animation. One is to double buffer the display memory. With double buffering there are two separate framebuffers. One buffer is displayed while drawing operations are applied to the other buffer. After all drawing is complete, the buffers are simply swapped. Without double buffering the contents of the framebuffer can be changed while being copied to the screen. This can cause serious artefacts. Another animation-supporting feature of GLUT is glutpostdisplay a function which posts an event, causing the display to be redrawn. The following additions to the display and main functions show a very simple animation of the previous example, in which the color of the square is changed randomly.
Simple animation Animation is achieved by redrawing the screen repeatedly. The GLUT interface to OpenGL has features to assist in animation. One is to double buffer the display memory. With double buffering there are two separate framebuffers. One buffer is displayed while drawing operations are applied to the other buffer. After all drawing is complete, the buffers are simply swapped. Without double buffering the contents of the framebuffer can be changed while being copied to the screen. This can cause serious artefacts. Another animation-supporting feature of GLUT is glutpostdisplay a function which posts an event, causing the display to be redrawn. The following additions to the display and main functions show a very simple animation of the previous example, in which the color of the square is changed randomly. The random color change is accomplished with the following change to the function display():
Simple animation Animation is achieved by redrawing the screen repeatedly. The GLUT interface to OpenGL has features to assist in animation. One is to double buffer the display memory. With double buffering there are two separate framebuffers. One buffer is displayed while drawing operations are applied to the other buffer. After all drawing is complete, the buffers are simply swapped. Without double buffering the contents of the framebuffer can be changed while being copied to the screen. This can cause serious artefacts. Another animation-supporting feature of GLUT is glutpostdisplay a function which posts an event, causing the display to be redrawn. The following additions to the display and main functions show a very simple animation of the previous example, in which the color of the square is changed randomly. The random color change is accomplished with the following change to the function display(): glcolor3i (rand(), rand(), rand()); 7
The double buffering and the re-running of the display() function are effected by the following:
The double buffering and the re-running of the display() function are effected by the following: glutswapbuffers(); glutpostredisplay();
The double buffering and the re-running of the display() function are effected by the following: glutswapbuffers(); glutpostredisplay(); The C function
The double buffering and the re-running of the display() function are effected by the following: glutswapbuffers(); glutpostredisplay(); The C function usleep(100000);
The double buffering and the re-running of the display() function are effected by the following: glutswapbuffers(); glutpostredisplay(); The C function usleep(100000); suspends execution of the process for 100000 microseconds (0.1 seconds). 8
void display(void) { /* clear all pixels */ glclear (GL_COLOR_BUFFER_BIT); /* draw random color rectangle with corners at * (0.25, 0.25, 0.0) and (0.75, 0.75, 0.0) */
void display(void) { /* clear all pixels */ glclear (GL_COLOR_BUFFER_BIT); /* draw random color rectangle with corners at * (0.25, 0.25, 0.0) and (0.75, 0.75, 0.0) */ glcolor3i (rand(), rand(), rand());
void display(void) { /* clear all pixels */ glclear (GL_COLOR_BUFFER_BIT); /* draw random color rectangle with corners at * (0.25, 0.25, 0.0) and (0.75, 0.75, 0.0) */ glcolor3i (rand(), rand(), rand()); glbegin(gl_polygon); glvertex3f (0.25, 0.25, 0.0); glvertex3f (0.75, 0.25, 0.0); glvertex3f (0.75, 0.75, 0.0); glvertex3f (0.25, 0.75, 0.0); glend(); /* start processing buffered OpenGL routines */
void display(void) { /* clear all pixels */ glclear (GL_COLOR_BUFFER_BIT); /* draw random color rectangle with corners at * (0.25, 0.25, 0.0) and (0.75, 0.75, 0.0) */ glcolor3i (rand(), rand(), rand()); glbegin(gl_polygon); glvertex3f (0.25, 0.25, 0.0); glvertex3f (0.75, 0.25, 0.0); glvertex3f (0.75, 0.75, 0.0); glvertex3f (0.25, 0.75, 0.0); glend(); /* start processing buffered OpenGL routines */ glflush ();
void display(void) { /* clear all pixels */ glclear (GL_COLOR_BUFFER_BIT); /* draw random color rectangle with corners at * (0.25, 0.25, 0.0) and (0.75, 0.75, 0.0) */ glcolor3i (rand(), rand(), rand()); glbegin(gl_polygon); glvertex3f (0.25, 0.25, 0.0); glvertex3f (0.75, 0.25, 0.0); glvertex3f (0.75, 0.75, 0.0); glvertex3f (0.25, 0.75, 0.0); glend(); /* start processing buffered OpenGL routines */ glflush (); glutswapbuffers();
void display(void) { /* clear all pixels */ glclear (GL_COLOR_BUFFER_BIT); /* draw random color rectangle with corners at * (0.25, 0.25, 0.0) and (0.75, 0.75, 0.0) */ glcolor3i (rand(), rand(), rand()); glbegin(gl_polygon); glvertex3f (0.25, 0.25, 0.0); glvertex3f (0.75, 0.25, 0.0); glvertex3f (0.75, 0.75, 0.0); glvertex3f (0.25, 0.75, 0.0); glend(); /* start processing buffered OpenGL routines */ glflush (); glutswapbuffers(); usleep(100000);
void display(void) { /* clear all pixels */ glclear (GL_COLOR_BUFFER_BIT); /* draw random color rectangle with corners at * (0.25, 0.25, 0.0) and (0.75, 0.75, 0.0) */ glcolor3i (rand(), rand(), rand()); glbegin(gl_polygon); glvertex3f (0.25, 0.25, 0.0); glvertex3f (0.75, 0.25, 0.0); glvertex3f (0.75, 0.75, 0.0); glvertex3f (0.25, 0.75, 0.0); glend(); /* start processing buffered OpenGL routines */ glflush (); glutswapbuffers(); usleep(100000); glutpostredisplay(); } 9
int main(int argc, char** argv) /* * Declare initial window size, position, and * display mode (double buffer and RGBA). * Open window with "new_hello" in its title bar. * Call initialization routines. * Register callback function to display graphics. * Enter main loop and process events. */ { glutinit(&argc, argv);
int main(int argc, char** argv) /* * Declare initial window size, position, and * display mode (double buffer and RGBA). * Open window with "new_hello" in its title bar. * Call initialization routines. * Register callback function to display graphics. * Enter main loop and process events. */ { glutinit(&argc, argv); glutinitdisplaymode (GLUT DOUBLE GLUT RGB);
int main(int argc, char** argv) /* * Declare initial window size, position, and * display mode (double buffer and RGBA). * Open window with "new_hello" in its title bar. * Call initialization routines. * Register callback function to display graphics. * Enter main loop and process events. */ { glutinit(&argc, argv); glutinitdisplaymode (GLUT DOUBLE GLUT RGB); } glutinitwindowsize (250, 250); glutinitwindowposition (100, 100); glutcreatewindow ("new_hello"); init (); glutdisplayfunc(display); glutmainloop(); return 0; /* ANSI C requires main to return int. */ 10