Download presentation
Presentation is loading. Please wait.
1
Drawing in the plane 455
2
Content Getting started OpenGL primitives Line Drawings in OpenGL
Interaction in OpenGL Design and use of menus
3
Making pictures Picture created in screen window
Window coordinates – integer values of pixel’s position Open GL device independent graphical library OpenGL is a State machine
4
Windows based programming
Event driven approach Event queue – press key, click mouse, move window etc. Program is running as an event loop waiting for until receives a trigger Events are associated with callback functions
5
Callback functions Each callback function should be first registered
Registering (callback) functions in main loop glutDisplayFunc(MyDisplay); Function MyDisplay (user function) is registered by glut - utility
6
Libraries of OpenGL The basic library with header gl.h contains low-level graphical commands and predefined constants and graphical types The OpenGL Utility library glu.h contains several routines that use lower-level OpenGL commands The OpenGL Utility Toolkit glut.h window system-independent toolkit for working with window system.
7
Main function int main() {
glutInitWindowSize(480,480);//size of initial window glutCreateWindow("My first attemt");//crete default window glutDisplayFunc(MyDisplay);//register user function MyInit();//initialization function glutMainLoop();// looping return 0; }
8
Initial function void MyInit(void) {
glClearColor(1.0,1.0,1.0,.0);//color of background glColor3f(1.0,0.0,0.0);//color for drawing glMatrixMode(GL_PROJECTION);//matrix state glLoadIdentity(); //initialization of projection matrix gluOrtho2D(0,480.0,0,480);//Size of world window }
9
Opening window for drawing
glutInitWindowSize(480,480) – size of the first occurrence of the window glutCreateWindow("My first attemt") – creating a window with given title glutDisplayFunc(MyDisplay) – registering of a drawing function glutMainLoop(); - looping the function
10
Basic primitives Each graphical object defined in OpenGL is composed by graphical primitives. Each primitive is described by its type, position and properties. Properties are given by the state of OpenGL
11
Basic primitives - example1
glBegin(GL_LINES);//line glVertex2d(0,240);//coordinates glVertex2d(480,240); glVertex2d(240,0); glVertex2d(240,480); glEnd();
12
Basic primitives - example 2
glColor4f(0.0,0.0,1.0,0.0);//new color glBegin(GL_TRIANGLES);//first triangle glVertex2d(100,100); glVertex2d(100,300); glVertex2d(300,100); glEnd();
13
Graphical primitives GL_POINTS individual points
GL_LINES pairs of vertices as individual line segments GL_LINE_STRIP series of connected line segments GL_LINE_LOOP closed series of connected line segments GL_TRIANGLES series of triangles GL_TRIANGLE_STRIP linked strip of triangles GL_TRIANGLE_FAN linked fan of triangles GL_QUADS quadruples interpreted as 4-gons GL_QUAD_STRIP linked list of 4-gons GL_POLYGON simple convex polygon
14
Including graphical libraries
#include <GL/glut.h> Prefixes gl – basic graphical functions glu, glut – graphical utilities (glut – graphical utilities toolbox)
15
First program in OpenGL
We draw too line segments in a square window Line segments are of different color and different thickness.
16
First program in OpenGL
void MyInit(void) { glClearColor(1.,1.,1.,.0);//white color of background glMatrixMode(GL_PROJECTION);//initialization of projection matrix glLineWidth(1.0);//initial width of lines glLoadIdentity(); gluOrtho2D(0,480,0,480);//size of world window }
17
First program in OpenGL
void MyDisplay(void) { glClear(GL_COLOR_BUFFER_BIT);//clear background glColor4f(0.0,0.0,1.0,0.0);//blue color of first line glBegin(GL_LINES);//first line glVertex2d(480,0);//coordinates glVertex2d(0,240); glEnd(); glLineWidth(2.0);//width of second line glColor4f(1.0,0.0,0.0,0.0);//red color of second line glBegin(GL_LINES); glVertex2d(0,0); glVertex2d(480,480); glFlush();//draw all primitives }
18
First program in OpenGL
int main() { glutInitWindowSize(480,480);// size o initial window glutCreateWindow("My first attemt");//crete default window glutDisplayFunc(MyDisplay);//register user display function MyInit(); glutMainLoop(); return 0; }
19
Drawing triangles The first triangle is filled with a default color
The second triangle has different colors of vertices. The fill color is an interpolation of vertex-colors.
20
Definition of primitives-triangles
glColor4f(0.0,0.0,1.0,0.0);//blue color of a triangle glBegin(GL_TRIANGLES);//first triangle glVertex2d(100,100);//coordinates of 1.triangle glVertex2d(100,300); glVertex2d(300,100); glColor4f(1.0,1.0,0.0,0.0);//changing color glVertex2d(300,300); //2.triangle glColor4f(0.0,1.0,0.0,0.0);//changing color glVertex2d(200,400); glColor4f(1.0,.0,0.0,0.0);//changing color glVertex2d(50,50); glEnd(); }
21
Drawing set of points Sierpinski gasket
Choose 3 points T0,T1,T2 in the plane Choose one of points p0 in random Choose one of points T0,T1,T2 , T in random Construct point pk as midpoint of pk-1 and T Draw point pk.
22
Implementation of Sierpinski gasket
class GLintPoint //definition of a class point { public: int x, y; }; void drawDot(GLintPoint p) //drawing a point glBegin(GL_POINTS); glVertex2i(p.x,p.y); glEnd(); }
23
Implementation of Sierpinski gasket
void Sierpinski(void) { int index=1; GLintPoint T[3]={{100,100},{500,100},{300,400}}; GLintPoint point=T[index]; glClear(GL_COLOR_BUFFER_BIT); drawDot(point); for (int i=0; i<10000; i++) index=rand()%3; point.x=(point.x+T[index].x)/2; point.y=(point.y+T[index].y)/2; } glFlush();
24
Drawing from the file void DrawDino(void) {
inStream.open("dino1.dat");//data of dinosaurus if(inStream.fail()) return; while(inStream) inStream>>numLines; glBegin(GL_LINE_STRIP);//different graphical primitives for(int i=0; i<numLines; i++) inStream>>x>>y; glVertex2i(x,y); } glEnd();
25
Drawing from the file Exaple of data file: 21 29 32 435 10 439 4 438
2 433 4 428 6 425 10 420 15 416 21 413 30 408 42 406 47 403 56 398 63 391 71 383 ………….. ………………
26
Graph of a function float func(float t) { float value;
value=exp(-t)* cos(2* *t); return value; } // y=e-t.cos(2πt)
27
Graph of a function Real values of coordinates does not fit to integer coordinates of the window Necessary transformation of coordinates point.x=A*x+B ; point.y=C*func(x) +D; glVertex2d(point.x,point.y); Problem : how to find parameters A,B,C,D ?
28
Graph of a function Possible solution: A=screenWidth/4.0;
B=screenWidth/2.0; C=screenHeight/6.0; D=screenHeight/2.0
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.