update: 2012/05/22
reference:
AustralSounds: Basic GLUT setup
A. 說明:
這一篇是用來補充(取代)先前的一篇: OpenGL: General Strucure .
--------------------------------------------------------------------------------
B. 新增專案:
1. Xcode > File > New > Project... > Mac OS X > Application >
Command Line Tool > Next
Product Name: MyOpenGL
Type: C++
Use Automatic Reference Counting: checked
> Next > Create
OpenGL
- OpenGL is the premier environment for developing portable,
interactive 2D and 3D graphics applications.
GLUT
- OpenGL Utility Toolkit- OpenGL is the premier environment for developing portable,
interactive 2D and 3D graphics applications.
GLUT
--------------------------------------------------------------------------------
C. 新增檔案:
1. "常數與巨集定義" 檔案:
Xcode > File > New > File...
> Mac OS X > C and C++ > Header File > Next
Save As: ConstantsAndMacros.h
Targets: MyOpenGL (勾選)> Create
**********************************************************
2. "回呼函式" 檔案: a. Xcode > File > New > File...
> Mac OS X > C and C++ > Header File > Next
(圖略)
Save As: CallbackFunctions.h
Targets: MyOpenGL (勾選)> Create
> Mac OS X > C and C++ > C++ File > Next
(圖略)
Save As: CallbackFunctions.cpp
Targets: MyOpenGL (勾選)> Create
**********************************************************
a. Xcode > File > New > File...
> Mac OS X > C and C++ > Header File > Next
(圖略)
Save As: draw.h
Targets: MyOpenGL (勾選)> Create
(圖略)
b. Xcode > File > New > File...
> Mac OS X > C and C++ > C++ File > Next
(圖略)
Save As: draw.cpp
Targets: MyOpenGL (勾選)> Create
(圖略)
**********************************************************
4. "公用函式" 檔案:
a. Xcode > File > New > File...
> Mac OS X > C and C++ > Header File > Next
(圖略)
Save As: Utility.h
Targets: MyOpenGL (勾選)> Create
(圖略)
b. Xcode > File > New > File...
> Mac OS X > C and C++ > C++ File > Next
(圖略)
Save As: Utility.cpp
Targets: MyOpenGL (勾選)> Create
(圖略)
**********************************************************
5. 整個專案檔案架構如下:
D. 新增 "常數":
開啓 ConstantsAndMacros.h 檔案, 修改如下:
//@update
/*
#ifndef MyOpenGL_ConstantsAndMacros_h
#define MyOpenGL_ConstantsAndMacros_h
#endif
*/
// Perspective Projection:
#define kPerspective_FieldOfViewY 60.0 // Field Of View Y
#define kPerspective_NearPlane 1.0 // Near Plane
#define kPerspective_FarPlane 10.0 // Far Plane
--------------------------------------------------------------------------------
E. 新增 "公用函式":
1. 開啓 Utility.h 檔案, 修改如下://@update
/*
#ifndef MyOpenGL_Utility_h
#define MyOpenGL_Utility_h
#endif
*/
// change LookAt
void changeLookAt();
2. 開啓 Utility.cpp 檔案, 修改如下:
#include <iostream>
//@add
#include <OpenGL/OpenGL.h> // OpenGL Library
#include <GLUT/GLUT.h> // OpenGL Utility Toolkit
// change LookAt
void changeLookAt()
{
gluLookAt(0, 0, 1, // eye position
0, 0, 0, // look at this point
0, 1, 0); // "up" vector
}
--------------------------------------------------------------------------------
F. 新增 "回呼函式":
1. 開啓 CallbackFunctions.h 檔案, 修改如下:
//@update
/*
#ifndef MyOpenGL_CallbackFunctions_h
#define MyOpenGL_CallbackFunctions_h
#endif
*/
// Idle Callbacks
void idle();
// User Input Callbacks
void keyboard(unsigned char key, int x, int y);
// Mouse Callbacks
void mouse(GLint button, GLint state, GLint x, GLint y);
// Reshape Callbacks
void resize(GLint width, GLint height);
// Special Callbacks
void special(GLint key, GLint x, GLint y);
2. 開啓 CallbackFunctions.cpp 檔案, 修改如下:
#include <iostream>
//@add
#include <OpenGL/OpenGL.h> // OpenGL Library
#include <GLUT/GLUT.h> // OpenGL Utility Toolkit
#include "ConstantsAndMacros.h"
#include "Utility.h"
// Reshape Callbacks
void resize(GLint width, GLint height)
{
// map the view port to the client area
glViewport(0, 0, (GLsizei)width, (GLsizei)height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(kPerspective_FieldOfViewY,
(GLfloat) width / (GLfloat) height,
kPerspective_NearPlane,
kPerspective_FarPlane);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// place the "camera"
changeLookAt();
}
// User Input Callbacks
void keyboard(unsigned char key, int x, int y)
{
switch(key)
{
case 'q': case 'Q':
exit(EXIT_SUCCESS);
break;
case 'r': case 'R':
std::cout << "call glutPostRedisplay() \n";
// refresh the screen
glutPostRedisplay();
break;
}
}
// Special Callbacks
void special(GLint key, GLint x, GLint y)
{
if( key == GLUT_KEY_LEFT)
{
std::cout << "Left arrow pressed \n";
}
if( key == GLUT_KEY_RIGHT)
{
std::cout << "Right arrow pressed \n";
}
if( key == GLUT_KEY_DOWN)
{
std::cout << "Down arrow pressed \n";
}
if( key == GLUT_KEY_UP)
{
std::cout << "Up arrow pressed \n";
}
if( key == GLUT_KEY_PAGE_UP)
{
std::cout << "PageUp arrow pressed \n";
}
if( key == GLUT_KEY_PAGE_DOWN)
{
std::cout << "PageDown arrow pressed \n";
}
// refresh the screen
glutPostRedisplay( );
}
// Mouse Callbacks
void mouse(GLint button, GLint state, GLint x, GLint y)
{
if( button == GLUT_LEFT_BUTTON )
{
// when left mouse button is down
if( state == GLUT_DOWN )
{
std::cout << "left mouse button down \n";
}
else
{
std::cout << "left mouse button up \n";
}
}
else if ( button == GLUT_RIGHT_BUTTON )
{
// when right mouse button down
if( state == GLUT_DOWN )
{
std::cout << "right mouse button down \n";
}
else
{
std::cout << "right mouse button up \n";
}
}
else
{
std::cout << "other mouse button pressed \n";
}
// refresh the screen
glutPostRedisplay( );
}
// Idle Callbacks
void idle()
{
// refresh the screen
glutPostRedisplay();
}
3. 開啓 draw.h 檔案, 修改如下:
//@update
/*
#ifndef MyOpenGL_draw_h
#define MyOpenGL_draw_h
#endif
*/
// Display Callbacks
void display();
4. 開啓 draw.cpp 檔案, 修改如下:
#include <iostream>
//@add
#include <OpenGL/OpenGL.h> // OpenGL Library
#include <GLUT/GLUT.h> // OpenGL Utility Toolkit
#include "Utility.h"
// Display Callbacks
void display()
{
// clear the color and depth buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// set line width
glLineWidth( 2.0f );
// save the current transformation state
glPushMatrix();
// set the view point
changeLookAt();
// Draw something
glBegin( GL_TRIANGLES );
glColor4f(1.0, 0.0, 0.0, 1.0);
glVertex3f(-1, -1, 0);
glColor4f(0.0, 1.0, 0.0, 1.0);
glVertex3f(0, 1, 0);
glColor4f(0.0, 0.0, 1.0, 1.0);
glVertex3f(1, -1, 0);
glEnd();
// restore state
glPopMatrix();
// flush!
glFlush();
// swap the double buffer
glutSwapBuffers();
}
--------------------------------------------------------------------------------
G. 修改主程式:
開啓 main.cpp 檔案, 修改如下:
#include <iostream>
//@add
#include <OpenGL/OpenGL.h> // OpenGL Library
#include <GLUT/GLUT.h> // OpenGL Utility Toolkit
#include "CallbackFunctions.h"
#include "draw.h"
// initialize
void init()
{
// This determines the background color, in this case: gray
// state: current clear color
glClearColor(0.5, 0.5, 0.5, 1.0); // R, G, B, A
// Determines which matrix to apply operations to.
// value: GL_PROJECTION or GL_MODELVIEW
glMatrixMode(GL_PROJECTION); // default
// Loads the Identity matrix
glLoadIdentity();
// 投影(Projection)
//
// default: 正投影(Orthographic Projection)
// Set the size and projection of the buffer
// (x left, x right, y bottom, y top)
// corner => (left, bottom), (right, top)
gluOrtho2D(-1.0, 1.0, -1.0, 1.0);
}
//int main(int argc, const char * argv[])
//@update: just remove "const"
int main(int argc, char * argv[])
{
// insert code here...
//std::cout << "Hello, World!\n";
// Init glut
glutInit(&argc, argv);
// Display mode
int mode = GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH;
glutInitDisplayMode(mode);
// Init Window
glutInitWindowSize(600, 480);
glutInitWindowPosition(100, 100);
glutCreateWindow("Hello OpenGL");
// initialize: set OpenGL state
init();
// Callback Functions Registration
glutDisplayFunc(display); // drawing
glutReshapeFunc(resize);
glutKeyboardFunc(keyboard);
glutSpecialFunc(special);
glutMouseFunc(mouse);
glutIdleFunc(idle);
// enter event processing loop
glutMainLoop();
// will not be executed
//return 0;
}
--------------------------------------------------------------------------------
H. 編譯並執行:
沒有留言:
張貼留言
注意:只有此網誌的成員可以留言。