Comp 410/510 Computer Graphics Spring Programming with OpenGL Part 4: Three Dimensions

Similar documents
Programming with OpenGL Part 5: More GLSL. Ed Angel Professor Emeritus of Computer Science University of New Mexico

Objectives. Programming with OpenGL Part 5: More GLSL. Program Object. Reading a Shader. Shader Reader. Linking Shaders with Application

Computer Graphics CS 543 Lecture 4 (Part 2) Building 3D Models (Part 2)

Building Models. CS 537 Interactive Computer Graphics Prof. David E. Breen Department of Computer Science

Comp 410/510 Computer Graphics Spring Programming with OpenGL Part 2: First Program

Computer Graphics (CS 4731) Lecture 11: Implementing Transformations. Prof Emmanuel Agu. Computer Science Dept. Worcester Polytechnic Institute (WPI)

CS 432 Interactive Computer Graphics

Interaction. CSCI 420 Computer Graphics Lecture 3

2/3/16. Interaction. Triangles (Clarification) Choice of Programming Language. Buffer Objects. The CPU-GPU bus. CSCI 420 Computer Graphics Lecture 3

Building Models. Prof. George Wolberg Dept. of Computer Science City College of New York

OpenGL pipeline Evolution and OpenGL Shading Language (GLSL) Part 2/3 Vertex and Fragment Shaders

Computer Graphics (CS 4731) Lecture 11: Implementing Transformations. Prof Emmanuel Agu. Computer Science Dept. Worcester Polytechnic Institute (WPI)

Lecture 3. Programming with OpenGL GLUT + GLEW. CS 354 Computer Graphics Sunday, January 20, 13

Transformations. CS 537 Interactive Computer Graphics Prof. David E. Breen Department of Computer Science

Objectives. transformation. General Transformations. Affine Transformations. Notation. Pipeline Implementation. Introduce standard transformations

COMP371 COMPUTER GRAPHICS

Today s Agenda. Shaders fundamentals. Programming with shader-based OpenGL

Geometry Shaders. And how to use them

Information Coding / Computer Graphics, ISY, LiTH. OpenGL! ! where it fits!! what it contains!! how you work with it 11(40)

Comp 410/510 Computer Graphics Spring Input & Interaction

Comp 410/510 Computer Graphics Spring Programming with OpenGL Part 3: Shaders

CS452/552; EE465/505. Review & Examples

Computer Graphics CS 543 Lecture 5 (Part 2) Implementing Transformations

Tutorial 04. Harshavardhan Kode. September 14, 2015

The Projection Matrix

Programming with OpenGL Part 3: Three Dimensions

CS452/552; EE465/505. Models & Viewing

gvirtualxray Tutorial 01: Creating a Window and an OpenGL Context Using GLUT

The Projection Matrix

Computer Graphics (CS 543) Lecture 4a: Linear Algebra for Graphics (Points, Scalars, Vectors)

Meshing and Geometry

CS452/552; EE465/505. Transformations

Ulf Assarsson Department of Computer Engineering Chalmers University of Technology

Lecture 3 Advanced Computer Graphics (CS & SE )

Computer Graphics (CS 543) Lecture 3b: Shader Setup & GLSL Introduction

Shadows. Prof. George Wolberg Dept. of Computer Science City College of New York

Shaders. Introduction. OpenGL Grows via Extensions. OpenGL Extensions. OpenGL 2.0 Added Shaders. Shaders Enable Many New Effects

Building Models. Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 2015

CSE 167: Introduction to Computer Graphics Lecture #7: GLSL. Jürgen P. Schulze, Ph.D. University of California, San Diego Spring Quarter 2016

Programming with OpenGL Shaders I. Adapted From: Ed Angel Professor of Emeritus of Computer Science University of New Mexico

CS 543 Lecture 1 (Part 3) Prof Emmanuel Agu. Computer Science Dept. Worcester Polytechnic Institute (WPI)

Programming with OpenGL Shaders I. Adapted From: Ed Angel Professor of Emeritus of Computer Science University of New Mexico

Programming with OpenGL Part 3: Shaders. Ed Angel Professor of Emeritus of Computer Science University of New Mexico

Announcement. Homework 1 has been posted in dropbox and course website. Due: 1:15 pm, Monday, September 12

CS452/552; EE465/505. Image Processing Frame Buffer Objects

Introduction to Computer Graphics with WebGL

Recall: Indexing into Cube Map

OpenGL/GLUT Intro. Week 1, Fri Jan 12

OpenGL refresher. Advanced Computer Graphics 2012

WebGL A quick introduction. J. Madeira V. 0.2 September 2017

BCA611 Video Oyunları için 3B Grafik

CS475/CS675 - Computer Graphics. OpenGL Drawing

C P S C 314 S H A D E R S, O P E N G L, & J S RENDERING PIPELINE. Mikhail Bessmeltsev

CSE 167. Discussion 03 ft. Glynn 10/16/2017

Shaders. Slide credit to Prof. Zwicker

3D Graphics and OpenGl. First Steps

Computer Viewing. CS 537 Interactive Computer Graphics Prof. David E. Breen Department of Computer Science

CS452/552; EE465/505. Image Formation

Models and Architectures. Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 2015

Interaction. CSCI 480 Computer Graphics Lecture 3

Introduction to Computer Graphics with WebGL

object (say a cube) will be made up of triangles, each with three vertices, each with known object coordinates.

Building Models. Objectives Introduce simple data structures for building polygonal models. Vertex lists Edge lists

// double buffering and RGB glutinitdisplaymode(glut_double GLUT_RGBA); // your own initializations

Interaction Computer Graphics I Lecture 3

Shader Programs. Lecture 30 Subsections 2.8.2, Robb T. Koether. Hampden-Sydney College. Wed, Nov 16, 2011

Computer Graphics Seminar

Today s Agenda. Basic design of a graphics system. Introduction to OpenGL

Introduction to Computer Graphics. Hardware Acceleration Review

CS 548: COMPUTER GRAPHICS INTRODUCTION TO OPENGL AND GLUT SPRING 2015 DR. MICHAEL J. REALE

OPENGL RENDERING PIPELINE

The Graphics Pipeline

Rasterization Overview

Animation Essentially a question of flipping between many still images, fast enough

Lecture 5 Vertex and Fragment Shaders-1. CITS3003 Graphics & Animation

The Graphics Pipeline

Computer Graphics (4731) Lecture 4: 2D Graphics Systems (Drawing Polylines, tiling, & Aspect Ratio)

ERKELEY DAVIS IRVINE LOS ANGELES RIVERSIDE SAN DIEGO SAN FRANCISCO EECS 104. Fundamentals of Computer Graphics. OpenGL

Mobile Application Programing: Android. OpenGL Operation

Programming with OpenGL Complete Programs Objectives Build a complete first program

Graphics Programming. Computer Graphics, VT 2016 Lecture 2, Chapter 2. Fredrik Nysjö Centre for Image analysis Uppsala University

CS559 Computer Graphics Fall 2015

Intro to OpenGL III. Don Fussell Computer Science Department The University of Texas at Austin

Methodology for Lecture

CPSC 436D Video Game Programming

Computer Graphics (CS 4731) & 2D Graphics Systems

From system point of view, a graphics application handles the user input, changes the internal state, called the virtual world by modeling or

Mobile Application Programming: Android. OpenGL Operation

WebGL: Hands On. DevCon5 NYC Kenneth Russell Software Engineer, Google, Inc. Chair, WebGL Working Group

QUESTION 1 [10] 2 COS340-A October/November 2009

Lecture 5: Viewing. CSE Computer Graphics (Fall 2010)

INTRODUCTION TO OPENGL PIPELINE

Computação Gráfica. Computer Graphics Engenharia Informática (11569) 3º ano, 2º semestre. Chap. 4 Windows and Viewports

DONALD HOUSE: CPSC 8170, FALL 2018 DESIGN OF A REAL-TIME ANIMATION PROGRAM

CS451Real-time Rendering Pipeline

Blue colour text questions Black colour text sample answers Red colour text further explanation or references for the sample answers

compatibility mode core mode

Computer Graphics. Bing-Yu Chen National Taiwan University

Ulf Assarsson Department of Computer Engineering Chalmers University of Technology

Copyright Khronos Group 2012 Page 1. Teaching GL. Dave Shreiner Director, Graphics and GPU Computing, ARM 1 December 2012

Transcription:

Comp 410/510 Computer Graphics Spring 2018 Programming with OpenGL Part 4: Three Dimensions

Objectives Develop a bit more sophisticated three-dimensional example - Rotating cube Introduce hidden-surface removal

Three-dimensional Applications In OpenGL, two-dimensional applications are a special case of three-dimensional graphics Going to 3D - Not much change - Use vec3, gluniform3f - Have to worry about the order in which primitives are rendered or use hidden-surface removal

Example Rotating cube (see the code) Create a 3D cube with different colors assigned to corners Need to rotate (continuously) Need to set up transformation ( modelview ) matrix We will also see briefly how to set up projection matrix A bit more about vertex shader Hidden surface removal

Adding Color Colors are ultimately set in the fragment shader but can be determined in either shader or in the application If we set a color in the application, we can send it to the shaders as a vertex attribute or as a uniform variable depending on how often it changes In this example, associate a color with each vertex Set up an array of same size as positions Send to GPU as a vertex buffer object

Smooth Color Default is smooth shading - OpenGL interpolates vertex colors across visible triangles Alternative is flat shading - Color of first vertex determines the fill color - Handled in shader or in application

Setting Colors typedef vec3 color3; color3 colors[numvertices]; vec3 points[numvertices]; //loop for setting positions and colors colors[i] =... position[i] =...

Setting Up Buffer Object //need larger buffer glbufferdata(gl_array_buffer, sizeof(points) + sizeof(colors), NULL, GL_STATIC_DRAW); //load data separately glbuffersubdata(gl_array_buffer, 0, sizeof(points), points); glbuffersubdata(gl_array_buffer, sizeof(points), sizeof(colors), colors);

Second Vertex Attribute Array // vposition and vcolor identifiers in vertex shader loc = glgetattriblocation(program, vposition ); glenablevertexattribarray(loc); glvertexattribpointer(loc, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0)); loc2 = glgetattriblocation(program, vcolor ); glenablevertexattribarray(loc2); glvertexattribpointer(loc2, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(sizeofpoints));

Modelview and Projection ModelView and Projection are uniform variables - 4x4 matrices - sent as input to vertex shader Code from init() function: ModelView = glgetuniformlocation(program, "ModelView ); Projection = glgetuniformlocation(program, "Projection ); Vertex shader code: in vec4 vposition, vcolor; out vec4 color; uniform mat4 ModelView, Projection; void main() { gl_position = Projection * ModelView * vposition; color = vcolor; }

Setting up Modelview Matrix Has to be set up in display callback since the cube keeps rotating void display( void ) { glclear(gl_color_buffer_bit GL_DEPTH_BUFFER_BIT); // Generate the model-view matrix const vec3 displacement( 0.0, 0.0, 0.0 ); mat4 model_view = ( Translate( displacement ) * RotateX( Theta[Xaxis] ) * RotateY( Theta[Yaxis] ) * RotateZ( Theta[Zaxis] ) ); gluniformmatrix4fv( ModelView, 1, GL_TRUE, model_view ); gldrawarrays( GL_TRIANGLES, 0, NumVertices ); glutswapbuffers(); } Translate(), RotateX(), RotateY(), RotateZ(): user-defined functions in mat.h Theta is a global array

Setting up Projection Matrix Better to define in init()since it is often set only once Defult is orthographic but you can use perspective as well Use functions from mat.h mat4 projection; projection = Ortho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); //projection = Perspective( 45.0, 1.0, 0.5, 3.0 ); gluniformmatrix4fv( Projection, 1, GL_TRUE, projection );

Idle Callback Idle callback specifies function to be executed when no other actions pending - glutidlefunc(myidle); void myidle() { /* here recompute display; change sth */ } glutpostredisplay();

Adding Double Buffering Request a double buffer - glutinitdisplaymode(glut_double GLUT_RGB) Swap buffers in display callback void mydisplay() { glclear(gl_color_buffer_bit); gldrawarrays( ); glutswapbuffers(); }

Double Buffering Updating the value of a uniform variable opens the door to animating in an application - Execute gluniform in display callback and change state - Force a redraw through glutpostredisplay() Need to prevent a partially redrawn frame buffer from being displayed Draw into back buffer Display front buffer Swap buffers after updating finished

Hidden-Surface Removal We want to see only those surfaces in front of other surfaces OpenGL uses a hidden-surface method called the z- buffer algorithm Saves depth information as objects are rendered so that only front objects appear in the image

Using the z-buffer algorithm The algorithm uses an extra buffer, i.e., z-buffer, to store depth information as geometry travels down the pipeline It must be - Requested in main() glutinitdisplaymode (GLUT_SINGLE GLUT_RGB GLUT_DEPTH) - Enabled in init() glenable(gl_depth_test) - Cleared in the display callback glclear(gl_color_buffer_bit GL_DEPTH_BUFFER_BIT)