root/cafu/trunk/CaTools/MaterialViewer.cpp

Revision 455, 40.9 KB (checked in by Carsten, 4 months ago)

Updated copyright banners (in C++ files).

Line 
1/*
2=================================================================================
3This file is part of Cafu, the open-source game engine and graphics engine
4for multiplayer, cross-platform, real-time 3D action.
5Copyright (C) 2002-2012 Carsten Fuchs Software.
6
7Cafu is free software: you can redistribute it and/or modify it under the terms
8of the GNU General Public License as published by the Free Software Foundation,
9either version 3 of the License, or (at your option) any later version.
10
11Cafu is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13PURPOSE. See the GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with Cafu. If not, see <http://www.gnu.org/licenses/>.
17
18For support and more information about Cafu, visit us at <http://www.cafu.de>.
19=================================================================================
20*/
21
22/***********************/
23/*** Material Viewer ***/
24/***********************/
25
26#include <math.h>
27#include <stdio.h>
28#include <stdlib.h>
29
30#ifdef _WIN32
31#define WIN32_LEAN_AND_MEAN
32#include <windows.h>
33#include <direct.h>
34    #if defined(_MSC_VER)
35        #if (_MSC_VER<1300)
36        #define for if (false) ; else for
37    #endif
38    #endif
39#else
40#include <cstring>
41#include <dlfcn.h>
42#define FreeLibrary dlclose
43#endif
44
45#include "ConsoleCommands/Console.hpp"
46#include "ConsoleCommands/ConsoleInterpreter.hpp"
47#include "ConsoleCommands/ConsoleStdout.hpp"
48#include "FileSys/FileManImpl.hpp"
49#include "Fonts/Font.hpp"
50#include "OpenGL/OpenGLWindow.hpp"
51#include "MaterialSystem/Material.hpp"
52#include "MaterialSystem/MaterialManager.hpp"
53#include "MaterialSystem/MaterialManagerImpl.hpp"
54#include "MaterialSystem/Mesh.hpp"
55#include "MaterialSystem/Renderer.hpp"
56#include "MaterialSystem/TextureMap.hpp"
57#include "Math3D/Plane3.hpp"
58#include "Math3D/Matrix.hpp"
59#include "Templates/Array.hpp"
60#include "Util/Util.hpp"
61#include "PlatformAux.hpp"
62
63
64static cf::ConsoleStdoutT ConsoleStdout;
65cf::ConsoleI* Console=&ConsoleStdout;
66
67static cf::FileSys::FileManImplT FileManImpl;
68cf::FileSys::FileManI* cf::FileSys::FileMan=&FileManImpl;
69
70ConsoleInterpreterI* ConsoleInterpreter=NULL;
71MaterialManagerI*    MaterialManager   =NULL;
72
73
74const char*         BaseDirectoryName="Games/DeathMatch";
75const char*         MaterialName=NULL;
76ArrayT<const char*> MaterialScriptNames;
77
78
79void CreateMeshes(ArrayT<MatSys::MeshT>& Meshes, const MaterialT& MyMaterial)
80{
81    const float ar=float(MyMaterial.GetPixelSizeY())/float(MyMaterial.GetPixelSizeX());
82
83    MatSys::MeshT* M=NULL;
84
85
86    // The small inner cubic box.
87    Meshes.PushBackEmpty();     // Front
88    M=&Meshes[Meshes.Size()-1];
89    M->Type   =MatSys::MeshT::TriangleFan;
90    M->Winding=MatSys::MeshT::CCW;
91    M->Vertices.PushBackEmpty(4);
92    M->Vertices[0].SetOrigin(-100.0, 1000.0, -100.0*ar); M->Vertices[0].SetTextureCoord(0.0, 1.0); M->Vertices[0].SetLightMapCoord(0.0, 3.0); M->Vertices[0].SetNormal( 0.0, -1.0, 0.0); M->Vertices[0].SetTangent( 1.0,  0.0,  0.0); M->Vertices[0].SetBiNormal( 0.0,  0.0, -1.0); M->Vertices[0].SetColor(1.0, 0.0, 0.0);
93    M->Vertices[1].SetOrigin( 100.0, 1000.0, -100.0*ar); M->Vertices[1].SetTextureCoord(1.0, 1.0); M->Vertices[1].SetLightMapCoord(2.0, 3.0); M->Vertices[1].SetNormal( 0.0, -1.0, 0.0); M->Vertices[1].SetTangent( 1.0,  0.0,  0.0); M->Vertices[1].SetBiNormal( 0.0,  0.0, -1.0); M->Vertices[1].SetColor(0.0, 1.0, 0.0);
94    M->Vertices[2].SetOrigin( 100.0, 1000.0,  100.0*ar); M->Vertices[2].SetTextureCoord(1.0, 0.0); M->Vertices[2].SetLightMapCoord(2.0, 0.0); M->Vertices[2].SetNormal( 0.0, -1.0, 0.0); M->Vertices[2].SetTangent( 1.0,  0.0,  0.0); M->Vertices[2].SetBiNormal( 0.0,  0.0, -1.0); M->Vertices[2].SetColor(0.0, 0.0, 1.0);
95    M->Vertices[3].SetOrigin(-100.0, 1000.0,  100.0*ar); M->Vertices[3].SetTextureCoord(0.0, 0.0); M->Vertices[3].SetLightMapCoord(0.0, 0.0); M->Vertices[3].SetNormal( 0.0, -1.0, 0.0); M->Vertices[3].SetTangent( 1.0,  0.0,  0.0); M->Vertices[3].SetBiNormal( 0.0,  0.0, -1.0); M->Vertices[3].SetColor(1.0, 1.0, 0.0);
96
97    Meshes.PushBackEmpty();     // Right
98    M=&Meshes[Meshes.Size()-1];
99    M->Type   =MatSys::MeshT::TriangleFan;
100    M->Winding=MatSys::MeshT::CCW;
101    M->Vertices.PushBackEmpty(4);
102    M->Vertices[0].SetOrigin( 100.0, 1000.0, -100.0*ar); M->Vertices[0].SetTextureCoord(0.0, 1.0); M->Vertices[0].SetLightMapCoord(0.0, 3.0); M->Vertices[0].SetNormal( 1.0,  0.0, 0.0); M->Vertices[0].SetTangent( 0.0,  1.0,  0.0); M->Vertices[0].SetBiNormal( 0.0,  0.0, -1.0); M->Vertices[0].SetColor(1.0, 0.0, 0.0);
103    M->Vertices[1].SetOrigin( 100.0, 1200.0, -100.0*ar); M->Vertices[1].SetTextureCoord(1.0, 1.0); M->Vertices[1].SetLightMapCoord(2.0, 3.0); M->Vertices[1].SetNormal( 1.0,  0.0, 0.0); M->Vertices[1].SetTangent( 0.0,  1.0,  0.0); M->Vertices[1].SetBiNormal( 0.0,  0.0, -1.0); M->Vertices[1].SetColor(0.0, 1.0, 0.0);
104    M->Vertices[2].SetOrigin( 100.0, 1200.0,  100.0*ar); M->Vertices[2].SetTextureCoord(1.0, 0.0); M->Vertices[2].SetLightMapCoord(2.0, 0.0); M->Vertices[2].SetNormal( 1.0,  0.0, 0.0); M->Vertices[2].SetTangent( 0.0,  1.0,  0.0); M->Vertices[2].SetBiNormal( 0.0,  0.0, -1.0); M->Vertices[2].SetColor(0.0, 0.0, 1.0);
105    M->Vertices[3].SetOrigin( 100.0, 1000.0,  100.0*ar); M->Vertices[3].SetTextureCoord(0.0, 0.0); M->Vertices[3].SetLightMapCoord(0.0, 0.0); M->Vertices[3].SetNormal( 1.0,  0.0, 0.0); M->Vertices[3].SetTangent( 0.0,  1.0,  0.0); M->Vertices[3].SetBiNormal( 0.0,  0.0, -1.0); M->Vertices[3].SetColor(1.0, 1.0, 0.0);
106
107    Meshes.PushBackEmpty();     // Back
108    M=&Meshes[Meshes.Size()-1];
109    M->Type   =MatSys::MeshT::TriangleFan;
110    M->Winding=MatSys::MeshT::CCW;
111    M->Vertices.PushBackEmpty(4);
112    M->Vertices[0].SetOrigin(-100.0, 1200.0,  100.0*ar); M->Vertices[0].SetTextureCoord(1.0, 0.0); M->Vertices[0].SetLightMapCoord(2.0, 0.0); M->Vertices[0].SetNormal( 0.0,  1.0, 0.0); M->Vertices[0].SetTangent(-1.0,  0.0,  0.0); M->Vertices[0].SetBiNormal( 0.0,  0.0, -1.0); M->Vertices[0].SetColor(1.0, 0.0, 0.0);
113    M->Vertices[1].SetOrigin( 100.0, 1200.0,  100.0*ar); M->Vertices[1].SetTextureCoord(0.0, 0.0); M->Vertices[1].SetLightMapCoord(0.0, 0.0); M->Vertices[1].SetNormal( 0.0,  1.0, 0.0); M->Vertices[1].SetTangent(-1.0,  0.0,  0.0); M->Vertices[1].SetBiNormal( 0.0,  0.0, -1.0); M->Vertices[1].SetColor(0.0, 1.0, 0.0);
114    M->Vertices[2].SetOrigin( 100.0, 1200.0, -100.0*ar); M->Vertices[2].SetTextureCoord(0.0, 1.0); M->Vertices[2].SetLightMapCoord(0.0, 3.0); M->Vertices[2].SetNormal( 0.0,  1.0, 0.0); M->Vertices[2].SetTangent(-1.0,  0.0,  0.0); M->Vertices[2].SetBiNormal( 0.0,  0.0, -1.0); M->Vertices[2].SetColor(0.0, 0.0, 1.0);
115    M->Vertices[3].SetOrigin(-100.0, 1200.0, -100.0*ar); M->Vertices[3].SetTextureCoord(1.0, 1.0); M->Vertices[3].SetLightMapCoord(2.0, 3.0); M->Vertices[3].SetNormal( 0.0,  1.0, 0.0); M->Vertices[3].SetTangent(-1.0,  0.0,  0.0); M->Vertices[3].SetBiNormal( 0.0,  0.0, -1.0); M->Vertices[3].SetColor(1.0, 1.0, 0.0);
116
117    Meshes.PushBackEmpty();     // Left
118    M=&Meshes[Meshes.Size()-1];
119    M->Type   =MatSys::MeshT::TriangleFan;
120    M->Winding=MatSys::MeshT::CCW;
121    M->Vertices.PushBackEmpty(4);
122    M->Vertices[0].SetOrigin(-100.0, 1200.0, -100.0*ar); M->Vertices[0].SetTextureCoord(0.0, 1.0); M->Vertices[0].SetLightMapCoord(0.0, 3.0); M->Vertices[0].SetNormal(-1.0,  0.0, 0.0); M->Vertices[0].SetTangent( 0.0, -1.0,  0.0); M->Vertices[0].SetBiNormal( 0.0,  0.0, -1.0); M->Vertices[0].SetColor(1.0, 0.0, 0.0);
123    M->Vertices[1].SetOrigin(-100.0, 1000.0, -100.0*ar); M->Vertices[1].SetTextureCoord(1.0, 1.0); M->Vertices[1].SetLightMapCoord(2.0, 3.0); M->Vertices[1].SetNormal(-1.0,  0.0, 0.0); M->Vertices[1].SetTangent( 0.0, -1.0,  0.0); M->Vertices[1].SetBiNormal( 0.0,  0.0, -1.0); M->Vertices[1].SetColor(0.0, 1.0, 0.0);
124    M->Vertices[2].SetOrigin(-100.0, 1000.0,  100.0*ar); M->Vertices[2].SetTextureCoord(1.0, 0.0); M->Vertices[2].SetLightMapCoord(2.0, 0.0); M->Vertices[2].SetNormal(-1.0,  0.0, 0.0); M->Vertices[2].SetTangent( 0.0, -1.0,  0.0); M->Vertices[2].SetBiNormal( 0.0,  0.0, -1.0); M->Vertices[2].SetColor(0.0, 0.0, 1.0);
125    M->Vertices[3].SetOrigin(-100.0, 1200.0,  100.0*ar); M->Vertices[3].SetTextureCoord(0.0, 0.0); M->Vertices[3].SetLightMapCoord(0.0, 0.0); M->Vertices[3].SetNormal(-1.0,  0.0, 0.0); M->Vertices[3].SetTangent( 0.0, -1.0,  0.0); M->Vertices[3].SetBiNormal( 0.0,  0.0, -1.0); M->Vertices[3].SetColor(1.0, 1.0, 0.0);
126
127    Meshes.PushBackEmpty();     // Top
128    M=&Meshes[Meshes.Size()-1];
129    M->Type   =MatSys::MeshT::TriangleFan;
130    M->Winding=MatSys::MeshT::CCW;
131    M->Vertices.PushBackEmpty(4);
132    M->Vertices[0].SetOrigin(-100.0, 1000.0,  100.0*ar); M->Vertices[0].SetTextureCoord(0.0, 1.0); M->Vertices[0].SetLightMapCoord(0.0, 3.0); M->Vertices[0].SetNormal( 0.0,  0.0, 1.0); M->Vertices[0].SetTangent( 1.0,  0.0,  0.0); M->Vertices[0].SetBiNormal( 0.0, -1.0,  0.0); M->Vertices[0].SetColor(1.0, 0.0, 0.0);
133    M->Vertices[1].SetOrigin( 100.0, 1000.0,  100.0*ar); M->Vertices[1].SetTextureCoord(1.0, 1.0); M->Vertices[1].SetLightMapCoord(2.0, 3.0); M->Vertices[1].SetNormal( 0.0,  0.0, 1.0); M->Vertices[1].SetTangent( 1.0,  0.0,  0.0); M->Vertices[1].SetBiNormal( 0.0, -1.0,  0.0); M->Vertices[1].SetColor(0.0, 1.0, 0.0);
134    M->Vertices[2].SetOrigin( 100.0, 1200.0,  100.0*ar); M->Vertices[2].SetTextureCoord(1.0, 0.0); M->Vertices[2].SetLightMapCoord(2.0, 0.0); M->Vertices[2].SetNormal( 0.0,  0.0, 1.0); M->Vertices[2].SetTangent( 1.0,  0.0,  0.0); M->Vertices[2].SetBiNormal( 0.0, -1.0,  0.0); M->Vertices[2].SetColor(0.0, 0.0, 1.0);
135    M->Vertices[3].SetOrigin(-100.0, 1200.0,  100.0*ar); M->Vertices[3].SetTextureCoord(0.0, 0.0); M->Vertices[3].SetLightMapCoord(0.0, 0.0); M->Vertices[3].SetNormal( 0.0,  0.0, 1.0); M->Vertices[3].SetTangent( 1.0,  0.0,  0.0); M->Vertices[3].SetBiNormal( 0.0, -1.0,  0.0); M->Vertices[3].SetColor(1.0, 1.0, 0.0);
136
137    Meshes.PushBackEmpty();     // Bottom
138    M=&Meshes[Meshes.Size()-1];
139    M->Type   =MatSys::MeshT::TriangleFan;
140    M->Winding=MatSys::MeshT::CCW;
141    M->Vertices.PushBackEmpty(4);
142    M->Vertices[0].SetOrigin(-100.0, 1200.0, -100.0*ar); M->Vertices[0].SetTextureCoord(0.0, 1.0); M->Vertices[0].SetLightMapCoord(0.0, 3.0); M->Vertices[0].SetNormal( 0.0,  0.0, -1.0); M->Vertices[0].SetTangent( 1.0,  0.0,  0.0); M->Vertices[0].SetBiNormal( 0.0,  1.0,  0.0); M->Vertices[0].SetColor(1.0, 0.0, 0.0);
143    M->Vertices[1].SetOrigin( 100.0, 1200.0, -100.0*ar); M->Vertices[1].SetTextureCoord(1.0, 1.0); M->Vertices[1].SetLightMapCoord(2.0, 3.0); M->Vertices[1].SetNormal( 0.0,  0.0, -1.0); M->Vertices[1].SetTangent( 1.0,  0.0,  0.0); M->Vertices[1].SetBiNormal( 0.0,  1.0,  0.0); M->Vertices[1].SetColor(0.0, 1.0, 0.0);
144    M->Vertices[2].SetOrigin( 100.0, 1000.0, -100.0*ar); M->Vertices[2].SetTextureCoord(1.0, 0.0); M->Vertices[2].SetLightMapCoord(2.0, 0.0); M->Vertices[2].SetNormal( 0.0,  0.0, -1.0); M->Vertices[2].SetTangent( 1.0,  0.0,  0.0); M->Vertices[2].SetBiNormal( 0.0,  1.0,  0.0); M->Vertices[2].SetColor(0.0, 0.0, 1.0);
145    M->Vertices[3].SetOrigin(-100.0, 1000.0, -100.0*ar); M->Vertices[3].SetTextureCoord(0.0, 0.0); M->Vertices[3].SetLightMapCoord(0.0, 0.0); M->Vertices[3].SetNormal( 0.0,  0.0, -1.0); M->Vertices[3].SetTangent( 1.0,  0.0,  0.0); M->Vertices[3].SetBiNormal( 0.0,  1.0,  0.0); M->Vertices[3].SetColor(1.0, 1.0, 0.0);
146
147
148    // The outer walls.
149    Meshes.PushBackEmpty();     // Back
150    M=&Meshes[Meshes.Size()-1];
151    M->Type   =MatSys::MeshT::TriangleFan;
152    M->Winding=MatSys::MeshT::CCW;
153    M->Vertices.PushBackEmpty(4);
154    M->Vertices[0].SetOrigin(-1000.0,  5000.0, -1000.0*ar); M->Vertices[0].SetTextureCoord(0.0, 1.0); M->Vertices[0].SetLightMapCoord(0.0, 3.0); M->Vertices[0].SetNormal( 0.0, -1.0, 0.0); M->Vertices[0].SetTangent( 1.0,  0.0,  0.0); M->Vertices[0].SetBiNormal( 0.0,  0.0, -1.0); M->Vertices[0].SetColor(1.0, 0.0, 0.0);
155    M->Vertices[1].SetOrigin( 1000.0,  5000.0, -1000.0*ar); M->Vertices[1].SetTextureCoord(1.0, 1.0); M->Vertices[1].SetLightMapCoord(2.0, 3.0); M->Vertices[1].SetNormal( 0.0, -1.0, 0.0); M->Vertices[1].SetTangent( 1.0,  0.0,  0.0); M->Vertices[1].SetBiNormal( 0.0,  0.0, -1.0); M->Vertices[1].SetColor(0.0, 1.0, 0.0);
156    M->Vertices[2].SetOrigin( 1000.0,  5000.0,  1000.0*ar); M->Vertices[2].SetTextureCoord(1.0, 0.0); M->Vertices[2].SetLightMapCoord(2.0, 0.0); M->Vertices[2].SetNormal( 0.0, -1.0, 0.0); M->Vertices[2].SetTangent( 1.0,  0.0,  0.0); M->Vertices[2].SetBiNormal( 0.0,  0.0, -1.0); M->Vertices[2].SetColor(0.0, 0.0, 1.0);
157    M->Vertices[3].SetOrigin(-1000.0,  5000.0,  1000.0*ar); M->Vertices[3].SetTextureCoord(0.0, 0.0); M->Vertices[3].SetLightMapCoord(0.0, 0.0); M->Vertices[3].SetNormal( 0.0, -1.0, 0.0); M->Vertices[3].SetTangent( 1.0,  0.0,  0.0); M->Vertices[3].SetBiNormal( 0.0,  0.0, -1.0); M->Vertices[3].SetColor(1.0, 1.0, 0.0);
158
159    Meshes.PushBackEmpty();     // Left
160    M=&Meshes[Meshes.Size()-1];
161    M->Type   =MatSys::MeshT::TriangleFan;
162    M->Winding=MatSys::MeshT::CCW;
163    M->Vertices.PushBackEmpty(4);
164    M->Vertices[0].SetOrigin(-1000.0, -5000.0, -1000.0*ar); M->Vertices[0].SetTextureCoord(0.0, 1.0); M->Vertices[0].SetLightMapCoord(0.0, 3.0); M->Vertices[0].SetNormal( 1.0,  0.0, 0.0); M->Vertices[0].SetTangent( 0.0,  1.0,  0.0); M->Vertices[0].SetBiNormal( 0.0,  0.0, -1.0); M->Vertices[0].SetColor(1.0, 0.0, 0.0);
165    M->Vertices[1].SetOrigin(-1000.0,  5000.0, -1000.0*ar); M->Vertices[1].SetTextureCoord(5.0, 1.0); M->Vertices[1].SetLightMapCoord(2.0, 3.0); M->Vertices[1].SetNormal( 1.0,  0.0, 0.0); M->Vertices[1].SetTangent( 0.0,  1.0,  0.0); M->Vertices[1].SetBiNormal( 0.0,  0.0, -1.0); M->Vertices[1].SetColor(0.0, 1.0, 0.0);
166    M->Vertices[2].SetOrigin(-1000.0,  5000.0,  1000.0*ar); M->Vertices[2].SetTextureCoord(5.0, 0.0); M->Vertices[2].SetLightMapCoord(2.0, 0.0); M->Vertices[2].SetNormal( 1.0,  0.0, 0.0); M->Vertices[2].SetTangent( 0.0,  1.0,  0.0); M->Vertices[2].SetBiNormal( 0.0,  0.0, -1.0); M->Vertices[2].SetColor(0.0, 0.0, 1.0);
167    M->Vertices[3].SetOrigin(-1000.0, -5000.0,  1000.0*ar); M->Vertices[3].SetTextureCoord(0.0, 0.0); M->Vertices[3].SetLightMapCoord(0.0, 0.0); M->Vertices[3].SetNormal( 1.0,  0.0, 0.0); M->Vertices[3].SetTangent( 0.0,  1.0,  0.0); M->Vertices[3].SetBiNormal( 0.0,  0.0, -1.0); M->Vertices[3].SetColor(1.0, 1.0, 0.0);
168
169    Meshes.PushBackEmpty();     // Front
170    M=&Meshes[Meshes.Size()-1];
171    M->Type   =MatSys::MeshT::TriangleFan;
172    M->Winding=MatSys::MeshT::CCW;
173    M->Vertices.PushBackEmpty(4);
174    M->Vertices[0].SetOrigin(-1000.0, -5000.0,  1000.0*ar); M->Vertices[0].SetTextureCoord(1.0, 0.0); M->Vertices[0].SetLightMapCoord(2.0, 0.0); M->Vertices[0].SetNormal( 0.0,  1.0, 0.0); M->Vertices[0].SetTangent(-1.0,  0.0,  0.0); M->Vertices[0].SetBiNormal( 0.0,  0.0, -1.0); M->Vertices[0].SetColor(1.0, 0.0, 0.0);
175    M->Vertices[1].SetOrigin( 1000.0, -5000.0,  1000.0*ar); M->Vertices[1].SetTextureCoord(0.0, 0.0); M->Vertices[1].SetLightMapCoord(0.0, 0.0); M->Vertices[1].SetNormal( 0.0,  1.0, 0.0); M->Vertices[1].SetTangent(-1.0,  0.0,  0.0); M->Vertices[1].SetBiNormal( 0.0,  0.0, -1.0); M->Vertices[1].SetColor(0.0, 1.0, 0.0);
176    M->Vertices[2].SetOrigin( 1000.0, -5000.0, -1000.0*ar); M->Vertices[2].SetTextureCoord(0.0, 1.0); M->Vertices[2].SetLightMapCoord(0.0, 3.0); M->Vertices[2].SetNormal( 0.0,  1.0, 0.0); M->Vertices[2].SetTangent(-1.0,  0.0,  0.0); M->Vertices[2].SetBiNormal( 0.0,  0.0, -1.0); M->Vertices[2].SetColor(0.0, 0.0, 1.0);
177    M->Vertices[3].SetOrigin(-1000.0, -5000.0, -1000.0*ar); M->Vertices[3].SetTextureCoord(1.0, 1.0); M->Vertices[3].SetLightMapCoord(2.0, 3.0); M->Vertices[3].SetNormal( 0.0,  1.0, 0.0); M->Vertices[3].SetTangent(-1.0,  0.0,  0.0); M->Vertices[3].SetBiNormal( 0.0,  0.0, -1.0); M->Vertices[3].SetColor(1.0, 1.0, 0.0);
178
179    Meshes.PushBackEmpty();     // Right
180    M=&Meshes[Meshes.Size()-1];
181    M->Type   =MatSys::MeshT::TriangleFan;
182    M->Winding=MatSys::MeshT::CCW;
183    M->Vertices.PushBackEmpty(4);
184    M->Vertices[0].SetOrigin( 1000.0,  5000.0, -1000.0*ar); M->Vertices[0].SetTextureCoord(0.0, 1.0); M->Vertices[0].SetLightMapCoord(0.0, 3.0); M->Vertices[0].SetNormal(-1.0,  0.0, 0.0); M->Vertices[0].SetTangent( 0.0, -1.0,  0.0); M->Vertices[0].SetBiNormal( 0.0,  0.0, -1.0); M->Vertices[0].SetColor(1.0, 0.0, 0.0);
185    M->Vertices[1].SetOrigin( 1000.0, -5000.0, -1000.0*ar); M->Vertices[1].SetTextureCoord(5.0, 1.0); M->Vertices[1].SetLightMapCoord(2.0, 3.0); M->Vertices[1].SetNormal(-1.0,  0.0, 0.0); M->Vertices[1].SetTangent( 0.0, -1.0,  0.0); M->Vertices[1].SetBiNormal( 0.0,  0.0, -1.0); M->Vertices[1].SetColor(0.0, 1.0, 0.0);
186    M->Vertices[2].SetOrigin( 1000.0, -5000.0,  1000.0*ar); M->Vertices[2].SetTextureCoord(5.0, 0.0); M->Vertices[2].SetLightMapCoord(2.0, 0.0); M->Vertices[2].SetNormal(-1.0,  0.0, 0.0); M->Vertices[2].SetTangent( 0.0, -1.0,  0.0); M->Vertices[2].SetBiNormal( 0.0,  0.0, -1.0); M->Vertices[2].SetColor(0.0, 0.0, 1.0);
187    M->Vertices[3].SetOrigin( 1000.0,  5000.0,  1000.0*ar); M->Vertices[3].SetTextureCoord(0.0, 0.0); M->Vertices[3].SetLightMapCoord(0.0, 0.0); M->Vertices[3].SetNormal(-1.0,  0.0, 0.0); M->Vertices[3].SetTangent( 0.0, -1.0,  0.0); M->Vertices[3].SetBiNormal( 0.0,  0.0, -1.0); M->Vertices[3].SetColor(1.0, 1.0, 0.0);
188}
189
190
191void DrawSilhouettes(MatSys::RendererI* Renderer, const ArrayT<MatSys::MeshT>& Meshes)
192{
193    // Assumptions:
194    // 1. All meshes are TriangleFans.
195    // 2. All TriangleFans are planar.
196    VectorT LightPosition(Renderer->GetCurrentLightSourcePosition()[0], Renderer->GetCurrentLightSourcePosition()[1], Renderer->GetCurrentLightSourcePosition()[2]);
197
198    for (unsigned long MeshNr=0; MeshNr<Meshes.Size(); MeshNr++)
199    {
200        const MatSys::MeshT& M=Meshes[MeshNr];
201
202        if (M.Type!=MatSys::MeshT::TriangleFan) continue;
203        if (M.Vertices.Size()<3) continue;
204
205        // Is this mesh backfacing to the lightsource?
206        const VectorT         Normal(M.Vertices[0].Normal[0], M.Vertices[0].Normal[1], M.Vertices[0].Normal[2]);
207        const Plane3T<double> Plane(Normal, dot(Normal, VectorT(M.Vertices[0].Origin[0], M.Vertices[0].Origin[1], M.Vertices[0].Origin[2])));
208
209        if (Plane.GetDistance(LightPosition)>=0.0) continue;
210
211        // Yes, backfacing the lightsource.
212        // Render the silhouette quads.
213        MatSys::MeshT SilhouetteMesh(MatSys::MeshT::QuadStrip, MatSys::MeshT::CCW);
214
215        for (unsigned long VertexNr=0; VertexNr<M.Vertices.Size(); VertexNr++)
216        {
217            // Yes, this could be further optimized...
218            const VectorT  A(M.Vertices[VertexNr].Origin[0], M.Vertices[VertexNr].Origin[1], M.Vertices[VertexNr].Origin[2]);
219            const VectorT LA=A-LightPosition;
220
221            // Note that the order is reversed because we're dealing with a back-facing polygon as occluder!
222            SilhouetteMesh.Vertices.PushBackEmpty(2);
223            SilhouetteMesh.Vertices[VertexNr*2+0].SetOrigin(LA.x, LA.y, LA.z, 0.0);
224            SilhouetteMesh.Vertices[VertexNr*2+1].SetOrigin( A.x,  A.y,  A.z, 1.0);
225        }
226
227        SilhouetteMesh.Vertices.PushBack(SilhouetteMesh.Vertices[0]);   // Close the silhouette for this occluder.
228        SilhouetteMesh.Vertices.PushBack(SilhouetteMesh.Vertices[1]);
229
230        Renderer->RenderMesh(SilhouetteMesh);
231
232
233        // Render the occluders near cap (front-facing wrt. the light source).
234        // (The MatSys expects vertices for front-facing polygons to be specified in CCW order.
235        //  However, as we are considering a *back-facing* polygon as a occluder (oriented CW when looked at from behind),
236        //  we have to reverse the order of its vertices in order to turn it into a CCW ordered, front-facing polygon.)
237        MatSys::MeshT M_front(MatSys::MeshT::TriangleFan, MatSys::MeshT::CCW);
238
239        M_front.Vertices.PushBackEmpty(M.Vertices.Size());
240
241        for (unsigned long VertexNr=0; VertexNr<M.Vertices.Size(); VertexNr++)
242            M_front.Vertices[M_front.Vertices.Size()-VertexNr-1].SetOrigin(M.Vertices[VertexNr].Origin[0],
243                                                                           M.Vertices[VertexNr].Origin[1],
244                                                                           M.Vertices[VertexNr].Origin[2], 1.0);
245
246        Renderer->RenderMesh(M_front);
247
248
249        // Render the occluders far cap (back-facing wrt. the light source).
250        // As we are already dealing with a back-facing polygon, the vertex order is already as required,
251        // we just have to project them to infinity as seen from the light source.
252        MatSys::MeshT M_back(MatSys::MeshT::TriangleFan, MatSys::MeshT::CCW);
253
254        M_back.Vertices.PushBackEmpty(M.Vertices.Size());
255
256        for (unsigned long VertexNr=0; VertexNr<M.Vertices.Size(); VertexNr++)
257            M_back.Vertices[VertexNr].SetOrigin(M.Vertices[VertexNr].Origin[0]-LightPosition.x,
258                                                M.Vertices[VertexNr].Origin[1]-LightPosition.y,
259                                                M.Vertices[VertexNr].Origin[2]-LightPosition.z, 0.0);
260
261        Renderer->RenderMesh(M_back);
262    }
263}
264
265
266void Usage()
267{
268    printf("\nOPTIONS:\n");
269    printf("\n");
270    printf("-m=MyMaterial specifies the name of the desired material, which must be\n");
271    printf("   defined in one of the material script files (see below).\n");
272    printf("\n");
273    printf("-bd=base/dir specifies the base directory from which I look both for material\n");
274    printf("   scripts and the materials associated textures.\n");
275    printf("   The default (if you don't use -bd) is %s\n", BaseDirectoryName);
276    printf("\n");
277    printf("-ms=MyMatScript.cmat specifies the material script that contains a definition\n");
278    printf("   of \"MyMaterial\" (see above). You may use -ms several times, making me look\n");
279    printf("   into each specified script for a definition of the material.\n");
280    printf("   If you do not use -ms at all, I'll look into ALL material scripts that I can\n");
281    printf("   find in %s/Materials, so you probably don't need it as well.\n", BaseDirectoryName);
282    printf("\n");
283    printf("-r=RendererXY overrides the automatic selection of the \"best\" renderer,\n");
284    printf("   and loads the renderer with base name RendererXY instead.\n");
285    printf("   Only provide the base name (e.g. RendererOpenGL12), no path and no suffix.\n");
286    printf("\n");
287
288    exit(0);
289}
290
291
292struct LightSourceInfoT
293{
294    bool  IsOn;
295    float Pos[3];
296    float Radius;
297    float DiffColor[3];
298    float SpecColor[3];
299
300    LightSourceInfoT()
301    {
302        IsOn=false;
303        Pos[0]=0.0; Pos[1]=0.0; Pos[2]=0.0;
304        Radius=1000.0;
305        DiffColor[0]=1.0; DiffColor[1]=1.0; DiffColor[2]=1.0;
306        SpecColor[0]=1.0; SpecColor[1]=1.0; SpecColor[2]=1.0;
307    }
308};
309
310
311int main(int ArgC, const char* ArgV[])
312{
313    printf("\nCafu Material Viewer (%s)\n\n", __DATE__);
314
315    // Initialize the FileMan by mounting the default file system.
316    // Note that specifying "./" (instead of "") as the file system description effectively prevents the use of
317    // absolute paths like "D:\abc\someDir\someFile.xy" or "/usr/bin/xy". This however should be fine for this application.
318    cf::FileSys::FileMan->MountFileSystem(cf::FileSys::FS_TYPE_LOCAL_PATH, "./", "");
319    cf::FileSys::FileMan->MountFileSystem(cf::FileSys::FS_TYPE_ZIP_ARCHIVE, "Games/DeathMatch/Textures/TechDemo.zip", "Games/DeathMatch/Textures/TechDemo/", "Ca3DE");
320    cf::FileSys::FileMan->MountFileSystem(cf::FileSys::FS_TYPE_ZIP_ARCHIVE, "Games/DeathMatch/Textures/SkyDomes.zip", "Games/DeathMatch/Textures/SkyDomes/", "Ca3DE");
321
322    std::string DesiredRendererName="";
323
324    // Process the command line options.
325    for (int ArgNr=1; ArgNr<ArgC; ArgNr++)
326    {
327             if (_strnicmp(ArgV[ArgNr], "-m=" , 3)==0) MaterialName=ArgV[ArgNr]+3;
328        else if (_strnicmp(ArgV[ArgNr], "-bd=", 4)==0) BaseDirectoryName=ArgV[ArgNr]+4;
329        else if (_strnicmp(ArgV[ArgNr], "-ms=", 4)==0) MaterialScriptNames.PushBack(ArgV[ArgNr]+4);
330        else if (_strnicmp(ArgV[ArgNr], "-r=" , 3)==0) DesiredRendererName=ArgV[ArgNr]+3;
331        else
332        {
333            printf("Sorry, I don't know what option \"%s\" means.\n", ArgV[ArgNr]);
334            Usage();
335        }
336    }
337
338    // Setup the global Material Manager pointer.
339    static MaterialManagerImplT MatManImpl;
340
341    MaterialManager=&MatManImpl;
342
343    // Make sure that a material has been specified.
344    if (MaterialName==NULL)
345    {
346        printf("Please use the -m option in order to specify the desired material!\n");
347        Usage();
348    }
349
350    // Register the material script files with the material manager.
351    if (MaterialScriptNames.Size()==0)
352    {
353        // The -ms option has not been used, so register all material script files in BaseDirectoryName/Materials.
354        MaterialManager->RegisterMaterialScriptsInDir(std::string(BaseDirectoryName)+"/Materials", std::string(BaseDirectoryName)+"/");
355    }
356    else
357    {
358        // Material script files have been specified - register them now.
359        for (unsigned long MSNNr=0; MSNNr<MaterialScriptNames.Size(); MSNNr++)
360            MaterialManager->RegisterMaterialScript(std::string(BaseDirectoryName)+"/"+MaterialScriptNames[MSNNr], std::string(BaseDirectoryName)+"/");
361    }
362
363    // Get the desired material.
364    MaterialT* MyMaterial=MaterialManager->GetMaterial(MaterialName);
365
366    if (MyMaterial==NULL)
367    {
368        printf("Sorry, I have not been able to get material \"%s\"\n", MaterialName);
369        printf("from the registered material script files. Possible causes:\n");
370        printf("- the material is not defined in any of the script files (material name typo?)\n");
371        printf("- the material script file(s) could not be opened (script file name typo?)\n");
372        printf("- the material script file contains bugs, i.e. syntax errors.\n");
373        return 0;
374    }
375
376
377    // Open an OpenGL window.
378    const char* ErrorMsg=SingleOpenGLWindow->Open("Cafu Material Viewer 1.0", 1024, 768, 32, false);
379
380    if (ErrorMsg)
381    {
382        printf("Unable to open OpenGL window: %s\n", ErrorMsg);
383        return 0;
384    }
385
386
387    // Get the renderer with the highest preference number that is supported.
388    HMODULE RendererDLL;
389    MatSys::Renderer=(DesiredRendererName=="") ? PlatformAux::GetBestRenderer(RendererDLL) : PlatformAux::GetRenderer(DesiredRendererName, RendererDLL);
390
391    if (MatSys::Renderer==NULL || RendererDLL==NULL) { printf("No renderer loaded.\n"); SingleOpenGLWindow->Close(); return 0; }
392    MatSys::Renderer->Initialize();
393
394
395    // Get the TextureMapManager from the RendererDLL.
396    MatSys::TextureMapManagerI* TextureMapManager=PlatformAux::GetTextureMapManager(RendererDLL);
397
398    if (TextureMapManager==NULL) { printf("No TextureMapManager obtained.\n"); FreeLibrary(RendererDLL); SingleOpenGLWindow->Close(); return 0; }
399
400
401    // Register the material with the renderer.
402    MatSys::RenderMaterialT* RenderMaterial=MatSys::Renderer->RegisterMaterial(MyMaterial);
403
404
405    // Create a very simple lightmap for the materials that need one, and register it with the renderer.
406 // char Data[]={   0,   0,   0,   0,   0,   0, 0, 0,
407 //                 0,   0,   0,   0,   0,   0, 0, 0 };
408    char Data[]={  50,  50,  50,  50,  50,  50, 0, 0,
409                   50,  50,  50,  50,  50,  50, 0, 0 };
410 // char Data[]={ 255,   0, 255,   255,   0,   0,    0, 0,
411 //               255, 255,   0,     0,   0, 255,    0, 0 };
412
413    MatSys::TextureMapI* LightMap=TextureMapManager->GetTextureMap2D(Data, 2, 2, 3, true, MapCompositionT(MapCompositionT::Linear, MapCompositionT::Linear));
414
415    MatSys::Renderer->SetCurrentLightMap(LightMap);
416    MatSys::Renderer->SetCurrentLightDirMap(NULL);      // The MatSys provides a default for LightDirMaps when NULL is set.
417
418
419    // Create draw meshes.
420    ArrayT<MatSys::MeshT> Meshes;
421
422    // CreateMeshes needs to know the pixel dimensions of MyMaterial in order to create geometry in the proper aspect ratio.
423    CreateMeshes(Meshes, *MyMaterial);
424
425
426    // Also create a font (mostly for testing the FontT class).
427    // It is created with new rather than on the stack so that we can delete it before the MatSys goes down.
428    FontT* MyFont=new FontT("Fonts/Arial");
429
430
431    // Master loop.
432    TimerT                   Timer;
433    ArrayT<LightSourceInfoT> LightSources;
434    float                    ViewerOrigin[3]={ 0.0, 0.0, 0.0 };
435    float                    ViewerHeading=0.0;
436
437    LightSources.PushBackEmpty(4);
438
439    while (true)
440    {
441        static double TotalTime=0.0;
442        const  double DeltaTime=Timer.GetSecondsSinceLastCall();
443
444        TotalTime+=DeltaTime;
445
446        // Render the frame.
447        MatSys::Renderer->BeginFrame(TotalTime);
448        MatSys::Renderer->SetMatrix(MatSys::RendererI::WORLD_TO_VIEW, MatrixT::GetRotateXMatrix(-90.0f));      // Rotate coordinate system axes to Cafu standard.
449        MatSys::Renderer->RotateZ  (MatSys::RendererI::WORLD_TO_VIEW, ViewerHeading);
450        MatSys::Renderer->Translate(MatSys::RendererI::WORLD_TO_VIEW, -ViewerOrigin[0], -ViewerOrigin[1], -ViewerOrigin[2]);
451        {
452            MatSys::Renderer->SetCurrentRenderAction(MatSys::RendererI::AMBIENT);
453            MatSys::Renderer->SetCurrentMaterial(RenderMaterial);
454            MatSys::Renderer->SetCurrentEyePosition(ViewerOrigin[0], ViewerOrigin[1], ViewerOrigin[2]);     // Also required in some ambient shaders, e.g. for the A_SkyDome.
455
456            for (unsigned long MeshNr=0; MeshNr<Meshes.Size(); MeshNr++)
457                MatSys::Renderer->RenderMesh(Meshes[MeshNr]);
458
459            // Render something at the positions of the light sources.
460            for (unsigned long LightNr=0; LightNr<LightSources.Size(); LightNr++)
461            {
462                if (!LightSources[LightNr].IsOn) continue;
463
464                MatSys::Renderer->PushMatrix(MatSys::RendererI::MODEL_TO_WORLD);
465
466                MatSys::Renderer->Translate(MatSys::RendererI::MODEL_TO_WORLD, LightSources[LightNr].Pos[0], LightSources[LightNr].Pos[1], LightSources[LightNr].Pos[2]);
467                MatSys::Renderer->Scale    (MatSys::RendererI::MODEL_TO_WORLD, 0.1f);
468
469                // Simply re-use the six sides of the inner central cube.
470                MatSys::Renderer->RenderMesh(Meshes[0]);
471                MatSys::Renderer->RenderMesh(Meshes[1]);
472                MatSys::Renderer->RenderMesh(Meshes[2]);
473                MatSys::Renderer->RenderMesh(Meshes[3]);
474                MatSys::Renderer->RenderMesh(Meshes[4]);
475                MatSys::Renderer->RenderMesh(Meshes[5]);
476
477                MatSys::Renderer->PopMatrix(MatSys::RendererI::MODEL_TO_WORLD);
478            }
479
480
481            // Render the per-lightsource parts of the scene.
482            for (unsigned long LightNr=0; LightNr<LightSources.Size(); LightNr++)
483            {
484                if (!LightSources[LightNr].IsOn) continue;
485
486                MatSys::Renderer->SetCurrentLightSourcePosition(LightSources[LightNr].Pos[0], LightSources[LightNr].Pos[1], LightSources[LightNr].Pos[2]);
487                MatSys::Renderer->SetCurrentLightSourceRadius(LightSources[LightNr].Radius);
488                MatSys::Renderer->SetCurrentLightSourceDiffuseColor (LightSources[LightNr].DiffColor[0], LightSources[LightNr].DiffColor[1], LightSources[LightNr].DiffColor[2]);
489                MatSys::Renderer->SetCurrentLightSourceSpecularColor(LightSources[LightNr].SpecColor[0], LightSources[LightNr].SpecColor[1], LightSources[LightNr].SpecColor[2]);
490
491                if (!MyMaterial->NoShadows)
492                {
493                    MatSys::Renderer->SetCurrentRenderAction(MatSys::RendererI::STENCILSHADOW);
494                    DrawSilhouettes(MatSys::Renderer, Meshes);
495                }
496
497                MatSys::Renderer->SetCurrentRenderAction(MatSys::RendererI::LIGHTING);
498
499                for (unsigned long MeshNr=0; MeshNr<Meshes.Size(); MeshNr++)
500                    MatSys::Renderer->RenderMesh(Meshes[MeshNr]);
501            }
502
503
504            MyFont->Print(SingleOpenGLWindow->GetWidth()-100, SingleOpenGLWindow->GetHeight()-18, float(SingleOpenGLWindow->GetWidth()), float(SingleOpenGLWindow->GetHeight()), 0x00FFFFFF, cf::va("%5.1f FPS", 1.0/DeltaTime));
505        }
506        MatSys::Renderer->EndFrame();
507        SingleOpenGLWindow->SwapBuffers();
508
509
510        // Rufe die Nachrichten der Windows-Nachrichtenschlange ab.
511        if (SingleOpenGLWindow->HandleWindowMessages()) break;
512
513        CaKeyboardEventT KE;
514        CaMouseEventT    ME;
515        bool             QuitProgram=false;
516
517        while (SingleOpenGLWindow->GetNextKeyboardEvent(KE)>0)
518        {
519            if (KE.Type!=CaKeyboardEventT::CKE_KEYDOWN) continue;
520
521            if (KE.Key==CaKeyboardEventT::CK_ESCAPE) QuitProgram=true;
522
523            if (KE.Key>=CaKeyboardEventT::CK_1 && KE.Key<=CaKeyboardEventT::CK_0)
524            {
525                unsigned long Index=KE.Key-CaKeyboardEventT::CK_1;
526
527                if (LightSources.Size()>Index)
528                    LightSources[Index].IsOn=!LightSources[Index].IsOn;
529            }
530
531            if (KE.Key==CaKeyboardEventT::CK_TAB || KE.Key==CaKeyboardEventT::CK_U)
532            {
533                // Re-load the textures of the current material.
534                // Because the RenderMaterial was the *only* material that we registered with the MatSys,
535                // free'ing it is enough to get the reference count of its textures in the texture manager to 0.
536                // This in turn deletes the textures from the tex-mans texture repository, which in turn is
537                // forced to re-load the texture from disk when MyMaterial is re-registered.
538                MatSys::Renderer->FreeMaterial(RenderMaterial);
539                RenderMaterial=MatSys::Renderer->RegisterMaterial(MyMaterial);
540            }
541
542            // switch (KE.Key)
543            // {
544            //     case CaKeyboardEventT::CK_X: RotXOn=!RotXOn; break;
545            //     case CaKeyboardEventT::CK_Y: RotYOn=!RotYOn; break;
546            //     case CaKeyboardEventT::CK_Z: RotZOn=!RotZOn; break;
547            //     case CaKeyboardEventT::CK_K: DrawLightSource=!DrawLightSource; break;
548            //     case CaKeyboardEventT::CK_ADD     : Model->SetSequenceNr(++SequenceNr); break;
549            //     case CaKeyboardEventT::CK_SUBTRACT: Model->SetSequenceNr(--SequenceNr); break;
550            //     default: break;
551            // }
552        }
553
554        while (SingleOpenGLWindow->GetNextMouseEvent(ME)>0)
555        {
556            switch (ME.Type)
557            {
558                case CaMouseEventT::CM_MOVE_X:   // x-axis
559                    if (SingleOpenGLWindow->GetMouseButtonState() & 0x01) ViewerHeading+=int(ME.Amount)/2;
560                    // if (SingleOpenGLWindow->GetKeyboardState()[CaKeyboardEventT::CK_R]) RotZ+=int(ME.Amount);
561                    // if (SingleOpenGLWindow->GetKeyboardState()[CaKeyboardEventT::CK_L]) LightSourceAzimut+=0.5*int(ME.Amount);
562                    // if (SingleOpenGLWindow->GetKeyboardState()[CaKeyboardEventT::CK_M]) DynamicLightingInfo[3].x+=int(ME.Amount);
563                    break;
564
565                case CaMouseEventT::CM_MOVE_Y:   // y-axis
566                    // if (SingleOpenGLWindow->GetKeyboardState()[CaKeyboardEventT::CK_R      ]) RotX+=int(ME.Amount);
567                    // if (SingleOpenGLWindow->GetKeyboardState()[CaKeyboardEventT::CK_L      ]) { float& e=LightSourceElevation; e-=0.5*int(ME.Amount); if (e>90.0) e=90.0; if (e<-90.0) e=-90.0; }
568                    // if (SingleOpenGLWindow->GetKeyboardState()[CaKeyboardEventT::CK_M      ]) DynamicLightingInfo[3].y-=int(ME.Amount);
569                    // if (SingleOpenGLWindow->GetKeyboardState()[CaKeyboardEventT::CK_NUMPAD1]) { double& r=DynamicLightingInfo[1].x; r-=int(ME.Amount)/255.0/2.0; if (r>1) r=1; if (r<0) r=0; }
570                    // if (SingleOpenGLWindow->GetKeyboardState()[CaKeyboardEventT::CK_NUMPAD2]) { double& g=DynamicLightingInfo[1].y; g-=int(ME.Amount)/255.0/2.0; if (g>1) g=1; if (g<0) g=0; }
571                    // if (SingleOpenGLWindow->GetKeyboardState()[CaKeyboardEventT::CK_NUMPAD3]) { double& b=DynamicLightingInfo[1].z; b-=int(ME.Amount)/255.0/2.0; if (b>1) b=1; if (b<0) b=0; }
572                    // if (SingleOpenGLWindow->GetKeyboardState()[CaKeyboardEventT::CK_NUMPAD4]) { double& r=AmbientLightingInfo[1].x; r-=int(ME.Amount)/255.0/2.0; if (r>1) r=1; if (r<0) r=0; }
573                    // if (SingleOpenGLWindow->GetKeyboardState()[CaKeyboardEventT::CK_NUMPAD5]) { double& g=AmbientLightingInfo[1].y; g-=int(ME.Amount)/255.0/2.0; if (g>1) g=1; if (g<0) g=0; }
574                    // if (SingleOpenGLWindow->GetKeyboardState()[CaKeyboardEventT::CK_NUMPAD6]) { double& b=AmbientLightingInfo[1].z; b-=int(ME.Amount)/255.0/2.0; if (b>1) b=1; if (b<0) b=0; }
575                    // if (SingleOpenGLWindow->GetKeyboardState()[CaKeyboardEventT::CK_NUMPAD7]) { double& r=ClearColor.x; r-=int(ME.Amount)/255.0/2.0; if (r>1) r=1; if (r<0) r=0; glClearColor(ClearColor.x, ClearColor.y, ClearColor.z, 0.0); }
576                    // if (SingleOpenGLWindow->GetKeyboardState()[CaKeyboardEventT::CK_NUMPAD8]) { double& g=ClearColor.y; g-=int(ME.Amount)/255.0/2.0; if (g>1) g=1; if (g<0) g=0; glClearColor(ClearColor.x, ClearColor.y, ClearColor.z, 0.0); }
577                    // if (SingleOpenGLWindow->GetKeyboardState()[CaKeyboardEventT::CK_NUMPAD9]) { double& b=ClearColor.z; b-=int(ME.Amount)/255.0/2.0; if (b>1) b=1; if (b<0) b=0; glClearColor(ClearColor.x, ClearColor.y, ClearColor.z, 0.0); }
578                    break;
579
580                case CaMouseEventT::CM_MOVE_Z:   // z-axis (mouse wheel)
581                    // if (SingleOpenGLWindow->GetKeyboardState()[CaKeyboardEventT::CK_L]) LightSourceDistance-=0.5*int(ME.Amount);
582                    //                                                                else DynamicLightingInfo[3].z-=int(ME.Amount);
583                    break;
584
585                default:
586                    // Ignore all other ME types.
587                    break;
588            }
589        }
590
591        if (QuitProgram) break;
592
593
594        const float angle=ViewerHeading/180.0f*3.1415926f;
595
596        if (SingleOpenGLWindow->GetKeyboardState()[CaKeyboardEventT::CK_UP   ] || SingleOpenGLWindow->GetKeyboardState()[CaKeyboardEventT::CK_W]) { ViewerOrigin[0]+=sin(angle)*500.0f*float(DeltaTime); ViewerOrigin[1]+=cos(angle)*500.0f*float(DeltaTime); }
597        if (SingleOpenGLWindow->GetKeyboardState()[CaKeyboardEventT::CK_DOWN ] || SingleOpenGLWindow->GetKeyboardState()[CaKeyboardEventT::CK_S]) { ViewerOrigin[0]-=sin(angle)*500.0f*float(DeltaTime); ViewerOrigin[1]-=cos(angle)*500.0f*float(DeltaTime); }
598        if (SingleOpenGLWindow->GetKeyboardState()[CaKeyboardEventT::CK_LEFT ] || SingleOpenGLWindow->GetKeyboardState()[CaKeyboardEventT::CK_A]) { ViewerOrigin[0]-=cos(angle)*500.0f*float(DeltaTime); ViewerOrigin[1]+=sin(angle)*500.0f*float(DeltaTime); }
599        if (SingleOpenGLWindow->GetKeyboardState()[CaKeyboardEventT::CK_RIGHT] || SingleOpenGLWindow->GetKeyboardState()[CaKeyboardEventT::CK_D]) { ViewerOrigin[0]+=cos(angle)*500.0f*float(DeltaTime); ViewerOrigin[1]-=sin(angle)*500.0f*float(DeltaTime); }
600
601
602        // Move the light sources.
603        for (unsigned long LightNr=0; LightNr<LightSources.Size(); LightNr++)
604        {
605            if (!LightSources[LightNr].IsOn) continue;
606
607            LightSources[LightNr].Pos[0]=sin(float(TotalTime)*0.5f/(LightNr+1))*800.0f;
608            LightSources[LightNr].Pos[1]=cos(float(TotalTime)*0.5f/(LightNr+1))*800.0f;
609            LightSources[LightNr].Pos[2]=-400.0f+LightNr*400.0f;
610            LightSources[LightNr].Radius=5000.0;//XXXLightNr==2 ? 10000.0 : 1000.0;
611
612            switch (LightNr % 4)
613            {
614                case 0:
615                    LightSources[LightNr].DiffColor[0]=1.0; LightSources[LightNr].DiffColor[1]=1.0; LightSources[LightNr].DiffColor[2]=1.0;
616                    LightSources[LightNr].SpecColor[0]=1.0; LightSources[LightNr].SpecColor[1]=1.0; LightSources[LightNr].SpecColor[2]=1.0;
617                    break;
618
619                case 1:
620                    LightSources[LightNr].DiffColor[0]=1.0; LightSources[LightNr].DiffColor[1]=0.0; LightSources[LightNr].DiffColor[2]=0.0;
621                    LightSources[LightNr].SpecColor[0]=1.0; LightSources[LightNr].SpecColor[1]=1.0; LightSources[LightNr].SpecColor[2]=1.0;
622                    break;
623
624                case 2:
625                    LightSources[LightNr].DiffColor[0]=0.0; LightSources[LightNr].DiffColor[1]=1.0; LightSources[LightNr].DiffColor[2]=0.0;
626                    LightSources[LightNr].SpecColor[0]=1.0; LightSources[LightNr].SpecColor[1]=1.0; LightSources[LightNr].SpecColor[2]=1.0;
627                    break;
628
629                case 3:
630                    LightSources[LightNr].DiffColor[0]=0.0; LightSources[LightNr].DiffColor[1]=0.0; LightSources[LightNr].DiffColor[2]=1.0;
631                    LightSources[LightNr].SpecColor[0]=1.0; LightSources[LightNr].SpecColor[1]=1.0; LightSources[LightNr].SpecColor[2]=1.0;
632                    break;
633            }
634        }
635    }
636
637
638    // Clean-up.
639    delete MyFont;
640    MyFont=NULL;
641    MatSys::Renderer->FreeMaterial(RenderMaterial);
642    MatSys::Renderer->Release();
643    MatSys::Renderer=NULL;
644    FreeLibrary(RendererDLL);
645    SingleOpenGLWindow->Close();
646    return 0;
647}
Note: See TracBrowser for help on using the browser.