lecture 3 introduction to opengl glut part 2 prof
play

Lecture 3: Introduction to OpenGL/GLUT (Part 2) Prof Emmanuel Agu - PowerPoint PPT Presentation

Computer Graphics (CS 4731) Lecture 3: Introduction to OpenGL/GLUT (Part 2) Prof Emmanuel Agu Computer Science Dept. Worcester Polytechnic Institute (WPI) Recall: OpenGL/GLUT Basics OpenGL: Specific version (e.g. 4.3)already on your


  1. Computer Graphics (CS 4731) Lecture 3: Introduction to OpenGL/GLUT (Part 2) Prof Emmanuel Agu Computer Science Dept. Worcester Polytechnic Institute (WPI)

  2. Recall: OpenGL/GLUT Basics  OpenGL: Specific version (e.g. 4.3)already on your graphics card Just need to check your graphics card, OpenGL version   GLUT: software that needs to be installed already installed in zoolab machines  GLUT: install it! OpenGL: already on graphics card

  3. Recall: OpenGL Skeleton void main(int argc, char** argv){ // First initialize toolkit, set display mode and create window glutInit(&argc, argv); // initialize toolkit glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(640, 480); glutInitWindowPosition(100, 150); glutCreateWindow(“my first attempt”); glewInit( ); 150 // … then register callback functions, my first attempt 100 // … do my initialization // .. wait in glutMainLoop for events 480 640 }

  4. Recall: Drawing Red Triangle  Rendering steps: Generate triangle corners (3 vertices) 1. Create GPU buffer for vertices 2. Move array of 3 vertices from CPU to GPU buffer 3. Draw 3 points from array on GPU using glDrawArray 4.  Simplified Execution model: 1. Generate 3 4. Move array of 3 vertices triangle corners from CPU to GPU buffer 3. Create GPU buffers 2. Store 3 vertices in array for vertices Application GPU Program (on CPU) 5. Draw points Rendered vertices using glDrawArrays

  5. Recall: OpenGL Skeleton: Where are we? void main(int argc, char** argv){ glutInit(&argc, argv); // initialize toolkit glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(640, 480); glutInitWindowPosition(100, 150); (0.0, 0.5) glutCreateWindow(“my first attempt”); glewInit( ); // … now register callback functions glutDisplayFunc(myDisplay); glutReshapeFunc(myReshape); (-0.5, -0.5) (0.5, -0.5) glutMouseFunc(myMouse); glutKeyboardFunc(myKeyboard); // generate 3 triangle vertices + store in array void generateGeometry( void ){ glewInit( ); points[0] = point2( -0.5, -0.5 ); generateGeometry( ); points[1] = point2( 0.0, 0.5 ); points[2] = point2( 0.5, -0.5 ); } glutMainLoop( ); }

  6. Recall: OpenGL Skeleton: Where are we? void main(int argc, char** argv){ glutInit(&argc, argv); // initialize toolkit glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(640, 480); glutInitWindowPosition(100, 150); VBO glutCreateWindow(“my first attempt”); glewInit( ); VAO VBO VBO // … now register callback functions glutDisplayFunc(myDisplay); glutReshapeFunc(myReshape); void initGPUBuffers( void ) { glutMouseFunc(myMouse); // Create a vertex array object glutKeyboardFunc(myKeyboard); GLuint vao; glGenVertexArrays( 1, &vao ); glBindVertexArray( vao ); glewInit( ); generateGeometry( ); // Create and initialize a buffer object GLuint buffer; initGPUBuffers( ); glGenBuffers( 1, &buffer ); glBindBuffer( GL_ARRAY_BUFFER, buffer ); glBufferData( GL_ARRAY_BUFFER, glutMainLoop( ); sizeof(points), points, GL_STATIC_DRAW ); } }

  7. Recall: OpenGL Program?  Usually has 3 files:  .cpp file: containing OpenGL code, main( ) function  Does initialization, generates/loads geometry to be drawn  Vertex shader: manipulates vertices (e.g. move vertices)  Fragment shader: manipulates pixels/fragments (e.g change color) .cpp program Rendered Image

  8. OpenGL Program: Shader Setup  OpenGL programs now have 3 parts: Main OpenGL program (.cpp file), vertex shader (e.g. vshader1.glsl), and  fragment shader (e.g. fshader1.glsl) in same Windows directory In main program, need to link names of vertex, fragment shader  initShader( ) is homegrown shader initialization function. More later  GLuint = program; GLuint program = InitShader( "vshader1.glsl", fshader1.glsl"); glUseProgram(program); initShader( ) Main Program Homegrown, connects main Program to shader files More on this later!! Vertex shader Fragment Shader

  9. Vertex Attributes Want to make 3 dots (vertices) accessible as variable vPosition in vertex  shader First declare vPosition in vertex shader, get its address  .cpp program (contains in vec4 vPosition main( ) ) Compiler puts all variables declared in shader into a table  Need to find location of vPosition in table of variables  Variable Variable 1 Location of vPosition vPosition …… Variable N GLuint loc = glGetAttribLocation( program, "vPosition" );

  10. Vertex Attributes Variable Variable 1 Location of vPosition vPosition …… Variable N Get location of vertex attribute vPosition GLuint loc = glGetAttribLocation( program, "vPosition" ); glEnableVertexAttribArray( loc ); Enable vertex array attribute at location of vPosition

  11. glVertexAttribPointer Data now in VBO on GPU, but need to specify meta format  (using glVertexAttribPointer ) Vertices are packed as array of values  E.g. 3 dots stored in array on VBO Vertices stored in array -0.5 -0.5 0.0 0.5 0.5 -0.5 VBO x y x y x y x y x y x y y x y x dot 3 dot 2 dot 1 vertex 1 vertex 2 ………. Padding between Consecutive vertices glVertexAttribPointer( loc, 2, GL_FLOAT, GL_FALSE, 0,BUFFER_OFFSET(0) ); Data starts at offset 2 (x,y) floats from start of array per vertex Location of vPosition Data not normalized in table of variables to 0-1 range

  12. Put it Together: Shader Set up void shaderSetup( void ) { // Load shaders and use the resulting shader program program = InitShader( "vshader1.glsl", "fshader1.glsl" ); glUseProgram( program ); // Initialize vertex position attribute from vertex shader GLuint loc = glGetAttribLocation( program, "vPosition" ); glEnableVertexAttribArray( loc ); glVertexAttribPointer( loc, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0) ); // sets white as color used to clear screen glClearColor( 1.0, 1.0, 1.0, 1.0 ); }

  13. OpenGL Skeleton: Where are we? void main(int argc, char** argv){ glutInit(&argc, argv); // initialize toolkit glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(640, 480); glutInitWindowPosition(100, 150); glutCreateWindow(“my first attempt”); glewInit( ); // … now register callback functions glutDisplayFunc(myDisplay); void shaderSetup( void ) glutReshapeFunc(myReshape); { glutMouseFunc(myMouse); // Load shaders and use the resulting shader program program = InitShader( "vshader1.glsl", "fshader1.glsl" ); glutKeyboardFunc(myKeyboard); glUseProgram( program ); // Initialize vertex position attribute from vertex shader glewInit( ); GLuint loc = glGetAttribLocation( program, "vPosition" ); generateGeometry( ); glEnableVertexAttribArray( loc ); initGPUBuffers( ); glVertexAttribPointer( loc, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0) ); void shaderSetup( ); // sets white as color used to clear screen glClearColor( 1.0, 1.0, 1.0, 1.0 ); glutMainLoop( ); } }

  14. Vertex Shader  We write a simple “pass - through” shader  Simply sets output vertex position = input position  gl_Position is built in variable (already declared) in vec4 vPosition void main( ) { gl_Position = vPosition; } input vertex position output vertex position (from .cpp file)

  15. Execution Model 1. Vertex data GPU Moved to GPU (glBufferData) Graphics Hardware (not programmable) Application Vertex Program Vertex Figures out which Shader Vertex (.cpp file Shader Pixels on screen on CPU) Shader Colored to draw dots 2. glDrawArrays Rendered 3. Vertex shader Vertices invoked on each vertex on GPU

  16. Fragment Shader We write a simple fragment shader (sets color of dots to red)  gl_FragColor is built in variable (already declared)  void main( ) { B R G gl_FragColor = vec(1.0, 0.0, 0.0, 1.0); } Set each drawn fragment color to red

  17. Execution Model OpenGL Program Application (.cpp file) Graphics Hardware (not programmable) Figures out Fragment Fragment Frame Buffer fragments (pixels) Shader Fragment to be colored to Shader Shader draw 3 dots 3.Rendered 1. Fragments 2. Fragment shader Fragment corresponding to invoked on each Color Rendered triangle fragment on GPU

  18. Recall: OpenGL Skeleton void main(int argc, char** argv){ // First initialize toolkit, set display mode and create window glutInit(&argc, argv); // initialize toolkit glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(640, 480); glutInitWindowPosition(100, 150); glutCreateWindow(“my first attempt”); glewInit( ); // … now register callback functions glutDisplayFunc(myDisplay);  -- Next… how to draw in myDisplay glutReshapeFunc(myReshape); glutMouseFunc(myMouse); glutKeyboardFunc(myKeyboard); myInit( ); glutMainLoop( ); }

  19. Recall: Draw points (from VBO) glDrawArrays(GL_LINE_LOOP, 0, N); Number of Render buffered Starting points to be data as line loop index rendered Display function using glDrawArrays:  void mydisplay(void){ glClear(GL_COLOR_BUFFER_BIT); // clear screen glDrawArrays(GL_LINE_LOOP, 0, 3); // draw the points glFlush( ); // force rendering to show }

  20. Other possible arguments to glDrawArrays instead of GL_LINE_LOOP? glDrawArrays (GL_POINTS, ….) glDrawArrays ((GL_LINES, … ) – draws dots – Connect vertex pairs to draw lines

  21. glDrawArrays ( ) Parameters glDrawArrays (GL_LINE_STRIP ,..) glDrawArrays (GL_POLYGON,..) – convex filled polygon – polylines glDrawArrays (GL_LINE_LOOP) – Close loop of polylines (Like GL_LINE_STRIP but closed)

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