]> Dogcows Code - chaz/yoink/blob - src/Moof/Scene.hh
refactoring the scene class
[chaz/yoink] / src / Moof / Scene.hh
1
2 /*******************************************************************************
3
4 Copyright (c) 2009, Charles McGarvey
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
10 * Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27 *******************************************************************************/
28
29 #ifndef _MOOF_SCENE_HH_
30 #define _MOOF_SCENE_HH_
31
32 #include <string>
33
34 #include <boost/shared_ptr.hpp>
35
36 #include <Moof/Drawable.hh>
37 #include <Moof/Entity.hh>
38 #include <Moof/Octree.hh>
39 #include <Moof/Resource.hh>
40 #include <Moof/Tilemap.hh>
41
42
43 namespace Mf {
44
45
46 OctreeP loadScene(const std::string& name);
47
48
49 class Quad : public Entity
50 {
51 Scalar vertices_[12];
52 Scalar texCoords_[8];
53
54 Tilemap tilemap_;
55
56 long detail_;
57 bool blending_;
58 bool fog_;
59
60 public:
61
62 Quad(const Vector3 vertices[4], const std::string& texture,
63 Tilemap::Index tileIndex) :
64 tilemap_(texture),
65 detail_(0),
66 blending_(false),
67 fog_(false)
68 {
69 for (int i = 0, num = 0; i < 4; ++i)
70 {
71 for (int j = 0; j < 3; ++j, ++num)
72 {
73 vertices_[num] = vertices[i][j];
74 }
75 }
76
77 if (!tilemap_.getTileCoords(tileIndex, texCoords_))
78 {
79 logWarning("no index %d in texture %s", tileIndex,
80 texture.c_str());
81
82 texCoords_[0] = texCoords_[1] =
83 texCoords_[3] = texCoords_[6] = 0.0;
84 texCoords_[2] = texCoords_[4] =
85 texCoords_[5] = texCoords_[7] = 1.0;
86 }
87
88 aabb_.encloseVertices(vertices, 4);
89 sphere_.point = aabb_.getCenter();
90 sphere_.radius = (aabb_.min - sphere_.point).length();
91 }
92
93 void setDetail(long detail)
94 {
95 detail_ = detail;
96 }
97
98 void setBlending(bool blending)
99 {
100 blending_ = blending;
101 }
102
103 void setFog(bool fog)
104 {
105 fog_ = fog;
106 }
107
108 void draw(Scalar alpha = 0.0) const
109 {
110 if (blending_)
111 {
112 glEnable(GL_BLEND);
113 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
114 }
115
116 if (fog_)
117 {
118 glEnable(GL_FOG);
119 glFogi(GL_FOG_MODE, GL_LINEAR);
120 }
121
122 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
123 tilemap_.bind();
124
125 glVertexPointer(3, GL_SCALAR, 0, vertices_);
126 glTexCoordPointer(2, GL_SCALAR, 0, texCoords_);
127
128 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
129
130 glDisable(GL_BLEND);
131 glDisable(GL_FOG);
132 }
133
134 bool isVisible(const Frustum& frustum) const
135 {
136 return sphere_.isVisible(frustum);
137 }
138 };
139
140
141 } // namespace Mf
142
143 #endif // _MOOF_SCENE_HH_
144
145 /** vim: set ts=4 sw=4 tw=80: *************************************************/
146
This page took 0.035057 seconds and 4 git commands to generate.