Starting to draw dealing with Windows which libraries? clipping program organization color geometric primitives curved objects
May be needed for some Windows Ok for all – check with class. In main, replace glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); with glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); At the end of display, replace glFlush(); with glutSwapBuffers(); note glut vs gl
First edition includes # include <iostream> # ifdef __APPLE__ # include <GLUT/glut.h> # else # include <GL/glut.h> # endif
Second edition includes #ifdef __APPLE__ # include <GL/glew.h> # include <GL/freeglut.h> # include <OpenGL/glext.h> #else # include <GL/glext.h> #pragma comment(lib, "glew32.lib") #endif
2nd edition (openGL4.3) requires other modifications of code we won't be doing yet – stick to first edition examples. Also for homework – I will not be using glew or freeglut.
square. cpp Add it to your project and get it running square.cpp Add it to your project and get it running. Make a triangle as below. glBegin(GL_POLYGON); glVertex3f(20.0, 20.0,0); glVertex3f(80.0, 20.0, 0.0); glVertex3f(80.0, 80.0, 0.0); glEnd(); square2.cpp part YYY partially outside the box in z direction. part ZZZ partially outside the box
square.cpp with clipping Modify the z value of the first vertex glBegin(GL_POLYGON); glVertex3f(20.0, 20.0,10); glVertex3f(80.0, 20.0, 0.0); glVertex3f(80.0, 80.0, 0.0); glEnd(); square2.cpp part YYY partially outside the box in z direction. part ZZZ partially outside the box
square.cpp with clipping glBegin(GL_POLYGON); glVertex3f(20.0, 20.0,10); glVertex3f(80.0, 20.0, 0.0); glVertex3f(80.0, 80.0, 0.0); glEnd(); square2.cpp part YYY partially outside the box in z direction. part ZZZ partially outside the box
Understanding the program organization: main // Main routine: defines window properties, creates window, // registers callback routines and begins processing. int main(int argc, char **argv) { // Initialize GLUT. glutInit(&argc, argv); // Set display mode as single-buffered and RGB color. glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // Set OpenGL window size. glutInitWindowSize(500, 500); // Set position of OpenGL window upper-left corner. glutInitWindowPosition(100, 100); // Create OpenGL window with title. glutCreateWindow("square.cpp"); notice glut prefix
Modify the window title – square.cpp with modifications or something.
main (cont) // Initialize. setup(); // Register display routine. glutDisplayFunc(drawScene); // Register reshape routine. glutReshapeFunc(resize); // Register keyboard routine. glutKeyboardFunc(keyInput); // Begin processing. glutMainLoop(); return 0; } note functions without glut prefix
Add print statements to see which functions are being called. Add these lines at the top of the appropriate functions, then run. Resize window – what happens? cout << "In drawScene" << endl; cout << "In resize" << endl; cout << "In setup" << endl; cout << "In keyInput" << endl;
void setup(void); // Initialization routine. void setup(void) { // Set background (or clearing) color. glClearColor(1.0, 1.0, 1.0, 0.0); }
void drawScene(void); // Drawing (display) routine. void drawScene(void) { // Clear screen to background color. glClear(GL_COLOR_BUFFER_BIT); // Set foreground (or drawing) color. glColor3f(0.0, 0.0, 0.0);
void drawScene(void); (cont) // Draw a polygon with specified vertices. glBegin(GL_POLYGON); glVertex3f(20.0, 20.0, 0.0); glVertex3f(80.0, 20.0, 0.0); glVertex3f(80.0, 80.0, 0.0); glVertex3f(20.0, 80.0, 0.0); glEnd(); // Flush created objects to the screen, i.e., force rendering. glFlush(); }
other functions Deal with someone changing the size of the window. void resize(int w, int h); Keyboard input processing routine. void keyInput(unsigned char key, int x, int y);
Color Sensitive Cones Credit: Ron Nave
Color in OpenGL glColor3f(red, green, blue) arguments between 0 and 1
state variables glColor3f(red,green,blue); drawing color glClearColor(red, green, blue, 0.0); background color glClear(GL_COLOR_BUFFER_BIT); Sets every pixel to background color. Remove and see what happens!
Modify square: Another rectangle of a different color. Immediate mode graphics - objects are drawn and forgotten! squareV2.cpp part 1 first do part 0 – old square
Modify square: Different vertices different colors Colors bound to vertices of a primitive are interpolated throughout the interior. squareV2.cpp part 2 Discuss importance to shading
OpenGL Geometric Primitives
More OpenGL Geometric Primitives
Try some of these primitives in square.cpp May want to add: glPointSize(3.0); to make the points bigger glLineWidth(4.0): to make the lines thicker
filled in or outlines glPolygonMode(face, mode) face can be GL_FRONT GL_BACK GL_FRONT_AND_BACK mode can be GL_FILL GL_LINE GL_POINT
Try several triangles, various ways. Can do GL_TRIANGLES GL_TRIANGLE_STRIP To see triangles can use glPolygonMode(GL_FRONT_AND_BACK,GL_LINE); To see filled in and edges, draw twice: filled in, change color, then outlines. squareV2.cpp part 3, triangle strips. squareV2.cpp part 4 added to part 3. Notice Part 4 is above part 3. Part 5 is fan Switch from FRONT_AND_BACK to two statements, one FRONT, one BACK to see if seeing front or back!
Beware of convexity! Try convexity Example.
Curved Objects Run the program circle.cpp. Observe instructions in comment and console screen global variables new version of color: glColor3ub use of rand() glutPostRedisplay in keyInput circle.cpp add as source to SquareForClass2
parametric equation of circle