lighting optimization

This commit is contained in:
David Rose 2008-11-14 00:23:40 +00:00
parent c6ec6b12be
commit d653bfc559
3 changed files with 167 additions and 116 deletions

View File

@ -12,3 +12,23 @@
//
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
// Function: TinyGraphicsStateGuardian::clear_light_state
// Access: Private
// Description: Removes the current list of active lights from the
// current state context.
////////////////////////////////////////////////////////////////////
INLINE void TinyGraphicsStateGuardian::
clear_light_state() {
_c->lighting_enabled = false;
#ifndef NDEBUG
GLLight *gl_light = _c->first_light;
while (gl_light != (GLLight *)NULL) {
GLLight *next = gl_light->next;
gl_light->next = NULL;
gl_light = next;
}
#endif // NDEBUG
_c->first_light = NULL;
}

View File

@ -438,6 +438,13 @@ end_scene() {
}
_c->zb = _current_frame_buffer;
}
// Clear the lighting state.
clear_light_state();
_plights.clear();
_dlights.clear();
_slights.clear();
GraphicsStateGuardian::end_scene();
}
@ -1571,15 +1578,7 @@ do_issue_light() {
}
// First, release all of the previously-assigned lights.
_c->lighting_enabled = false;
GLLight *gl_light = _c->first_light;
while (gl_light != (GLLight *)NULL) {
GLLight *next = gl_light->next;
gl_light->next = NULL;
gl_light = next;
}
_c->first_light = NULL;
clear_light_state();
// Now, assign new lights.
if (_target._light != (LightAttrib *)NULL) {
@ -1604,21 +1603,18 @@ do_issue_light() {
} else {
// Other kinds of lights each get their own GLLight object.
nassertv(num_enabled < MAX_LIGHTS);
GLLight *gl_light = &_c->lights[num_enabled];
memset(gl_light, 0, sizeof(GLLight));
gl_light->next = _c->first_light;
_c->first_light = gl_light;
light_obj->bind(this, light, num_enabled);
num_enabled++;
// Handle the diffuse color here, since all lights have this
// property.
GLLight *gl_light = _c->first_light;
nassertv(gl_light != NULL);
const Colorf &diffuse = light_obj->get_color();
gl_light->diffuse.v[0] = diffuse[0];
gl_light->diffuse.v[1] = diffuse[1];
gl_light->diffuse.v[2] = diffuse[2];
gl_light->diffuse.v[3] = diffuse[3];
light_obj->bind(this, light, num_enabled);
num_enabled++;
}
}
}
@ -1643,8 +1639,11 @@ do_issue_light() {
////////////////////////////////////////////////////////////////////
void TinyGraphicsStateGuardian::
bind_light(PointLight *light_obj, const NodePath &light, int light_id) {
GLLight *gl_light = _c->first_light;
nassertv(gl_light != (GLLight *)NULL);
pair<Lights::iterator, bool> lookup = _plights.insert(Lights::value_type(light, GLLight()));
GLLight *gl_light = &(*lookup.first).second;
if (lookup.second) {
// It's a brand new light. Define it.
memset(gl_light, 0, sizeof(GLLight));
const Colorf &specular = light_obj->get_specular_color();
gl_light->specular.v[0] = specular[0];
@ -1676,6 +1675,13 @@ bind_light(PointLight *light_obj, const NodePath &light, int light_id) {
gl_light->attenuation[0] = att[0];
gl_light->attenuation[1] = att[1];
gl_light->attenuation[2] = att[2];
}
nassertv(gl_light->next == NULL);
// Add it to the linked list of active lights.
gl_light->next = _c->first_light;
_c->first_light = gl_light;
}
////////////////////////////////////////////////////////////////////
@ -1688,8 +1694,11 @@ bind_light(PointLight *light_obj, const NodePath &light, int light_id) {
////////////////////////////////////////////////////////////////////
void TinyGraphicsStateGuardian::
bind_light(DirectionalLight *light_obj, const NodePath &light, int light_id) {
GLLight *gl_light = _c->first_light;
nassertv(gl_light != (GLLight *)NULL);
pair<Lights::iterator, bool> lookup = _dlights.insert(Lights::value_type(light, GLLight()));
GLLight *gl_light = &(*lookup.first).second;
if (lookup.second) {
// It's a brand new light. Define it.
memset(gl_light, 0, sizeof(GLLight));
const Colorf &specular = light_obj->get_specular_color();
gl_light->specular.v[0] = specular[0];
@ -1728,6 +1737,13 @@ bind_light(DirectionalLight *light_obj, const NodePath &light, int light_id) {
gl_light->attenuation[0] = 1.0f;
gl_light->attenuation[1] = 0.0f;
gl_light->attenuation[2] = 0.0f;
}
nassertv(gl_light->next == NULL);
// Add it to the linked list of active lights.
gl_light->next = _c->first_light;
_c->first_light = gl_light;
}
////////////////////////////////////////////////////////////////////
@ -1740,8 +1756,11 @@ bind_light(DirectionalLight *light_obj, const NodePath &light, int light_id) {
////////////////////////////////////////////////////////////////////
void TinyGraphicsStateGuardian::
bind_light(Spotlight *light_obj, const NodePath &light, int light_id) {
GLLight *gl_light = _c->first_light;
nassertv(gl_light != (GLLight *)NULL);
pair<Lights::iterator, bool> lookup = _plights.insert(Lights::value_type(light, GLLight()));
GLLight *gl_light = &(*lookup.first).second;
if (lookup.second) {
// It's a brand new light. Define it.
memset(gl_light, 0, sizeof(GLLight));
const Colorf &specular = light_obj->get_specular_color();
gl_light->specular.v[0] = specular[0];
@ -1786,6 +1805,13 @@ bind_light(Spotlight *light_obj, const NodePath &light, int light_id) {
gl_light->attenuation[0] = att[0];
gl_light->attenuation[1] = att[1];
gl_light->attenuation[2] = att[2];
}
nassertv(gl_light->next == NULL);
// Add it to the linked list of active lights.
gl_light->next = _c->first_light;
_c->first_light = gl_light;
}
////////////////////////////////////////////////////////////////////

View File

@ -22,12 +22,9 @@
#include "simpleLru.h"
#include "zmath.h"
#include "zbuffer.h"
#include "zgl.h"
class TinyTextureContext;
struct GLContext;
struct GLVertex;
struct GLMaterial;
struct GLTexture;
////////////////////////////////////////////////////////////////////
// Class : TinyGraphicsStateGuardian
@ -126,6 +123,8 @@ private:
static int get_color_blend_op(ColorBlendAttrib::Operand operand);
static ZB_lookupTextureFunc get_tex_filter_func(Texture::FilterType filter);
INLINE void clear_light_state();
public:
// Filled in by the Tiny*GraphicsWindow at begin_frame().
ZBuffer *_current_frame_buffer;
@ -150,6 +149,12 @@ private:
CPT(TransformState) _scissor_mat;
// Cache the data necessary to bind each particular light each
// frame, so if we bind a given light multiple times, we only have
// to compute its data once.
typedef pmap<NodePath, GLLight> Lights;
Lights _plights, _dlights, _slights;
// Used during being_draw_primitives() .. end_draw_primitives().
int _min_vertex;
int _max_vertex;