0% found this document useful (0 votes)
13 views16 pages

- 2 - openGL

Uploaded by

dawitkebedewoldu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views16 pages

- 2 - openGL

Uploaded by

dawitkebedewoldu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Introduction to OpenGL

What is OpenGL?
• “A software interface to graphics hardware”
• Written in C (NOT C++)
• Very fast (a standard to be accelerated)

• Portable

• Open standard

• What it isn’t
– A modeling tool
– A new ‘language’

• Other options:
– Direct3D
– MesaGL
– VirtualGL
OpenGL/GLU/GLUT
• OpenGL is the “core” library that is platform
independent

• GLU is an auxiliary library that handles a variety


of graphics accessory functions

• GLUT is an auxiliary library that handles window


creation, OS system calls (mouse buttons,
movement, keyboard, etc), callbacks.
Headers and Linking
• OpenGL - main stuff (the only thing that is required)
– #include <GL/gl.h>
– Link opengl32.lib (PC)
• opengl.lib is an SGI implementation
• opengl32.lib is a Microsoft implementation

• GLU - auxillary functions


– #include <GL/glu.h>
– Link glu32.lib (PC)
Headers and Libraries
• GLUT - Window management (requires download of
library)
– #include <GL/glut.h>
– Link glut32.lib (PC), or -lglut (UNIX)

• Instead of GLUT, you can use GLX or WGL


GLUT
• GLUT is a Window Manager
– Makes it easy to create windows
– Platform independent
– Gets you up and running, though it isn’t
extremely customizable
– Not meant for “commercial” products
Approach to programming in GL
• Use old code again!

• http://www.opengl.org/documentation/
• Use the function glGetError() to check for errors
• Note about function names:
– gl means OpenGL (ex. glVertex)
– glu means GLU (ex. gluPerspective)
– glut means GLUT (ex. glutCreateWindow)
Basic GL Commands
• glEnable/glDisable
• GL has many states of operation, and the
way you change them is through Enabling
and Disabling them.
• Ex.
– glEnable(GL_DEPTH_TEST);
– glDisable(GL_DEPTH_TEST);
– glDepthFunc(GL_LESS);
Geometry Commands
• glBegin
• glVertex
• glColor
• glNormal
• glTexCoord
• glEnd
Lighting
• glEnable(GL_LIGHTING);
• glEnable(GL_LIGHT0);
• Glfloat fLightPosition[3];
– fLightPosition[0]=1;
– fLightPosition[1]=1;
– fLightPosition[2]=1;
• glLightfv(GL_LIGHT0,GL_POSITION,fLightPosition);
Example
glBegin(GL_TRIANGLES);
glVertex3f(0,1,0);
glVertex3f(0,0,1);
glVertex3f(0,0,0);
glEnd();
glEnable(GL_LIGHTING);
glBegin(GL_TRIANGLES);
glVertex3f(0,0,1);
glVertex3f(1,0,0);
glVertex3f(0,0,0);
glEnd();
glDisable(GL_LIGHTING);
State Machine
• OpenGL has several global variables:
– GL_DEPTH_TEST
– GL_LIGHTING
– GL_SHADE_MODEL
– GL_CULL_FACE
– Color
– Normal
• They are the current values till changed
State Examples
• glBegin(GL_TRIANGLES);
• glColor4f(1.0, 1.0, 1.0, 1.0);
• glVertex3f(0.0, 0.0, 0.0);
• glVertex3f(0.0, 1.0, 0.0);
• glColor4f(1.0, 0.0, 0.0, 1.0);
• glVertex3f(0.0, 0.0, 1.0);
• glEnd();
Stack
• Transformations are based on a stack
architecture.
glVertex3f(1,0,0);
glTranslatef(2,0,0);
glVertex3f(1,0,0);
glRotatef(90,1,0,0);
glTranslatef(2,0,0);
glVertex3f(1,0,0);
Stack

glRotatef(27,.71,.71,0);
glTranslatef(-2,-1,1.2);
glPushMatrix();
glScalef(1.5,1.5,1.5);

glRotatef(45,0,0,1);
glPushMatrix();
glTranslatef(1,0.8,2.4);
Stack
glTranslatef(0.1, 0.5, 0.5);
DrawTorso();
glPushMatrix();
glTranslatef(0.4, 0.4, 0.8);
glRotatef(fArmRotation,0,0,1);
glDrawUpperArm();
glTranslatef(0,0,-1);
glRotatef(fLowerArmRotation,0.2,0.2,0.7);
glPopMatrix();

You might also like