| 1 |
root |
1.1 |
#include <cmath> |
| 2 |
|
|
|
| 3 |
root |
1.45 |
#include "opengl.h" |
| 4 |
root |
1.26 |
|
| 5 |
root |
1.37 |
#include "view.h" |
| 6 |
root |
1.1 |
#include "oct.h" |
| 7 |
|
|
|
| 8 |
root |
1.55 |
using namespace gl; |
| 9 |
|
|
|
| 10 |
root |
1.68 |
pass pass_depth (0); |
| 11 |
|
|
|
| 12 |
root |
1.37 |
vector<GLuint> occ_query_objects; |
| 13 |
|
|
|
| 14 |
|
|
static GLuint begin_occ_query () |
| 15 |
|
|
{ |
| 16 |
|
|
GLuint id; |
| 17 |
|
|
|
| 18 |
|
|
if (occ_query_objects.size ()) |
| 19 |
|
|
{ |
| 20 |
|
|
id = *(occ_query_objects.end () - 1); |
| 21 |
|
|
occ_query_objects.pop_back (); |
| 22 |
|
|
} |
| 23 |
|
|
else |
| 24 |
root |
1.66 |
glGenQueries (1, &id); |
| 25 |
root |
1.37 |
|
| 26 |
root |
1.66 |
glBeginQuery (GL_SAMPLES_PASSED, id); |
| 27 |
root |
1.37 |
return id; |
| 28 |
|
|
} |
| 29 |
|
|
|
| 30 |
|
|
inline void end_occ_query () |
| 31 |
|
|
{ |
| 32 |
root |
1.66 |
glEndQuery (GL_SAMPLES_PASSED); |
| 33 |
root |
1.37 |
} |
| 34 |
|
|
|
| 35 |
|
|
static GLuint occ_query_result (GLuint id) |
| 36 |
|
|
{ |
| 37 |
|
|
GLuint count; |
| 38 |
|
|
|
| 39 |
root |
1.66 |
glGetQueryObjectuiv (id, GL_QUERY_RESULT, &count); |
| 40 |
root |
1.37 |
occ_query_objects.push_back (id); |
| 41 |
|
|
|
| 42 |
|
|
return count; |
| 43 |
|
|
} |
| 44 |
|
|
|
| 45 |
root |
1.17 |
view::view () |
| 46 |
root |
1.60 |
: gamma(1.0), nz_near (1.F), nz_far (2.F) |
| 47 |
root |
1.1 |
{ |
| 48 |
|
|
} |
| 49 |
|
|
|
| 50 |
root |
1.17 |
view::~view () |
| 51 |
root |
1.1 |
{ |
| 52 |
|
|
} |
| 53 |
|
|
|
| 54 |
root |
1.51 |
void view::begin_occ_query (recv_occ_query &recv, void *id) |
| 55 |
root |
1.37 |
{ |
| 56 |
root |
1.51 |
occ_queries.push_back (oq_data (&recv, ::begin_occ_query (), id)); |
| 57 |
root |
1.37 |
} |
| 58 |
|
|
|
| 59 |
|
|
void view::end_occ_query () |
| 60 |
|
|
{ |
| 61 |
|
|
::end_occ_query (); |
| 62 |
|
|
} |
| 63 |
|
|
|
| 64 |
root |
1.34 |
bool view::may_draw (const entity *e) |
| 65 |
root |
1.1 |
{ |
| 66 |
|
|
if (drawn.find (e) != drawn.end ()) |
| 67 |
|
|
return false; |
| 68 |
|
|
|
| 69 |
|
|
drawn.insert (e); |
| 70 |
|
|
return true; |
| 71 |
|
|
} |
| 72 |
|
|
|
| 73 |
root |
1.51 |
visibility_base *visible::get_visibility (view &ctx) |
| 74 |
|
|
{ |
| 75 |
|
|
view::visibility_map::iterator i = ctx.vismap.find (this); |
| 76 |
|
|
visibility_base *vs; |
| 77 |
|
|
|
| 78 |
|
|
if (i == ctx.vismap.end ()) |
| 79 |
|
|
{ |
| 80 |
|
|
vs = new_visibility (); |
| 81 |
|
|
ctx.vismap.insert (view::visibility_map::value_type (this, vs)); |
| 82 |
|
|
} |
| 83 |
|
|
else |
| 84 |
|
|
{ |
| 85 |
|
|
vs = i->second; |
| 86 |
|
|
|
| 87 |
|
|
if (vs->generation != ctx.generation) |
| 88 |
|
|
{ |
| 89 |
|
|
if (vs->generation + 1 != ctx.generation) |
| 90 |
|
|
clear_visibility (vs); |
| 91 |
|
|
|
| 92 |
|
|
vs->generation = ctx.generation; |
| 93 |
|
|
} |
| 94 |
|
|
} |
| 95 |
|
|
|
| 96 |
|
|
return vs; |
| 97 |
|
|
} |
| 98 |
|
|
|
| 99 |
root |
1.18 |
void view::reset_projection () |
| 100 |
root |
1.1 |
{ |
| 101 |
root |
1.42 |
renormalize (orig, p); |
| 102 |
root |
1.3 |
|
| 103 |
root |
1.1 |
glViewport (0, 0, w, h); |
| 104 |
|
|
|
| 105 |
|
|
glMatrixMode (GL_PROJECTION); |
| 106 |
|
|
glLoadIdentity (); |
| 107 |
|
|
|
| 108 |
|
|
GLdouble aspect = (GLdouble)w/h; |
| 109 |
root |
1.59 |
GLdouble ftan = tan (fov * (.5 * M_PI / 180.)); |
| 110 |
|
|
GLdouble ymax = z_near * ftan; |
| 111 |
|
|
GLdouble xmax = ymax * aspect; |
| 112 |
root |
1.1 |
|
| 113 |
root |
1.59 |
glFrustum (-xmax, xmax, -ymax, ymax, z_near, z_far); |
| 114 |
root |
1.1 |
|
| 115 |
root |
1.59 |
perspfact = z_near / ymax * 0.5F * h; |
| 116 |
root |
1.48 |
|
| 117 |
root |
1.6 |
d = normalize (d);//D |
| 118 |
|
|
u = normalize (u);//D |
| 119 |
root |
1.41 |
|
| 120 |
root |
1.1 |
vec3 rz = -d; |
| 121 |
|
|
vec3 rx = cross (u, rz); |
| 122 |
|
|
vec3 ry = cross (rz, rx); |
| 123 |
|
|
|
| 124 |
root |
1.17 |
matrix &m = projection; |
| 125 |
root |
1.16 |
m(0,0) = rx.x; m(0,1) = rx.y; m(0,2) = rx.z; m(0,3) = 0; |
| 126 |
|
|
m(1,0) = ry.x; m(1,1) = ry.y; m(1,2) = ry.z; m(1,3) = 0; |
| 127 |
|
|
m(2,0) = rz.x; m(2,1) = rz.y; m(2,2) = rz.z; m(2,3) = 0; |
| 128 |
root |
1.3 |
m(3,0) = 0; m(3,1) = 0; m(3,2) = 0; m(3,3) = 1; |
| 129 |
root |
1.37 |
|
| 130 |
root |
1.38 |
diagfact = abs (rz.x) + abs (rz.y) + abs (rz.z); |
| 131 |
root |
1.48 |
//printf ("diagfact = %f\n", diagfact); |
| 132 |
root |
1.41 |
diagfact = sqrtf (3.);//D WHY??? |
| 133 |
root |
1.3 |
|
| 134 |
root |
1.38 |
glMultMatrixf (m); |
| 135 |
root |
1.63 |
|
| 136 |
root |
1.38 |
glGetFloatv (GL_PROJECTION_MATRIX, m); |
| 137 |
root |
1.5 |
|
| 138 |
root |
1.17 |
frustum.l = plane ( m(3,0) + m(0,0), m(3,1) + m(0,1), m(3,2) + m(0,2), m(3,3) + m(0,3) ); |
| 139 |
|
|
frustum.r = plane ( m(3,0) - m(0,0), m(3,1) - m(0,1), m(3,2) - m(0,2), m(3,3) - m(0,3) ); |
| 140 |
|
|
frustum.b = plane ( m(3,0) + m(1,0), m(3,1) + m(1,1), m(3,2) + m(1,2), m(3,3) + m(1,3) ); |
| 141 |
|
|
frustum.t = plane ( m(3,0) - m(1,0), m(3,1) - m(1,1), m(3,2) - m(1,2), m(3,3) - m(1,3) ); |
| 142 |
|
|
frustum.n = plane ( m(3,0) + m(2,0), m(3,1) + m(2,1), m(3,2) + m(2,2), m(3,3) + m(2,3) ); |
| 143 |
|
|
frustum.f = plane ( m(3,0) - m(2,0), m(3,1) - m(2,1), m(3,2) - m(2,2), m(3,3) - m(2,3) ); |
| 144 |
root |
1.1 |
|
| 145 |
root |
1.59 |
{ |
| 146 |
|
|
GLdouble frustlen = z_far - z_near; |
| 147 |
|
|
GLdouble fheight = frustlen * ftan; |
| 148 |
|
|
GLdouble fwidth = fheight * w / h; |
| 149 |
|
|
point half (0, 0, z_near + frustlen * .5); |
| 150 |
|
|
point corner (fwidth, fheight, frustlen); |
| 151 |
|
|
|
| 152 |
|
|
frustum.s = sphere (p + d * (.5 * frustlen), length (corner - half)); |
| 153 |
|
|
} |
| 154 |
|
|
|
| 155 |
|
|
{ |
| 156 |
|
|
GLdouble depth = h / ftan; |
| 157 |
|
|
|
| 158 |
|
|
frustum.c = cone (p, d, atan (sqrt (GLdouble (w * w + h * h)) * ftan / h)); |
| 159 |
|
|
} |
| 160 |
|
|
|
| 161 |
root |
1.13 |
glMatrixMode (GL_MODELVIEW); |
| 162 |
root |
1.12 |
glLoadIdentity (); |
| 163 |
root |
1.67 |
glTranslatef (-p.x, -p.y, -p.z); |
| 164 |
root |
1.18 |
} |
| 165 |
|
|
|
| 166 |
|
|
void view::begin () |
| 167 |
|
|
{ |
| 168 |
root |
1.53 |
generation++; |
| 169 |
|
|
|
| 170 |
root |
1.19 |
vislist.clear (); |
| 171 |
root |
1.18 |
|
| 172 |
root |
1.60 |
z_near = max (nz_near, 1.F); |
| 173 |
|
|
z_far = max (nz_far, z_near * 2.F); |
| 174 |
root |
1.43 |
c_far = nc_far; |
| 175 |
root |
1.48 |
|
| 176 |
root |
1.43 |
reset_projection (); |
| 177 |
|
|
|
| 178 |
root |
1.65 |
nz_near = 100.; |
| 179 |
root |
1.60 |
nc_far = nz_far = 1.F; |
| 180 |
root |
1.18 |
} |
| 181 |
|
|
|
| 182 |
|
|
void view::end () |
| 183 |
|
|
{ |
| 184 |
root |
1.40 |
vislist.clear (); |
| 185 |
root |
1.18 |
} |
| 186 |
|
|
|
| 187 |
root |
1.50 |
#define DEPTH_OFFSET (1. / (GLdouble)(1L << 16)) |
| 188 |
|
|
|
| 189 |
root |
1.68 |
void view::render (enum pass_type p, pass &data) |
| 190 |
root |
1.18 |
{ |
| 191 |
root |
1.68 |
pass_type = p; |
| 192 |
|
|
pass_data = &data; |
| 193 |
root |
1.18 |
|
| 194 |
root |
1.68 |
switch (pass_type) |
| 195 |
root |
1.18 |
{ |
| 196 |
root |
1.52 |
case DEPTH: |
| 197 |
root |
1.63 |
glColorMask (1, 1, 1, 1); |
| 198 |
|
|
glDepthMask (1); |
| 199 |
|
|
|
| 200 |
|
|
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); |
| 201 |
|
|
|
| 202 |
|
|
//glEnable (GL_STENCIL_TEST); // for depth-passes |
| 203 |
|
|
//glStencilOp (GL_KEEP, GL_KEEP, GL_REPLACE); |
| 204 |
|
|
//glStencilFunc (GL_LESS, 1, 255); |
| 205 |
|
|
|
| 206 |
root |
1.52 |
//glEnable (GL_POLYGON_OFFSET_FILL); |
| 207 |
|
|
//glPolygonOffset (0, 5); |
| 208 |
|
|
glDisable (GL_MINMAX); |
| 209 |
|
|
glDepthRange (DEPTH_OFFSET, 1.); |
| 210 |
|
|
glDepthFunc (GL_LESS); |
| 211 |
|
|
glEnable (GL_DEPTH_TEST); |
| 212 |
|
|
glColorMask (0, 0, 0, 0); |
| 213 |
root |
1.59 |
glDisable (GL_DEPTH_CLAMP_NV); |
| 214 |
root |
1.54 |
|
| 215 |
root |
1.61 |
world.detect_visibility (*this); |
| 216 |
root |
1.54 |
|
| 217 |
root |
1.52 |
break; |
| 218 |
|
|
|
| 219 |
|
|
case POSTDEPTH: |
| 220 |
root |
1.59 |
glDepthRange (0., 1. - DEPTH_OFFSET); |
| 221 |
root |
1.57 |
glDepthFunc (GL_LESS); |
| 222 |
root |
1.52 |
glColorMask (0, 0, 0, 0); |
| 223 |
|
|
glDepthMask (0); |
| 224 |
root |
1.59 |
glEnable (GL_DEPTH_CLAMP_NV); |
| 225 |
root |
1.63 |
glDisable (GL_STENCIL_TEST); |
| 226 |
root |
1.53 |
|
| 227 |
|
|
// check occlusion queries |
| 228 |
|
|
for (vector<oq_data>::iterator i = occ_queries.begin (); i != occ_queries.end (); ++i) |
| 229 |
|
|
{ |
| 230 |
|
|
occ_query oq(*this, i->data, ::occ_query_result (i->id)); |
| 231 |
|
|
i->recv->event (oq); |
| 232 |
|
|
} |
| 233 |
|
|
|
| 234 |
|
|
occ_queries.clear (); |
| 235 |
|
|
|
| 236 |
root |
1.52 |
break; |
| 237 |
|
|
|
| 238 |
|
|
case LIGHTED: |
| 239 |
root |
1.63 |
glClear (GL_STENCIL_BUFFER_BIT); |
| 240 |
|
|
//glEnable (GL_STENCIL_TEST); |
| 241 |
root |
1.52 |
glEnable (GL_MINMAX); |
| 242 |
|
|
glDepthRange (0., 1. - DEPTH_OFFSET); |
| 243 |
|
|
glDepthFunc (GL_LESS); |
| 244 |
root |
1.53 |
glColorMask (1, 1, 1, 1); |
| 245 |
root |
1.52 |
glDepthMask (0); |
| 246 |
root |
1.59 |
glDisable (GL_DEPTH_CLAMP_NV); |
| 247 |
root |
1.54 |
|
| 248 |
root |
1.52 |
break; |
| 249 |
root |
1.18 |
} |
| 250 |
|
|
|
| 251 |
root |
1.61 |
for (vector<octant *>::iterator i = vislist.begin (); i != vislist.end (); ++i) |
| 252 |
|
|
(*i)->display (*this); |
| 253 |
root |
1.62 |
|
| 254 |
root |
1.68 |
if (pass_type == DEPTH) |
| 255 |
root |
1.62 |
printf ("fps %f NF %f:%f vis %d CAM (%d,%d,%d)\n", timer.fps, z_near, z_far, drawn.size (), orig.x, orig.y, orig.z);//D |
| 256 |
root |
1.61 |
|
| 257 |
root |
1.56 |
drawn.clear (); |
| 258 |
|
|
|
| 259 |
root |
1.52 |
#if 0 |
| 260 |
root |
1.56 |
if (pass == view::DEPTH) |
| 261 |
root |
1.25 |
{ |
| 262 |
root |
1.27 |
glEnable (GL_DEPTH_CLAMP_NV); |
| 263 |
root |
1.42 |
glDepthMask (0); |
| 264 |
root |
1.30 |
glDepthFunc (GL_LESS); |
| 265 |
root |
1.25 |
|
| 266 |
root |
1.37 |
for (vector<octant *>::iterator i = farlist.begin (); i != farlist.end (); ++i) |
| 267 |
|
|
{ |
| 268 |
root |
1.48 |
begin_occ_query (**i); |
| 269 |
root |
1.37 |
(*i)->draw_bbox (*this); |
| 270 |
root |
1.48 |
end_occ_query (); |
| 271 |
root |
1.30 |
} |
| 272 |
root |
1.27 |
|
| 273 |
|
|
glDisable (GL_DEPTH_CLAMP_NV); |
| 274 |
root |
1.25 |
} |
| 275 |
root |
1.52 |
#endif |
| 276 |
root |
1.1 |
} |
| 277 |
root |
1.6 |
|
| 278 |
root |
1.69 |
void light::enable () |
| 279 |
|
|
{ |
| 280 |
|
|
lightpos->set (p); |
| 281 |
|
|
} |
| 282 |
|
|
|
| 283 |
|
|
void light::disable () |
| 284 |
|
|
{ |
| 285 |
|
|
} |
| 286 |
|
|
|
| 287 |
|
|
static shader::varying_1f camdist; |
| 288 |
|
|
static shader::varying_3f lightvec; |
| 289 |
|
|
|
| 290 |
|
|
void linear_light::vsh () |
| 291 |
|
|
{ |
| 292 |
|
|
using namespace shader::compile; |
| 293 |
|
|
|
| 294 |
|
|
lightvec = xyz (lightpos - model_view_matrix * vin.vertex); |
| 295 |
|
|
camdist = max (1 - length (lightvec) / radius, 0); |
| 296 |
|
|
} |
| 297 |
|
|
|
| 298 |
|
|
shader::temp_3f linear_light::operator ()() |
| 299 |
|
|
{ |
| 300 |
|
|
using namespace shader::compile; |
| 301 |
|
|
|
| 302 |
|
|
temp_3f res; |
| 303 |
root |
1.70 |
res = float3 (c.r / 255.F, c.g / 255.F, c.b / 255.F) * intensity * camdist; |
| 304 |
root |
1.69 |
|
| 305 |
|
|
return res; |
| 306 |
|
|
} |
| 307 |
|
|
|
| 308 |
root |
1.1 |
|