Presentation is loading. Please wait.

Presentation is loading. Please wait.

NoufNaief.net 1 TA: Nouf Al-Harbi.

Similar presentations


Presentation on theme: "NoufNaief.net 1 TA: Nouf Al-Harbi."— Presentation transcript:

1 NoufNaief.net nouf200@hotmail.com 1 TA: Nouf Al-Harbi

2 Computer graphics lab 2 Using colors & Changing Settings..

3 Review of Lab 1 - Prepare the graphics work environment - Include OpenGL Files to your computer - Link libraries to your project - Writing the 1 st graphics program - Initialize GLUT - Create the window - Display the drawing 3

4 Lab 2 objectives  By the end of this lab you will be able to :  Know Examples of primitive shapes  Use Colors in OpenGL  Change the default settings of the window  Draw 3D objects 4

5 Part 1 Examples of primitive shapes 5

6 OpenGL Geometric Primitives 6

7  Primitives are specified using glBegin( type ); … glEnd(); 7

8 Exercise  Add to the previous program the following shapes  GL_LINE_STRIP  4 lines  GL_POINTS  3 points 8

9 Change some properties  Points:  Size glPointSize( 4.0); //cann’t place it between Begin & end  Lines:  Width glLineWidth( 4.0); //cann’t place it between Begin & end 9

10 Part 2 Using colors in OpenGL 10

11 Exercise 11  Create the following shape  Windows application coordinates

12 Exercise.. 12 #include "GL\glut.h" void display() { glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_POLYGON); glVertex2f( 0,1); glVertex2f(1,0); glVertex2f(0,-1); glVertex2f(-1,0); glEnd(); glFlush(); } int main(int argc,char** argv) { glutInit(&argc,argv); glutCreateWindow("colorizing Shapes"); glutDisplayFunc(display); glutMainLoop(); } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

13 Using RGB to Define a Color 13  There are many ways to specify a color in computer graphics  the RGB color model  one of the simplest and most widely used methods  RGB stands for the colors red, green and blue  Each of these colors is given a value  In OpenGL usually a value between 0 and 1  1 means as much of that color as possible  0 means none of that color

14 Using RGB to Define a Color  We can mix these three colors together to give us a complete range of colors..  pure red is represented as (1, 0, 0)  Full blue is (0, 0, 1)  White is the combination of all three (1, 1, 1)  Black is the absence of all three (0, 0, 0)  Yellow is the combination of red and green (1, 1, 0)  Orange is yellow with slightly less green (1, 0.5, 0) 14

15 Exercise..  Modify the previous code 15 #include "GL\glut.h" void display() { glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_POLYGON); glColor3f(0.5,0.5,1); glVertex2f( 0.0, 1); glVertex2f( 1, 0); glVertex2f(0, -1); glVertex2f(-1,0); glEnd(); glFlush(); } int main(int argc,char** argv) { glutInit(&argc,argv); glutCreateWindow("colorizing Shapes"); glutDisplayFunc(display); glutMainLoop(); } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

16 Exercise..  Try different values for R,G,B 16

17 Using glColor3f Function NameParametersFunction Job Void glColor3f(Glfloat red, Glfloat green, GLfloat blue) takes 3 arguments: the red, green and blue components of the color you want. Sets the current color. After you use glColor3f, everything you draw will be in that color. 17  glcolor has two major variants  glcolor3f specify new red, green, and blue values explicitly set the current alpha value to 1.0 (full intensity) implicitly. ex: glColor3ub  its values range between 0,255  Glcolor4f specify all four color components explicitly

18 Giving Individual Vertices Different Colors  glColor3f can be used to give each vertex it's own color 18 #include "GL\glut.h" void display() { glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_POLYGON); glColor3f(1,0,0); glVertex2f( 0.0, 1); glColor3f(0,0,1); glVertex2f( 1, 0); glColor3f(0,1,0); glVertex2f(0, -1); glColor3f(0,0,0.3); glVertex2f(-1,0); glEnd(); glFlush(); } int main(int argc,char** argv) { glutInit(&argc,argv); glutCreateWindow("colorizing Shapes"); glutDisplayFunc(display); glutMainLoop(); } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

19 Background Color  To Setting the Background Color we use  glClearColor(R,G,B,A)  Exercise:  Set the Background color of the previous program to GREEN 19

20 Graphical Tip How we can draw any shape with border.. By using just what we learned until now..?? 20

21 Part 3 Change the default settings of the window 21

22 Changing GLUT defaults in Window Size 22 Function NameParametersFunction Job void glutInitWindowSize ( int width, int height ); Width of the window Height of the window Specifies the initial height and width, in pixels, of the window `` Application Window Screen `` Application Window Screen

23 Changing GLUT defaults in Window Position 23 Function NameParametersFunction Job void glutInitWindowPositio n ( int x, int y ); x - the number of pixels from the left of the screen y - the number of pixels from the top of the screen. Set window position (0, 0) `` (x, y) y Application Window Screen x

24 When these functions should be called  All functions that change the settings of the window and the mode of display should be called before creating the window 24

25 Exercise  Modify the previous program to change the default settings as follows:  Window position:  100,100  Window size:  320,400 25

26 Exercise Solution.. 26 : int main(int argc,char** argv) { glutInit(&argc,argv); glutInitWindowPosition(100,100); glutInitWindowSize(320,400); glutCreateWindow("colorizing Shapes"); glutDisplayFunc(display); glutMainLoop(); } 1 2 3 4 5 6 7 8 9 10 11

27 Part 3 Drawing 3D Shapes 27

28 Drawing 3-D Objects 28  GLUT has a series of drawing routines that are used to create three dimensional models.  Each object comes in two modes  wire  solid.

29 Drawing 3-D Objects 29  Available objects are:  glutWireSphere (GLdouble radius, GLint slices, GLint stacks)  glutSolidSphere (GLdouble radius, GLint slices, GLint stacks)  glutWireCube (GLdouble size )  glutSolidCube (GLdouble size)  glutWireCone (GLdouble base, GLdouble height, GLint slices, GLint stacks)  glutSolidCone (GLdouble base, GLdouble height, GLint slices, GLint stacks)  glutWireTeapot (GLdouble size)  glutSolidTeapot (GLdouble size)

30 Drawing 3d objects.. 30

31 Drawing 3d objects..  We can’t differentiate between 2D objects and 3D objects without making some transformations.. (to be the topic of the next lab) 31

32 HW1  Write a program using OpenGL to make a simple drawing.  All details will be available on the website 32

33 Examples of Simple OpenGL Programs 33

34 REFERENCES:  Materials of this lab are prepared using:  An Introduction to Graphics Programming with OpenGL by : Toby Howard, School of Computer Science, University of Manchester  Lighthouse3d: http://www.lighthouse3d.com/opengl/glut/index. php?1  OpenGL Programming Guide 'The Red Book’  Computer Graphics Lab -1st semester – 2010-2011 by : Ins. Ruwaida Al-harbi & Ins. Nouf Al-harbi  http://en.wikibooks.org/wiki/OpenGL_Programmin g/Basics/Color 34


Download ppt "NoufNaief.net 1 TA: Nouf Al-Harbi."

Similar presentations


Ads by Google