GL_COLOR_BUFFER_BIT, GL_PROJECTION, GL_MODELVIEW

Similar documents
Programming with OpenGL Part 2: Complete Programs Computer Graphics I, Fall

Computer Graphics Course 2005

Programming using OpenGL: A first Introduction

CMSC 425: Lecture 4 More about OpenGL and GLUT Tuesday, Feb 5, 2013

Introduction to Computer Graphics with OpenGL/GLUT

Lecture 2 CISC440/640 Spring Department of Computer and Information Science

Graphics Programming

Lectures OpenGL Introduction

Drawing Primitives. OpenGL basics

CS559: Computer Graphics. Lecture 12: OpenGL Li Zhang Spring 2008

RECITATION - 1. Ceng477 Fall

Programming with OpenGL Part 1: Background

Teacher Assistant : Tamir Grossinger Reception hours: by - Building 37 / office -102 Assignments: 4 programing using

Introduction to OpenGL Week 1

Objectives. Image Formation Revisited. Physical Approaches. The Programmer s Interface. Practical Approach. Introduction to OpenGL Week 1

COMPUTER GRAPHICS LAB # 3

CS Computer Graphics: OpenGL, Continued

CS Computer Graphics: OpenGL, Continued

Introduction to OpenGL

Basic Graphics Programming

To Do. Computer Graphics (Fall 2008) Course Outline. Course Outline. Methodology for Lecture. Demo: Surreal (HW 3)

GLUT. What is OpenGL? Introduction to OpenGL and GLUT

Computer Graphics. Making Pictures. Computer Graphics CSC470 1

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

Computer Graphics. Bing-Yu Chen National Taiwan University

Computer Graphics. OpenGL

Graphics Programming. August 31, Programming of the Sierpinski gasket. Programming with OpenGL and C/C++

Announcements OpenGL. Computer Graphics. Autumn 2009 CS4815

Announcements OpenGL. Computer Graphics. Spring CS4815

Lecture 4 of 41. Lab 1a: OpenGL Basics

CS 4731 Lecture 3: Introduction to OpenGL and GLUT: Part II. Emmanuel Agu

Precept 2 Aleksey Boyko February 18, 2011

CS 432 Interactive Computer Graphics

Lecture 3 Advanced Computer Graphics (CS & SE )

Computer Graphics 1 Computer Graphics 1

VR-programming tools (procedural) More VRML later in this course! (declarative)

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

Computer Graphics, Chapt 08

Computer Graphics (Basic OpenGL)

C++ is Fun Part 13 at Turbine/Warner Bros.! Russell Hanson

Early History of APIs. PHIGS and X. SGI and GL. Programming with OpenGL Part 1: Background. Objectives

Programming with OpenGL Part 3: Three Dimensions

An Overview GLUT GLSL GLEW

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

2. OpenGL -I. 2.1 What is OpenGL? Things OpenGL can do: -23-

Programming with OpenGL Part 1: Background

OpenGL/GLUT Intro. Week 1, Fri Jan 12

Lecture 2 2D transformations Introduction to OpenGL

CS Computer Graphics: Intro to OpenGL

CS Computer Graphics: Intro to OpenGL

Introduction to OpenGL

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


Assignment 1. Simple Graphics program using OpenGL

Duc Nguyen CSE 420 Computer Graphics 10/10/2018 Homework 1

by modifying the glutinitwindowsize() function you can change the screen size to whatever you please.

Image Processing. Geometry Processing. Reading: (Not really covered in our text. See Sects 18.1, 18.2.) Overview: Display

Lecture 3. Understanding of OPenGL programming

Computer Graphics Primitive Attributes

OpenGL Introduction Computer Graphics and Visualization

Cameras (and eye) Ideal Pinhole. Real Pinhole. Real + lens. Depth of field

CS 4204 Computer Graphics

Computer graphic -- Programming with OpenGL 2

Computer Graphics. Downloaded from: LectureNotes 1 CSC-254

CS 543 Lecture 1 (Part II): Intro to OpenGL and GLUT (Part I) Emmanuel Agu

Project Sketchpad. Ivan Sutherland (MIT 1963) established the basic interactive paradigm that characterizes interactive computer graphics:

Graphics Programming. 1. The Sierpinski Gasket. Chapter 2. Introduction:

Computer Graphics Introduction to OpenGL

Basic Graphics Programming

3D computer graphics: geometric modeling of objects in the computer and rendering them

COMP 371/4 Computer Graphics Week 1

Overview of Graphics Systems Hearn & Baker Chapter 2. Some slides are taken from Robert Thomsons notes.

C OMPUTER G RAPHICS Thursday

Computer graphics MN1

2 Transformations and Homogeneous Coordinates

Lecture 2. Determinants. Ax = 0. a 11 x 1 + a 12 x a 1n x n = 0 a 21 x 1 + a 22 x a 2n x n = 0

11/1/13. Basic Graphics Programming. Teaching Assistant. What is OpenGL. Course Producer. Where is OpenGL used. Graphics library (API)

Books, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, PointClouds, and G3D

Computer Graphics Anatomy of GUI. Computer Graphics CSC470 1

Rendering. Part 1 An introduction to OpenGL

UNIT 7 LIGHTING AND SHADING. 1. Explain phong lighting model. Indicate the advantages and disadvantages. (Jun2012) 10M

OpenGL Tutorial. Ceng 477 Introduction to Computer Graphics

1 (Practice 1) Introduction to OpenGL

CS418 OpenGL & GLUT Programming Tutorial (I) Presented by : Wei-Wen Feng 1/30/2008

Display Lists in OpenGL

Introduction to OpenGL: Part 2

CSC Graphics Programming. Budditha Hettige Department of Statistics and Computer Science

CS559: Computer Graphics. Lecture 12: OpenGL Transformation Li Zhang Spring 2008

OpenGL refresher. Advanced Computer Graphics 2012

OpenGL JOGL. OpenGL & JOGL. Shaoting Zhang, or Tony. September 12, 2007

Books, OpenGL, GLUT, CUDA, OpenCL, OpenCV, PointClouds, G3D, and Qt

OpenGL Programming Guide Chapter 1, Introduction to OpenGL 1

An Interactive Introduction to OpenGL Programming

Chapter 13 Selection and Feedback

Input and Interaction

Introduction to OpenGL. CSCI 4229/5229 Computer Graphics Fall 2012

Computer Graphics (CS 4731) OpenGL/GLUT(Part 1)

Bob s Concise Introduction to Doxygen

OpenGL. Jimmy Johansson Norrköping Visualization and Interaction Studio Linköping University

Computer graphics MN1

Transcription:

OpenGL Syntax Functions have prefix gl and initial capital letters for each word glclearcolor(), glenable(), glpushmatrix() glu for GLU functions glulookat(), gluperspective() constants begin with GL_, use all capital letters GL_COLOR_BUFFER_BIT, GL_PROJECTION, GL_MODELVIEW Extra letters in some commands indicate the number and type of variables glcolor3f(), glvertex3f() OpenGL data types GLfloat, GLdouble, GLint, GLenum,

OpenGL function format function name dimensions glvertex3f(x,y,z) belongs to GL library x,y,z are floats glvertex3fv(p) p is a pointer to an array

OpenGL Syntax Examples Setting the current color using glcolor Colors may have 3 components (RGB) or 4 components (RGBA). Think of A (or alpha) as opacity. Floating point - color component values range from 0 to 1 glcolor3f(0.0, 0.5, 1.0); This is 0% Red, 50% Green, 100% Blue; glcolor4f(0.0, 0.5, 1.0, 0.3); This is 0% Red, 50% Green, 100% Blue, 30% Opacity GLfloat color[4] = { 0.0, 0.5, 1.0, 0.3 }; glcolor4fv(color); 0% Red, 50% Green, 100% Blue, 30% Opacity

OpenGL Syntax Examples Unsigned byte color component values range from 0 to 255 (same as C s unsigned char). glcolor3ub (0, 127, 255); This is: 0% Red, 50% Green, 100% Blue glcolor4ub (0, 127, 255, 76); This is 0% Red, 50% Green, 100% Blue, 30% Opacity

Windowing with OpenGL OpenGL is independent of any specific window system GLUT provide a portable API for creating window and interacting with I/O devices

GLUT Developed by Mark Kilgard Hides the complexities of differing window system APIs Default user interface for class projects Glut routines have prefix glut glutcreatewindow() Has very limited GUI interface Glui is the C++ extension of glut that provides buttons, checkboxes, radio buttons, etc.

Glut Routines Initialization:glutInit() processes (and removes) command line arguments that may be of interest to glut and the window system and does general initialization of Glut and OpenGL Must be called before any other glut routines Display Mode: The next procedure, glutinitdisplaymode(), performs initializations informing OpenGL how to set up the frame buffer. Display Mode Meaning GLUT_RGB Use RGB colors GLUT_RGBA Use RGB plus alpha (for transparency) GLUT_DOUBLE Use double buffering (recommended) GLUT_SINGLE recommended) Use single buffering (not GLUT_DEPTH removal.) Use depth buffer (for hidden surface

Glut Routines Window Setup glutinitwindowsize(int width,int height) glutinitwindowposition(int x, int y) glutcreatewindow(char* title)

A Simple Program Generate a square on a solid background

simple.c #include <GL/glut.h> void mydisplay() { glclear(gl_color_buffer_bit); glbegin(gl_polygon); glvertex2f(-0.5, -0.5); glvertex2f(-0.5, 0.5); glvertex2f(0.5, 0.5); glvertex2f(0.5, -0.5); glend(); glflush(); } int main(int argc, char** argv) { glutinit(&argc, argv); glutinitdisplaymode (GLUT_SINGLE GLUT_RGB); glutinitwindowsize(500,500); glutinitwindowposition(0,0); glutcreatewindow("simple"); glutdisplayfunc(mydisplay); init(); glutmainloop(); }

The function main() #include <GL/glut.h> int main(int argc, char** argv) { glutinit(&argc,argv); glutinitdisplaymode(glut_single GLUT_RGB); glutinitwindowsize(500,500); glutinitwindowposition(0,0); } glutcreatewindow("simple"); glutdisplayfunc(mydisplay); init(); glutmainloop(); includes gl.h and glu.h define window properties display callback set OpenGL state enter event loop The program goes into a infinite loop waiting for events

The function init() void init() { glclearcolor (0.0, 0.0, 0.0, 1.0); black clear color opaque window glcolor3f(1.0, 1.0, 1.0); fill/draw with white } glmatrixmode (GL_PROJECTION); glloadidentity (); gluortho2d(-1.0, 1.0, -1.0, 1.0); viewport

Callback functions Most of window-based programs are event-driven Even driven means do nothing until an event happens, and then execute some predefined functions Events key press, mouse button press and release, window resize, etc.

Callbacks Virtually all interactive graphics programs are event driven Glut uses callbacks to handle events Windows system invokes a particular procedure when an event of particular type occurs. MOST IMPORTANT: display event Signaled when window first displays and whenever portions of the window reveals from blocking window glutdisplayfunc(void (*func)(void)) registers the display callback function Running the program: glutmainloop() Main event loop. Never exit()

More Callbacks glutreshapefunc(void (*func)(int w, int h)) indicates what action should be taken when the window is resized. glutkeyboardfunc(void (*func)(unsigned char key, int x, int y)) and glutmousefunc(void (*func)(int button, int state, int x, int y)) allow you to link a keyboard key or a mouse button with a routine that's invoked when the key or mouse button is pressed or released. glutmotionfunc(void (*func)(int x, int y)) registers a routine to call back when the mouse is moved while a mouse button is also pressed. glutmousefunc(void (*func)(int button, int state, int x, int y)) registers a function that's to be executed if a mouse button event occurs. The argument button can be GLUT_LEFT_BUTTON or GLUT_RIGHT_BUTTON. The argument state can be GLUT_UP or GLUT_DOWN. The arguments x and y indicated the mouse cursor position when the button was clicked.

glutdisplayfunc(void (*func)(void ) ) int main(int argc, char** argv) { glutdisplayfunc(mydisplay); } void display() the function you provide. It contains all the OpenGL drawing function calls and will be called when pixels in the window need to be refreshed.

Event Queue Event queue Keyboard. MainLoop() Mouse Window Mouse_callback() {. { Keypress_callback() {. { window_callback() {. {

glut Functions glutkeyboardfunc() register the callback that will be called when a key is pressed glutmousefunc() register the callback that will be called when a mouse button is pressed glutmotionfunc() register the callback that will be called when the mouse is in motion while a buton is pressed glutidlefunc() register the callback that will be called when nothing is going on (no event)

OpenGL Drawing Steps in the display function Clear the window Set drawing attributes Send drawing commands Flush the buffer

Clear the Window glclear(gl_color_buffer_bit) Clears the frame buffer by overwriting it with the background color. Background color is a state set by glclearcolor(glfloat r, GLfloat g, GLfloat b, GLfloat a) in the init().

Drawing Attributes: Color glcolor3f(glfloat r, GLfloat g, GLfloat b) sets the drawing color glcolor3d(), glcolor3ui() can also be used OpenGL is a state machine Once set, the attribute applies to all subsequent defined objects until it is set to some other value glcolor3fv() takes a flat array as input

A color lookup table with 24 bits per entry that is accessed from a frame buffer with 8 bits per pixel. A value of 196 stored at pixel position (x, y) references the location in this table containing the hexadecimal value 0x0821 (a decimal value of 2081). Each 8-bit segment of this entry controls the intensity level of one of the three electron guns in an RGB monitor.

Drawing Commands Simple Objects glrectf() Complex Objects Use construct glbegin(mode) and glend() and a list of vertices in between glbegin(mode) glvertex(v0); glvertex(v1);... glend();

Drawing Attributes Besides glvertex() commands, other attributes commands can also be used between glbegin() and glend(), e.g. glcolor3f(). There are more drawing attributes than color Point size: glpointsize() Line width: gllinewidth() Dash or dotted line: gllinestipple() Polygon pattern: glpolygonstipple()

Primitive Types

Polygon Issues OpenGL will only display polygons correctly that are Simple: edges cannot cross Convex: All points on line segment between two points in a polygon are also in the polygon Flat: all vertices are in the same plane User program can check if above true OpenGL will produce output if these conditions are violated but it may not be what is desired Triangles satisfy all conditions nonsimple polygon nonconvex polygon

Display Callback void mydisplay() { glclear(gl_color_buffer_bit); glbegin(gl_polygon); glvertex2f(-0.5, -0.5); glvertex2f(-0.5, 0.5); glvertex2f(0.5, 0.5); glvertex2f(0.5, -0.5); glend(); glflush(); }

Projection and Viewport

Orthographic projection Orthographic projection used for 2D drawing, Perspective project often used for 3D drawing 2D Viewing: Orthographic View gluortho2d(left, right, bottom, top) Specifies the coordinates of 2D region to be projected into the viewport. Any drawing outside the region will be automatically clipped away. z=0

Viewports Do not have to use the entire window for the image: glviewport(x,y,w,h) Values in pixels (screen coordinates)

Window to Viewport mapping Aspect Ratio: Height/Width If the aspect ratio of the window Is different from that of the viewport, the picture will be distorted.