[译]GLUT教程 - 整合代码4
Lighthouse3d.com >> GLUT Tutorial >> Pop-up Menus >> The Code So Far IV
以下代码使用了位图字体.它在每个雪人的上方显示一个数字.该数字可以用鼠标右键的弹出菜单来设置.
#include <stdlib.h>
#include <stdio.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_BITMAP_TIMES_ROMAN_24; #define INT_GLUT_BITMAP_8_BY_13 1
#define INT_GLUT_BITMAP_9_BY_15 2
#define INT_GLUT_BITMAP_TIMES_ROMAN_10 3
#define INT_GLUT_BITMAP_TIMES_ROMAN_24 4
#define INT_GLUT_BITMAP_HELVETICA_10 5
#define INT_GLUT_BITMAP_HELVETICA_12 6
#define INT_GLUT_BITMAP_HELVETICA_18 7 void changeSize(int w, int h) { // 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 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+));
renderBitmapString(0.0f, 0.5f, 0.0f, (void *)font ,number);
glPopMatrix();
} 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 INT_GLUT_BITMAP_8_BY_13:
font = GLUT_BITMAP_8_BY_13;
break;
case INT_GLUT_BITMAP_9_BY_15:
font = GLUT_BITMAP_9_BY_15;
break;
case INT_GLUT_BITMAP_TIMES_ROMAN_10:
font = GLUT_BITMAP_TIMES_ROMAN_10;
break;
case INT_GLUT_BITMAP_TIMES_ROMAN_24:
font = GLUT_BITMAP_TIMES_ROMAN_24;
break;
case INT_GLUT_BITMAP_HELVETICA_10:
font = GLUT_BITMAP_HELVETICA_10;
break;
case INT_GLUT_BITMAP_HELVETICA_12:
font = GLUT_BITMAP_HELVETICA_12;
break;
case INT_GLUT_BITMAP_HELVETICA_18:
font = GLUT_BITMAP_HELVETICA_18;
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("BITMAP_8_BY_13 ",INT_GLUT_BITMAP_8_BY_13 );
glutAddMenuEntry("BITMAP_9_BY_15",INT_GLUT_BITMAP_9_BY_15 );
glutAddMenuEntry("BITMAP_TIMES_ROMAN_10 ",INT_GLUT_BITMAP_TIMES_ROMAN_10 );
glutAddMenuEntry("BITMAP_TIMES_ROMAN_24",INT_GLUT_BITMAP_TIMES_ROMAN_24 );
glutAddMenuEntry("BITMAP_HELVETICA_10 ",INT_GLUT_BITMAP_HELVETICA_10 );
glutAddMenuEntry("BITMAP_HELVETICA_12",INT_GLUT_BITMAP_HELVETICA_12 );
glutAddMenuEntry("BITMAP_HELVETICA_18",INT_GLUT_BITMAP_HELVETICA_18 ); 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教程 - 整合代码4的更多相关文章
- [译]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教程 - 整合代码5
Lighthouse3d.com >> GLUT Tutorial >> Extras >> The Code So Far V 该代码与位图字体的代码类似.区别是 ...
- [译]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的游戏模式是为开启 ...
随机推荐
- POJ 3080-Blue Jeans【kmp,字符串剪接】
Blue Jeans Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20695 Accepted: 9167 Descr ...
- Cookie和Session在Node.JS中的实践(三)
Cookie和Session在Node.JS中的实践(三) 前面作者写的COOKIE篇.SESSION篇,算是已经比较详细的说明了两者间的区别.机制.联系了.阅读时间可能稍长,因为作者本身作图也做了不 ...
- C++完美实现Singleton模式[转]
Singleton模式是常用的设计模式之一,但是要实现一个真正实用的设计模式却也不是件容易的事情.1. 标准的实现class Singleton{public: static Singleton * ...
- Timeline简单配置
Timeline是一个Jquery时间轴插件.效果如图 获取地址:https://github.com/ka215/jquery.timeline 配置 (1)html表头加入 <link re ...
- Java高级特性—锁
1).synchronized 加同步格式: synchronized( 需要一个任意的对象(锁) ){ 代码块中放操作共享数据的代码. } synchronized的缺陷 synchronized是 ...
- 转: 使用 Velocity 模板引擎快速生成代码
from:https://www.ibm.com/developerworks/cn/java/j-lo-velocity1/ 评注: 1. velocity 的基本语法 2. 生成代码的用法.
- 原来,多年以来,我一直是个curl/CRUD程序员
curl,就是create,update,remove,list的首字母简写.说是CRUD似乎更流行些,不过无所谓,知道是一个意思就好. curl程序员,就是增改删查程序员,中文说增删改查更加顺口. ...
- 倍福TwinCAT(贝福Beckhoff)基础教程2.0 TwinCAT常用快捷键
F5:运行程序 CTRL+F5:停止运行当前程序 F12:登出 F11:登录 CTRL+F7:强制写入数值 更多教学视频和资料下载,欢迎关注以下信息: 我的优酷空间: http://i.you ...
- C++003基础
1.C++对C的扩展 1简单的C++程序 1.1求圆的周长和面积 数据描写叙述: 半径.周长,面积均用实型数表示 数据处理: 输入半径 r. 计算周长 = 2*π*r : 计算面积 = π* r2 . ...
- Cygwin-安装和配置ssh服务
Cygwin介绍: Cygwin是一个在windows平台上执行的类UNIX模拟环境.它对于学习UNIX/Linux操作环境,或者从UNIX到Windows的应用程序移植,或者进行某些特殊的开发工作, ...