SlideShare ist ein Scribd-Unternehmen logo
1 von 34
OpenGL Interaction




                         http://www.learncax.com/



                                        Centre for Computational Technologies
CCTech Recruitment Brochure                             Simulation is The Future!
Outline
•   Client/Server Model
•   Callbacks
•   Double Buffering
•   Simple Transformations
•   Example




                             Centre for Computational Technologies
      OpenGL                                 Simulation is The Future!
Surface Orientation (Clarification)
• Right-hand rule
• Triangle strip drawn 0-1-2, 2-1-3, 2-3-4, etc.




• All triangles face same direction (here: back)
• Similarly for quad strips 0-1-3-2, 2-3-5-4, etc.
• Orientable surfaces; discard back faces:
  glEnable(GL_CULL_FACE);
  glCullFace(GL_BACK); /* do not draw back faces */

                                    Centre for Computational Technologies
    OpenGL                                          Simulation is The Future!
Choice of Programming Language
•   OpenGL lives close to the hardware
•   OpenGL is not object-oriented
•   OpenGL is not functional
•   Use C to expose and exploit low-level details
•   Use C++, Java ... for toolkits
•   Support for C and C++ in assignments




                                    Centre for Computational Technologies
      OpenGL                                        Simulation is The Future!
Client/Server Model
• Graphics hardware and caching




• Important for efficiency
• Need to be aware where data are stored
• Examples: vertex arrays, display lists


                                  Centre for Computational Technologies
    OpenGL                                        Simulation is The Future!
Display Lists
• Encapsulate a sequence of drawing commands
• Optimize and store on server
  GLuint listName = glGenLists(1); /* new name */
  glNewList (listName, GL_COMPILE); /* new list */
      glColor3f(1.0, 0.0, 1.0);
      glBegin(GL_TRIANGLES);
             glVertex3f(0.0, 0.0, 0.0);
             ...
      glEnd();
      glTranslatef(1.5, 0.0, 0.0); /* offset next object */
  glEndList();
  glCallList(listName); /* draw one */



                                                Centre for Computational Technologies
    OpenGL                                                      Simulation is The Future!
Display Lists Details
•   Useful for sequences of transformations
•   Important for complex surfaces
•   Another example: fonts
•   Hierarchical display lists supported
•   Display lists cannot be changed
•   Display lists can be replaced
•   Not necessary in first assignment




                                   Centre for Computational Technologies
      OpenGL                                       Simulation is The Future!
Vertex Arrays
•   Draw cube with 6*4=24 or with 8 vertices?
•   Expense in drawing and transformation
•   Strips help to some extent
•   Vertex arrays provide general solution
•   Advanced (new in OpenGL 1.2)
    – Define (transmit) array of vertices, colors, normals
    – Draw using index into array(s)
    – Vertex sharing for efficient operations
• Not needed for first assignment




                                           Centre for Computational Technologies
      OpenGL                                                 Simulation is The Future!
Outline
•   Client/Server Model
•   Callbacks
•   Double Buffering
•   Simple Transformations
•   Example




                             Centre for Computational Technologies
      OpenGL                                 Simulation is The Future!
Main Event Loop
•   Standard technique for interaction
•   Main loop processes events
•   Dispatch to functions specified by client
•   Callbacks also common in operating systems
•   Poor man’s functional programming
•   Mediates between client and window system




                                  Centre for Computational Technologies
      OpenGL                                      Simulation is The Future!
Types of Callbacks
•   Display ( ): when window must be drawn
•   Idle ( ): when no other events to be handled
•   Keyboard (unsigned char key, int x, int y): key
•   Menu (...): after selection from menu
•   Mouse (int button, int state, int x, int y): mouse
•   Motion (...): mouse movement
•   Reshape (int w, int h): window resize
•   Any callback can be NULL




                                       Centre for Computational Technologies
      OpenGL                                           Simulation is The Future!
Outline
•   Client/Server Model
•   Callbacks
•   Double Buffering
•   Simple Transformations
•   Example




                             Centre for Computational Technologies
      OpenGL                                 Simulation is The Future!
Refresh Screen
•   Common: 60-100 Hz
•   Flicker if drawing overlaps screen refresh
•   Problem during animation
•   Example (cube_single.c)
•   Solution two frame buffers:
    – Draw into one buffer
    – Swap and display, while drawing into other buffer
• Desirable frame rate >= 30 fps (frames/second)




                                         Centre for Computational Technologies
      OpenGL                                              Simulation is The Future!
Enabling Modes
•   One example of many
•   glutInitDisplayMode (GLUT_SINGLE);
•   glutInitDisplayMode (GLUT_DOUBLE);
•   glutSwapBuffers ();
•   If something has no effect, check mode




                                   Centre for Computational Technologies
      OpenGL                                       Simulation is The Future!
Outline
•   Client/Server Model
•   Callbacks
•   Double Buffering
•   Simple Transformations
•   Example




                             Centre for Computational Technologies
     OpenGL                                  Simulation is The Future!
Specifying the Viewing Volume
• Clip everything not in viewing volume
• Separate matrices for transformation and projection
         glMatrixMode (GL_PROJECTION);
         glLoadIdentity();
         ... Set viewing volume ...
         glMatrixMode(GL_MODELVIEW);




                                         Centre for Computational Technologies
    OpenGL                                               Simulation is The Future!
Parallel Viewing
• Orthographic projection
• Camera points in negative z direction
• glOrtho(xmin, xmax, ymin, ymax, near, far)




                                  Centre for Computational Technologies
    OpenGL                                        Simulation is The Future!
Perspective Viewing
• Slightly more complex
• glFrustum(xmin, xmax, ymin, ymax, near, far)




                                 Centre for Computational Technologies
    OpenGL                                       Simulation is The Future!
Transformations Simple
• Rotate by given angle (in degrees) about ray from origin
  through (x, y, z)
      glRotate{fd}(angle, x, y, z);

• Translate by the given x, y, and z values
      glTranslate{fd}(x, y, z);

• Scale with a factor in the x, y, and z direction
      glScale{fd}(x, y, z);




                                     Centre for Computational Technologies
     OpenGL                                          Simulation is The Future!
Outline
•   Client/Server Model
•   Callbacks
•   Double Buffering
•   Simple Transformations
•   Example




                             Centre for Computational Technologies
      OpenGL                                 Simulation is The Future!
Example: Rotating Color Cube
• Problem:
  – Draw a color cube
  – Rotate it about x, y, or z axis, depending on left, middle or right
    mouse click
  – Stop when space bar is pressed
  – Quit when q or Q is pressed




                                          Centre for Computational Technologies
    OpenGL                                                Simulation is The Future!
Step 1: Defining the Vertices
• Use parallel arrays for vertices and colors
  /* vertices of cube about the origin */
  GLfloat vertices[8][3] =
       {{-1.0, -1.0, -1.0}, {1.0, -1.0, -1.0},
       {1.0, 1.0, -1.0}, {-1.0, 1.0, -1.0}, {-1.0, -1.0, 1.0},
       {1.0, -1.0, 1.0}, {1.0, 1.0, 1.0}, {-1.0, 1.0, 1.0}};
  /* colors to be assigned to edges */
  GLfloat colors[8][3] =
       {{0.0, 0.0, 0.0}, {1.0, 0.0, 0.0},
       {1.0, 1.0, 0.0}, {0.0, 1.0, 0.0}, {0.0, 0.0, 1.0},
       {1.0, 0.0, 1.0}, {1.0, 1.0, 1.0}, {0.0, 1.0, 1.0}};

                                        Centre for Computational Technologies
     OpenGL                                             Simulation is The Future!
Step 2: Set Up
• Enable depth testing and double buffering
   int main(int argc, char **argv)
   {
         glutInit (&argc, argv);
         /* double buffering for smooth animation */
         glutInitDisplayMode (GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB);
         ... /* window creation and callbacks here */
         glEnable (GL_DEPTH_TEST);
         glutMainLoop();
         return(0);
   }




                                        Centre for Computational Technologies
       OpenGL                                           Simulation is The Future!
Step 3: Install Callbacks
• Create window and set callbacks

   glutInitWindowSize(500, 500);
   glutCreateWindow("cube");
   glutReshapeFunc(myReshape);
   glutDisplayFunc(display);
   glutIdleFunc(spinCube);
   glutMouseFunc(mouse);
   glutKeyboardFunc(keyboard);



                                Centre for Computational Technologies
    OpenGL                                      Simulation is The Future!
Step 4: Reshape Callback
• Enclose cube, preserve aspect ratio
   void myReshape(int w, int h)
   {
         GLfloat aspect = (GLfloat) w / (GLfloat) h;
         glViewport(0, 0, w, h);
         glMatrixMode (GL_PROJECTION);
         glLoadIdentity();
         if (w <= h) /* aspect <= 1 */
             glOrtho(-2.0, 2.0, -2.0/aspect, 2.0/aspect, -10.0, 10.0);
         else /* aspect > 1 */
             glOrtho(-2.0*aspect, 2.0*aspect, -2.0, 2.0, -10.0, 10.0);
         glMatrixMode (GL_MODELVIEW);
   }


                                                 Centre for Computational Technologies
       OpenGL                                                       Simulation is The Future!
Step 5: Display Callback
• Clear, rotate, draw, flush, swap
   GLfloat theta[3] = {0.0, 0.0, 0.0};
   void display(void)
   {
         glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
         glLoadIdentity();
         glRotatef(theta[0], 1.0, 0.0, 0.0);
         glRotatef(theta[1], 0.0, 1.0, 0.0);
         glRotatef(theta[2], 0.0, 0.0, 1.0);
         colorcube();
         glFlush();
         glutSwapBuffers();
   }


                                          Centre for Computational Technologies
       OpenGL                                             Simulation is The Future!
Step 6: Drawing Faces
• Call face(a, b, c, d) with vertex index
• Orient consistently
   void colorcube(void)
   {
         face(0,3,2,1);
         face(2,3,7,6);
         face(0,4,7,3);
         face(1,2,6,5);
         face(4,5,6,7);
         face(0,1,5,4);
   }




                                     Centre for Computational Technologies
       OpenGL                                        Simulation is The Future!
Step 7: Drawing a Face
• Use vector form of primitives and attributes
   void face(int a, int b, int c, int d)
   {
   glBegin(GL_POLYGON);
       glColor3fv(colors[a]);
       glVertex3fv(vertices[a]);
       glColor3fv(colors[b]);
       glVertex3fv(vertices[b]);
       glColor3fv(colors[c]);
       glVertex3fv(vertices[c]);
       glColor3fv(colors[d]);
       glVertex3fv(vertices[d]);
   glEnd();
   }

                                           Centre for Computational Technologies
    OpenGL                                                 Simulation is The Future!
Step 8: Animation
• Set idle callback
   GLfloat delta = 2.0;
   GLint axis = 2;
   void spinCube()
   {
         /* spin cube delta degrees about selected axis */
         theta[axis] += delta;
         if (theta[axis] > 360.0)
             theta[axis] -= 360.0;
         /* display result */
         glutPostRedisplay();
   }



                                               Centre for Computational Technologies
       OpenGL                                                  Simulation is The Future!
Step 9: Change Axis of Rotation
• Mouse callback
  void mouse (int btn, int state, int x, int y)
   {
         if (btn==GLUT_LEFT_BUTTON
             && state == GLUT_DOWN) axis = 0;
         if (btn==GLUT_MIDDLE_BUTTON
             && state == GLUT_DOWN) axis = 1;
         if (btn==GLUT_RIGHT_BUTTON
             && state == GLUT_DOWN) axis = 2;
   }




                                            Centre for Computational Technologies
       OpenGL                                               Simulation is The Future!
Step 10: Toggle Rotation or Exit
• Keyboard callback

void keyboard (unsigned char key, int x, int y)
{
  if (key=='q' || key == 'Q') exit(0);
  if (key==' ') {stop = !stop;};
  if (stop)
        glutIdleFunc(NULL);
  else
        glutIdleFunc(spinCube);
}

                                    Centre for Computational Technologies
    OpenGL                                          Simulation is The Future!
Summary
•   Client/Server Model
•   Callbacks
•   Double Buffering
•   Simple Transformations
•   Example




                             Centre for Computational Technologies
      OpenGL                                 Simulation is The Future!
33

                             Questions?




              Centre for Computational Technologies
     OpenGL                   Simulation is The Future!
Thank You




                   Mail Us @ sandip@cctech.co.in
                    Visit Us @ www.cctech.co.in
                  Call Us @ +91 20 4009 8381/82
                    Mobile @ +91 98508 60725

         Centre for Computational Technologies Pvt. Ltd.


                1, Akshay Residancy, 50 Anand Park,
                          Aundh, Pune -7

                                     Centre for Computational Technologies
OpenGL                                                Simulation is The Future!

Weitere ähnliche Inhalte

Was ist angesagt?

Avoiding 19 Common OpenGL Pitfalls
Avoiding 19 Common OpenGL PitfallsAvoiding 19 Common OpenGL Pitfalls
Avoiding 19 Common OpenGL PitfallsMark Kilgard
 
GFX Part 1 - Introduction to GPU HW and OpenGL ES specifications
GFX Part 1 - Introduction to GPU HW and OpenGL ES specificationsGFX Part 1 - Introduction to GPU HW and OpenGL ES specifications
GFX Part 1 - Introduction to GPU HW and OpenGL ES specificationsPrabindh Sundareson
 
Animation Framework: A Step Towards Modern UIs
Animation Framework: A Step Towards Modern UIsAnimation Framework: A Step Towards Modern UIs
Animation Framework: A Step Towards Modern UIsaccount inactive
 
SIGGRAPH Asia 2012 Exhibitor Talk: OpenGL 4.3 and Beyond
SIGGRAPH Asia 2012 Exhibitor Talk: OpenGL 4.3 and BeyondSIGGRAPH Asia 2012 Exhibitor Talk: OpenGL 4.3 and Beyond
SIGGRAPH Asia 2012 Exhibitor Talk: OpenGL 4.3 and BeyondMark Kilgard
 
CS 354 Introduction
CS 354 IntroductionCS 354 Introduction
CS 354 IntroductionMark Kilgard
 
Graphics programming in open gl
Graphics programming in open glGraphics programming in open gl
Graphics programming in open glArvind Devaraj
 
CS 354 Viewing Stuff
CS 354 Viewing StuffCS 354 Viewing Stuff
CS 354 Viewing StuffMark Kilgard
 
Introduction of openGL
Introduction  of openGLIntroduction  of openGL
Introduction of openGLGary Yeh
 
OpenGL Introduction.
OpenGL Introduction.OpenGL Introduction.
OpenGL Introduction.Girish Ghate
 
Virtual Reality Features of NVIDIA GPUs
Virtual Reality Features of NVIDIA GPUsVirtual Reality Features of NVIDIA GPUs
Virtual Reality Features of NVIDIA GPUsMark Kilgard
 
GTC 2012: GPU-Accelerated Path Rendering
GTC 2012: GPU-Accelerated Path RenderingGTC 2012: GPU-Accelerated Path Rendering
GTC 2012: GPU-Accelerated Path Rendering Mark Kilgard
 
NVIDIA OpenGL in 2016
NVIDIA OpenGL in 2016NVIDIA OpenGL in 2016
NVIDIA OpenGL in 2016Mark Kilgard
 
Open Graphics Library
Open Graphics  Library Open Graphics  Library
Open Graphics Library Azmeen Gadit
 
13th kandroid OpenGL and EGL
13th kandroid OpenGL and EGL13th kandroid OpenGL and EGL
13th kandroid OpenGL and EGLJungsoo Nam
 

Was ist angesagt? (20)

OpenGL for 2015
OpenGL for 2015OpenGL for 2015
OpenGL for 2015
 
Avoiding 19 Common OpenGL Pitfalls
Avoiding 19 Common OpenGL PitfallsAvoiding 19 Common OpenGL Pitfalls
Avoiding 19 Common OpenGL Pitfalls
 
OpenGL basics
OpenGL basicsOpenGL basics
OpenGL basics
 
GFX Part 1 - Introduction to GPU HW and OpenGL ES specifications
GFX Part 1 - Introduction to GPU HW and OpenGL ES specificationsGFX Part 1 - Introduction to GPU HW and OpenGL ES specifications
GFX Part 1 - Introduction to GPU HW and OpenGL ES specifications
 
What is OpenGL ?
What is OpenGL ?What is OpenGL ?
What is OpenGL ?
 
Animation Framework: A Step Towards Modern UIs
Animation Framework: A Step Towards Modern UIsAnimation Framework: A Step Towards Modern UIs
Animation Framework: A Step Towards Modern UIs
 
SIGGRAPH Asia 2012 Exhibitor Talk: OpenGL 4.3 and Beyond
SIGGRAPH Asia 2012 Exhibitor Talk: OpenGL 4.3 and BeyondSIGGRAPH Asia 2012 Exhibitor Talk: OpenGL 4.3 and Beyond
SIGGRAPH Asia 2012 Exhibitor Talk: OpenGL 4.3 and Beyond
 
CS 354 Introduction
CS 354 IntroductionCS 354 Introduction
CS 354 Introduction
 
Graphics programming in open gl
Graphics programming in open glGraphics programming in open gl
Graphics programming in open gl
 
CS 354 Viewing Stuff
CS 354 Viewing StuffCS 354 Viewing Stuff
CS 354 Viewing Stuff
 
Introduction of openGL
Introduction  of openGLIntroduction  of openGL
Introduction of openGL
 
Open gl
Open glOpen gl
Open gl
 
OpenGL Introduction.
OpenGL Introduction.OpenGL Introduction.
OpenGL Introduction.
 
Virtual Reality Features of NVIDIA GPUs
Virtual Reality Features of NVIDIA GPUsVirtual Reality Features of NVIDIA GPUs
Virtual Reality Features of NVIDIA GPUs
 
GTC 2012: GPU-Accelerated Path Rendering
GTC 2012: GPU-Accelerated Path RenderingGTC 2012: GPU-Accelerated Path Rendering
GTC 2012: GPU-Accelerated Path Rendering
 
NVIDIA OpenGL in 2016
NVIDIA OpenGL in 2016NVIDIA OpenGL in 2016
NVIDIA OpenGL in 2016
 
Open Graphics Library
Open Graphics  Library Open Graphics  Library
Open Graphics Library
 
Cross Platform Qt
Cross Platform QtCross Platform Qt
Cross Platform Qt
 
Advance analysis of algo
Advance analysis of algoAdvance analysis of algo
Advance analysis of algo
 
13th kandroid OpenGL and EGL
13th kandroid OpenGL and EGL13th kandroid OpenGL and EGL
13th kandroid OpenGL and EGL
 

Andere mochten auch

Creo tookit geometric traversal and evaluator
Creo tookit  geometric traversal and evaluatorCreo tookit  geometric traversal and evaluator
Creo tookit geometric traversal and evaluatorSandip Jadhav
 
Creo toolkit introduction
Creo toolkit  introductionCreo toolkit  introduction
Creo toolkit introductionSandip Jadhav
 
PTC Creo customization using VB API - Lecture 1 - Overview
PTC Creo customization using VB API - Lecture 1 - OverviewPTC Creo customization using VB API - Lecture 1 - Overview
PTC Creo customization using VB API - Lecture 1 - OverviewSandip Jadhav
 
Creo 3.0 tips and tricks r4
Creo 3.0 tips and tricks r4Creo 3.0 tips and tricks r4
Creo 3.0 tips and tricks r4Evan Winter
 
Creo parametric tips and tricks
Creo parametric tips and tricksCreo parametric tips and tricks
Creo parametric tips and tricksEvan Winter
 
Top 10 Graphic Design Mistakes - Part 1
Top 10 Graphic Design Mistakes - Part 1Top 10 Graphic Design Mistakes - Part 1
Top 10 Graphic Design Mistakes - Part 1Malron Sanders
 
CS 354 Graphics Math
CS 354 Graphics MathCS 354 Graphics Math
CS 354 Graphics MathMark Kilgard
 
104 2-5 ca-ddoctor軟體介紹(崇道國際)
104 2-5 ca-ddoctor軟體介紹(崇道國際)104 2-5 ca-ddoctor軟體介紹(崇道國際)
104 2-5 ca-ddoctor軟體介紹(崇道國際)Rich Man
 
Shadow Mapping with Today's OpenGL Hardware
Shadow Mapping with Today's OpenGL HardwareShadow Mapping with Today's OpenGL Hardware
Shadow Mapping with Today's OpenGL HardwareMark Kilgard
 
20090924 姿勢推定と回転行列
20090924 姿勢推定と回転行列20090924 姿勢推定と回転行列
20090924 姿勢推定と回転行列Toru Tamaki
 
3 Strategies for Robust Modeling in Creo Parametric
3 Strategies for Robust Modeling in Creo Parametric3 Strategies for Robust Modeling in Creo Parametric
3 Strategies for Robust Modeling in Creo ParametricEvan Winter
 
クラウドを使ったデザイン データ活用 - Autodesk Forge ご紹介 @ デブサミ 2016
クラウドを使ったデザイン データ活用 - Autodesk Forge ご紹介 @ デブサミ 2016クラウドを使ったデザイン データ活用 - Autodesk Forge ご紹介 @ デブサミ 2016
クラウドを使ったデザイン データ活用 - Autodesk Forge ご紹介 @ デブサミ 2016Isezaki Toshiaki
 
Key to Successful Design to Manufacturing - Siddharth Desai, I-Flow Corporation
Key to Successful Design to Manufacturing - Siddharth Desai, I-Flow CorporationKey to Successful Design to Manufacturing - Siddharth Desai, I-Flow Corporation
Key to Successful Design to Manufacturing - Siddharth Desai, I-Flow Corporationmarcus evans Network
 

Andere mochten auch (16)

Creo tookit geometric traversal and evaluator
Creo tookit  geometric traversal and evaluatorCreo tookit  geometric traversal and evaluator
Creo tookit geometric traversal and evaluator
 
Creo toolkit introduction
Creo toolkit  introductionCreo toolkit  introduction
Creo toolkit introduction
 
PTC Creo customization using VB API - Lecture 1 - Overview
PTC Creo customization using VB API - Lecture 1 - OverviewPTC Creo customization using VB API - Lecture 1 - Overview
PTC Creo customization using VB API - Lecture 1 - Overview
 
Creo 3.0 tips and tricks r4
Creo 3.0 tips and tricks r4Creo 3.0 tips and tricks r4
Creo 3.0 tips and tricks r4
 
Creo parametric tips and tricks
Creo parametric tips and tricksCreo parametric tips and tricks
Creo parametric tips and tricks
 
Top 10 Graphic Design Mistakes - Part 1
Top 10 Graphic Design Mistakes - Part 1Top 10 Graphic Design Mistakes - Part 1
Top 10 Graphic Design Mistakes - Part 1
 
CS 354 Graphics Math
CS 354 Graphics MathCS 354 Graphics Math
CS 354 Graphics Math
 
Opengl Lighting Basic
Opengl Lighting BasicOpengl Lighting Basic
Opengl Lighting Basic
 
104 2-5 ca-ddoctor軟體介紹(崇道國際)
104 2-5 ca-ddoctor軟體介紹(崇道國際)104 2-5 ca-ddoctor軟體介紹(崇道國際)
104 2-5 ca-ddoctor軟體介紹(崇道國際)
 
Shadow Mapping with Today's OpenGL Hardware
Shadow Mapping with Today's OpenGL HardwareShadow Mapping with Today's OpenGL Hardware
Shadow Mapping with Today's OpenGL Hardware
 
Week 2 understand lighting (semester 1)
Week 2 understand lighting (semester 1)Week 2 understand lighting (semester 1)
Week 2 understand lighting (semester 1)
 
20090924 姿勢推定と回転行列
20090924 姿勢推定と回転行列20090924 姿勢推定と回転行列
20090924 姿勢推定と回転行列
 
3 Strategies for Robust Modeling in Creo Parametric
3 Strategies for Robust Modeling in Creo Parametric3 Strategies for Robust Modeling in Creo Parametric
3 Strategies for Robust Modeling in Creo Parametric
 
クラウドを使ったデザイン データ活用 - Autodesk Forge ご紹介 @ デブサミ 2016
クラウドを使ったデザイン データ活用 - Autodesk Forge ご紹介 @ デブサミ 2016クラウドを使ったデザイン データ活用 - Autodesk Forge ご紹介 @ デブサミ 2016
クラウドを使ったデザイン データ活用 - Autodesk Forge ご紹介 @ デブサミ 2016
 
Key to Successful Design to Manufacturing - Siddharth Desai, I-Flow Corporation
Key to Successful Design to Manufacturing - Siddharth Desai, I-Flow CorporationKey to Successful Design to Manufacturing - Siddharth Desai, I-Flow Corporation
Key to Successful Design to Manufacturing - Siddharth Desai, I-Flow Corporation
 
Cad cam
Cad camCad cam
Cad cam
 

Ähnlich wie OpenGL Interaction

Android open gl2_droidcon_2014
Android open gl2_droidcon_2014Android open gl2_droidcon_2014
Android open gl2_droidcon_2014Droidcon Berlin
 
CeedMath & CeedGL, Let's talk 3D...
CeedMath & CeedGL, Let's talk 3D...CeedMath & CeedGL, Let's talk 3D...
CeedMath & CeedGL, Let's talk 3D...rsebbe
 
openGL basics for sample program (1).ppt
openGL basics for sample program (1).pptopenGL basics for sample program (1).ppt
openGL basics for sample program (1).pptHIMANKMISHRA2
 
openGL basics for sample program.ppt
openGL basics for sample program.pptopenGL basics for sample program.ppt
openGL basics for sample program.pptHIMANKMISHRA2
 
Computer Graphics - Lecture 01 - 3D Programming I
Computer Graphics - Lecture 01 - 3D Programming IComputer Graphics - Lecture 01 - 3D Programming I
Computer Graphics - Lecture 01 - 3D Programming I💻 Anton Gerdelan
 
C game programming - SDL
C game programming - SDLC game programming - SDL
C game programming - SDLWingston
 
Tessellation on any_budget-gdc2011
Tessellation on any_budget-gdc2011Tessellation on any_budget-gdc2011
Tessellation on any_budget-gdc2011basisspace
 
Advanced Graphics Workshop - GFX2011
Advanced Graphics Workshop - GFX2011Advanced Graphics Workshop - GFX2011
Advanced Graphics Workshop - GFX2011Prabindh Sundareson
 
IEEE-754 standard format to handle Floating-Point calculations in RISC-V CPUs...
IEEE-754 standard format to handle Floating-Point calculations in RISC-V CPUs...IEEE-754 standard format to handle Floating-Point calculations in RISC-V CPUs...
IEEE-754 standard format to handle Floating-Point calculations in RISC-V CPUs...zeeshanshanzy009
 
UML for Aspect Oriented Design
UML for Aspect Oriented DesignUML for Aspect Oriented Design
UML for Aspect Oriented DesignEdison Lascano
 
An Introductory course on Verilog HDL-Verilog hdl ppr
An Introductory course on Verilog HDL-Verilog hdl pprAn Introductory course on Verilog HDL-Verilog hdl ppr
An Introductory course on Verilog HDL-Verilog hdl pprPrabhavathi P
 
CG simple openGL point & line-course 2
CG simple openGL point & line-course 2CG simple openGL point & line-course 2
CG simple openGL point & line-course 2fungfung Chen
 

Ähnlich wie OpenGL Interaction (20)

september11.ppt
september11.pptseptember11.ppt
september11.ppt
 
Android open gl2_droidcon_2014
Android open gl2_droidcon_2014Android open gl2_droidcon_2014
Android open gl2_droidcon_2014
 
CeedMath & CeedGL, Let's talk 3D...
CeedMath & CeedGL, Let's talk 3D...CeedMath & CeedGL, Let's talk 3D...
CeedMath & CeedGL, Let's talk 3D...
 
openGL basics for sample program (1).ppt
openGL basics for sample program (1).pptopenGL basics for sample program (1).ppt
openGL basics for sample program (1).ppt
 
openGL basics for sample program.ppt
openGL basics for sample program.pptopenGL basics for sample program.ppt
openGL basics for sample program.ppt
 
Computer Graphics - Lecture 01 - 3D Programming I
Computer Graphics - Lecture 01 - 3D Programming IComputer Graphics - Lecture 01 - 3D Programming I
Computer Graphics - Lecture 01 - 3D Programming I
 
2D graphics
2D graphics2D graphics
2D graphics
 
Introduction to 2D/3D Graphics
Introduction to 2D/3D GraphicsIntroduction to 2D/3D Graphics
Introduction to 2D/3D Graphics
 
18csl67 vtu lab manual
18csl67 vtu lab manual18csl67 vtu lab manual
18csl67 vtu lab manual
 
Opengl (1)
Opengl (1)Opengl (1)
Opengl (1)
 
C game programming - SDL
C game programming - SDLC game programming - SDL
C game programming - SDL
 
IN4308 1
IN4308 1IN4308 1
IN4308 1
 
Tessellation on any_budget-gdc2011
Tessellation on any_budget-gdc2011Tessellation on any_budget-gdc2011
Tessellation on any_budget-gdc2011
 
Advanced Graphics Workshop - GFX2011
Advanced Graphics Workshop - GFX2011Advanced Graphics Workshop - GFX2011
Advanced Graphics Workshop - GFX2011
 
Chapter-3.pdf
Chapter-3.pdfChapter-3.pdf
Chapter-3.pdf
 
IEEE-754 standard format to handle Floating-Point calculations in RISC-V CPUs...
IEEE-754 standard format to handle Floating-Point calculations in RISC-V CPUs...IEEE-754 standard format to handle Floating-Point calculations in RISC-V CPUs...
IEEE-754 standard format to handle Floating-Point calculations in RISC-V CPUs...
 
UML for Aspect Oriented Design
UML for Aspect Oriented DesignUML for Aspect Oriented Design
UML for Aspect Oriented Design
 
An Introductory course on Verilog HDL-Verilog hdl ppr
An Introductory course on Verilog HDL-Verilog hdl pprAn Introductory course on Verilog HDL-Verilog hdl ppr
An Introductory course on Verilog HDL-Verilog hdl ppr
 
Graphics Libraries
Graphics LibrariesGraphics Libraries
Graphics Libraries
 
CG simple openGL point & line-course 2
CG simple openGL point & line-course 2CG simple openGL point & line-course 2
CG simple openGL point & line-course 2
 

Kürzlich hochgeladen

One India vs United India by Dream Tamilnadu
One India vs United India by Dream TamilnaduOne India vs United India by Dream Tamilnadu
One India vs United India by Dream TamilnaduDreamTamilnadu
 
Green Aesthetic Ripped Paper Thesis Defense Presentation_20240311_111012_0000...
Green Aesthetic Ripped Paper Thesis Defense Presentation_20240311_111012_0000...Green Aesthetic Ripped Paper Thesis Defense Presentation_20240311_111012_0000...
Green Aesthetic Ripped Paper Thesis Defense Presentation_20240311_111012_0000...virgfern3011
 
Anantkumar Hegde
Anantkumar Hegde  Anantkumar Hegde
Anantkumar Hegde NewsFeed1
 
Light Rail in Canberra: Too much, too little, too late: Is the price worth th...
Light Rail in Canberra: Too much, too little, too late: Is the price worth th...Light Rail in Canberra: Too much, too little, too late: Is the price worth th...
Light Rail in Canberra: Too much, too little, too late: Is the price worth th...University of Canberra
 
Européennes 2024 : projection du Parlement européen à trois mois du scrutin
Européennes 2024 : projection du Parlement européen à trois mois du scrutinEuropéennes 2024 : projection du Parlement européen à trois mois du scrutin
Européennes 2024 : projection du Parlement européen à trois mois du scrutinIpsos France
 
19032024_First India Newspaper Jaipur.pdf
19032024_First India Newspaper Jaipur.pdf19032024_First India Newspaper Jaipur.pdf
19032024_First India Newspaper Jaipur.pdfFIRST INDIA
 
Por estos dos motivos, defensa de JOH solicita repetir juicio
Por estos dos motivos, defensa de JOH solicita repetir juicioPor estos dos motivos, defensa de JOH solicita repetir juicio
Por estos dos motivos, defensa de JOH solicita repetir juicioAlexisTorres963861
 
Ministry of Justice Extradition Eswatini 3.pdf
Ministry of Justice Extradition Eswatini 3.pdfMinistry of Justice Extradition Eswatini 3.pdf
Ministry of Justice Extradition Eswatini 3.pdfSABC News
 
Another Day, Another Default Judgment Against Gabe Whitley
Another Day, Another Default Judgment Against Gabe WhitleyAnother Day, Another Default Judgment Against Gabe Whitley
Another Day, Another Default Judgment Against Gabe WhitleyAbdul-Hakim Shabazz
 

Kürzlich hochgeladen (9)

One India vs United India by Dream Tamilnadu
One India vs United India by Dream TamilnaduOne India vs United India by Dream Tamilnadu
One India vs United India by Dream Tamilnadu
 
Green Aesthetic Ripped Paper Thesis Defense Presentation_20240311_111012_0000...
Green Aesthetic Ripped Paper Thesis Defense Presentation_20240311_111012_0000...Green Aesthetic Ripped Paper Thesis Defense Presentation_20240311_111012_0000...
Green Aesthetic Ripped Paper Thesis Defense Presentation_20240311_111012_0000...
 
Anantkumar Hegde
Anantkumar Hegde  Anantkumar Hegde
Anantkumar Hegde
 
Light Rail in Canberra: Too much, too little, too late: Is the price worth th...
Light Rail in Canberra: Too much, too little, too late: Is the price worth th...Light Rail in Canberra: Too much, too little, too late: Is the price worth th...
Light Rail in Canberra: Too much, too little, too late: Is the price worth th...
 
Européennes 2024 : projection du Parlement européen à trois mois du scrutin
Européennes 2024 : projection du Parlement européen à trois mois du scrutinEuropéennes 2024 : projection du Parlement européen à trois mois du scrutin
Européennes 2024 : projection du Parlement européen à trois mois du scrutin
 
19032024_First India Newspaper Jaipur.pdf
19032024_First India Newspaper Jaipur.pdf19032024_First India Newspaper Jaipur.pdf
19032024_First India Newspaper Jaipur.pdf
 
Por estos dos motivos, defensa de JOH solicita repetir juicio
Por estos dos motivos, defensa de JOH solicita repetir juicioPor estos dos motivos, defensa de JOH solicita repetir juicio
Por estos dos motivos, defensa de JOH solicita repetir juicio
 
Ministry of Justice Extradition Eswatini 3.pdf
Ministry of Justice Extradition Eswatini 3.pdfMinistry of Justice Extradition Eswatini 3.pdf
Ministry of Justice Extradition Eswatini 3.pdf
 
Another Day, Another Default Judgment Against Gabe Whitley
Another Day, Another Default Judgment Against Gabe WhitleyAnother Day, Another Default Judgment Against Gabe Whitley
Another Day, Another Default Judgment Against Gabe Whitley
 

OpenGL Interaction

  • 1. OpenGL Interaction http://www.learncax.com/ Centre for Computational Technologies CCTech Recruitment Brochure Simulation is The Future!
  • 2. Outline • Client/Server Model • Callbacks • Double Buffering • Simple Transformations • Example Centre for Computational Technologies OpenGL Simulation is The Future!
  • 3. Surface Orientation (Clarification) • Right-hand rule • Triangle strip drawn 0-1-2, 2-1-3, 2-3-4, etc. • All triangles face same direction (here: back) • Similarly for quad strips 0-1-3-2, 2-3-5-4, etc. • Orientable surfaces; discard back faces: glEnable(GL_CULL_FACE); glCullFace(GL_BACK); /* do not draw back faces */ Centre for Computational Technologies OpenGL Simulation is The Future!
  • 4. Choice of Programming Language • OpenGL lives close to the hardware • OpenGL is not object-oriented • OpenGL is not functional • Use C to expose and exploit low-level details • Use C++, Java ... for toolkits • Support for C and C++ in assignments Centre for Computational Technologies OpenGL Simulation is The Future!
  • 5. Client/Server Model • Graphics hardware and caching • Important for efficiency • Need to be aware where data are stored • Examples: vertex arrays, display lists Centre for Computational Technologies OpenGL Simulation is The Future!
  • 6. Display Lists • Encapsulate a sequence of drawing commands • Optimize and store on server GLuint listName = glGenLists(1); /* new name */ glNewList (listName, GL_COMPILE); /* new list */ glColor3f(1.0, 0.0, 1.0); glBegin(GL_TRIANGLES); glVertex3f(0.0, 0.0, 0.0); ... glEnd(); glTranslatef(1.5, 0.0, 0.0); /* offset next object */ glEndList(); glCallList(listName); /* draw one */ Centre for Computational Technologies OpenGL Simulation is The Future!
  • 7. Display Lists Details • Useful for sequences of transformations • Important for complex surfaces • Another example: fonts • Hierarchical display lists supported • Display lists cannot be changed • Display lists can be replaced • Not necessary in first assignment Centre for Computational Technologies OpenGL Simulation is The Future!
  • 8. Vertex Arrays • Draw cube with 6*4=24 or with 8 vertices? • Expense in drawing and transformation • Strips help to some extent • Vertex arrays provide general solution • Advanced (new in OpenGL 1.2) – Define (transmit) array of vertices, colors, normals – Draw using index into array(s) – Vertex sharing for efficient operations • Not needed for first assignment Centre for Computational Technologies OpenGL Simulation is The Future!
  • 9. Outline • Client/Server Model • Callbacks • Double Buffering • Simple Transformations • Example Centre for Computational Technologies OpenGL Simulation is The Future!
  • 10. Main Event Loop • Standard technique for interaction • Main loop processes events • Dispatch to functions specified by client • Callbacks also common in operating systems • Poor man’s functional programming • Mediates between client and window system Centre for Computational Technologies OpenGL Simulation is The Future!
  • 11. Types of Callbacks • Display ( ): when window must be drawn • Idle ( ): when no other events to be handled • Keyboard (unsigned char key, int x, int y): key • Menu (...): after selection from menu • Mouse (int button, int state, int x, int y): mouse • Motion (...): mouse movement • Reshape (int w, int h): window resize • Any callback can be NULL Centre for Computational Technologies OpenGL Simulation is The Future!
  • 12. Outline • Client/Server Model • Callbacks • Double Buffering • Simple Transformations • Example Centre for Computational Technologies OpenGL Simulation is The Future!
  • 13. Refresh Screen • Common: 60-100 Hz • Flicker if drawing overlaps screen refresh • Problem during animation • Example (cube_single.c) • Solution two frame buffers: – Draw into one buffer – Swap and display, while drawing into other buffer • Desirable frame rate >= 30 fps (frames/second) Centre for Computational Technologies OpenGL Simulation is The Future!
  • 14. Enabling Modes • One example of many • glutInitDisplayMode (GLUT_SINGLE); • glutInitDisplayMode (GLUT_DOUBLE); • glutSwapBuffers (); • If something has no effect, check mode Centre for Computational Technologies OpenGL Simulation is The Future!
  • 15. Outline • Client/Server Model • Callbacks • Double Buffering • Simple Transformations • Example Centre for Computational Technologies OpenGL Simulation is The Future!
  • 16. Specifying the Viewing Volume • Clip everything not in viewing volume • Separate matrices for transformation and projection glMatrixMode (GL_PROJECTION); glLoadIdentity(); ... Set viewing volume ... glMatrixMode(GL_MODELVIEW); Centre for Computational Technologies OpenGL Simulation is The Future!
  • 17. Parallel Viewing • Orthographic projection • Camera points in negative z direction • glOrtho(xmin, xmax, ymin, ymax, near, far) Centre for Computational Technologies OpenGL Simulation is The Future!
  • 18. Perspective Viewing • Slightly more complex • glFrustum(xmin, xmax, ymin, ymax, near, far) Centre for Computational Technologies OpenGL Simulation is The Future!
  • 19. Transformations Simple • Rotate by given angle (in degrees) about ray from origin through (x, y, z) glRotate{fd}(angle, x, y, z); • Translate by the given x, y, and z values glTranslate{fd}(x, y, z); • Scale with a factor in the x, y, and z direction glScale{fd}(x, y, z); Centre for Computational Technologies OpenGL Simulation is The Future!
  • 20. Outline • Client/Server Model • Callbacks • Double Buffering • Simple Transformations • Example Centre for Computational Technologies OpenGL Simulation is The Future!
  • 21. Example: Rotating Color Cube • Problem: – Draw a color cube – Rotate it about x, y, or z axis, depending on left, middle or right mouse click – Stop when space bar is pressed – Quit when q or Q is pressed Centre for Computational Technologies OpenGL Simulation is The Future!
  • 22. Step 1: Defining the Vertices • Use parallel arrays for vertices and colors /* vertices of cube about the origin */ GLfloat vertices[8][3] = {{-1.0, -1.0, -1.0}, {1.0, -1.0, -1.0}, {1.0, 1.0, -1.0}, {-1.0, 1.0, -1.0}, {-1.0, -1.0, 1.0}, {1.0, -1.0, 1.0}, {1.0, 1.0, 1.0}, {-1.0, 1.0, 1.0}}; /* colors to be assigned to edges */ GLfloat colors[8][3] = {{0.0, 0.0, 0.0}, {1.0, 0.0, 0.0}, {1.0, 1.0, 0.0}, {0.0, 1.0, 0.0}, {0.0, 0.0, 1.0}, {1.0, 0.0, 1.0}, {1.0, 1.0, 1.0}, {0.0, 1.0, 1.0}}; Centre for Computational Technologies OpenGL Simulation is The Future!
  • 23. Step 2: Set Up • Enable depth testing and double buffering int main(int argc, char **argv) { glutInit (&argc, argv); /* double buffering for smooth animation */ glutInitDisplayMode (GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB); ... /* window creation and callbacks here */ glEnable (GL_DEPTH_TEST); glutMainLoop(); return(0); } Centre for Computational Technologies OpenGL Simulation is The Future!
  • 24. Step 3: Install Callbacks • Create window and set callbacks glutInitWindowSize(500, 500); glutCreateWindow("cube"); glutReshapeFunc(myReshape); glutDisplayFunc(display); glutIdleFunc(spinCube); glutMouseFunc(mouse); glutKeyboardFunc(keyboard); Centre for Computational Technologies OpenGL Simulation is The Future!
  • 25. Step 4: Reshape Callback • Enclose cube, preserve aspect ratio void myReshape(int w, int h) { GLfloat aspect = (GLfloat) w / (GLfloat) h; glViewport(0, 0, w, h); glMatrixMode (GL_PROJECTION); glLoadIdentity(); if (w <= h) /* aspect <= 1 */ glOrtho(-2.0, 2.0, -2.0/aspect, 2.0/aspect, -10.0, 10.0); else /* aspect > 1 */ glOrtho(-2.0*aspect, 2.0*aspect, -2.0, 2.0, -10.0, 10.0); glMatrixMode (GL_MODELVIEW); } Centre for Computational Technologies OpenGL Simulation is The Future!
  • 26. Step 5: Display Callback • Clear, rotate, draw, flush, swap GLfloat theta[3] = {0.0, 0.0, 0.0}; void display(void) { glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glRotatef(theta[0], 1.0, 0.0, 0.0); glRotatef(theta[1], 0.0, 1.0, 0.0); glRotatef(theta[2], 0.0, 0.0, 1.0); colorcube(); glFlush(); glutSwapBuffers(); } Centre for Computational Technologies OpenGL Simulation is The Future!
  • 27. Step 6: Drawing Faces • Call face(a, b, c, d) with vertex index • Orient consistently void colorcube(void) { face(0,3,2,1); face(2,3,7,6); face(0,4,7,3); face(1,2,6,5); face(4,5,6,7); face(0,1,5,4); } Centre for Computational Technologies OpenGL Simulation is The Future!
  • 28. Step 7: Drawing a Face • Use vector form of primitives and attributes void face(int a, int b, int c, int d) { glBegin(GL_POLYGON); glColor3fv(colors[a]); glVertex3fv(vertices[a]); glColor3fv(colors[b]); glVertex3fv(vertices[b]); glColor3fv(colors[c]); glVertex3fv(vertices[c]); glColor3fv(colors[d]); glVertex3fv(vertices[d]); glEnd(); } Centre for Computational Technologies OpenGL Simulation is The Future!
  • 29. Step 8: Animation • Set idle callback GLfloat delta = 2.0; GLint axis = 2; void spinCube() { /* spin cube delta degrees about selected axis */ theta[axis] += delta; if (theta[axis] > 360.0) theta[axis] -= 360.0; /* display result */ glutPostRedisplay(); } Centre for Computational Technologies OpenGL Simulation is The Future!
  • 30. Step 9: Change Axis of Rotation • Mouse callback void mouse (int btn, int state, int x, int y) { if (btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN) axis = 0; if (btn==GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) axis = 1; if (btn==GLUT_RIGHT_BUTTON && state == GLUT_DOWN) axis = 2; } Centre for Computational Technologies OpenGL Simulation is The Future!
  • 31. Step 10: Toggle Rotation or Exit • Keyboard callback void keyboard (unsigned char key, int x, int y) { if (key=='q' || key == 'Q') exit(0); if (key==' ') {stop = !stop;}; if (stop) glutIdleFunc(NULL); else glutIdleFunc(spinCube); } Centre for Computational Technologies OpenGL Simulation is The Future!
  • 32. Summary • Client/Server Model • Callbacks • Double Buffering • Simple Transformations • Example Centre for Computational Technologies OpenGL Simulation is The Future!
  • 33. 33 Questions? Centre for Computational Technologies OpenGL Simulation is The Future!
  • 34. Thank You Mail Us @ sandip@cctech.co.in Visit Us @ www.cctech.co.in Call Us @ +91 20 4009 8381/82 Mobile @ +91 98508 60725 Centre for Computational Technologies Pvt. Ltd. 1, Akshay Residancy, 50 Anand Park, Aundh, Pune -7 Centre for Computational Technologies OpenGL Simulation is The Future!