Lighthouse3d.com >> GLUT Tutorial >> Extras >> The Code So Far VII

以下是子窗体的最终版本代码.

#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, ly = 0.0f; // XZ position of the camera
float x=0.0f, z=5.0f, y = 1.75f; // the key states. These variables will be zero
//when no key is being presses
float deltaAngle = 0.0f;
float deltaMove = ;
int xOrigin = -; // width and height of the window
int h,w; // variables to compute frames per second
int frame;
long time, timebase;
char s[]; // variables to hold window identifiers
int mainWindow, subWindow1,subWindow2,subWindow3;
//border between subwindows
int border = ; void setProjection(int w1, int h1)
{
float ratio;
// Prevent a divide by zero, when window is too short
// (you cant make a window of zero width).
ratio = 1.0f * w1 / h1;
// Reset the coordinate system before modifying
glMatrixMode(GL_PROJECTION);
glLoadIdentity(); // Set the viewport to be the entire window
glViewport(, , w1, h1); // Set the clipping volume
gluPerspective(,ratio,0.1,);
glMatrixMode(GL_MODELVIEW);
} void changeSize(int w1,int h1) { if(h1 == )
h1 = ; // we're keeping these values cause we'll need them latter
w = w1;
h = h1; // set subwindow 1 as the active window
glutSetWindow(subWindow1);
// resize and reposition the sub window
glutPositionWindow(border,border);
glutReshapeWindow(w-*border, h/ - border*/);
setProjection(w-*border, h/ - border*/); // set subwindow 2 as the active window
glutSetWindow(subWindow2);
// resize and reposition the sub window
glutPositionWindow(border,(h+border)/);
glutReshapeWindow(w/-border*/, h/ - border*/);
setProjection(w/-border*/,h/ - border*/); // set subwindow 3 as the active window
glutSetWindow(subWindow3);
// resize and reposition the sub window
glutPositionWindow((w+border)/,(h+border)/);
glutReshapeWindow(w/-border*/,h/ - border*/);
setProjection(w/-border*/,h/ - border*/);
} void drawSnowMan() { 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(1.0f, 0.5f, 0.5f);
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 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;
} // Common Render Items for all subwindows
void renderScene2() { // 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
for(int i = -; i < ; i++)
for(int j=-; j < ; j++){
glPushMatrix();
glTranslatef(i*10.0f, 0.0f, j * 10.0f);
drawSnowMan();
glPopMatrix();
}
} // Display func for main window
void renderScene() {
glutSetWindow(mainWindow);
glClear(GL_COLOR_BUFFER_BIT);
glutSwapBuffers();
} // Display func for sub window 1
void renderScenesw1() { glutSetWindow(subWindow1); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity();
gluLookAt(x, y, z,
x + lx,y + ly,z + lz,
0.0f,1.0f,0.0f); renderScene2(); // display fps in the top window
frame++; time=glutGet(GLUT_ELAPSED_TIME);
if (time - timebase > ) {
sprintf(s,"Lighthouse3D - FPS:%4.2f",
frame*1000.0/(time-timebase));
timebase = time;
frame = ;
} setOrthographicProjection(); glPushMatrix();
glLoadIdentity();
renderBitmapString(,,,GLUT_BITMAP_HELVETICA_12,s);
glPopMatrix(); restorePerspectiveProjection(); glutSwapBuffers();
} // Display func for sub window 2
void renderScenesw2() { glutSetWindow(subWindow2); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity();
gluLookAt(x, y+, z,
x ,y - ,z,
lx,,lz); // Draw red cone at the location of the main camera
glPushMatrix();
glColor3f(1.0,0.0,0.0);
glTranslatef(x,y,z);
glRotatef(-(angle+deltaAngle)*180.0/3.14,0.0,1.0,0.0);
glutSolidCone(0.2,0.8f,,);
glPopMatrix(); renderScene2(); glutSwapBuffers();
} // Display func for sub window 3
void renderScenesw3() { glutSetWindow(subWindow3); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity();
gluLookAt(x-lz* , y, z+lx*,
x ,y ,z ,
0.0f,1.0f,0.0f); // Draw red cone at the location of the main camera
glPushMatrix();
glColor3f(1.0,0.0,0.0);
glTranslatef(x,y,z);
glRotatef(-(angle+deltaAngle)*180.0/3.14,0.0,1.0,0.0);
glutSolidCone(0.2,0.8f,,);
glPopMatrix(); renderScene2(); glutSwapBuffers();
} // Global idle func
void renderSceneAll() { // check for keyboard movement
if (deltaMove)
computePos(deltaMove); renderScenesw1();
renderScenesw2();
renderScenesw3();
} // -----------------------------------
// KEYBOARD
// ----------------------------------- void processNormalKeys(unsigned char key, int xx, int yy) { if (key == ) {
glutDestroyWindow(subWindow1);
glutDestroyWindow(subWindow2);
glutDestroyWindow(subWindow3);
glutDestroyWindow(mainWindow);
exit();
}
} 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;
deltaAngle = 0.0f;
xOrigin = -;
}
else {// state = GLUT_DOWN
xOrigin = x; }
}
} // -----------------------------------
// MAIN and INIT
// ----------------------------------- void init() { glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE); // register callbacks
glutIgnoreKeyRepeat();
glutKeyboardFunc(processNormalKeys);
glutSpecialFunc(pressKey);
glutSpecialUpFunc(releaseKey);
glutMouseFunc(mouseButton);
glutMotionFunc(mouseMove);
} int main(int argc, char **argv) { // init GLUT and create main window
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(,);
glutInitWindowSize(,);
mainWindow = glutCreateWindow("Lighthouse3D - GLUT Tutorial"); // callbacks for main window
glutDisplayFunc(renderScene);
glutReshapeFunc(changeSize);
glutIdleFunc(renderSceneAll);
init(); // sub windows
subWindow1 = glutCreateSubWindow(mainWindow, border,border,w-*border, h/ - border*/);
glutDisplayFunc(renderScenesw1);
init(); subWindow2 = glutCreateSubWindow(mainWindow, border,(h+border)/,w/-border*/, h/ - border*/);
glutDisplayFunc(renderScenesw2);
init(); subWindow3 = glutCreateSubWindow(mainWindow, (w+border)/,(h+border)/,w/-border*/,h/ - border*/);
glutDisplayFunc(renderScenesw3);
init(); // enter GLUT event processing cycle
glutMainLoop(); return ;
}

[译]GLUT教程 - 整合代码7的更多相关文章

  1. [译]GLUT教程 - 整合代码2

    Lighthouse3d.com >> GLUT Tutorial >> Input >> The Code So Far II 以下是前面几节的完整整合代码: # ...

  2. [译]GLUT教程 - 整合代码1

    Lighthouse3d.com >> GLUT Tutorial >> Input >> The Code So Far 以下是前面几节的完整整合代码: #inc ...

  3. [译]GLUT教程 - 整合代码8

    Lighthouse3d.com >> GLUT Tutorial >> Avoiding the Idle Func >> The Code So Far VII ...

  4. [译]GLUT教程 - 整合代码6

    Lighthouse3d.com >> GLUT Tutorial >> Extras >> The Code So Far VI 下面代码以窗体模式启动.你可以在 ...

  5. [译]GLUT教程 - 整合代码5

    Lighthouse3d.com >> GLUT Tutorial >> Extras >> The Code So Far V 该代码与位图字体的代码类似.区别是 ...

  6. [译]GLUT教程 - 整合代码4

    Lighthouse3d.com >> GLUT Tutorial >> Pop-up Menus >> The Code So Far IV 以下代码使用了位图字 ...

  7. [译]GLUT教程 - 整合代码3

    Lighthouse3d.com >> GLUT Tutorial >> Pop-up Menus >> The Code So Far III 这里我们准备包含一 ...

  8. [译]GLUT教程(目录)

    http://www.lighthouse3d.com/tutorials/glut-tutorial/ GLUT是OpenGL Utility Toolkit的意思.作者Mark J. Kilgar ...

  9. [译]GLUT教程 - 游戏模式

    Lighthouse3d.com >> GLUT Tutorial >> Extras >> Game Mode 根据GLUT官网的说明,GLUT的游戏模式是为开启 ...

随机推荐

  1. 网络爬虫框架Webmagic

    1 谈谈网络爬虫 1.1 什么是网络爬虫 在大数据时代,信息的采集是一项重要的工作,而互联网中的数据是海量的,如果单纯靠人力进行信息采集,不仅低效繁琐,搜集的成本也会提高.如何自动高效地获取互联网中我 ...

  2. 2.2多线程(java学习笔记)线程状态及线程操作的相关方法

    一.线程的状态 线程一般具有五种状态,即创建.就绪.运行.阻塞.终止. 它们之间的关系: 二.线程操作相关方法 1.设置和取得线程名称. 如果不设置线程名称,系统会自动分配线程名,一般格式为Threa ...

  3. spring属性注入

    1,set方法注入 (1)对于值类型的属性: 在对象中一定要有set方法 package com.songyan.demo1; import com.songyan.injection.Car; /* ...

  4. Android自定义View(二)

    前言 魅族手机的闹钟应用中有个倒计时,这个控件还是蛮有趣的.左边是魅族闹钟,右边是我们最终实现的效果,虽然有些细节还需优化,不过基本上已经达到了想要的效果,我们先来就来看看如何实现吧. 分析 确定宽高 ...

  5. 级联关系(内容大部分来自JavaEE轻量型解决方案其余的是我的想法)

    1. 级联关系 在Hibernate程序中持久化的对象之间会通过关联关系互相引用.对象进行保存.更新和删除等操作时,有时需要被关联的对象也执行相应的操作,如:假设需要关联关系的主动方对象执行操作时,被 ...

  6. remmina rdp远程连接windows

    一.remmina rdp远程连接windows sudo apt-get install remmina 二.ubuntu设置桌面快捷方式 ①找到Remmina远程桌面客户端 比如在[搜索您的本地和 ...

  7. python常见的编程错误

    常见的编程错误 2.1 试图访问一个未赋值的变量,会产生运行时错误. 2.2 ==,!=, >=和<=这几个运算符的两个符号之间出现空格,会造成语法错误. 2.3 !=,<>, ...

  8. log4j教程 11、日志记录到文件

    要写日志信息到一个文件中,必须使用org.apache.log4j.FileAppender.有以下FileAppender的配置参数: FileAppender配置: 属性 描述 immediate ...

  9. HDOJ 3359 Kind of a Blur

    用高斯消元对高斯模糊的图像还原.... Kind of a Blur Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/327 ...

  10. 【共享单车】—— React后台管理系统开发手记:权限设置和菜单调整(未完)

    前言:以下内容基于React全家桶+AntD实战课程的学习实践过程记录.最终成果github地址:https://github.com/66Web/react-antd-manager,欢迎star. ...