[译]GLUT教程 - 整合代码5
Lighthouse3d.com >> GLUT Tutorial >> Extras >> The Code So Far V
该代码与位图字体的代码类似.区别是用了笔划字体来显示每个雪人上的数字,然后雪人是用正交投影来显示,还有一段用位图字体显示的文本.后面包含了每秒帧数的文本不受镜头移动的影响,它一直停留在屏幕的相同位置.
字体菜单也改成了用笔划字体代替位图字体.
#include <stdio.h>
#include <stdlib.h>
#include <math.h> #ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif // angle of rotation for the camera direction
float angle = 0.0f; // actual vector representing the camera's direction
float lx=0.0f,lz=-1.0f; // XZ position of the camera
float x=0.0f, z=5.0f; // the key states. These variables will be zero
//when no key is being presses
float deltaAngle = 0.0f;
float deltaMove = ;
int xOrigin = -; // Constant definitions for Menus
#define RED 1
#define GREEN 2
#define BLUE 3
#define ORANGE 4 #define FILL 1
#define LINE 2 // Pop up menu identifiers
int fillMenu, fontMenu, mainMenu, colorMenu; // color for the nose
float red = 1.0f, blue=0.5f, green=0.5f; // scale of snowman
float scale = 1.0f; // menu status
int menuFlag = ; // default font
void *font = GLUT_STROKE_ROMAN; // width and height of the window
int h,w; // variables to compute frames per second
int frame;
long time, timebase;
char s[]; void changeSize(int ww, int hh) { h = hh;
w = ww;
// Prevent a divide by zero, when window is too short
// (you cant make a window of zero width).
if (h == )
h = ; float ratio = w * 1.0 / h; // Use the Projection Matrix
glMatrixMode(GL_PROJECTION); // Reset Matrix
glLoadIdentity(); // Set the viewport to be the entire window
glViewport(, , w, h); // Set the correct perspective.
gluPerspective(45.0f, ratio, 0.1f, 100.0f); // Get Back to the Modelview
glMatrixMode(GL_MODELVIEW);
} void drawSnowMan() { glScalef(scale, scale, scale);
glColor3f(1.0f, 1.0f, 1.0f); // Draw Body
glTranslatef(0.0f ,0.75f, 0.0f);
glutSolidSphere(0.75f,,); // Draw Head
glTranslatef(0.0f, 1.0f, 0.0f);
glutSolidSphere(0.25f,,); // Draw Eyes
glPushMatrix();
glColor3f(0.0f,0.0f,0.0f);
glTranslatef(0.05f, 0.10f, 0.18f);
glutSolidSphere(0.05f,,);
glTranslatef(-0.1f, 0.0f, 0.0f);
glutSolidSphere(0.05f,,);
glPopMatrix(); // Draw Nose
glColor3f(red, green, blue);
glRotatef(0.0f,1.0f, 0.0f, 0.0f);
glutSolidCone(0.08f,0.5f,,); glColor3f(1.0f, 1.0f, 1.0f); } void renderBitmapString(
float x,
float y,
float z,
void *font,
char *string) { char *c;
glRasterPos3f(x, y,z);
for (c=string; *c != '\0'; c++) {
glutBitmapCharacter(font, *c);
}
} void renderStrokeFontString(
float x,
float y,
float z,
void *font,
char *string) { char *c;
glPushMatrix();
glTranslatef(x, y,z);
glScalef(0.002f, 0.002f, 0.002f);
for (c=string; *c != '\0'; c++) {
glutStrokeCharacter(font, *c);
}
glPopMatrix();
} void restorePerspectiveProjection() { glMatrixMode(GL_PROJECTION);
// restore previous projection matrix
glPopMatrix(); // get back to modelview mode
glMatrixMode(GL_MODELVIEW);
} void setOrthographicProjection() { // switch to projection mode
glMatrixMode(GL_PROJECTION); // save previous matrix which contains the
//settings for the perspective projection
glPushMatrix(); // reset matrix
glLoadIdentity(); // set a 2D orthographic projection
gluOrtho2D(, w, h, ); // switch back to modelview mode
glMatrixMode(GL_MODELVIEW);
} void computePos(float deltaMove) { x += deltaMove * lx * 0.1f;
z += deltaMove * lz * 0.1f;
} void renderScene(void) { if (deltaMove)
computePos(deltaMove); // Clear Color and Depth Buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Reset transformations
glLoadIdentity();
// Set the camera
gluLookAt( x, 1.0f, z,
x+lx, 1.0f, z+lz,
0.0f, 1.0f, 0.0f); // Draw ground glColor3f(0.9f, 0.9f, 0.9f);
glBegin(GL_QUADS);
glVertex3f(-100.0f, 0.0f, -100.0f);
glVertex3f(-100.0f, 0.0f, 100.0f);
glVertex3f( 100.0f, 0.0f, 100.0f);
glVertex3f( 100.0f, 0.0f, -100.0f);
glEnd(); // Draw 36 SnowMen
char number[];
for(int i = -; i < ; i++)
for(int j=-; j < ; j++) {
glPushMatrix();
glTranslatef(i*10.0f, 0.0f, j * 10.0f);
drawSnowMan();
sprintf(number,"%d",(i+)*+(j+));
renderStrokeFontString(0.0f, 0.5f, 0.0f, (void *)font ,number);
glPopMatrix();
} // Code to compute frames per second
frame++; time=glutGet(GLUT_ELAPSED_TIME);
if (time - timebase > ) {
sprintf(s,"Lighthouse3D - FPS:%4.2f",
frame*1000.0/(time-timebase));
timebase = time;
frame = ;
} // Code to display a string (fps) with bitmap fonts
setOrthographicProjection(); glPushMatrix();
glLoadIdentity();
renderBitmapString(,,,GLUT_BITMAP_HELVETICA_18,s);
glPopMatrix(); restorePerspectiveProjection(); glutSwapBuffers();
} // -----------------------------------
// KEYBOARD
// ----------------------------------- void processNormalKeys(unsigned char key, int xx, int yy) { switch (key) {
case :
glutDestroyMenu(mainMenu);
glutDestroyMenu(fillMenu);
glutDestroyMenu(colorMenu);
glutDestroyMenu(fontMenu);
exit();
break;
}
} void pressKey(int key, int xx, int yy) { switch (key) {
case GLUT_KEY_UP : deltaMove = 0.5f; break;
case GLUT_KEY_DOWN : deltaMove = -0.5f; break;
}
} void releaseKey(int key, int x, int y) { switch (key) {
case GLUT_KEY_UP :
case GLUT_KEY_DOWN : deltaMove = ;break;
}
} // -----------------------------------
// MOUSE
// ----------------------------------- void mouseMove(int x, int y) { // this will only be true when the left button is down
if (xOrigin >= ) { // update deltaAngle
deltaAngle = (x - xOrigin) * 0.001f; // update camera's direction
lx = sin(angle + deltaAngle);
lz = -cos(angle + deltaAngle);
}
} void mouseButton(int button, int state, int x, int y) { // only start motion if the left button is pressed
if (button == GLUT_LEFT_BUTTON) { // when the button is released
if (state == GLUT_UP) {
angle += deltaAngle;
xOrigin = -;
}
else {// state = GLUT_DOWN
xOrigin = x;
}
}
} // -----------------------------------
// MENUS
// ----------------------------------- void processMenuStatus(int status, int x, int y) { if (status == GLUT_MENU_IN_USE)
menuFlag = ;
else
menuFlag = ;
} void processMainMenu(int option) { // nothing to do in here
// all actions are for submenus
} void processFillMenu(int option) { switch (option) { case FILL: glPolygonMode(GL_FRONT, GL_FILL); break;
case LINE: glPolygonMode(GL_FRONT, GL_LINE); break;
}
} void processFontMenu(int option) { switch (option) {
case : font = GLUT_STROKE_ROMAN;
break;
case : font = GLUT_STROKE_MONO_ROMAN;
break;
}
} void processColorMenu(int option) { switch (option) {
case RED :
red = 1.0f;
green = 0.0f;
blue = 0.0f; break;
case GREEN :
red = 0.0f;
green = 1.0f;
blue = 0.0f; break;
case BLUE :
red = 0.0f;
green = 0.0f;
blue = 1.0f; break;
case ORANGE :
red = 1.0f;
green = 0.5f;
blue = 0.5f; break;
}
} void createPopupMenus() { fontMenu = glutCreateMenu(processFontMenu); glutAddMenuEntry("STROKE_ROMAN", );
glutAddMenuEntry("STROKE_MONO_ROMAN", );
fillMenu = glutCreateMenu(processFillMenu); glutAddMenuEntry("Fill",FILL);
glutAddMenuEntry("Line",LINE); colorMenu = glutCreateMenu(processColorMenu);
glutAddMenuEntry("Red",RED);
glutAddMenuEntry("Blue",BLUE);
glutAddMenuEntry("Green",GREEN);
glutAddMenuEntry("Orange",ORANGE); mainMenu = glutCreateMenu(processMainMenu); glutAddSubMenu("Polygon Mode", fillMenu);
glutAddSubMenu("Color", colorMenu);
glutAddSubMenu("Font",fontMenu);
// attach the menu to the right button
glutAttachMenu(GLUT_RIGHT_BUTTON); // this will allow us to know if the menu is active
glutMenuStatusFunc(processMenuStatus);
} // -----------------------------------
// MAIN
// ----------------------------------- int main(int argc, char **argv) { // init GLUT and create window
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(,);
glutInitWindowSize(,);
glutCreateWindow("Lighthouse3D - GLUT Tutorial"); // register callbacks
glutDisplayFunc(renderScene);
glutReshapeFunc(changeSize);
glutIdleFunc(renderScene); glutIgnoreKeyRepeat();
glutKeyboardFunc(processNormalKeys);
glutSpecialFunc(pressKey);
glutSpecialUpFunc(releaseKey); // here are the two new functions
glutMouseFunc(mouseButton);
glutMotionFunc(mouseMove); // OpenGL init
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE); // init Menus
createPopupMenus(); // enter GLUT event processing cycle
glutMainLoop(); return ;
}
[译]GLUT教程 - 整合代码5的更多相关文章
- [译]GLUT教程 - 整合代码2
Lighthouse3d.com >> GLUT Tutorial >> Input >> The Code So Far II 以下是前面几节的完整整合代码: # ...
- [译]GLUT教程 - 整合代码1
Lighthouse3d.com >> GLUT Tutorial >> Input >> The Code So Far 以下是前面几节的完整整合代码: #inc ...
- [译]GLUT教程 - 整合代码8
Lighthouse3d.com >> GLUT Tutorial >> Avoiding the Idle Func >> The Code So Far VII ...
- [译]GLUT教程 - 整合代码7
Lighthouse3d.com >> GLUT Tutorial >> Extras >> The Code So Far VII 以下是子窗体的最终版本代码. ...
- [译]GLUT教程 - 整合代码6
Lighthouse3d.com >> GLUT Tutorial >> Extras >> The Code So Far VI 下面代码以窗体模式启动.你可以在 ...
- [译]GLUT教程 - 整合代码4
Lighthouse3d.com >> GLUT Tutorial >> Pop-up Menus >> The Code So Far IV 以下代码使用了位图字 ...
- [译]GLUT教程 - 整合代码3
Lighthouse3d.com >> GLUT Tutorial >> Pop-up Menus >> The Code So Far III 这里我们准备包含一 ...
- [译]GLUT教程(目录)
http://www.lighthouse3d.com/tutorials/glut-tutorial/ GLUT是OpenGL Utility Toolkit的意思.作者Mark J. Kilgar ...
- [译]GLUT教程 - 游戏模式
Lighthouse3d.com >> GLUT Tutorial >> Extras >> Game Mode 根据GLUT官网的说明,GLUT的游戏模式是为开启 ...
随机推荐
- csu1808
csu1808 题意 n 个点间有 m 条地铁,每条地铁可能属于不同的线路,每条地铁有权值即通过时花费的时间,如果乘坐第 i 条地铁来到地铁站 s,再乘坐第 j 条地铁离开,需要花费额外的时间 \(| ...
- POJ 2566:Bound Found(Two pointers)
[题目链接] http://poj.org/problem?id=2566 [题目大意] 给出一个序列,求一个子段和,使得其绝对值最接近给出值, 输出这个区间的左右端点和区间和. [题解] 因为原序列 ...
- 每天一个liunx命令4之 ps -ef ,ps -aux ,ps aux
1ps aux和ps –aux 请注意"ps -aux"不同于"ps aux".POSIX和UNIX的标准要求"ps -aux"打印用户名为 ...
- Word中如何设置图片与段落的间距为半行
第一种: 正文为5号,那么图片或者Viso对象前后空一行,设置字号为7号或者更小,这样设置的间距就是那个7号字的间距,比5号小,看着空白不是那么大. 第二种: Visio对象转为jpg,然后选中图片和 ...
- Java继承是复制还是共用?答案共用
继承是复制还是共用?例如 Dog类继承Animal类,在Dog中的属性修改,Animal中的属性也会一起改变吗?如果用Dog和Animal分别实例化对象,dog和animal,这两者的属性是公用还是各 ...
- 单元测试方法属性(Unit Test Method Attribute)
Additional test attributes(可以在测试方法上使用的属性)As you have seen, the unit-testing subsystem within Visual ...
- iis7文件夹 首页设置
iis7在默认文档中可以设置首页,一般可以直接设置网站根目录下的页面为首页,但是当页面不在根目录下时,直接按路径添加就不行了,如“admin/default.aspx”.添加完后只有“default. ...
- 在linux下玩转usb摄像头
硬件平台:PC机一台 .usb摄像头 操作系统:Linux3.0.8 交叉编译环境:arm-none-Linux-gnueabi-gcc 4.5.1 调试步骤: 一.linux 内核解压 1.1使用 ...
- Windows API常用函数
转自:http://www.cnblogs.com/xiashengwang/p/4026259.html .NET中虽然类库很强,但还是有些时候功能有限,掌握常用的api函数, 会给我们解决问题提供 ...
- 深入理解ES6里的promise
一.ES6 Promise是什么? 复杂的概念先不讲,我们先简单粗暴地把Promise用一下,有个直观感受.那么第一个问题来了,Promise是什么呢?是一个类?对象?数组?函数? 别猜了,直接打印出 ...