Download presentation
Presentation is loading. Please wait.
Published byBerthold Simen Modified over 6 years ago
1
Starting to draw dealing with Windows which libraries? clipping
program organization color geometric primitives curved objects
2
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
3
First edition includes
# include <iostream> # ifdef __APPLE__ # include <GLUT/glut.h> # else # include <GL/glut.h> # endif
4
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
5
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.
6
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
7
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
8
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
9
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
10
Modify the window title – square.cpp with modifications or something.
11
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
12
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;
13
void setup(void); // Initialization routine. void setup(void) { // Set background (or clearing) color. glClearColor(1.0, 1.0, 1.0, 0.0); }
14
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);
15
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(); }
16
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);
17
Color Sensitive Cones Credit: Ron Nave
18
Color in OpenGL glColor3f(red, green, blue) arguments between 0 and 1
19
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!
20
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
21
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
22
OpenGL Geometric Primitives
23
More OpenGL Geometric Primitives
24
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
25
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
26
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!
27
Beware of convexity! Try convexity Example.
28
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
29
parametric equation of circle
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.