Introduction to opengl
This presentation is the property of its rightful owner.
Sponsored Links
1 / 32

INTRODUCTION TO OPENGL PowerPoint PPT Presentation


  • 179 Views
  • Uploaded on
  • Presentation posted in: General

INTRODUCTION TO OPENGL. INTRODUCTION TO OPENGL. What is OpenGL API Functions Event Driven Programming Using OpenGL. OpenGL. OpenGL is a platform-independent API that is Easy to use Close enough to the hardware to get excellent performance Focus on rendering

Download Presentation

INTRODUCTION TO OPENGL

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Introduction to opengl

INTRODUCTION TO OPENGL


Introduction to opengl1

INTRODUCTION TO OPENGL

  • What is OpenGL API

  • Functions

  • Event Driven Programming Using OpenGL


Opengl

OpenGL

OpenGL is a platform-independent API that is

  • Easy to use

  • Close enough to the hardware to get excellent performance

  • Focus on rendering

  • Omitted windowing and input to avoid window system dependencies


Opengl interface

OpenGL Interface

  • Components of the OpenGL Interface

    • GL: core OpenGL functions

    • GLU: graphics utility library

      (helpers for creating common objects, eg. Spheres)

    • GLUT: GL Utility Toolkit

      (interface to windowing system)

    • GLX: low-level interface to X Windows


Basic opengl syntax

Basic OpenGL Syntax

  • Function names from GL library

    • prefixed with gl

  • Symbolic constants

    • Capital letters with underscore

  • OpenGL built-in data types

  • glBegin, glClear, glCopyPixels

  • GL_2D, GL_RGB, GL_POLYGON

  • GLbyte, GLshort, Gint, GLfloat, GLdouble, GLboolean


Introduction to opengl

GLUT

  • OpenGL Utility Toolkit (GLUT)

    • Provides functionality common to all window systems

      • Open a window

      • Get input from mouse and keyboard

      • Menus

      • Event-driven

    • Code is portable but GLUT lacks the functionality of a good toolkit for a specific platform

      • No slide bars


Introduction to opengl

GLUT

  • Function names from GLUT library

    • prefixed with glut

  • Initializes GLUT and should be called before any OpenGL functions.

  • void glutInit (int argc, char ** argv)

  • void glutCreateWindow (*char title)

  • Creates a window on the screen with the title given by the argument.


Event loops callback function

Event Loops & Callback Function

  • Events– mouse, keyboard, windows events.

  • Callback function – define how the program should react to specific events.

  • The function func() is called each time there is a display callback

  • void glutDisplayFunc (void (*func) (void))


Event loops callback function1

Event Loops & Callback Function

  • void glutMainLoop()

  • Causes the program to enter an event-processing loop.

  • Should be the last function in main().


Opengl function format

OpenGL function format

function name

dimensions

glVertex3f(x,y,z)

x,y,zare floats

belongs to GL library


A simple program

A Simple Program

#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(intargc, char** argv){

glutCreateWindow("simple");

glutDisplayFunc(mydisplay);

glutMainLoop();

}


A simple program1

A Simple Program


Event loop

Event Loop

  • Note that the program defines a display callback function named mydisplay

    • Every glut program must have a display callback

    • The display callback is executed whenever OpenGL decides the display must be refreshed, for example when the window is opened

    • The main function ends with the program entering an event loop


Using rgb color

USING RGB COLOR

#include <GL\glut.h>

void display()

{

glClear (GL_COLOR_BUFFER_BIT);

glColor3f(1.0, 0.0, 0.0);

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(intargc, char** argv)

{

glutInit (&argc, argv);

glutCreateWindow (“Simple");

glClearColor(1.0, 1.0, 1.0, 0.0);

glutDisplayFunc (display);

glutMainLoop ();

}


Using rgb color1

USING RGB COLOR

  • Set a particular color:

  • glColor3f(r, g, b);

  • Set a background color:

  • glClearColor(r, g, b, 1);

  • Clear the window to background color:

  • glClear(GL_COLOR_BUFFER_BIT);


Using rgb color2

USING RGB COLOR


Program structure

Program Structure

  • Most OpenGL programs have a similar structure that consists of the following functions

    • main():

      • defines the callback functions

      • opens one or more windows with the required properties

      • enters event loop (last executable statement)

    • init(): sets the state variables

      • Viewing

      • Attributes

    • callbacks

      • Display function

      • Input and window functions


Introduction to opengl

main

#include <GL/glut.h>

int main(intargc, char** argv)

{

glutInit(&argc,argv);

glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);

glutInitWindowSize(500,500);

glutInitWindowPosition(0,0);

glutCreateWindow("simple");

glutDisplayFunc(mydisplay);

init();

glutMainLoop();

}


Glut functions

GLUT functions

  • glutInitallows application to get command line arguments and initializes system

  • gluInitDisplayModerequests properties for the window (the rendering context)

    • RGB color

    • Single buffering

    • Properties logically ORed together

  • glutWindowSizein pixels

  • glutWindowPositionfrom top-left corner of display

  • glutCreateWindowcreate window with title “simple”

  • glutDisplayFuncdisplay callback

  • glutMainLoopenter infinite event loop


Coordinate systems

Coordinate Systems

  • The units in glVertex are determined by the application and are called object or problem coordinates

  • The viewing specifications are also in object coordinates and it is the size of the viewing volume that determines what will appear in the image

  • Internally, OpenGL will convert to camera (eye) coordinates and later to screen coordinates

  • OpenGL also uses some internal representations that usually are not visible to the application


Transformations and viewing

Transformations and Viewing

  • In OpenGL, projection is carried out by a projection matrix (transformation)

  • There is only one set of transformation functions so we must set the matrix mode first

    glMatrixMode (GL_PROJECTION)

  • Transformation functions are incremental so we start with an identity matrix and alter it with a projection matrix that gives the view volume

    glLoadIdentity();

    glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);


Two and three dimensional viewing

Two- and three-dimensional viewing

  • In glOrtho(left, right, bottom, top, near, far) the near and far distances are measured from the camera

  • Two-dimensional vertex commands place all vertices in the plane z=0

  • If the application is in two dimensions, we can use the function

    gluOrtho2D(left, right,bottom,top)

  • In two dimensions, the view or clipping volume becomes a clipping window


Opengl primitives

OpenGL Primitives

[Edward Angel, Interactive computer Graphics, 2009]

GL_POINTS

GL_LINE_STRIP

GL_POLYGON

GL_LINES

GL_LINE_LOOP

GL_TRIANGLES

GL_QUAD_STRIP

GL_TRIANGLE_FAN

GL_TRIANGLE_STRIP


Event driven programming

Event Driven Programming

  • Mouse Event

    • Event that occurs when the mouse button is pressed or released.

  • Mouse Motion Event

    • Event that occurs when the mouse is moved while one of the buttons is pressed.


Mouse event

Mouse Event

  • Contains the following information:

    • Button: The mouse button that is pressed – left, middle, right.

    • State: The state of the button – up, down.

    • Position: The position of the mouse when the event occurs (x, y).


Mouse event1

Mouse Event

  • Registering with MouseEvent:

  • glutMouseFunc(myMouse);

  • Call-back function:

  • void myMouse(int button, int state, int x, int y)

  • Values for button:

  • GLUT_LEFT_BUTTON

  • GLUT_MIDDLE_BUTTON

  • GLUT_RIGHT_BUTTON

  • Values for state:

  • GLUT_UP

  • GLUT_DOWN

  • x, y:

  • screen coordinates of mouse position

  • (origin at top-left corner)


Mouse motion event

Mouse Motion Event

  • Contains the following information:

    • Position: The current position of the mouse as the mouse is being dragged holding one of the buttons pressed. The events are continuously generated as the mouse button is pressed and dragged.


Mouse motion event1

Mouse Motion Event

  • Registering with MouseMotionEvent:

  • glutMotionFunc(myMovedMouse);

  • Call-back function:

  • void myMovedMouse(int x, int y)

  • x, y:

  • screen coordinates of mouse position

  • (origin at top-left corner)


Keyboard event

Keyboard Event

  • Contains the following information:

    • key: The ASCII value of the key pressed.

    • Position: The current position of the mouse when the key is pressed.


Keyboard event1

Keyboard Event

  • Registering with KeyboardEvent:

  • glutKeyboardFunc(myKeyboard);

  • Call-back function:

  • void myKeyboard(unsigned int key, int x, int y)

  • Values for keys:

  • ASCII values for normal keys

  • GLUT_KEY_LEFT, GLUT_KEY_RIGHT, … (arrow keys)

  • x, y:

  • screen coordinates of mouse position

  • (origin at top-left corner)


Window events

Window Events

  • The Window Redraw Event occurs whenever the window needs to be redrawn. This happens when the window is first opened and when the window is exposed by moving another window off of it.

  • The Window Reshape Event is generated when the window is resized with the mouse.


Window events1

Window Events

  • Registering with WindowRedrawEvent:

  • glutDisplayFunc(myDisplay);

  • Registering with WindowReshapeEvent:

  • glutReshapeFunc(Reshape);


  • Login