Browse Source

fixes for dx8 cube maps

David Rose 20 years ago
parent
commit
828307a431

+ 10 - 11
panda/src/display/graphicsOutput.cxx

@@ -39,21 +39,20 @@ PStatCollector GraphicsOutput::_make_current_pcollector("Draw:Make current");
 PStatCollector GraphicsOutput::_copy_texture_pcollector("Draw:Copy texture");
 
 struct CubeFaceDef {
-  CubeFaceDef(const char *name, const LPoint3f &look_at, const LVector3f &up) :
-    _name(name), _look_at(look_at), _up(up) { }
+  CubeFaceDef(const char *name, float h, float p, float r) :
+    _name(name), _hpr(h, p, r) { }
 
   const char *_name;
-  LPoint3f _look_at;
-  LVector3f _up;
+  LVecBase3f _hpr;
 };
 
 static CubeFaceDef cube_faces[6] = {
-  CubeFaceDef("positive_x", LPoint3f(1, 0, 0), LVector3f(0, -1, 0)),
-  CubeFaceDef("negative_x", LPoint3f(-1, 0, 0), LVector3f(0, -1, 0)),
-  CubeFaceDef("positive_y", LPoint3f(0, 1, 0), LVector3f(0, 0, 1)),
-  CubeFaceDef("negative_y", LPoint3f(0, -1, 0), LVector3f(0, 0, -1)),
-  CubeFaceDef("positive_z", LPoint3f(0, 0, 1), LVector3f(0, -1, 0)),
-  CubeFaceDef("negative_z", LPoint3f(0, 0, -1), LVector3f(0, -1, 0))
+  CubeFaceDef("positive_x", -90, 0, -180),
+  CubeFaceDef("negative_x", 90, 0, -180),
+  CubeFaceDef("positive_y", 0, 90, 0),
+  CubeFaceDef("negative_y", 0, -90, 0),
+  CubeFaceDef("positive_z", 180, 0, -180),
+  CubeFaceDef("negative_z", 0, 0, -180),
 };
   
 ////////////////////////////////////////////////////////////////////
@@ -656,7 +655,7 @@ make_cube_map(const string &name, int size, bool to_ram,
     camera->set_lens(lens);
     camera->set_camera_mask(camera_mask);
     NodePath camera_np = camera_rig.attach_new_node(camera);
-    camera_np.look_at(cube_faces[i]._look_at, cube_faces[i]._up);
+    camera_np.set_hpr(cube_faces[i]._hpr);
     
     DisplayRegion *dr = buffer->make_display_region();
     dr->set_cube_map_index(i);

+ 30 - 7
panda/src/dxgsg8/dxGraphicsStateGuardian8.cxx

@@ -2242,6 +2242,21 @@ do_issue_texture() {
     TexGenAttrib::Mode mode = _current_tex_gen->get_mode(stage);
     bool any_point_sprite = false;
 
+    // These transforms are used in the below to invert certain
+    // computed component values in various modes, to emulate the
+    // behavior of OpenGL, so we get a consistent behavior between the
+    // two of them.
+    static CPT(TransformState) invert_z =
+      TransformState::make_mat(LMatrix4f(1.0f, 0.0f, 0.0f, 0.0f,
+                                         0.0f, 1.0f, 0.0f, 0.0f,
+                                         0.0f, 0.0f, -1.0f, 0.0f,
+                                         0.0f, 0.0f, 0.0f, 1.0f));
+    static CPT(TransformState) invert_y =
+      TransformState::make_mat(LMatrix4f(1.0f, 0.0f, 0.0f, 0.0f,
+                                         0.0f, -1.0f, 0.0f, 0.0f,
+                                         0.0f, 0.0f, 1.0f, 0.0f,
+                                         0.0f, 0.0f, 0.0f, 1.0f));
+
     switch (mode) {
     case TexGenAttrib::M_off:
     case TexGenAttrib::M_light_vector:
@@ -2275,14 +2290,16 @@ do_issue_texture() {
         _d3d_device->SetTextureStageState(i, D3DTSS_TEXCOORDINDEX, 
                                           texcoord_index | D3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR);
         texcoords_3d = true;
-        CPT(TransformState) camera_transform = _scene_setup->get_camera_transform()->compose(_inv_cs_transform);
-        tex_mat = tex_mat->compose(camera_transform->set_pos(LVecBase3f::zero()));
+        CPT(TransformState) camera_transform = _cs_transform->compose(_scene_setup->get_camera_transform())->compose(_inv_cs_transform);
+        CPT(TransformState) rotate_transform = invert_z->compose(camera_transform);
+        tex_mat = tex_mat->compose(rotate_transform->set_pos(LVecBase3f::zero()));
       }
       break;
 
     case TexGenAttrib::M_eye_cube_map:
       _d3d_device->SetTextureStageState(i, D3DTSS_TEXCOORDINDEX, 
                                         texcoord_index | D3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR);
+      tex_mat = tex_mat->compose(invert_z);
       texcoords_3d = true;
       break;
 
@@ -2295,14 +2312,16 @@ do_issue_texture() {
         _d3d_device->SetTextureStageState(i, D3DTSS_TEXCOORDINDEX, 
                                           texcoord_index | D3DTSS_TCI_CAMERASPACENORMAL);
         texcoords_3d = true;
-        CPT(TransformState) camera_transform = _scene_setup->get_camera_transform()->compose(_inv_cs_transform);
-        tex_mat = tex_mat->compose(camera_transform->set_pos(LVecBase3f::zero()));
+        CPT(TransformState) camera_transform = _cs_transform->compose(_scene_setup->get_camera_transform())->compose(_inv_cs_transform);
+        CPT(TransformState) rotate_transform = invert_z->compose(camera_transform);
+        tex_mat = tex_mat->compose(rotate_transform->set_pos(LVecBase3f::zero()));
       }
       break;
 
     case TexGenAttrib::M_eye_normal:
       _d3d_device->SetTextureStageState(i, D3DTSS_TEXCOORDINDEX, 
                                         texcoord_index | D3DTSS_TCI_CAMERASPACENORMAL);
+      tex_mat = tex_mat->compose(invert_z);
       texcoords_3d = true;
       break;
 
@@ -2336,17 +2355,21 @@ do_issue_texture() {
 
     if (!tex_mat->is_identity()) {
       LMatrix4f m = tex_mat->get_mat();
+      _d3d_device->SetTransform(get_tex_mat_sym(i), (D3DMATRIX *)m.get_data());
+
       if (!texcoords_3d) {
         // For 2-d texture coordinates, we have to reorder the matrix.
         m.set(m(0, 0), m(0, 1), m(0, 3), 0.0f,
               m(1, 0), m(1, 1), m(1, 3), 0.0f,
               m(3, 0), m(3, 1), m(3, 3), 0.0f,
               0.0f, 0.0f, 0.0f, 1.0f);
+        _d3d_device->SetTextureStageState(i, D3DTSS_TEXTURETRANSFORMFLAGS,
+                                          D3DTTFF_COUNT2);
+      } else {
+        _d3d_device->SetTextureStageState(i, D3DTSS_TEXTURETRANSFORMFLAGS,
+                                          D3DTTFF_COUNT3);
       }
 
-      _d3d_device->SetTransform(get_tex_mat_sym(i), (D3DMATRIX *)m.get_data());
-      _d3d_device->SetTextureStageState(i, D3DTSS_TEXTURETRANSFORMFLAGS,
-                                        D3DTTFF_COUNT2);
     } else {
       _d3d_device->SetTextureStageState(i, D3DTSS_TEXTURETRANSFORMFLAGS,
                                         D3DTTFF_DISABLE);

+ 2 - 4
panda/src/glstuff/glGraphicsStateGuardian_src.cxx

@@ -4250,10 +4250,8 @@ finish_modify_state() {
             // tempting to try, we can't safely convert to object
             // space, since this method doesn't get called with each
             // different object.
-            CPT(TransformState) transform = 
-              _cs_transform->compose(_scene_setup->get_world_transform());
-            transform = transform->invert_compose(TransformState::make_identity());
-            LMatrix4f mat = transform->get_mat();
+            CPT(TransformState) camera_transform = _cs_transform->compose(_scene_setup->get_camera_transform())->compose(_inv_cs_transform);
+            LMatrix4f mat = camera_transform->get_mat();
             mat.set_row(3, LVecBase3f(0.0f, 0.0f, 0.0f));
             GLP(MatrixMode)(GL_TEXTURE);
             GLP(MultMatrixf)(mat.get_data());