| 1 | /* |
| 2 | * Descent 3 |
| 3 | * Copyright (C) 2024 Parallax Software |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | --- HISTORICAL COMMENTS FOLLOW --- |
| 19 | |
| 20 | * $Source: $ |
| 21 | * $Revision: 8 $ |
| 22 | * $Author: Samir $ |
| 23 | * $Date: 8/13/99 2:00p $ |
| 24 | * |
| 25 | * |
| 26 | * |
| 27 | * $Log: /DescentIII/Main/sndlib/sndrender.cpp $ |
| 28 | * |
| 29 | * 8 8/13/99 2:00p Samir |
| 30 | * more aureal and geometry fixes. |
| 31 | * |
| 32 | * 7 8/11/99 3:12p Samir |
| 33 | * fixes for aureal support. |
| 34 | * |
| 35 | * 6 5/13/99 3:49p Ardussi |
| 36 | * changes for compiling on the Mac |
| 37 | * |
| 38 | * 5 4/16/99 3:33a Jeff |
| 39 | * ifdefs added to remove windows specific code |
| 40 | * |
| 41 | * 4 4/14/99 10:46a Kevin |
| 42 | * Removed OutrageMessageBox from release builds |
| 43 | * |
| 44 | * 3 4/06/99 8:30p Samir |
| 45 | * added reflection support. |
| 46 | * |
| 47 | * 2 4/01/99 4:28p Samir |
| 48 | * hardware geometry integration if it's available. |
| 49 | * |
| 50 | */ |
| 51 | |
| 52 | #include <cstring> |
| 53 | |
| 54 | #include "hlsoundlib.h" |
| 55 | #include "ddsndgeometry.h" |
| 56 | #include "sndrender.h" |
| 57 | #include "room.h" |
| 58 | #include "object.h" |
| 59 | #include "application.h" |
| 60 | #include "soundload.h" |
| 61 | #include "gametexture.h" |
| 62 | |
| 63 | |
| 64 | static llsGeometry *Geometry = NULL; |
| 65 | static int16_t Sound_room_list[SOUND_RENDER_ROOM_LIMIT + 1]; |
| 66 | |
| 67 | // resets sound geometry system (called beginning of each level) |
| 68 | void SoundRenderReset() { Sound_room_list[0] = -1; } |
| 69 | |
| 70 | // if this fails, renderer won't work, do emulation. |
| 71 | bool sound_render_start_frame() { |
| 72 | Geometry = Sound_system.m_ll_sound_ptr->GetGeometryInterface(); |
| 73 | if (Geometry) { |
| 74 | Geometry->StartFrame(); |
| 75 | } |
| 76 | return Geometry ? true : false; |
| 77 | } |
| 78 | |
| 79 | // ends a frame |
| 80 | void sound_render_end_frame() { |
| 81 | if (Geometry) { |
| 82 | Geometry->EndFrame(); |
| 83 | } |
| 84 | Geometry = NULL; |
| 85 | } |
| 86 | |
| 87 | // render one room's geometry |
| 88 | // dont render portal faces! |
| 89 | void sound_render_room_geometry(int iroom, int slot) { |
| 90 | int iface, reflect_face; |
| 91 | room *rp; |
| 92 | face *rfacelist; |
| 93 | vector *vertlist[MAX_VERTS_PER_FACE]; |
| 94 | unsigned room_tag = (iroom << 20); // 12 bits for room (4096 rooms max per level) |
| 95 | // 14 bits for faces (16384 faces per room) |
| 96 | // 6 bits per face (64 primatives per face.) |
| 97 | unsigned face_tag; |
| 98 | |
| 99 | ASSERT(Geometry); |
| 100 | if (iroom == -1) { |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | if (iroom != Sound_room_list[slot]) { |
| 105 | if (ROOMNUM_OUTSIDE(iroom)) { |
| 106 | Geometry->Clear(); |
| 107 | Sound_room_list[slot] = iroom; |
| 108 | return; |
| 109 | } |
| 110 | |
| 111 | Geometry->StartPolygonGroup(slot); |
| 112 | |
| 113 | rp = &Rooms[iroom]; |
| 114 | rfacelist = rp->faces; |
| 115 | |
| 116 | // for now, lets create the geometry for the current room the viewer is in. |
| 117 | for (iface = 0, reflect_face = 0; iface < rp->num_faces; iface++, reflect_face = (iface / 3)) { |
| 118 | // add polygon to geometry manager |
| 119 | tSoundMaterial material = SNDGEO_MATERIAL_NONE; |
| 120 | face *rfacep = &rfacelist[iface]; |
| 121 | int ivert; |
| 122 | |
| 123 | face_tag = (iface << 6); |
| 124 | |
| 125 | // don't render portals! |
| 126 | if (rfacep->portal_num > -1) { |
| 127 | |
| 128 | } else { |
| 129 | for (ivert = 0; ivert < rfacep->num_verts; ivert++) { |
| 130 | vertlist[ivert] = &rp->verts[rfacep->face_verts[ivert]]; |
| 131 | } |
| 132 | |
| 133 | // do reflections |
| 134 | if (iface == (reflect_face * 3)) { |
| 135 | // mprintf(0, "reflect "); |
| 136 | int flags = GameTextures[rfacep->tmap].flags; |
| 137 | |
| 138 | if (flags & TF_WATER) { |
| 139 | material = SNDGEO_MATERIAL_WATER; |
| 140 | } else if (flags & TF_METAL) { |
| 141 | material = SNDGEO_MATERIAL_METAL; |
| 142 | } else { |
| 143 | material = SNDGEO_MATERIAL_ROCK; |
| 144 | } |
| 145 | } |
| 146 | Geometry->AddPoly(rfacep->num_verts, vertlist, (room_tag + face_tag), material); |
| 147 | } |
| 148 | } |
| 149 | Geometry->EndPolygonGroup(slot); |
| 150 | Geometry->RenderGroup(slot); |
| 151 | Sound_room_list[slot] = iroom; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | // render all rooms within a certain radius from player |
| 156 | // the radius is actually scaled according to orientation. radius is maximum radius, really, which is forward |
| 157 | // |
| 158 | // returns rooms rendered, terminate list with -1. |
| 159 | // |
| 160 | // algorithm: |
| 161 | // |
| 162 | // routine (Ci,Ri) |
| 163 | // start at listener position in room Ri (i=0) |
| 164 | // render room Ri |
| 165 | // for each portal, connect to room Rj. |
| 166 | // take care of special cases like doors, which are 'openings' |
| 167 | // find center point in Cj. |
| 168 | // if Distance(Cj,Ci)) < (max_radius*Orient(R0)) then |
| 169 | // call routine(Cj,Rj) |
| 170 | // else |
| 171 | // end routine |
| 172 | // endif |
| 173 | // next portal |
| 174 | // end routine |
| 175 | // |
| 176 | int16_t *sound_render_audible_rooms(pos_state *listener_pos, float max_radius) { |
| 177 | int iroom = 0; |
| 178 | |
| 179 | sound_render_room_geometry(listener_pos->roomnum, iroom); |
| 180 | Sound_room_list[iroom + 1] = -1; |
| 181 | |
| 182 | return (int16_t *)Sound_room_list; |
| 183 | } |