3 d programming d programming
play

3 D Programming D Programming What about solid shapes? - PDF document


  1. ������������������������ �������������������������������� ������������������������������ �������������������� � Cs 480 3 D Programming D Programming • What about solid shapes? – glutSolidSphere – glutSolidCube – etc…. – and you guessed it… – glutSolidTeapot • However, drawing these isn’t as straight forward as the 2 D shapes and the wireframes….. • Lets look at the result of changing from glutWireTeapot glutWireTeapot to glutSolidTeapot glutSolidTeapot in the previous program. Cs 480 1

  2. ������������������������ �������������������������������� ������������������������������ �������������������� � 3 D Programming D Programming What happened? Why can’t we see a solid teapot? Cs 480 3 D Programming D Programming • For many reasons, but in short… LIGHTING!!! • Without lighting, there are no: – shadows or highlights • These provide us with the illusion of depth and 3 dimensions. Cs 480 2

  3. ������������������������ �������������������������������� ������������������������������ �������������������� � 3 D Programming D Programming • Lets turn on the lights. • glEnable() is used to turn many OpenGL features on .. • One of these is lighting, thus: • glEnable(GL_LIGHTING); – this can be called in the main or the myInit when the window is being initialized. Cs 480 3 D Programming D Programming • Now, what does the teapot look like? • Not exactly the effect we were hoping for! • So far we have enabled lighting, but we haven’t ac- tually turned any lights on!! Cs 480 3

  4. ������������������������ �������������������������������� ������������������������������ �������������������� � 3 D Programming D Programming • We have to call glEnable() again to turn on a light, thus: • glEnable(GL_LIGHT 0 ); • There are a maximum of 8 lights you can use, GL_LIGHT 0 , GL_LIGHT 1 , .., GL_LIGHT 7 • And the result…. Cs 480 ` ` 3 D Programming D Programming Better. But not great if you know what is possible with OpenGL. Cs 480 ´ ´ 4

  5. ������������������������ �������������������������������� ������������������������������ �������������������� � 3 D Programming D Programming • How can we improve this? • First, we select a Shade Model. • This is set using glShadeModel(); The glShadeModel() determines how the model will be shaded. . • There are two types of shading: – GL_FLAT – GL_SMOOTH Cs 480 ^ ^ 3 D Programming D Programming glShadeModel glShadeModel(GL_FLAT); (GL_FLAT); Cs 480 5

  6. ������������������������ �������������������� � ������������������������������ �������������������������������� 3 D Programming D Programming glShadeModel glShadeModel(GL_SMOOTH); (GL_SMOOTH); Cs 480 3 D Programming D Programming • This improves the teapot’s surface, but it still isn’t quite right. • There are parts of the teapot that shouldn’t be displayed. • e.g. the hidden parts. • We need a depth test! 1 ] The glShadeModel 1 ] The glShadeModel () () determines how the model will be shaded. 2 2 ] GL_DEPTH_TEST ] GL_DEPTH_TEST allows OpenGL to check for the depth of parts of the image being drawn and thus will make sure that any shapes behind other shapes are not drawn. glEnable (GL_DEPTH_TEST); // “ for removal of hidden surfaces” GL_NORMALIZE, while not that necessary with this teapot, helps us when Cs 480 drawing 3 D shapes as it automatically normalizes normal vectors for us. 6

  7. ������������������������ �������������������������������� ������������������������������ �������������������� � 3 D Programming D Programming glEnable(GL_DEPTH_TEST); Cs 480 3 D Programming D Programming glEnable(GL_NORMALIZE); Normalize vectors for proper shading Cs 480 7

  8. ������������������������ �������������������������������� ������������������������������ �������������������� � 3 D Programming D Programming • Initialisation of the drawing environment for 3 D. main( int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH ); glutInitWindowSize(SCREENWIDTH, SCREENHEIGHT); glutInitWindowPosition( 100 , 100 ); glutCreateWindow("Solid"); glutDisplayFunc(displaySolid); glEnable(GL_LIGHTING); glEnable(GL_LIGHT 0 ); glShadeModel(GL_SMOOTH); glEnable(GL_DEPTH_TEST); glEnable(GL_NORMALIZE); glClearColor( 0.1 f, 0.1 f, 0.1 f, 0.0 f); glViewport( 0,0 , SCREENWIDTH, SCREENHEIGHT); glutMainLoop(); return 1; } Cs 480 3 D Programming D Programming • To display the teapot. This function is registered with myDisplayFunc(). void displaySolid(void) { glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho( - 1 , 1, -1, 1, 0.1, 100.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt( 2. 3 , 1 .3 , 2 , 0 , 0. 25 , 0, 0 . 0 , 1 .0 , 0 . 0 ); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glColor3 f( 0, 1 , 0 ); glPushMatrix(); glTranslated( 0 . 4, 0 . 5, 0.4); glRotated( 30 , 0 , 1, 0 ); glutSolidTeapot( 0 . 5 ); glPopMatrix(); } Cs 480 8

  9. ������������������������ �������������������������������� ������������������������������ �������������������� � 3 D Programming D Programming • The light source; its location is specified by the glLightfv () call. • This example uses the default color for light zero (GL_LIGHT 0 ), which is white • You can also locate the lights wherever you desire - • you can position them near the scene, as a desk lamp would be, or an infinite distance away, like the sun. • In addition, you can control whether a light produces a narrow, focused beam or a wider beam. Computer Graphics CSC Computer Graphics CSC 476 476 17 17 3 D Programming D Programming • Positioning the Light • lightPosition = x, y, z, w GLfloat lightPosition[]={ 2.0f,3 . 0f,3. 0 f, 0.0f}; glLightfv(GL_LIGHT 0 , GL_POSITION, lightPosition); Cs 480 ´ ´ 9

  10. ������������������������ �������������������������������� ������������������������������ �������������������� � 3 D Programming D Programming lightPosition[]={ 2.0 f, 6.0 f, 3.0 f, 0.0 f}; lightPosition[]={ 1.0 f, 2.0 f, 4.0 f, 0.0 f}; Cs 480 ^ ^ 3 D Programming D Programming • Turning up the Lights • lightIntensity = red, green, blue, alpha GLfloat lightIntensity[] = { 0.7 f, 0.7 f, 0.7 f, 1.0 f}; glLightfv(GL_LIGHT 0 , GL_DIFFUSE, lightIntensity); Cs 480 10

  11. ������������������������ �������������������������������� ������������������������������ �������������������� � 3 D Programming D Programming lightIntensity[] = {1.0 f, 1.0 f, 1.0 f, 1.0 f}; lightIntensity[] = {0.2 f, 0.2 f, 0.2 f, 1.0 f}; Cs 480 3 D Programming D Programming lightIntensity[] = {0.2 f, 1.0 f, 0.2 f, 1.0 f}; lightIntensity[] = {0.2 f, 0.0 f, 0.8 f, 1.0 f}; Cs 480 11

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend