CSC 307 1.0
Graphics Programming
Budditha Hettige
Department of Statistics and Computer Science
Graphics Programming
Viewing
2Budditha Hettige
Translate
• void glTranslate{fd}(TYPE x, TYPE y, TYPE z);
3Budditha Hettige
Rotate
void glRotate{fd}(TYPE angle, TYPE x, TYPE y, TYPE z);
glRotatef(45.0, 0.0, 0.0, 1.0) - rotation of 45 degrees
about the z-axis
4Budditha Hettige
Scale
void glScale{fd}(TYPE x, TYPE y, TYPE z);
Example: glScalef(2.0, –0.5, 1.0)
5Budditha Hettige
Viewing and Modeling
Transformations
• Rotating First or Translating First
6Budditha Hettige
Viewpoint
• Object and Viewpoint at the Origin
7Budditha Hettige
Viewpoint and the Object
• Separating the Viewpoint and the Object
• glTranslatef(0.0, 0.0, -5.0);
8Budditha Hettige
Using the gluLookAt()
void gluLookAt
(
GLdouble eyex, GLdouble eyey, GLdouble eyez,
GLdouble centerx, GLdouble centery, GLdouble centerz,
GLdouble upx, GLdouble upy, GLdouble upz
);
9Budditha Hettige
Default Camera Position
10
gluLookAt(0.0, 0.0, 0.0, 0.0, 0.0, -100.0, 0.0, 1.0, 0.0);
Budditha Hettige
Using gluLookAt()
gluLookAt(4.0, 2.0, 1.0, 2.0, 4.0, -3.0, 2.0, 2.0, -1.0);
11Budditha Hettige
Projection Transformations
• Perspective Projection
void glFrustum(GLdouble left, GLdouble right,
GLdouble bottom, GLdouble top,
GLdouble near, GLdouble far);
12Budditha Hettige
Orthographic Projection
void glOrtho(GLdouble left, GLdouble right,
GLdouble bottom, GLdouble top,
GLdouble near, GLdouble far);
13Budditha Hettige
Viewport Transformation
• Defining the Viewport
• void glViewport(GLint x, GLint y,
GLsizei width, GLsizei height);
14Budditha Hettige
The Transformed Depth Coordinate
• void glDepthRange(GLclampd near,
GLclampd far);
15Budditha Hettige
Animation
• Is an important part of computer graphics
• Taking a sequence of pictures and projecting
them at 24 frames per second on the screen
• void glutSwapBuffers(void);
16Budditha Hettige
Example
# include <windows.h>
#include <GL/glut.h>
#include <stdlib.h>
static GLfloat spin = 0.0;
void spinDisplay(void)
{
spin = spin + 0.05;
if (spin > 360.0)
spin = spin - 360.0;
glutPostRedisplay();
}
17
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glRotatef(spin, 0.0, 0.0, 1.0);
glColor3f(1.0, 1.0, 1.0);
glRectf(-25.0, -25.0, 25.0, 25.0);
glPopMatrix();
glutSwapBuffers();
spinDisplay();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-50.0, 50.0, -50.0, 50.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}Budditha Hettige
Example
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(250, 250);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
18Budditha Hettige
Building a Solar System
• planet example (planet.cpp)
19Budditha Hettige
Building an Articulated Robot Arm
• robotArm.cpp
20Budditha Hettige

Viewing

  • 1.
    CSC 307 1.0 GraphicsProgramming Budditha Hettige Department of Statistics and Computer Science
  • 2.
  • 3.
    Translate • void glTranslate{fd}(TYPEx, TYPE y, TYPE z); 3Budditha Hettige
  • 4.
    Rotate void glRotate{fd}(TYPE angle,TYPE x, TYPE y, TYPE z); glRotatef(45.0, 0.0, 0.0, 1.0) - rotation of 45 degrees about the z-axis 4Budditha Hettige
  • 5.
    Scale void glScale{fd}(TYPE x,TYPE y, TYPE z); Example: glScalef(2.0, –0.5, 1.0) 5Budditha Hettige
  • 6.
    Viewing and Modeling Transformations •Rotating First or Translating First 6Budditha Hettige
  • 7.
    Viewpoint • Object andViewpoint at the Origin 7Budditha Hettige
  • 8.
    Viewpoint and theObject • Separating the Viewpoint and the Object • glTranslatef(0.0, 0.0, -5.0); 8Budditha Hettige
  • 9.
    Using the gluLookAt() voidgluLookAt ( GLdouble eyex, GLdouble eyey, GLdouble eyez, GLdouble centerx, GLdouble centery, GLdouble centerz, GLdouble upx, GLdouble upy, GLdouble upz ); 9Budditha Hettige
  • 10.
    Default Camera Position 10 gluLookAt(0.0,0.0, 0.0, 0.0, 0.0, -100.0, 0.0, 1.0, 0.0); Budditha Hettige
  • 11.
    Using gluLookAt() gluLookAt(4.0, 2.0,1.0, 2.0, 4.0, -3.0, 2.0, 2.0, -1.0); 11Budditha Hettige
  • 12.
    Projection Transformations • PerspectiveProjection void glFrustum(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far); 12Budditha Hettige
  • 13.
    Orthographic Projection void glOrtho(GLdoubleleft, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far); 13Budditha Hettige
  • 14.
    Viewport Transformation • Definingthe Viewport • void glViewport(GLint x, GLint y, GLsizei width, GLsizei height); 14Budditha Hettige
  • 15.
    The Transformed DepthCoordinate • void glDepthRange(GLclampd near, GLclampd far); 15Budditha Hettige
  • 16.
    Animation • Is animportant part of computer graphics • Taking a sequence of pictures and projecting them at 24 frames per second on the screen • void glutSwapBuffers(void); 16Budditha Hettige
  • 17.
    Example # include <windows.h> #include<GL/glut.h> #include <stdlib.h> static GLfloat spin = 0.0; void spinDisplay(void) { spin = spin + 0.05; if (spin > 360.0) spin = spin - 360.0; glutPostRedisplay(); } 17 void display(void) { glClear(GL_COLOR_BUFFER_BIT); glPushMatrix(); glRotatef(spin, 0.0, 0.0, 1.0); glColor3f(1.0, 1.0, 1.0); glRectf(-25.0, -25.0, 25.0, 25.0); glPopMatrix(); glutSwapBuffers(); spinDisplay(); } void reshape(int w, int h) { glViewport(0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-50.0, 50.0, -50.0, 50.0, -1.0, 1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); }Budditha Hettige
  • 18.
    Example int main(int argc,char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize(250, 250); glutInitWindowPosition(100, 100); glutCreateWindow(argv[0]); glClearColor(0.0, 0.0, 0.0, 0.0); glShadeModel(GL_FLAT); glutDisplayFunc(display); glutReshapeFunc(reshape); glutMainLoop(); return 0; } 18Budditha Hettige
  • 19.
    Building a SolarSystem • planet example (planet.cpp) 19Budditha Hettige
  • 20.
    Building an ArticulatedRobot Arm • robotArm.cpp 20Budditha Hettige