introduction to opengl & hw1 announcement 劉軒銘, 網媒所 碩二 icg 2012 fall

39
Introduction to OpenGL & HW1 Announcement 劉劉劉 , 劉劉劉 劉劉 ICG 2012 Fall

Upload: beatrix-cole

Post on 19-Jan-2016

231 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to OpenGL & HW1 Announcement 劉軒銘, 網媒所 碩二 ICG 2012 Fall

Introduction to OpenGL &

HW1 Announcement

劉軒銘 , 網媒所 碩二ICG 2012 Fall

Page 2: Introduction to OpenGL & HW1 Announcement 劉軒銘, 網媒所 碩二 ICG 2012 Fall

Introduction to OpenGL&

HW1 Announcement

劉軒銘 , 網媒所 碩二ICG 2012 Fall

• Introduction to openGL

• Coordinate system and transformations in openGL

• A simple sample

• Requirements of HW1

• Others

Page 3: Introduction to OpenGL & HW1 Announcement 劉軒銘, 網媒所 碩二 ICG 2012 Fall

Introduction to openGL

A Graphics rendering API introduced in 1992 by Silicon Graphics Inc

Now managed by Khronos Group

Provide the API functions to access graphics hardware directly

Cross-platform

Page 4: Introduction to OpenGL & HW1 Announcement 劉軒銘, 網媒所 碩二 ICG 2012 Fall

Introduction to openGL

GLU (OpenGL Utility Library)◦Part of OpenGL◦Use the prefix of glu (ex: gluLookAt())

GLUT (OpenGL Utility Toolkit)◦Not officially part of OpenGL◦hide the complexities of differing window

system APIs. ◦Use the prefix of glut

(ex:glutDisplayFunc())

Page 5: Introduction to OpenGL & HW1 Announcement 劉軒銘, 網媒所 碩二 ICG 2012 Fall

World coordinates

object coordinates

Coordinate system

Page 6: Introduction to OpenGL & HW1 Announcement 劉軒銘, 網媒所 碩二 ICG 2012 Fall

World coordinates

Camera coordinates

Coordinate system

Page 7: Introduction to OpenGL & HW1 Announcement 劉軒銘, 網媒所 碩二 ICG 2012 Fall

World coordinates

Camera coordinates

Clip coordinates

Coordinate system

Page 8: Introduction to OpenGL & HW1 Announcement 劉軒銘, 網媒所 碩二 ICG 2012 Fall

Coordinate system

Model-view

transformation

Projectiontransformat

ionVertices

Worldcoordinates

Cameracoordinates

Clipcoordinates

Vertices

There are two matrix stacks.◦ ModelView matrix (GL_MODELVIEW)◦ Projection matrix (GL_PROJECTION)

CTM

Page 9: Introduction to OpenGL & HW1 Announcement 劉軒銘, 網媒所 碩二 ICG 2012 Fall

Coordinate system

Model-view

transformation

Projectiontransformat

ionVertices

Worldcoordinates

Cameracoordinates

Clipcoordinates

Vertices

glMatrixMode(GL_MODELVIEW);//now we are in modelview matrix stack!//do modelview transformation here…..

glMatrixMode(GL_PROJECTION);//now we are in projection matrix stack!//do projection transformation here….

When we call functions of transformation, we should change to the appropriate matrix stack first.

Page 10: Introduction to OpenGL & HW1 Announcement 劉軒銘, 網媒所 碩二 ICG 2012 Fall

openGL as state machine

Put a value into various states, then it will remain in effect until being changed.◦e.g. glColor*()

Many state variables are enabled or disabled with glEnable(), glDisable()◦e.g. glEnable(GL_LIGHT0)

Page 11: Introduction to OpenGL & HW1 Announcement 劉軒銘, 網媒所 碩二 ICG 2012 Fall

openGL as state machine

glBegin(GL_LINES);

R G BglColor3f(0.0f, 1.0f, 0.0f);

glVertex3f(1.0, 1.0, 0.0);

glVertex3f(-1.0, -1.0, 0.0);glColor3f(1.0f, 0.0f, 0.0f);

glVertex3f(-1.0, 1.0, 0.0);

glVertex3f(1.0, -1.0, 0.0);

glEnd();

(1,1,0)

(-1,-1,0)

(-1,1,0)

(1,-1,0)

Page 12: Introduction to OpenGL & HW1 Announcement 劉軒銘, 網媒所 碩二 ICG 2012 Fall

openGL as state machine

glLoadIdentity();

glTranslatef( distance,0, 0); glRotatef(angleX, 1.0, 0.0, 0.0);glRotatef(angleY, 0.0, 1.0, 0.0);

glBegin(GL_QUADS);glVertex3f(-7, 7, 7);…

glEnd();

(1,1,0)

(-1,-1,0)

(-1,1,0)

(1,-1,0)

Current Transformation

matrix(CTM)

Page 13: Introduction to OpenGL & HW1 Announcement 劉軒銘, 網媒所 碩二 ICG 2012 Fall

Matrix in OpenGL

Mantain matrix stack◦ glPushMatrix() : used to save current

stack◦ glPopMatrix() : used to restore

previous stack

glPushMatirx()

x

glRotatef glPopMatrix()

13

Page 14: Introduction to OpenGL & HW1 Announcement 劉軒銘, 網媒所 碩二 ICG 2012 Fall

World coordinates

glBegin(TYPE)glVertex3f(x,y,z)…….

glEnd()

Coordinate system

Page 15: Introduction to OpenGL & HW1 Announcement 劉軒銘, 網媒所 碩二 ICG 2012 Fall

Primitives

Page 16: Introduction to OpenGL & HW1 Announcement 劉軒銘, 網媒所 碩二 ICG 2012 Fall

Transformations : Model-View

glMatrixMode(GL_MODELVIEW)//Affine Transformation

glRotatef(angle,direction)glTranslatef(displacement)glScalef(scale coefficients)

glBegin(TYPE)glVertex3f(x,y,z)

………………….glEnd()

World coordinates

Camera coordinates

V’<- [R][T][S] V

Page 17: Introduction to OpenGL & HW1 Announcement 劉軒銘, 網媒所 碩二 ICG 2012 Fall

glMatrixMode(GL_MODELVIEW)

gluLookAt({eye},{look at},{{up})

//Affine Transformation

glBegin(TYPE)glVertex3f(v0)

glEnd()

World coordinates

Camera

coordinates

Transformations : Model-View

Page 18: Introduction to OpenGL & HW1 Announcement 劉軒銘, 網媒所 碩二 ICG 2012 Fall

glMatrixMode(GL_PROJECTION)//projection TransformationglOrtho(clipping volume)gluPerspective(fov,ciipping Volume)glFrustrum(clipping volume)

glMatrixMode(GL_MODELVIEW)gluLookAt//Affine TransformationglBegin(TYPE)

glVertex3f(v0)………..

glEnd()

World coordinates

Camera coordinates

Image coordinates

Transformations : Projection

Page 19: Introduction to OpenGL & HW1 Announcement 劉軒銘, 網媒所 碩二 ICG 2012 Fall

Notice!!

Each affine and projective transformation is implemented as a matrix, the order of function calls plays an important role.

glRotatef

glTranslatef

glScalef

Code:

glBeginglVertex3f……glEnd

Matrix multiplication

gluLookAt

glMatrixMode(GL_MODELVIEW)

glMatrixMode(GL_PROJECTION) gluperspective

= VgluLookAt

glRotatef glTranslatef

glScalef

glVertex3f

gluperspective

Page 20: Introduction to OpenGL & HW1 Announcement 劉軒銘, 網媒所 碩二 ICG 2012 Fall

A Simple Example

Page 21: Introduction to OpenGL & HW1 Announcement 劉軒銘, 網媒所 碩二 ICG 2012 Fall

A Simple Example#include <GL/glut.h>

int main(int argc, char** argv)

{

glutInit(&argc, argv);

glutInitWindowSize(600, 800);

glutInitWindowPosition(500, 500);

glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);

glutCreateWindow("ICG simple");

glutReshapeFunc(GL_reshape);

glutDisplayFunc(GL_display);

glutMainLoop();

return 0;

}

Page 22: Introduction to OpenGL & HW1 Announcement 劉軒銘, 網媒所 碩二 ICG 2012 Fall

A Simple Examplevoid GL_display()

{

glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

glClear(GL_COLOR_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

glBegin( GL_LINES );

glColor3f(0.0f, 1.0f, 0.0f);

glVertex3f(1.0, 1.0, 0.0);

glVertex3f(-1.0, -1.0, 0.0);

glColor3f(1.0f, 0.0f, 0.0f);

glVertex3f(-1.0, 1.0, 0.0);

glVertex3f(1.0, -1.0, 0.0);

glEnd();

glutSwapBuffers();

}

Page 23: Introduction to OpenGL & HW1 Announcement 劉軒銘, 網媒所 碩二 ICG 2012 Fall

A Simple Examplevoid GL_display()

{

glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

glClear(GL_COLOR_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

glBegin( GL_LINES );

glColor3f(0.0f, 1.0f, 0.0f);

glVertex3f(1.0, 1.0, 0.0);

glVertex3f(-1.0, -1.0, 0.0);

glColor3f(1.0f, 0.0f, 0.0f);

glVertex3f(-1.0, 1.0, 0.0);

glVertex3f(1.0, -1.0, 0.0);

glEnd();

glutSwapBuffers();

}

Page 24: Introduction to OpenGL & HW1 Announcement 劉軒銘, 網媒所 碩二 ICG 2012 Fall

A Simple Examplevoid GL_display()

{

glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

glClear(GL_COLOR_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

glBegin( GL_LINES );

glColor3f(0.0f, 1.0f, 0.0f);

glVertex3f(1.0, 1.0, 0.0);

glVertex3f(-1.0, -1.0, 0.0);

glColor3f(1.0f, 0.0f, 0.0f);

glVertex3f(-1.0, 1.0, 0.0);

glVertex3f(1.0, -1.0, 0.0);

glEnd();

glutSwapBuffers();

}

Page 25: Introduction to OpenGL & HW1 Announcement 劉軒銘, 網媒所 碩二 ICG 2012 Fall

A Simple Examplevoid GL_display()

{

glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

glClear(GL_COLOR_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

glBegin( GL_LINES );

glColor3f(0.0f, 1.0f, 0.0f);

glVertex3f(1.0, 1.0, 0.0);

glVertex3f(-1.0, -1.0, 0.0);

glColor3f(1.0f, 0.0f, 0.0f);

glVertex3f(-1.0, 1.0, 0.0);

glVertex3f(1.0, -1.0, 0.0);

glEnd();

glutSwapBuffers();

}

Page 26: Introduction to OpenGL & HW1 Announcement 劉軒銘, 網媒所 碩二 ICG 2012 Fall

A Simple Examplevoid GL_display()

{

glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

glClear(GL_COLOR_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

glBegin( GL_LINES );

glColor3f(0.0f, 1.0f, 0.0f);

glVertex3f(1.0, 1.0, 0.0);

glVertex3f(-1.0, -1.0, 0.0);

glColor3f(1.0f, 0.0f, 0.0f);

glVertex3f(-1.0, 1.0, 0.0);

glVertex3f(1.0, -1.0, 0.0);

glEnd();

glutSwapBuffers();

}

Page 27: Introduction to OpenGL & HW1 Announcement 劉軒銘, 網媒所 碩二 ICG 2012 Fall

A Simple Example

void GL_reshape(GLsizei w, GLsizei h){ glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION);

glLoadIdentity(); glOrtho(-2.0f, 2.0f, -2.0f, 2.0f, -2.0f, 2.0f);

}

Page 28: Introduction to OpenGL & HW1 Announcement 劉軒銘, 網媒所 碩二 ICG 2012 Fall

A Simple Example

void GL_reshape(GLsizei w, GLsizei h){ glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION);

glLoadIdentity(); glOrtho(-2.0f, 2.0f, -2.0f, 2.0f, -2.0f, 2.0f);

}

Page 29: Introduction to OpenGL & HW1 Announcement 劉軒銘, 網媒所 碩二 ICG 2012 Fall

A Simple Example

void GL_reshape(GLsizei w, GLsizei h){ glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION);

glLoadIdentity(); glOrtho(-2.0f, 2.0f, -2.0f, 2.0f, -2.0f, 2.0f);

}

Page 30: Introduction to OpenGL & HW1 Announcement 劉軒銘, 網媒所 碩二 ICG 2012 Fall

A Simple Example#include <GL\glut.h>

void GL_reshape(GLsizei w, GLsizei h);void GL_display();

int main(int argc, char** argv){ glutInit(&argc, argv);glutInitWindowSize(600, 800);glutInitWindowPosition(500, 500);glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);glutCreateWindow("ICG simple"); glutReshapeFunc(GL_reshape); glutDisplayFunc(GL_display); glutMainLoop(); return 0;}

void GL_reshape(GLsizei w, GLsizei h){glViewport(0, 0, w, h);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

glOrtho(-2.0f, 2.0f, -2.0f, 2.0f, -2.0f, 2.0f);

}

void GL_display(){glClearColor(0.0f, 0.0f, 0.0f, 0.0f);glClear(GL_COLOR_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);glLoadIdentity();

glBegin( GL_LINES );glColor3f(0.0f, 1.0f, 0.0f);glVertex3f(1.0, 1.0, 0.0);glVertex3f(-1.0, -1.0, 0.0);glColor3f(1.0f, 0.0f, 0.0f);glVertex3f(-1.0, 1.0, 0.0);glVertex3f(1.0, -1.0, 0.0);glEnd();

glutSwapBuffers();}

Page 31: Introduction to OpenGL & HW1 Announcement 劉軒銘, 網媒所 碩二 ICG 2012 Fall

Requirements of HW1Due : 10/24

You are required to do the following in homework #1: (6 points )1. Load a 3D model file of TRI format. Draw it in wireframe mode and view it in perspective view.(2 pts) 2. Implement basic transformations such as rotation, translation, scaling and shearing.(3 pts) 

3. Object rotation around x , y and z-axis.(4 pts)** You have to make these transformation matrix by yourself in problem 2 and 3** glTranslate , glRotate, glScale are not allowed 4. Clipping implementation. Try to show the difference between clipping and non-clipping.  (5 pts)** glOrtho, gluPerspective and glFrustum are not allowed in problem 4

5. Bonus. Any kind of effort you made more than requirements above.(6 pts)

Page 32: Introduction to OpenGL & HW1 Announcement 劉軒銘, 網媒所 碩二 ICG 2012 Fall

z

x

y

screen at z = 0

clipping box is enclosed by: x = 1,x=-1,y=1,y=-1,z=1,z=-1

Notice!! How does openGL set its clipping volume for window??

Using homogeneous coordinatesclipping box

nonhomogeneous coordinate:

xyzw

, w =/= 1

Page 33: Introduction to OpenGL & HW1 Announcement 劉軒銘, 網媒所 碩二 ICG 2012 Fall

z

x

y

screen at z = 0

clipping box is enclosed by: x = 1,x=-1,y=1,y=-1,z=1,z=-1

Notice!! How does openGL set its clipping volume for window??

Using homogeneous coordinatesclipping box

homogeneous coordinate:

x/wy/wz/w1

Page 34: Introduction to OpenGL & HW1 Announcement 劉軒銘, 網媒所 碩二 ICG 2012 Fall

Notice!! How does openGL set its clipping volume for window??

Using homogeneous image coordinatesclipping boxYou’d better set your own homogeneous image

coordinates clipping box different from openGL’s.

※Here the homogeneous image coordinates clipping volume is different from theOne described in textbook , which is refer to camera coordinates clipping volume.

Page 35: Introduction to OpenGL & HW1 Announcement 劉軒銘, 網媒所 碩二 ICG 2012 Fall

OthersGlut window functions:

glutInit(&argc, argv);glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);glutInitWindowSize(800, 600);glutInitWindowPosition(100, 100);glutCreateWindow(“Name");

glutDisplayFunc(display);glutReshapeFunc(reshape);glutKeyboardFunc(keyboard);glutMouseFunc(mouse);glutMainLoop();

Page 36: Introduction to OpenGL & HW1 Announcement 劉軒銘, 網媒所 碩二 ICG 2012 Fall

OthersInteractive setting:

void keyboard(unsigned char key, int x, int y){switch(key){

………case 'a':

glClearColor(1., 1., 1., 1.);startPosX -= 10;glutPostRedisplay();break;

}}

Page 37: Introduction to OpenGL & HW1 Announcement 劉軒銘, 網媒所 碩二 ICG 2012 Fall

OthersInteractive setting:

void mouse(int button, int state, int x, int y){static float preX, preY;switch(state){

case GLUT_DOWN:preX = x;preY = y;glutPostRedisplay();break;

……………….}

}

Page 38: Introduction to OpenGL & HW1 Announcement 劉軒銘, 網媒所 碩二 ICG 2012 Fall

OthersSetting up openGL(visual C++):

1.download glut-3.7.6-bin.zip or similar - GLUT for Win32 dll, lib and header file - there.

2.Make a directory called "GL" under C:\Program Files\Microsoft Visual Studio 9.0\VC\include or similar

3.Copy the header files into C:\programme\microsoft visual studio\vc98\include\GL

4.Copy all *.lib files into C:\programme\microsoft visual studio\vc98\Lib

5.Copy all .dll files into C:\Windows\System32

6.In the Menu of VC++ go through to -> project -> settings -> Link and add (do not remove the others!) the following libraries to the Object/libary modules line:glut32.lib glu32.lib opengl32.lib glaux.lib

Page 39: Introduction to OpenGL & HW1 Announcement 劉軒銘, 網媒所 碩二 ICG 2012 Fall

Reference1.Introduction to OpenGL PPT, Presented by Chung-Lin Wen ICG 2006 Fall

2.Interactive Computer Graphics – A Top-Down Approach Using openGL