to do to do computer graphics spring 2008 computer
play

To Do To Do Computer Graphics (Spring 2008) Computer Graphics - PDF document

To Do To Do Computer Graphics (Spring 2008) Computer Graphics (Spring 2008) Start thinking (now) about HW 3. Milestones are due soon. COMS 4160, Lecture 9: OpenGL 1 http://www.cs.columbia.edu/~cs4160 Course Outline Course Outline


  1. To Do To Do Computer Graphics (Spring 2008) Computer Graphics (Spring 2008) � Start thinking (now) about HW 3. Milestones are due soon. COMS 4160, Lecture 9: OpenGL 1 http://www.cs.columbia.edu/~cs4160 Course Outline Course Outline Course Outline Course Outline � 3D Graphics Pipeline � 3D Graphics Pipeline Rendering Rendering Modeling Modeling (Creating, shading images from (Creating, shading images from (Creating 3D Geometry) (Creating 3D Geometry) geometry, lighting, materials) geometry, lighting, materials) Unit 1: Transformations Weeks 1,2. Ass 1 due Feb 14 Unit 3: OpenGL Weeks 5-7. Unit 2: Spline Curves Ass 3 due ?? Weeks 3,4. Ass 2 due Feb 26 Midterm on units 1-3: Mar 10 Demo: Surreal (HW 3) Demo: Surreal (HW 3) Methodology for Lecture Methodology for Lecture � This unit different from others in course � Other units stress mathematical understanding � This stresses implementation details and programming � I am going to show (maybe write) actual code � Same code (with comments) available online to help you understand how to implement basic concepts � I hope the online code helps you understand HW 3 better � ASK QUESTIONS if confused!! � Simple demo 4160-opengl\opengl1\opengl1-orig.exe � This lecture deals with very basic OpenGL setup. Next 2 lectures will likely be more interesting

  2. Outline Introduction to OpenGL Outline Introduction to OpenGL � Basic idea about OpenGL � OpenGL is a graphics API � Software library � Basic setup and buffers � Layer between programmer and graphics hardware (and software) � Matrix modes � OpenGL can fit in many places � Window system interaction and callbacks � Between application and graphics system � Between higher level API and graphics system � Drawing basic OpenGL primitives Best source for OpenGL is the redbook. Of course, this is more a reference manual than a textbook, and you are better off implementing rather reading end to end. Though if you do have time, the book is actually quite readable Programmer Programmer’ ’s View s View OpenGL Rendering Pipeline OpenGL Rendering Pipeline Application Geometry Vertices Primitive Operations Fragment Scan Framebuffer Application Graphics Package Conversion Operations OpenGL Application Programming Interface Pixel Texture Images Operations Memory Hardware and software Many operations controlled by state (projection matrix, transformation matrix, color etc.) Output Device Input Device Input Device OpenGL is a large state machine OpenGL Rendering Pipeline OpenGL Rendering Pipeline GPUs GPUs and Programmability and Programmability Programmable in � Since 2003, can write vertex/pixel shaders Modern GPUs Programmable in Geometry � Fixed function pipeline special type of shader Modern GPUs Vertices Primitive Operations Fragment Scan � Like writing C programs (see back of OpenGL book) Framebuffer Operations Conversion � Performance >> CPU (even used for non-graphics) Pixel Texture Images Operations Memory Traditional Approach: Fixed function pipeline (state machine) New Development (2003-): Programmable pipeline

  3. GPUs and Programmability and Programmability Why OpenGL? GPUs Why OpenGL? � Since 2003, can write vertex/pixel shaders � Fast � Fixed function pipeline special type of shader � Simple � Like writing C programs (see back of OpenGL book) � Window system independent � Performance >> CPU (even used for non-graphics) � Supports some high-end graphics features � But parallel paradigm � Geometric and pixel processing � All pixels/vertices operate in parallel � Severe performance overheads for control flow, loops � Standard, available on many platforms (limitations beginning to be relaxed in modern releases) � Not directly covered in COMS 4160 � But you can make use of in assignments for extra credit Outline Outline Buffers and Window Interactions Buffers and Window Interactions � Basic idea about OpenGL � Buffers: Color (front, back, left, right), depth (z), accumulation, stencil. When you draw, you write to � Basic setup and buffers some buffer (most simply, front and depth) � Matrix modes � No window system interactions (for portability) � Window system interaction and callbacks � But can use GLUT (or Motif, GLX, Tcl/Tk) � Callbacks to implement mouse, keyboard interaction � Drawing basic OpenGL primitives Outline Outline Basic setup code (you will likely copy) Basic setup code (you will likely copy) int main(int argc, char** argv) { � Basic idea about OpenGL glutInit(&argc, argv); � Basic setup and buffers // Requests the type of buffers (Single, RGB). // Think about what buffers you would need... glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); � Matrix modes glutInitWindowSize (500, 500); glutInitWindowPosition (100, 100); � Window system interaction and callbacks glutCreateWindow ("Simple Demo"); init (); // Always initialize first � Drawing basic OpenGL primitives // Now, we define callbacks and functions for various tasks. glutDisplayFunc(display); glutReshapeFunc(reshape) ; glutKeyboardFunc(keyboard); glutMouseFunc(mouse) ; glutMotionFunc(mousedrag) ; glutMainLoop(); // Start the main code return 0; /* ANSI C requires main to return int. */ }

  4. Viewing in OpenGL Basic initialization code Viewing in OpenGL Basic initialization code #include <GL/glut.h> � Viewing consists of two parts #include <stdlib.h> � Object positioning: model view transformation matrix int mouseoldx, mouseoldy ; // For mouse motion � View projection: projection transformation matrix GLdouble eyeloc = 2.0 ; // Where to look from; initially 0 -2, 2 � OpenGL supports both perspective and orthographic viewing transformations void init (void) { � OpenGL’s camera is always at the origin, pointing in the – z /* select clearing color */ glClearColor (0.0, 0.0, 0.0, 0.0); direction /* initialize viewing values */ � Transformations move objects relative to the camera glMatrixMode(GL_PROJECTION); glLoadIdentity(); � Matrices right-multiply top of stack. // Think about this. Why is the up vector not normalized? (Last transform in code is first actually applied) glMatrixMode(GL_MODELVIEW) ; glLoadIdentity() ; gluLookAt(0,-eyeloc,eyeloc,0,0,0,0,1,1) ; } Outline Outline Window System Interaction Window System Interaction � Basic idea about OpenGL � Not part of OpenGL � Basic setup and buffers � Toolkits (GLUT) available � Matrix modes � Callback functions for events � Keyboard, Mouse, etc. � Window system interaction and callbacks � Open, initialize, resize window � Similar to other systems (X, Java, etc.) � Drawing basic OpenGL primitives � Our main func included glutDisplayFunc(display); glutReshapeFunc(reshape) ; glutKeyboardFunc(keyboard); glutMouseFunc(mouse) ; glutMotionFunc(mousedrag) ; Basic window interaction code Basic window interaction code Mouse motion Mouse motion (demo (demo 4160 e ) ) 4160- -opengl1 opengl1\ \opengl1 opengl1- -orig.ex orig.exe /* Defines a Mouse callback to zoom in and out */ /* Defines what to do when various keys are pressed */ /* This is done by modifying gluLookAt */ void keyboard (unsigned char key, int x, int y) /* The actual motion is in mousedrag */ { /* mouse simply sets state for mousedrag */ switch (key) { void mouse(int button, int state, int x, int y) case 27: // Escape to quit { exit(0) ; if (button == GLUT_LEFT_BUTTON) { break ; if (state == GLUT_UP) { default: // Do Nothing ; break ; } } else if (state == GLUT_DOWN) { } mouseoldx = x ; mouseoldy = y ; // so we can move wrt x , y } /* Reshapes the window appropriately */ } void reshape(int w, int h) else if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) { { // Reset gluLookAt glViewport (0, 0, (GLsizei) w, (GLsizei) h); eyeloc = 2.0 ; glMatrixMode(GL_PROJECTION); glMatrixMode(GL_MODELVIEW) ; glLoadIdentity(); glLoadIdentity() ; gluLookAt(0,-eyeloc,eyeloc,0,0,0,0,1,1) ; gluPerspective(30.0, (GLdouble)w/(GLdouble)h, 1.0, 10.0) ; glutPostRedisplay() ; } } }

Recommend


More recommend