Code/Resource
Windows Develop
Linux-Unix program
Internet-Socket-Network
Web Server
Browser Client
Ftp Server
Ftp Client
Browser Plugins
Proxy Server
Email Server
Email Client
WEB Mail
Firewall-Security
Telnet Server
Telnet Client
ICQ-IM-Chat
Search Engine
Sniffer Package capture
Remote Control
xml-soap-webservice
P2P
WEB(ASP,PHP,...)
TCP/IP Stack
SNMP
Grid Computing
SilverLight
DNS
Cluster Service
Network Security
Communication-Mobile
Game Program
Editor
Multimedia program
Graph program
Compiler program
Compress-Decompress algrithms
Crypt_Decrypt algrithms
Mathimatics-Numerical algorithms
MultiLanguage
Disk/Storage
Java Develop
assembly language
Applications
Other systems
Database system
Embeded-SCM Develop
FlashMX/Flex
source in ebook
Delphi VCL
OS Develop
MiddleWare
MPI
MacOS develop
LabView
ELanguage
Software/Tools
E-Books
Artical/Document
main.cpp
Package: chap03.rar [view]
Upload User: gzqinmao
Upload Date: 2022-07-13
Package Size: 472k
Code Size: 4k
Category:
OpenGL program
Development Platform:
Visual C++
- #include <stdio.h>
- #include <conio.h>
- #include "Gut.h"
- #include "GutInput.h"
- #include "GutTimer.h"
- #include "render_dx9.h"
- #include "render_opengl.h"
- #include "render_data.h"
- #ifdef _ENABLE_DX10_
- #include "render_dx10.h"
- #endif
- GutTimer g_Timer;
- void GetUserInput(void)
- {
- // `读取鼠标`
- GutMouseInfo mouse;
- GutReadMouse(&mouse);
- // `读取键盘`
- char keyboard_state[256];
- GutReadKeyboard(keyboard_state);
- // `获得画完前一个画面到现在所经历的时间`
- float time_diff = g_Timer.Stop();
- g_Timer.Restart();
- float moving_speed = 2.0f * time_diff;
- float rotation_speed = 1.0 * time_diff;
- // `极坐标系统`
- static float theta = -MATH_PI * 0.5f;
- static float phi = 0.0f;
- // `如果左击, 就旋转镜头.`
- if ( mouse.button[0] )
- {
- theta += mouse.x * rotation_speed;
- phi -= mouse.y * rotation_speed;
- }
- float sin_phi, cos_phi;
- float sin_theta, cos_theta;
- FastMath::SinCos(phi, sin_phi, cos_phi);
- FastMath::SinCos(theta, sin_theta, cos_theta);
- // `计算镜头的法线`
- Vector4 camera_facing;
- camera_facing[0] = cos_phi * cos_theta;
- camera_facing[1] = sin_phi;
- camera_facing[2] = cos_phi * sin_theta;
- // `计算镜头正上方的轴向`
- Vector4 camera_up;
- FastMath::SinCos(phi + MATH_PI*0.5f, sin_phi, cos_phi);
- camera_up[0] = cos_phi * cos_theta;
- camera_up[1] = sin_phi;
- camera_up[2] = cos_phi * sin_theta;
- // `获得镜面右方的方向`
- Vector4 camera_right = Vector3CrossProduct(camera_facing, camera_up);
- // `按下W或方向键向上`
- if ( keyboard_state[GUTKEY_W] || keyboard_state[GUTKEY_UP] )
- {
- g_eye += camera_facing * moving_speed;
- }
- // `按下S或方向键向下`
- if ( keyboard_state[GUTKEY_S] || keyboard_state[GUTKEY_DOWN] )
- {
- g_eye -= camera_facing * moving_speed;
- }
- // `按下A或方向键向左`
- if ( keyboard_state[GUTKEY_A] || keyboard_state[GUTKEY_LEFT] )
- {
- g_eye -= camera_right * moving_speed;
- }
- // `按下D或方向键向右`
- if ( keyboard_state[GUTKEY_D] || keyboard_state[GUTKEY_RIGHT] )
- {
- g_eye += camera_right * moving_speed;
- }
- // `计算出镜头对准的点, 生成镜头转换矩阵时会用到.`
- g_lookat = g_eye + camera_facing;
- // `因为是对2个轴转动, 需要更新镜头朝上的轴.`
- g_up = camera_up;
- }
- void main(int argc, char *argv[])
- {
- // 默认使用DirectX 9来绘图
- char *device = "dx9";
- void (*render)(void) = RenderFrameDX9;
- bool (*init_resource)(void) = InitResourceDX9;
- bool (*release_resource)(void) = ReleaseResourceDX9;
- #ifdef _ENABLE_DX10_
- printf("Pressn(1) for Direct3D9n(2) for OpenGLn(3) for Direct3D10n");
- #else
- printf("Pressn(1) for Direct3D9n(2) for OpenGLn");
- #endif
- int c = getche();
- switch(c)
- {
- default:
- case '1':
- render = RenderFrameDX9;
- init_resource = InitResourceDX9;
- release_resource = ReleaseResourceDX9;
- break;
- case '2':
- device = "opengl";
- init_resource = InitResourceOpenGL;
- release_resource = ReleaseResourceOpenGL;
- render = RenderFrameOpenGL;
- break;
- case '3':
- #ifdef _ENABLE_DX10_
- device = "dx10";
- init_resource = InitResourceDX10;
- release_resource = ReleaseResourceDX10;
- render = RenderFrameDX10;
- #endif
- break;
- }
- // 在(100,100)的位置打开一个大小为(512x512)的窗口
- GutCreateWindow(100, 100, 512, 512, device);
- // 做OpenGL或DirectX初始化
- if ( !GutInitGraphicsDevice(device) )
- {
- printf("Failed to initialize %s devicen", device);
- exit(0);
- }
- GutInputInit();
- g_view_matrix.Identity();
- // 载入shader
- if ( !init_resource() )
- {
- release_resource();
- printf("Failed to load resourcesn");
- exit(0);
- }
- // 主循环
- while( GutProcessMessage() )
- {
- GetUserInput();
- render();
- }
- // 释放shader
- release_resource();
- // 关闭OpenGL/DirectX绘图装备
- GutReleaseGraphicsDevice();
- }