SlideShare a Scribd company logo
Getting Started Drawing Figures (chapter 2, F.S. Hill) In this chapter… Writing programs to produce pictures Basics of Open GL Develop elementary graphics tools for drawing Control the program by Input Devices
2.1 Getting started Environment: window, coordinate system,  Drawing Figures
2.1 Getting started 2.1.1 Device-independence and OpenGL API Libraries 2.1.2 Windows-based Programming Event-driven, event queue and callbacks Old style “ do this then do this…..” New style “do nothing until an event occurs, and then do the specified thing” Callback-function System-Independent (glut) Drawing Figures
2.1 Getting started A skeleton of an event-driven program: #include <gl/Gl.h> #include <gl/glut.h> //<<<<<<<<<<<<<<<<<<<<<<<< main >>>>>>>>>>>>>>>>>>>>>> void main(int argc, char** argv) { // initialize // create screen window glutDisplayFunc (myDisplay); glutMouseFunc (myMouse); glutKeyboardFunc (myKeyboard); glutMainLoop();    // go into a perpetual loop } Drawing Figures
2.1 Getting started 2.1.3 Opening a window for drawing (framework) #include <windows.h> #include <gl/Gl.h> #include <gl/glut.h> //<<<<<<<<<<<<<<<<<<<<<<<< main >>>>>>>>>>>>>>>>>>>>>> void main(int argc, char** argv) { glutInit(&argc, argv);  // initialize the toolkit glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // set display mode glutInitWindowSize(640,480);  // set window size glutInitWindowPosition(100, 150); // set window position on  //screen glutCreateWindow(&quot;my first attempt&quot;); // open the screen window glutDisplayFunc(myDisplay);  // register redraw function myInit();  glutMainLoop();    // go into a perpetual loop } Drawing Figures
2.2 Drawing basic graphics primitives Line drawing primitives : glBegin, glEnd, vertices Example: glBegin (GL_POINTS); glVertex2i(100,50); glVertex2i(100,130); glVertex2i(150,130); glEnd(); GL_POINTS GL_LINES GL_POLYGONE OpenGL commands: glVertex2i (…), 2f, etc Drawing Figures
2.2 Drawing basic graphics primitives OpenGL data types :  Drawing Figures
2.2 Drawing basic graphics primitives OpenGL state: current state variables.  Size of point and line Back/Foreground Color glColor3f (red, green, blue) glClearcolor(red,green,blue) glClear (GL_COLOR_BUFFER_BIT) Establish coordinate system (gluOrtho2d(..)) Drawing Figures
2.2 Drawing basic graphics primitives Drawing Figures #include <windows.h>  // use as needed for your system #include <GL/Gl.h> #include <GL/glut.h> //<<<<<<<<<<<<<<<<<<<<<<< myInit >>>>>>>>>>>>>>>>>>>> void myInit(void) { glClearColor(1.0,1.0,1.0,0.0);  // set white background color glColor3f(0.0f, 0.0f, 0.0f);  // set the drawing color  glPointSize(4.0);   // a ‘dot’ is 4 by 4 pixels glMatrixMode(GL_PROJECTION);  glLoadIdentity(); gluOrtho2D(0.0, 640.0, 0.0, 480.0); } A Complete Program
2.2 Drawing basic graphics primitives Drawing Figures //<<<<<<<<<<<<<<<<<<<<<<<< myDisplay >>>>>>>>>>>>>>>>> void myDisplay(void) { glClear(GL_COLOR_BUFFER_BIT);  // clear the screen  glBegin(GL_POINTS); glVertex2i(100, 50);  // draw three points glVertex2i(100, 130); glVertex2i(150, 130); glEnd(); glFlush();   // send all output to display  } A Complete Program (continue)
2.2 Drawing basic graphics primitives Drawing Figures //<<<<<<<<<<<<<<<<<<<<<<<< main >>>>>>>>>>>>>>>>>>>>>> void main(int argc, char** argv) { glutInit(&argc, argv);  // initialize the toolkit glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // set display mode glutInitWindowSize(640,480);  // set window size glutInitWindowPosition(100, 150); // set window position on screen glutCreateWindow(&quot;my first attempt&quot;); // open the screen window glutDisplayFunc(myDisplay);  // register redraw function myInit();  glutMainLoop();    // go into a perpetual loop } A Complete Program (continue)
2.2 Drawing basic graphics primitives 2.2.1 Drawing dot constellations The Sierpinski Gasket Drawing Figures
2.2 Drawing basic graphics primitives Drawing Figures void Sierpinski(void)  { GLintPoint T[3]= {{10,10},{300,30},{200, 300}}; int index = random(3);  // 0, 1, or 2 equally likely  GLintPoint point = T[index];   // initial point  drawDot(point.x, point.y);  // draw initial point  for(int i = 0; i < 9000; i++)  // draw 1000 dots {   index = random(3);    point.x = (point.x + T[index].x) / 2;   point.y = (point.y + T[index].y) / 2;   drawDot(point.x,point.y);  }  glFlush(); }
2.2 Drawing basic graphics primitives Drawing Figures class GLintPoint{ public: GLint x,y; }; int random(int m) {   return rand()%m; } void drawDot(GLint x, GLint y) { glBegin(GL_POINTS); glVertex2i(x,y);  glEnd(); }
2.2 Drawing basic graphics primitives Simple dot plots
2.2 Drawing basic graphics primitives Simple dot plots f(x) = e -x  cos(2px) BASIC PROCESS glBegin(GL_POINTS); for(GLdouble x = 0; x < 4.0 ; x += 0.005)   glVertex2d(x,f(x)); glEnd(); Scaling x sx  =  x  *  screenWidth  /  4.0;  AS  sx = A x + B Scaling and shifting y sy  =  ( y + 1.0 ) *  screenHeight / 2.0;  AS sy = C y + D
2.2 Drawing basic graphics primitives GLdouble A, B, C, D;  A = screenWidth / 4.0;  B = 0.0; C = D = screenHeight / 2.0; glBegin(GL_POINTS); for(GLdouble x = 0; x < 4.0 ; x += 0.005) {   GLdouble  func = exp(-x) * cos(2 * 3.14159265 * x);  glVertex2d(A * x + B, C * func + D);   } glEnd(); glFlush(); Simple dot plots
2.2 Drawing basic graphics primitives Drawing Figures Plot a Function
Plot a Function (Continued) Drawing Figures
2.3 Line Drawings glBegin (GL_LINES); glVertex2i(40, 100); glVertex2i(202, 96); // More than 2 vertices : paired glEnd(); glLineWidth(1.0) // default General function for line Void drawLineint(GLint x1,GLint y1, GLint x1,GLint y1) { glBegin (GL_LINES); glVertex2i(x1, y1); glVertex2i(x2, y2); glEnd(); } Drawing Figures
2.3 Line Drawings 2.3.1 Drawing polylines and polygons Polyline: collection of joined line segments, specified by ordered list of points. GL_LINE_STRIP (open line segment) GL_LINE_LOOP (closed line segment – not fillable) GL_POLYGON  (closed line segment – fillable) Drawing Figures
2.3 Line Drawings Drawing figures by stored vertices from file void drawPolyLineFile(char * fileName) { fstream inStream; inStream.open(fileName, ios ::in); // open the file if(inStream.fail()) return; glClear(GL_COLOR_BUFFER_BIT);  // clear the screen  GLint numpolys, numLines, x ,y; inStream >> numpolys;   // read the number of polylines for(int j = 0; j < numpolys; j++)  // read each polyline { inStream >> numLines; glBegin(GL_LINE_STRIP);   // draw the next polyline for (int i = 0; i < numLines; i++) { inStream >> x >> y;  // read the next x, y pair glVertex2i(x, y); } glEnd(); } glFlush(); inStream.close(); } Drawing Figures
2.3 Line Drawings Drawing a house void hardwiredHouse(void) { glBegin(GL_LINE_LOOP); glVertex2i(40, 40); // draw the shell of house  glVertex2i(40, 90); glVertex2i(70, 120); glVertex2i(100, 90); glVertex2i(100, 40); glEnd(); glBegin(GL_LINE_STRIP);  glVertex2i(50, 100); // draw the chimney  glVertex2i(50, 120); glVertex2i(60, 120); glVertex2i(60, 110); glEnd(); // draw the door  // draw the window  } Drawing Figures
2.3 Line Drawings Parameterizing figures, polyline drawer void parameterizedHouse(GLintPoint peak, GLint width, GLint height) // the top of house is at the peak; the size of house is given //  by height and width { glBegin(GL_LINE_LOOP); // draw shell of house    glVertex2i(peak.x, peak.y);  //draw left roof   glVertex2i(peak.x + width / 2, peak.y - 3 * height /8);     glVertex2i(peak.x + width / 2, peak.y - height);  // left wall     glVertex2i(peak.x - width / 2, peak.y - height);  // bottom  glVertex2i(peak.x - width / 2, peak.y - 3 * height /8);  // right wall glEnd(); //draw chimney in the same fashion //draw the door //draw the window } Drawing Figures
2.3 Line Drawings 2.3.2 Line drawing with  moveto()  and  lineto() GLintPoint CP;   // global current position //<<<<<<<<<<<<< moveto >>>>>>>>>>>>>> void moveto(GLint x, GLint y) { CP.x = x; CP.y = y; // update the CP  } //<<<<<<<<<<<< lineTo >>>>>>>>>>>>>>>>> void lineto(GLint x, GLint y) { glBegin(GL_LINES);   // draw the line  glVertex2i(CP.x, CP.y); glVertex2i(x, y); glEnd(); glFlush(); CP.x = x; CP.y = y;  // update the CP  } Drawing Figures
2.3 Line Drawings 2.3.3 Drawing aligned rectangles glRecti(x1,y1,x2,y2)// (example programs) 2.3.4 Aspect ratio of aligned rectangle width/height Drawing Figures
2.3 Line Drawings 2.3.5 Filling polygons Must be convex! 2.3.6 Other graphics primitives: GL_TRIANGLES,GL_QUADS,GL_TRIANGLE_STRIP GL_TRIANGLE_FAN,GL_QUAD_STRIP Drawing Figures

More Related Content

What's hot (20)

PDF
OpenGL L06-Performance
Mohammad Shaker
 
PDF
C++ TUTORIAL 9
Farhan Ab Rahman
 
PPTX
Lecture 6 introduction to open gl and glut
simpleok
 
PDF
C++ TUTORIAL 10
Farhan Ab Rahman
 
PDF
Inheritance and polymorphism
mohamed sikander
 
PDF
OpenGL 4.4 Reference Card
The Khronos Group Inc.
 
PPTX
OOXX
Weihong Lee
 
PDF
Implementing stack
mohamed sikander
 
PDF
C++ TUTORIAL 1
Farhan Ab Rahman
 
PDF
Java programs
TAUQEER ABBAS
 
PPTX
New presentation oop
Ch shampi Ch shampi
 
PDF
Static and const members
mohamed sikander
 
PDF
Lec 11 12_sept [compatibility mode]
Palak Sanghani
 
PDF
54602399 c-examples-51-to-108-programe-ee01083101
premrings
 
PDF
C++ TUTORIAL 7
Farhan Ab Rahman
 
PDF
Nonlinear 2nd order analysis of 2 d fixed support beam with plastic hinge con...
Salar Delavar Qashqai
 
PDF
C++ TUTORIAL 4
Farhan Ab Rahman
 
DOCX
Assignement of programming & problem solving u.s ass.(1)
Syed Umair
 
PDF
Introduction of openGL
Gary Yeh
 
DOC
project report in C++ programming and SQL
vikram mahendra
 
OpenGL L06-Performance
Mohammad Shaker
 
C++ TUTORIAL 9
Farhan Ab Rahman
 
Lecture 6 introduction to open gl and glut
simpleok
 
C++ TUTORIAL 10
Farhan Ab Rahman
 
Inheritance and polymorphism
mohamed sikander
 
OpenGL 4.4 Reference Card
The Khronos Group Inc.
 
Implementing stack
mohamed sikander
 
C++ TUTORIAL 1
Farhan Ab Rahman
 
Java programs
TAUQEER ABBAS
 
New presentation oop
Ch shampi Ch shampi
 
Static and const members
mohamed sikander
 
Lec 11 12_sept [compatibility mode]
Palak Sanghani
 
54602399 c-examples-51-to-108-programe-ee01083101
premrings
 
C++ TUTORIAL 7
Farhan Ab Rahman
 
Nonlinear 2nd order analysis of 2 d fixed support beam with plastic hinge con...
Salar Delavar Qashqai
 
C++ TUTORIAL 4
Farhan Ab Rahman
 
Assignement of programming & problem solving u.s ass.(1)
Syed Umair
 
Introduction of openGL
Gary Yeh
 
project report in C++ programming and SQL
vikram mahendra
 

Similar to Drawing Figures (20)

PPT
Hill ch2ed3
Kunal Verma
 
PPTX
Computer Graphics with OpenGL presentation Slides.pptx
AnandM62785
 
PPT
Intro to Computer Graphics.ppt
adil104135
 
PPT
opengl.ppt
Subiksha57
 
PPT
Practicing 2d drawing primitives
Technology & Education
 
PPT
CS 354 Viewing Stuff
Mark Kilgard
 
PDF
Open gl basics
saad siddiqui
 
DOCX
Lab Practices and Works Documentation / Report on Computer Graphics
Rup Chowdhury
 
DOCX
computer graphics at openGL (2)
Yasir Khan
 
PPT
Computer Graphics involves technology to access. The Process transforms and p...
ErNandiniDharne
 
PDF
The Ring programming language version 1.5.2 book - Part 121 of 181
Mahmoud Samir Fayed
 
PPTX
Chapter02 graphics-programming
Mohammed Romi
 
PDF
Computer Graphics Report
Koshy Geoji
 
PPTX
CGChapter 3.pptx
YazanAlbilleh
 
PDF
main.pdf java programming practice for programs
RavinderKSingla
 
PPT
Open gl
ch samaram
 
PPT
openGL basics for sample program (1).ppt
HIMANKMISHRA2
 
PPT
openGL basics for sample program.ppt
HIMANKMISHRA2
 
PDF
The Ring programming language version 1.7 book - Part 155 of 196
Mahmoud Samir Fayed
 
Hill ch2ed3
Kunal Verma
 
Computer Graphics with OpenGL presentation Slides.pptx
AnandM62785
 
Intro to Computer Graphics.ppt
adil104135
 
opengl.ppt
Subiksha57
 
Practicing 2d drawing primitives
Technology & Education
 
CS 354 Viewing Stuff
Mark Kilgard
 
Open gl basics
saad siddiqui
 
Lab Practices and Works Documentation / Report on Computer Graphics
Rup Chowdhury
 
computer graphics at openGL (2)
Yasir Khan
 
Computer Graphics involves technology to access. The Process transforms and p...
ErNandiniDharne
 
The Ring programming language version 1.5.2 book - Part 121 of 181
Mahmoud Samir Fayed
 
Chapter02 graphics-programming
Mohammed Romi
 
Computer Graphics Report
Koshy Geoji
 
CGChapter 3.pptx
YazanAlbilleh
 
main.pdf java programming practice for programs
RavinderKSingla
 
Open gl
ch samaram
 
openGL basics for sample program (1).ppt
HIMANKMISHRA2
 
openGL basics for sample program.ppt
HIMANKMISHRA2
 
The Ring programming language version 1.7 book - Part 155 of 196
Mahmoud Samir Fayed
 
Ad

More from Ghaffar Khan (20)

PPT
World is beautiful ... ...
Ghaffar Khan
 
PPTX
My Presentation On Ajax
Ghaffar Khan
 
PPT
Sorting
Ghaffar Khan
 
PPT
How A Computer Works
Ghaffar Khan
 
PPT
For Loop
Ghaffar Khan
 
PPT
Exponential and Logarthmic funtions
Ghaffar Khan
 
PPT
Exponential and Logarthmic funtions (1)
Ghaffar Khan
 
PPT
Functions
Ghaffar Khan
 
PPT
Quadratic And Polinomial Function
Ghaffar Khan
 
PPT
Quadratic And Polinomial Function
Ghaffar Khan
 
PPT
Exponentioal And Logarthmic Functions
Ghaffar Khan
 
PPT
Internet Protocol
Ghaffar Khan
 
PPT
Introduction to Computer Networks
Ghaffar Khan
 
PPT
Network Layer
Ghaffar Khan
 
PPT
Control Structures
Ghaffar Khan
 
PPT
Input And Output
Ghaffar Khan
 
PPT
Surfaces
Ghaffar Khan
 
PPT
Vector Tools
Ghaffar Khan
 
PPT
Drawing Tools
Ghaffar Khan
 
PPT
Computer Graphics Introduction
Ghaffar Khan
 
World is beautiful ... ...
Ghaffar Khan
 
My Presentation On Ajax
Ghaffar Khan
 
Sorting
Ghaffar Khan
 
How A Computer Works
Ghaffar Khan
 
For Loop
Ghaffar Khan
 
Exponential and Logarthmic funtions
Ghaffar Khan
 
Exponential and Logarthmic funtions (1)
Ghaffar Khan
 
Functions
Ghaffar Khan
 
Quadratic And Polinomial Function
Ghaffar Khan
 
Quadratic And Polinomial Function
Ghaffar Khan
 
Exponentioal And Logarthmic Functions
Ghaffar Khan
 
Internet Protocol
Ghaffar Khan
 
Introduction to Computer Networks
Ghaffar Khan
 
Network Layer
Ghaffar Khan
 
Control Structures
Ghaffar Khan
 
Input And Output
Ghaffar Khan
 
Surfaces
Ghaffar Khan
 
Vector Tools
Ghaffar Khan
 
Drawing Tools
Ghaffar Khan
 
Computer Graphics Introduction
Ghaffar Khan
 
Ad

Recently uploaded (20)

PDF
Human-centred design in online workplace learning and relationship to engagem...
Tracy Tang
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
PPTX
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PDF
Wojciech Ciemski for Top Cyber News MAGAZINE. June 2025
Dr. Ludmila Morozova-Buss
 
PDF
Apache CloudStack 201: Let's Design & Build an IaaS Cloud
ShapeBlue
 
PPTX
✨Unleashing Collaboration: Salesforce Channels & Community Power in Patna!✨
SanjeetMishra29
 
PDF
Persuasive AI: risks and opportunities in the age of digital debate
Speck&Tech
 
PDF
TrustArc Webinar - Data Privacy Trends 2025: Mid-Year Insights & Program Stra...
TrustArc
 
PDF
Predicting the unpredictable: re-engineering recommendation algorithms for fr...
Speck&Tech
 
PDF
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
PDF
Rethinking Security Operations - SOC Evolution Journey.pdf
Haris Chughtai
 
PDF
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
PDF
Empowering Cloud Providers with Apache CloudStack and Stackbill
ShapeBlue
 
PDF
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
PDF
July Patch Tuesday
Ivanti
 
PPTX
Extensions Framework (XaaS) - Enabling Orchestrate Anything
ShapeBlue
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
Ampere Offers Energy-Efficient Future For AI And Cloud
ShapeBlue
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
Human-centred design in online workplace learning and relationship to engagem...
Tracy Tang
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
Wojciech Ciemski for Top Cyber News MAGAZINE. June 2025
Dr. Ludmila Morozova-Buss
 
Apache CloudStack 201: Let's Design & Build an IaaS Cloud
ShapeBlue
 
✨Unleashing Collaboration: Salesforce Channels & Community Power in Patna!✨
SanjeetMishra29
 
Persuasive AI: risks and opportunities in the age of digital debate
Speck&Tech
 
TrustArc Webinar - Data Privacy Trends 2025: Mid-Year Insights & Program Stra...
TrustArc
 
Predicting the unpredictable: re-engineering recommendation algorithms for fr...
Speck&Tech
 
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
Rethinking Security Operations - SOC Evolution Journey.pdf
Haris Chughtai
 
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
Empowering Cloud Providers with Apache CloudStack and Stackbill
ShapeBlue
 
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
July Patch Tuesday
Ivanti
 
Extensions Framework (XaaS) - Enabling Orchestrate Anything
ShapeBlue
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
Ampere Offers Energy-Efficient Future For AI And Cloud
ShapeBlue
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 

Drawing Figures

  • 1. Getting Started Drawing Figures (chapter 2, F.S. Hill) In this chapter… Writing programs to produce pictures Basics of Open GL Develop elementary graphics tools for drawing Control the program by Input Devices
  • 2. 2.1 Getting started Environment: window, coordinate system, Drawing Figures
  • 3. 2.1 Getting started 2.1.1 Device-independence and OpenGL API Libraries 2.1.2 Windows-based Programming Event-driven, event queue and callbacks Old style “ do this then do this…..” New style “do nothing until an event occurs, and then do the specified thing” Callback-function System-Independent (glut) Drawing Figures
  • 4. 2.1 Getting started A skeleton of an event-driven program: #include <gl/Gl.h> #include <gl/glut.h> //<<<<<<<<<<<<<<<<<<<<<<<< main >>>>>>>>>>>>>>>>>>>>>> void main(int argc, char** argv) { // initialize // create screen window glutDisplayFunc (myDisplay); glutMouseFunc (myMouse); glutKeyboardFunc (myKeyboard); glutMainLoop(); // go into a perpetual loop } Drawing Figures
  • 5. 2.1 Getting started 2.1.3 Opening a window for drawing (framework) #include <windows.h> #include <gl/Gl.h> #include <gl/glut.h> //<<<<<<<<<<<<<<<<<<<<<<<< main >>>>>>>>>>>>>>>>>>>>>> void main(int argc, char** argv) { glutInit(&argc, argv); // initialize the toolkit glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // set display mode glutInitWindowSize(640,480); // set window size glutInitWindowPosition(100, 150); // set window position on //screen glutCreateWindow(&quot;my first attempt&quot;); // open the screen window glutDisplayFunc(myDisplay); // register redraw function myInit(); glutMainLoop(); // go into a perpetual loop } Drawing Figures
  • 6. 2.2 Drawing basic graphics primitives Line drawing primitives : glBegin, glEnd, vertices Example: glBegin (GL_POINTS); glVertex2i(100,50); glVertex2i(100,130); glVertex2i(150,130); glEnd(); GL_POINTS GL_LINES GL_POLYGONE OpenGL commands: glVertex2i (…), 2f, etc Drawing Figures
  • 7. 2.2 Drawing basic graphics primitives OpenGL data types : Drawing Figures
  • 8. 2.2 Drawing basic graphics primitives OpenGL state: current state variables. Size of point and line Back/Foreground Color glColor3f (red, green, blue) glClearcolor(red,green,blue) glClear (GL_COLOR_BUFFER_BIT) Establish coordinate system (gluOrtho2d(..)) Drawing Figures
  • 9. 2.2 Drawing basic graphics primitives Drawing Figures #include <windows.h> // use as needed for your system #include <GL/Gl.h> #include <GL/glut.h> //<<<<<<<<<<<<<<<<<<<<<<< myInit >>>>>>>>>>>>>>>>>>>> void myInit(void) { glClearColor(1.0,1.0,1.0,0.0); // set white background color glColor3f(0.0f, 0.0f, 0.0f); // set the drawing color glPointSize(4.0); // a ‘dot’ is 4 by 4 pixels glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0, 640.0, 0.0, 480.0); } A Complete Program
  • 10. 2.2 Drawing basic graphics primitives Drawing Figures //<<<<<<<<<<<<<<<<<<<<<<<< myDisplay >>>>>>>>>>>>>>>>> void myDisplay(void) { glClear(GL_COLOR_BUFFER_BIT); // clear the screen glBegin(GL_POINTS); glVertex2i(100, 50); // draw three points glVertex2i(100, 130); glVertex2i(150, 130); glEnd(); glFlush(); // send all output to display } A Complete Program (continue)
  • 11. 2.2 Drawing basic graphics primitives Drawing Figures //<<<<<<<<<<<<<<<<<<<<<<<< main >>>>>>>>>>>>>>>>>>>>>> void main(int argc, char** argv) { glutInit(&argc, argv); // initialize the toolkit glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // set display mode glutInitWindowSize(640,480); // set window size glutInitWindowPosition(100, 150); // set window position on screen glutCreateWindow(&quot;my first attempt&quot;); // open the screen window glutDisplayFunc(myDisplay); // register redraw function myInit(); glutMainLoop(); // go into a perpetual loop } A Complete Program (continue)
  • 12. 2.2 Drawing basic graphics primitives 2.2.1 Drawing dot constellations The Sierpinski Gasket Drawing Figures
  • 13. 2.2 Drawing basic graphics primitives Drawing Figures void Sierpinski(void) { GLintPoint T[3]= {{10,10},{300,30},{200, 300}}; int index = random(3); // 0, 1, or 2 equally likely GLintPoint point = T[index]; // initial point drawDot(point.x, point.y); // draw initial point for(int i = 0; i < 9000; i++) // draw 1000 dots { index = random(3); point.x = (point.x + T[index].x) / 2; point.y = (point.y + T[index].y) / 2; drawDot(point.x,point.y); } glFlush(); }
  • 14. 2.2 Drawing basic graphics primitives Drawing Figures class GLintPoint{ public: GLint x,y; }; int random(int m) { return rand()%m; } void drawDot(GLint x, GLint y) { glBegin(GL_POINTS); glVertex2i(x,y); glEnd(); }
  • 15. 2.2 Drawing basic graphics primitives Simple dot plots
  • 16. 2.2 Drawing basic graphics primitives Simple dot plots f(x) = e -x cos(2px) BASIC PROCESS glBegin(GL_POINTS); for(GLdouble x = 0; x < 4.0 ; x += 0.005) glVertex2d(x,f(x)); glEnd(); Scaling x sx = x * screenWidth / 4.0; AS sx = A x + B Scaling and shifting y sy = ( y + 1.0 ) * screenHeight / 2.0; AS sy = C y + D
  • 17. 2.2 Drawing basic graphics primitives GLdouble A, B, C, D; A = screenWidth / 4.0; B = 0.0; C = D = screenHeight / 2.0; glBegin(GL_POINTS); for(GLdouble x = 0; x < 4.0 ; x += 0.005) { GLdouble func = exp(-x) * cos(2 * 3.14159265 * x); glVertex2d(A * x + B, C * func + D); } glEnd(); glFlush(); Simple dot plots
  • 18. 2.2 Drawing basic graphics primitives Drawing Figures Plot a Function
  • 19. Plot a Function (Continued) Drawing Figures
  • 20. 2.3 Line Drawings glBegin (GL_LINES); glVertex2i(40, 100); glVertex2i(202, 96); // More than 2 vertices : paired glEnd(); glLineWidth(1.0) // default General function for line Void drawLineint(GLint x1,GLint y1, GLint x1,GLint y1) { glBegin (GL_LINES); glVertex2i(x1, y1); glVertex2i(x2, y2); glEnd(); } Drawing Figures
  • 21. 2.3 Line Drawings 2.3.1 Drawing polylines and polygons Polyline: collection of joined line segments, specified by ordered list of points. GL_LINE_STRIP (open line segment) GL_LINE_LOOP (closed line segment – not fillable) GL_POLYGON (closed line segment – fillable) Drawing Figures
  • 22. 2.3 Line Drawings Drawing figures by stored vertices from file void drawPolyLineFile(char * fileName) { fstream inStream; inStream.open(fileName, ios ::in); // open the file if(inStream.fail()) return; glClear(GL_COLOR_BUFFER_BIT); // clear the screen GLint numpolys, numLines, x ,y; inStream >> numpolys; // read the number of polylines for(int j = 0; j < numpolys; j++) // read each polyline { inStream >> numLines; glBegin(GL_LINE_STRIP); // draw the next polyline for (int i = 0; i < numLines; i++) { inStream >> x >> y; // read the next x, y pair glVertex2i(x, y); } glEnd(); } glFlush(); inStream.close(); } Drawing Figures
  • 23. 2.3 Line Drawings Drawing a house void hardwiredHouse(void) { glBegin(GL_LINE_LOOP); glVertex2i(40, 40); // draw the shell of house glVertex2i(40, 90); glVertex2i(70, 120); glVertex2i(100, 90); glVertex2i(100, 40); glEnd(); glBegin(GL_LINE_STRIP); glVertex2i(50, 100); // draw the chimney glVertex2i(50, 120); glVertex2i(60, 120); glVertex2i(60, 110); glEnd(); // draw the door // draw the window } Drawing Figures
  • 24. 2.3 Line Drawings Parameterizing figures, polyline drawer void parameterizedHouse(GLintPoint peak, GLint width, GLint height) // the top of house is at the peak; the size of house is given // by height and width { glBegin(GL_LINE_LOOP); // draw shell of house glVertex2i(peak.x, peak.y); //draw left roof glVertex2i(peak.x + width / 2, peak.y - 3 * height /8); glVertex2i(peak.x + width / 2, peak.y - height); // left wall glVertex2i(peak.x - width / 2, peak.y - height); // bottom glVertex2i(peak.x - width / 2, peak.y - 3 * height /8); // right wall glEnd(); //draw chimney in the same fashion //draw the door //draw the window } Drawing Figures
  • 25. 2.3 Line Drawings 2.3.2 Line drawing with moveto() and lineto() GLintPoint CP; // global current position //<<<<<<<<<<<<< moveto >>>>>>>>>>>>>> void moveto(GLint x, GLint y) { CP.x = x; CP.y = y; // update the CP } //<<<<<<<<<<<< lineTo >>>>>>>>>>>>>>>>> void lineto(GLint x, GLint y) { glBegin(GL_LINES); // draw the line glVertex2i(CP.x, CP.y); glVertex2i(x, y); glEnd(); glFlush(); CP.x = x; CP.y = y; // update the CP } Drawing Figures
  • 26. 2.3 Line Drawings 2.3.3 Drawing aligned rectangles glRecti(x1,y1,x2,y2)// (example programs) 2.3.4 Aspect ratio of aligned rectangle width/height Drawing Figures
  • 27. 2.3 Line Drawings 2.3.5 Filling polygons Must be convex! 2.3.6 Other graphics primitives: GL_TRIANGLES,GL_QUADS,GL_TRIANGLE_STRIP GL_TRIANGLE_FAN,GL_QUAD_STRIP Drawing Figures