Docsity

An Introduction to OpenGL Programming: Architecture and Functions, Study notes of Computer Science

Partial preview of the text

Download An Introduction to OpenGL Programming: Architecture and Functions and more Study notes Computer Science in PDF only on Docsity!

Programming with OpenGL

Part 1: Background

Adapted from Edward Angel, UNM Angel: Interactive Computer Graphics 3E © Addison-Wesley 2002

Objectives

• Get you started ASAP!!!

• OpenGL Architecture

  • OpenGL as a state machine

• Functions

  • Types
  • Formats

• Simple program

Angel: Interactive Computer Graphics 3E © Addison-Wesley 2002^3

SGI and GL

• Silicon Graphics (SGI) revolutionized the

graphics workstation by implementing the

pipeline in hardware (1982)

• To access the system, application

programmers used a library called GL

• With GL, it was relatively simple to

program three dimensional interactive

applications

OpenGL

• The success of GL lead to OpenGL

(1992), a platform-independent API that

was

  • Easy to use
  • Close enough to the hardware to get excellent performance
  • Focus on rendering
  • Omitted windowing and input to avoid window system dependencies

Angel: Interactive Computer Graphics 3E © Addison-Wesley 2002^7

GLUT

• OpenGL Utility Library (GLUT) (glut32.lib)

  • 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 - Slide bars Download at: http://

Software Organization

GLUT GLU GL

GLX, AGL or WGL X, Win32, Mac O/S

software and/or hardware

application program

widget or similarOpenGL Motif

Angel: Interactive Computer Graphics 3E © Addison-Wesley 2002^9

OpenGL Architecture

Immediate Mode

Display List

Polynomial Evaluator

Per Vertex Operations &Primitive Assembly

Rasterization Per FragmentOperations

MemoryTexture

CPU

Pixel Operations

FrameBuffer

Geometric pipeline

curves & surfaces

OpenGL Functions

  • Primitives
    • Points
    • Line Segments
    • Polygons
    • (curves and surfaces…maybe later!)
  • Attributes
  • Transformations
    • Viewing
    • Modeling
  • Control
  • Input (GLUT)

Angel: Interactive Computer Graphics 3E © Addison-Wesley 2002^13

OpenGL function format

glVertex3f(x,y,z)

belongs to GL library

function name

x,y,z are floats

glVertex3fv(p) p is a pointer to an array

OpenGL #defines

  • Most constants are defined in the include files

gl.h , glu.h and glut.h

  • Note #include <glut.h> should automatically include the others
  • Examples
  • glBegin(GL_POLYGON)
  • glClear(GL_COLOR_BUFFER_BIT)
  • often used as switches for functions
  • include files also define OpenGL data types:

Glfloat , Gldouble ,….

Angel: Interactive Computer Graphics 3E © Addison-Wesley 2002^15

A Simple Program

Generate a square on a solid background

simple.c

#include <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){ glutCreateWindow("simple"); glutDisplayFunc(mydisplay); glutMainLoop(); }**

Compilation on Windows

• .Net

  • Get glut.h, glut32.lib and glut32.dll
    • http://www.opengl.org/resources/libraries/glut.html
  • Create a console application
  • Add opengl32.lib, glut32.lib, glut32.lib to project settings (under link tab)
  • Make sure the compiler can find the includes and libraries (under options in VC++)