Debian Codegraph @

renderer/HardwareSetup.cpp

raw symbols SCIP
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
19 #include <stdlib.h>
20 #include <string.h>
21 #include "3d.h"
22 #include "pserror.h"
23 #include "HardwareInternal.h"
24 #include "renderer.h"
25
26 // User-specified aspect ratio, stored as w/h
27 static float sAspect = 0.0f;
28
29 // initialize the 3d system
30 void g3_Init(void) { atexit(g3_Close); }
31
32 // close down the 3d system
33 void g3_Close(void) {}
34
35 // allows the user to specify an aspect ratio that overrides the renderer's
36 // The parameter is the w/h of the screen pixels
37 void g3_SetAspectRatio(float aspect) { sAspect = aspect; }
38 // returns the user-specified aspect ratio used to override the renderer's
39 float g3_GetAspectRatio() { return sAspect; }
40
41 void g3_GetViewPortMatrix(float *viewMat) {
42 // extract the viewport data from the renderer
43 int viewportWidth, viewportHeight;
44 int viewportX, viewportY;
45 rend_GetProjectionScreenParameters(viewportX, viewportY, viewportWidth, viewportHeight);
46
47 float viewportWidthOverTwo = ((float)viewportWidth) * 0.5f;
48 float viewportHeightOverTwo = ((float)viewportHeight) * 0.5f;
49
50 // setup the matrix
51 memset(viewMat, 0, sizeof(float) * 16);
52 viewMat[0] = viewportWidthOverTwo;
53 viewMat[5] = -viewportHeightOverTwo;
54 viewMat[12] = viewportWidthOverTwo + (float)viewportX;
55 viewMat[13] = viewportHeightOverTwo + (float)viewportY;
56 viewMat[10] = viewMat[15] = 1.0f;
57 }
58
59 void g3_GetProjectionMatrix(float zoom, float *projMat) {
60 // get window size
61 int viewportWidth, viewportHeight;
62 rend_GetProjectionParameters(&viewportWidth, &viewportHeight);
63
64 // compute aspect ratio for this ViewPort
65 float screenAspect = rend_GetAspectRatio();
66 if (sAspect != 0.0f) {
67 // check for user override
68 screenAspect = screenAspect * 4.0f / 3.0f / sAspect;
69 }
70 float s = screenAspect * ((float)viewportWidth) / ((float)viewportHeight);
71
72 // setup the matrix
73 memset(projMat, 0, sizeof(float) * 16);
74
75 // calculate 1/tan(fov)
76 float oOT = 1.0f / zoom;
77
78 // fill in the matrix
79 projMat[0] = oOT;
80 projMat[5] = oOT * s;
81 projMat[10] = 1.0f;
82 projMat[11] = 1.0f;
83 projMat[14] = -1.0f;
84 }
85
86 // start the frame
87 void g3_StartFrame(vector *view_pos, matrix *view_matrix, float zoom) {
88 // initialize the viewport transform
89 g3_GetViewPortMatrix((float *)gTransformViewPort);
90 g3_GetProjectionMatrix(zoom, (float *)gTransformProjection);
91 g3_GetModelViewMatrix(view_pos, view_matrix, (float *)gTransformModelView);
92 g3_UpdateFullTransform();
93
94 // get window size
95 rend_GetProjectionParameters(&Window_width, &Window_height);
96
97 // Set vars for projection
98 Window_w2 = ((float)Window_width) * 0.5f;
99 Window_h2 = ((float)Window_height) * 0.5f;
100
101 // Compute aspect ratio for this window
102 float screen_aspect = rend_GetAspectRatio();
103 if (sAspect != 0.0f) {
104 // check for user override
105 screen_aspect = screen_aspect * 4.0f / 3.0f / sAspect;
106 }
107 float s = screen_aspect * (float)Window_height / (float)Window_width;
108
109 if (s <= 0.0f) // JEFF: Should this have been 1.0f?
110 {
111 // scale x
112 Matrix_scale.x = s;
113 Matrix_scale.y = 1.0f;
114 } else {
115 Matrix_scale.y = 1.0f / s;
116 Matrix_scale.x = 1.0f;
117 }
118
119 Matrix_scale.z = 1.0f;
120
121 // Set the view variables
122 View_position = *view_pos;
123 View_zoom = zoom;
124 Unscaled_matrix = *view_matrix;
125
126 // Compute matrix scale for zoom and aspect ratio
127 if (View_zoom <= 1.0f) {
128 // zoom in by scaling z
129 Matrix_scale.z = Matrix_scale.z * View_zoom;
130 } else {
131 // zoom out by scaling x and y
132 float oOZ = 1.0f / View_zoom;
133 Matrix_scale.x = Matrix_scale.x * oOZ;
134 Matrix_scale.y = Matrix_scale.y * oOZ;
135 }
136
137 // Scale the matrix elements
138 View_matrix.rvec = Unscaled_matrix.rvec * Matrix_scale.x;
139 View_matrix.uvec = Unscaled_matrix.uvec * Matrix_scale.y;
140 View_matrix.fvec = Unscaled_matrix.fvec * Matrix_scale.z;
141
142 // Reset the list of free points
143 InitFreePoints();
144
145 // Reset the far clip plane
146 g3_ResetFarClipZ();
147 }
148
149 // this doesn't do anything, but is here for completeness
150 void g3_EndFrame(void) {
151 // make sure temp points are free
152 CheckTempPoints();
153 }
154
155 // get the current view position
156 void g3_GetViewPosition(vector *vp) { *vp = View_position; }
157
158 void g3_GetViewMatrix(matrix *mat) { *mat = View_matrix; }
159
160 void g3_GetUnscaledMatrix(matrix *mat) { *mat = Unscaled_matrix; }
161
162 // Gets the matrix scale vector
163 void g3_GetMatrixScale(vector *matrix_scale) { *matrix_scale = Matrix_scale; }