-
Notifications
You must be signed in to change notification settings - Fork 0
/
Render.cpp
203 lines (168 loc) · 5.99 KB
/
Render.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*-----------------------------------------------------------------------------
Render.cpp
2006 Shamus Young
-------------------------------------------------------------------------------
This is the core of the gl rendering functions. This contains the main
rendering function RenderScene (), which initiates the various
other renders in the other modules.
-----------------------------------------------------------------------------*/
#define RENDER_DISTANCE 2048
#include "precompiled.h"
static PIXELFORMATDESCRIPTOR pfd = {
sizeof( PIXELFORMATDESCRIPTOR ),
1, // Version Number
PFD_DRAW_TO_WINDOW | // Format Must Support Window
PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
PFD_DOUBLEBUFFER, // Must Support Double Buffering
PFD_TYPE_RGBA, // Request An RGBA Format
32, // Select Our glRgbaDepth
0, 0, 0, 0, 0, 0, // glRgbaBits Ignored
0, // No Alpha Buffer
0, // Shift Bit Ignored
0, // Accumulation Buffers
0, 0, 0, 0, // Accumulation Bits Ignored
16, // Z-Buffer (Depth Buffer) bits
0, // Stencil Buffers
1, // Auxiliary Buffers
PFD_MAIN_PLANE, // Main Drawing Layer
0, // Reserved
0, 0, 0 // Layer Masks Ignored
};
static HDC hDC;
static HGLRC hRC;
static int render_width;
static int render_height;
static float render_aspect;
static unsigned char* buffer;
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void RenderResize( void )
{
int left, top;
if( buffer )
delete[] buffer;
render_width = WinWidth();
render_height = WinHeight();
left = 0;
top = 0;
render_aspect = ( float )render_width / ( float )render_height;
glViewport( left, top, render_width, render_height );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 45.0f, render_aspect, 0.1f, RENDER_DISTANCE );
glMatrixMode( GL_MODELVIEW );
buffer = new unsigned char[WinWidth() * WinHeight() * 4];
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void RenderTerm( void )
{
if( !hRC )
return;
wglDeleteContext( hRC );
hRC = NULL;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void RenderInit( void )
{
HWND hWnd;
unsigned PixelFormat;
hWnd = WinHwnd();
if( !( hDC = GetDC( hWnd ) ) ) { // Did We Get A Device Context?
WinPopup( "Can't Create A GL Device Context." );
return;
}
if( !( PixelFormat = ChoosePixelFormat( hDC, &pfd ) ) ) { // Did Windows Find A Matching Pixel Format?
WinPopup( "Can't Find A Suitable PixelFormat." );
return;
}
if( !SetPixelFormat( hDC, PixelFormat, &pfd ) ) { // Are We Able To Set The Pixel Format?
WinPopup( "Can't Set The PixelFormat." );
return;
}
if( !( hRC = wglCreateContext( hDC ) ) ) { // Are We Able To Get A Rendering Context?
WinPopup( "Can't Create A GL Rendering Context." );
return;
}
if( !wglMakeCurrent( hDC, hRC ) ) { // Try To Activate The Rendering Context
WinPopup( "Can't Activate The GL Rendering Context." );
return;
}
glViewport( 0, 0, WinWidth(), WinHeight() );
glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
SwapBuffers( hDC );
RenderResize();
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void RenderUpdate( void )
{
vec4 light_vector;
rgba light_color;
rgba ambient_color;
rgba fog_color;
vec3 pos;
vec3 angle;
light_vector = WorldLightQuat();
light_color = WorldLightColor();
fog_color = WorldFogColor();
ambient_color = WorldAmbientColor();;
glViewport( 0, 0, WinWidth(), WinHeight() );
glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
glShadeModel( GL_SMOOTH );
glCullFace( GL_BACK );
glDepthFunc( GL_LEQUAL );
//glEnable (GL_FOG); //enable this for fog
glFogi( GL_FOG_MODE, GL_LINEAR );
glFogf( GL_FOG_START, 484.0f );
glFogf( GL_FOG_END, 5880.0f );
glFogfv( GL_FOG_COLOR, &light_color.red );
glEnable( GL_COLOR_MATERIAL );
glEnable( GL_DEPTH_TEST );
glEnable( GL_CULL_FACE );
glCullFace( GL_BACK );
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
glLoadIdentity();
pos = CameraPosition();
angle = CameraAngle();
glRotatef( angle.x, 1.0f, 0.0f, 0.0f );
glRotatef( angle.y, 0.0f, 1.0f, 0.0f );
glRotatef( angle.z, 0.0f, 0.0f, 1.0f );
glTranslatef( -pos.x, -pos.y, -pos.z );
//This was part of a failed experiment. I made a system to allow stuff to
//fade in over time, like in Grand Theft Auto. This ius very effective,
//but since the scene is drawn twice it is quite hard on framerate.
if( 0 ) {
glDrawBuffer( GL_AUX0 );
glFogfv( GL_FOG_COLOR, &fog_color.red );
glClearColor( 0.0f, 0.0f, 1.0f, 1.0f );
glClear( GL_DEPTH_BUFFER_BIT );
EntityRenderFadeIn();
glDrawBuffer( GL_BACK );
glClearColor( 1.0f, 0.0f, 0.0f, 1.0f );
glClear( GL_DEPTH_BUFFER_BIT );
EntityRender();
glEnable( GL_BLEND );
glDisable( GL_TEXTURE_2D );
glReadBuffer( GL_AUX0 );
glDrawBuffer( GL_AUX0 );
glReadPixels( 0, 0, WinWidth(), WinHeight(), GL_RGBA, GL_UNSIGNED_BYTE, buffer );
glReadBuffer( GL_BACK );
glDrawBuffer( GL_BACK );
glColor4f( 1.0f, 1.0f, 1.0f, 0.1f );
glPixelTransferf( GL_ALPHA_SCALE, WorldFade() );
glDisable( GL_FOG );
glDrawPixels( WinWidth(), WinHeight(), GL_RGBA, GL_UNSIGNED_BYTE, buffer );
glReadBuffer( GL_BACK );
glDrawBuffer( GL_BACK );
glPixelTransferf( GL_ALPHA_SCALE, 1.0f );
} else { //this will just render everything once, with no fancy fade
glClearColor( 1.0f, 0.0f, 0.0f, 1.0f );
glClear( GL_DEPTH_BUFFER_BIT );
EntityRender();
}
SwapBuffers( hDC );
}