OculusRoomModel.cpp
Go to the documentation of this file.
00001 /************************************************************************************
00002 
00003 Filename    :   OculusRoomModel.cpp
00004 Content     :   Creates a simple room scene from hard-coded geometry
00005 Created     :   October 4, 2012
00006 
00007 Copyright   :   Copyright 2012-2013 Oculus, Inc. All Rights reserved.
00008 
00009 Licensed under the Apache License, Version 2.0 (the "License");
00010 you may not use this file except in compliance with the License.
00011 You may obtain a copy of the License at
00012 
00013 http://www.apache.org/licenses/LICENSE-2.0
00014 
00015 Unless required by applicable law or agreed to in writing, software
00016 distributed under the License is distributed on an "AS IS" BASIS,
00017 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00018 See the License for the specific language governing permissions and
00019 limitations under the License.
00020 
00021 *************************************************************************************/
00022 
00023 #include "RenderTiny_Device.h"
00024 
00025 using namespace OVR;
00026 using namespace OVR::RenderTiny;
00027 
00028 
00029 //-------------------------------------------------------------------------------------
00030 // ***** Room Model
00031 
00032 // This model is hard-coded out of axis-aligned solid-colored slabs.
00033 // Room unit dimensions are in meters. Player starts in the middle.
00034 //
00035 
00036 enum BuiltinTexture
00037 {
00038     Tex_None,
00039     Tex_Checker,
00040     Tex_Block,
00041     Tex_Panel,
00042     Tex_Count
00043 };
00044 
00045 struct Slab
00046 {
00047     float x1, y1, z1;
00048     float x2, y2, z2;
00049     Color c;
00050 };
00051 
00052 struct SlabModel
00053 {
00054     int   Count;
00055     Slab* pSlabs;
00056     BuiltinTexture tex;
00057 };
00058 
00059 Slab FloorSlabs[] =
00060 {
00061     // Floor
00062     { -10.0f,  -0.1f,  -20.0f,  10.0f,  0.0f, 20.1f,  Color(128,128,128) }
00063 };
00064 
00065 SlabModel Floor = {sizeof(FloorSlabs)/sizeof(Slab), FloorSlabs, Tex_Checker};
00066 
00067 Slab CeilingSlabs[] =
00068 {
00069     { -10.0f,  4.0f,  -20.0f,  10.0f,  4.1f, 20.1f,  Color(128,128,128) }
00070 };
00071 
00072 SlabModel Ceiling = {sizeof(FloorSlabs)/sizeof(Slab), CeilingSlabs, Tex_Panel};
00073 
00074 Slab RoomSlabs[] =
00075 {
00076     // Left Wall
00077     { -10.1f,   0.0f,  -20.0f, -10.0f,  4.0f, 20.0f,  Color(128,128,128) },
00078     // Back Wall
00079     { -10.0f,  -0.1f,  -20.1f,  10.0f,  4.0f, -20.0f, Color(128,128,128) },
00080 
00081     // Right Wall
00082     {  10.0f,  -0.1f,  -20.0f,  10.1f,  4.0f, 20.0f,  Color(128,128,128) },
00083 };
00084 
00085 SlabModel Room = {sizeof(RoomSlabs)/sizeof(Slab), RoomSlabs, Tex_Block};
00086 
00087 Slab FixtureSlabs[] =
00088 {
00089     // Right side shelf
00090     {   9.5f,   0.75f,  3.0f,  10.1f,  2.5f,   3.1f,  Color(128,128,128) }, // Verticals
00091     {   9.5f,   0.95f,  3.7f,  10.1f,  2.75f,  3.8f,  Color(128,128,128) },
00092     {   9.5f,   1.20f,  2.5f,  10.1f,  1.30f,  3.8f,  Color(128,128,128) }, // Horizontals
00093     {   9.5f,   2.00f,  3.0f,  10.1f,  2.10f,  4.2f,  Color(128,128,128) },
00094 
00095     // Right railing    
00096     {   5.0f,   1.1f,   20.0f,  10.0f,  1.2f,  20.1f, Color(128,128,128) },
00097     // Bars
00098     {   9.0f,   1.1f,   20.0f,   9.1f,  0.0f,  20.1f, Color(128,128,128) },
00099     {   8.0f,   1.1f,   20.0f,   8.1f,  0.0f,  20.1f, Color(128,128,128) },
00100     {   7.0f,   1.1f,   20.0f,   7.1f,  0.0f,  20.1f, Color(128,128,128) },
00101     {   6.0f,   1.1f,   20.0f,   6.1f,  0.0f,  20.1f, Color(128,128,128) },
00102     {   5.0f,   1.1f,   20.0f,   5.1f,  0.0f,  20.1f, Color(128,128,128) },
00103 
00104     // Left railing    
00105     {  -10.0f,   1.1f, 20.0f,   -5.0f,   1.2f, 20.1f, Color(128,128,128) },
00106     // Bars
00107     {  -9.0f,   1.1f,   20.0f,  -9.1f,  0.0f,  20.1f, Color(128,128,128) },
00108     {  -8.0f,   1.1f,   20.0f,  -8.1f,  0.0f,  20.1f, Color(128,128,128) },
00109     {  -7.0f,   1.1f,   20.0f,  -7.1f,  0.0f,  20.1f, Color(128,128,128) },
00110     {  -6.0f,   1.1f,   20.0f,  -6.1f,  0.0f,  20.1f, Color(128,128,128) },
00111     {  -5.0f,   1.1f,   20.0f,  -5.1f,  0.0f,  20.1f, Color(128,128,128) },
00112 
00113     // Bottom Floor 2
00114     { -15.0f,  -6.1f,   18.0f,  15.0f, -6.0f, 30.0f,  Color(128,128,128) },
00115 };
00116 
00117 SlabModel Fixtures = {sizeof(FixtureSlabs)/sizeof(Slab), FixtureSlabs};
00118 
00119 Slab FurnitureSlabs[] =
00120 {
00121     // Table
00122     {  -1.8f, 0.7f, 1.0f,  0.0f,      0.8f, 0.0f,      Color(128,128,88) },
00123     {  -1.8f, 0.7f, 0.0f, -1.8f+0.1f, 0.0f, 0.0f+0.1f, Color(128,128,88) }, // Leg 1
00124     {  -1.8f, 0.7f, 1.0f, -1.8f+0.1f, 0.0f, 1.0f-0.1f, Color(128,128,88) }, // Leg 2
00125     {   0.0f, 0.7f, 1.0f,  0.0f-0.1f, 0.0f, 1.0f-0.1f, Color(128,128,88) }, // Leg 2
00126     {   0.0f, 0.7f, 0.0f,  0.0f-0.1f, 0.0f, 0.0f+0.1f, Color(128,128,88) }, // Leg 2
00127 
00128     // Chair
00129     {  -1.4f, 0.5f, -1.1f, -0.8f,       0.55f, -0.5f,       Color(88,88,128) }, // Set
00130     {  -1.4f, 1.0f, -1.1f, -1.4f+0.06f, 0.0f,  -1.1f+0.06f, Color(88,88,128) }, // Leg 1
00131     {  -1.4f, 0.5f, -0.5f, -1.4f+0.06f, 0.0f,  -0.5f-0.06f, Color(88,88,128) }, // Leg 2
00132     {  -0.8f, 0.5f, -0.5f, -0.8f-0.06f, 0.0f,  -0.5f-0.06f, Color(88,88,128) }, // Leg 2
00133     {  -0.8f, 1.0f, -1.1f, -0.8f-0.06f, 0.0f,  -1.1f+0.06f, Color(88,88,128) }, // Leg 2
00134     {  -1.4f, 0.97f,-1.05f,-0.8f,       0.92f, -1.10f,      Color(88,88,128) }, // Back high bar
00135 };
00136 
00137 SlabModel Furniture = {sizeof(FurnitureSlabs)/sizeof(Slab), FurnitureSlabs};
00138 
00139 Slab PostsSlabs[] = 
00140 {
00141     // Posts
00142     {  0,  0.0f, 0.0f,   0.1f, 1.3f, 0.1f, Color(128,128,128) },
00143     {  0,  0.0f, 0.4f,   0.1f, 1.3f, 0.5f, Color(128,128,128) },
00144     {  0,  0.0f, 0.8f,   0.1f, 1.3f, 0.9f, Color(128,128,128) },
00145     {  0,  0.0f, 1.2f,   0.1f, 1.3f, 1.3f, Color(128,128,128) },
00146     {  0,  0.0f, 1.6f,   0.1f, 1.3f, 1.7f, Color(128,128,128) },
00147     {  0,  0.0f, 2.0f,   0.1f, 1.3f, 2.1f, Color(128,128,128) },
00148     {  0,  0.0f, 2.4f,   0.1f, 1.3f, 2.5f, Color(128,128,128) },
00149     {  0,  0.0f, 2.8f,   0.1f, 1.3f, 2.9f, Color(128,128,128) },
00150     {  0,  0.0f, 3.2f,   0.1f, 1.3f, 3.3f, Color(128,128,128) },
00151     {  0,  0.0f, 3.6f,   0.1f, 1.3f, 3.7f, Color(128,128,128) },
00152 };
00153 
00154 SlabModel Posts = {sizeof(PostsSlabs)/sizeof(Slab), PostsSlabs};
00155 
00156 
00157 // Temporary helper class used to initialize fills used by model. 
00158 class FillCollection
00159 {
00160 public:
00161     Ptr<ShaderFill> LitSolid;
00162     Ptr<ShaderFill> LitTextures[4];
00163 
00164     FillCollection(RenderDevice* render);
00165   
00166 };
00167 
00168 FillCollection::FillCollection(RenderDevice* render)
00169 {
00170     Ptr<Texture> builtinTextures[Tex_Count];
00171 
00172     // Create floor checkerboard texture.
00173     {
00174         Color checker[256*256];
00175         for (int j = 0; j < 256; j++)
00176             for (int i = 0; i < 256; i++)
00177                 checker[j*256+i] = (((i/4 >> 5) ^ (j/4 >> 5)) & 1) ?
00178                 Color(180,180,180,255) : Color(80,80,80,255);
00179         builtinTextures[Tex_Checker] = *render->CreateTexture(Texture_RGBA|Texture_GenMipmaps, 256, 256, checker);
00180         builtinTextures[Tex_Checker]->SetSampleMode(Sample_Anisotropic|Sample_Repeat);
00181     }
00182 
00183     // Ceiling panel texture.
00184     {
00185         Color panel[256*256];
00186         for (int j = 0; j < 256; j++)
00187             for (int i = 0; i < 256; i++)
00188                 panel[j*256+i] = (i/4 == 0 || j/4 == 0) ?
00189                 Color(80,80,80,255) : Color(180,180,180,255);
00190         builtinTextures[Tex_Panel] = *render->CreateTexture(Texture_RGBA|Texture_GenMipmaps, 256, 256, panel);
00191         builtinTextures[Tex_Panel]->SetSampleMode(Sample_Anisotropic|Sample_Repeat);
00192     }
00193 
00194     // Wall brick textures.
00195     {
00196         Color block[256*256];
00197         for (int j = 0; j < 256; j++)
00198             for (int i = 0; i < 256; i++)
00199                 block[j*256+i] = (((j/4 & 15) == 0) || (((i/4 & 15) == 0) && ((((i/4 & 31) == 0) ^ ((j/4 >> 4) & 1)) == 0))) ?
00200                 Color(60,60,60,255) : Color(180,180,180,255);
00201         builtinTextures[Tex_Block] = *render->CreateTexture(Texture_RGBA|Texture_GenMipmaps, 256, 256, block);
00202         builtinTextures[Tex_Block]->SetSampleMode(Sample_Anisotropic|Sample_Repeat);
00203     }
00204 
00205     LitSolid = *new ShaderFill(*render->CreateShaderSet());
00206     LitSolid->GetShaders()->SetShader(render->LoadBuiltinShader(Shader_Vertex, VShader_MVP)); 
00207     LitSolid->GetShaders()->SetShader(render->LoadBuiltinShader(Shader_Fragment, FShader_LitGouraud)); 
00208 
00209     for (int i = 1; i < Tex_Count; i++)
00210     {
00211         LitTextures[i] = *new ShaderFill(*render->CreateShaderSet());
00212         LitTextures[i]->GetShaders()->SetShader(render->LoadBuiltinShader(Shader_Vertex, VShader_MVP)); 
00213         LitTextures[i]->GetShaders()->SetShader(render->LoadBuiltinShader(Shader_Fragment, FShader_LitTexture)); 
00214         LitTextures[i]->SetTexture(0, builtinTextures[i]);
00215     }
00216 
00217 }
00218 
00219 
00220 
00221 // Helper function to create a model out of Slab arrays.
00222 Model* CreateModel(Vector3f pos, SlabModel* sm, const FillCollection& fills)
00223 {
00224     Model* m = new Model(Prim_Triangles);
00225     m->SetPosition(pos);
00226 
00227     for(int i=0; i< sm->Count; i++)
00228     {
00229         Slab &s = sm->pSlabs[i];
00230         m->AddSolidColorBox(s.x1, s.y1, s.z1, s.x2, s.y2, s.z2, s.c);
00231     }
00232 
00233     if (sm->tex > 0)
00234         m->Fill = fills.LitTextures[sm->tex];
00235     else
00236         m->Fill = fills.LitSolid;
00237     return m;
00238 }
00239 
00240 
00241 // Adds sample models and lights to the argument scene.
00242 void PopulateRoomScene(Scene* scene, RenderDevice* render)
00243 {
00244     FillCollection fills(render);  
00245 
00246     scene->World.Add(Ptr<Model>(*CreateModel(Vector3f(0,0,0),  &Room,       fills)));
00247     scene->World.Add(Ptr<Model>(*CreateModel(Vector3f(0,0,0),  &Floor,      fills)));
00248     scene->World.Add(Ptr<Model>(*CreateModel(Vector3f(0,0,0),  &Ceiling,    fills)));
00249     scene->World.Add(Ptr<Model>(*CreateModel(Vector3f(0,0,0),  &Fixtures,   fills)));
00250     scene->World.Add(Ptr<Model>(*CreateModel(Vector3f(0,0,0),  &Furniture,  fills)));
00251     scene->World.Add(Ptr<Model>(*CreateModel(Vector3f(0,0,4),  &Furniture,  fills)));
00252     scene->World.Add(Ptr<Model>(*CreateModel(Vector3f(-3,0,3), &Posts,      fills)));
00253   
00254 
00255     scene->SetAmbient(Vector4f(0.65f,0.65f,0.65f,1));
00256     scene->AddLight(Vector3f(-2,4,-2), Vector4f(8,8,8,1));
00257     scene->AddLight(Vector3f(3,4,-3),  Vector4f(2,1,1,1));
00258     scene->AddLight(Vector3f(-4,3,25), Vector4f(3,6,3,1));
00259 }
00260 


oculus_sdk
Author(s):
autogenerated on Fri Aug 28 2015 11:53:10