aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.Client/glWrapper.cpp
blob: d13e1a6917e61480e0bbc13e4e1a9970cbb892ec (plain)
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#include "stdafx.h"
#include "..\Minecraft.World\FloatBuffer.h"
#include "..\Minecraft.World\IntBuffer.h"
#include "..\Minecraft.World\ByteBuffer.h"

void glViewport(int x, int y, int w, int h)
{
	// We don't really need anything here because minecraft doesn't current do anything other than the default viewport
}

void glTranslatef(float x,float y,float z)
{
	RenderManager.MatrixTranslate(x,y,z);
}

void glRotatef(float angle, float x, float y, float z)
{
	RenderManager.MatrixRotate(angle*(PI/180.0f),x,y,z);
}

void glPopMatrix()
{
	RenderManager.MatrixPop();
}

void glPushMatrix()
{
	RenderManager.MatrixPush();
}

void glScalef(float x, float y, float z)
{
	RenderManager.MatrixScale(x,y,z);
}

void glMultMatrixf(float *m)
{
	RenderManager.MatrixMult(m);
}

void glMatrixMode(int type)
{
	RenderManager.MatrixMode(type);
}

void glLoadIdentity()
{
	RenderManager.MatrixSetIdentity();
}

// AAR - Use the aspect ratio passed by the caller.  For single-player this
// equals g_iAspectRatio (screen width / height), but for split-screen
// getFovAndAspect adjusts it to match the viewport dimensions.
void gluPerspective(float fovy, float aspect, float zNear, float zFar)
{
	RenderManager.MatrixPerspective(fovy, aspect, zNear, zFar);
}

void glOrtho(float left,float right,float bottom,float top,float zNear,float zFar)
{
	RenderManager.MatrixOrthogonal(left,right,bottom,top,zNear,zFar);
}

void glScaled(double x,double y,double z)
{
	RenderManager.MatrixScale(static_cast<float>(x),static_cast<float>(y),static_cast<float>(z));
}

void glGetFloat(int type, FloatBuffer *buff)
{
	memcpy(buff->_getDataPointer(),RenderManager.MatrixGet(type),64);
}

void glDeleteLists(int first,int count)
{
	RenderManager.CBuffDelete(first,count);
}

int glGenLists(int count)
{
	return RenderManager.CBuffCreate(count);
}

void glNewList(int index, int mode)
{
	RenderManager.CBuffStart(index);
}

void glEndList(int vertexCount)
{
#ifdef _XBOX
	RenderManager.CBuffEnd(vertexCount);
#else
	RenderManager.CBuffEnd();
#endif
}

void glCallList(int index)
{
	RenderManager.CBuffCall(index);
}

void glCallLists(IntBuffer *ib)
{
	for(unsigned int i = 0; i < ib->limit(); i++)
	{
		RenderManager.CBuffCall(ib->get(i));
	}
}

void glClear(int flags)
{
	RenderManager.Clear(flags);
}

void glClearColor(float r, float g, float b, float a)
{
#ifdef _XBOX
	int ir = (int)(r * 255.0f);	if( ir < 0 ) ir = 0;	if( ir > 255 ) ir = 255;
	int ig = (int)(g * 255.0f);	if( ig < 0 ) ig = 0;	if( ig > 255 ) ig = 255;
	int ib = (int)(b * 255.0f);	if( ib < 0 ) ib = 0;	if( ib > 255 ) ib = 255;
	int ia = (int)(a * 255.0f);	if( ia < 0 ) ia = 0;	if( ia > 255 ) ia = 255;

	RenderManager.SetClearColour(D3DCOLOR_RGBA(ir,ig,ib,ia));
#else
	float rgba[4] = {r,g,b,a};
	RenderManager.SetClearColour(rgba);
#endif
}

void Display::update()
{
}

void Display::swapBuffers()
{
}

void glBindTexture(int target,int texture)
{
	RenderManager.TextureBind(texture);
}

void glTexImage2D(int target,int level,int internalformat,int width,int height,int border,int format,int type, ByteBuffer *data)
{
	RenderManager.TextureData(width,height,data->getBuffer(),level);
}

void glDeleteTextures(IntBuffer *ib)
{
	for(unsigned int i = 0; i < ib->limit(); i++)
	{
		RenderManager.TextureFree(ib->get(i));
	}
}

// 4J Stu - I'm pretty sure this is what it should do
void glDeleteTextures(int id)
{
	RenderManager.TextureFree(id);
}

void glGenTextures(IntBuffer *ib)
{
	for(unsigned int i = 0; i < ib->limit(); i++)
	{
		ib->put(RenderManager.TextureCreate());
	}
}

// 4J Stu - I'm pretty sure this is what it should do
int glGenTextures()
{
	return RenderManager.TextureCreate();
}

void glColor3f(float r, float g, float b)
{
	RenderManager.StateSetColour(r,g,b,1.0f);
}

void glColor4f(float r, float g, float b, float a)
{
	RenderManager.StateSetColour(r,g,b,a);
}

void glDisable(int state)
{
	switch(state)
	{
		case GL_TEXTURE_2D:
			RenderManager.TextureBind(-1);
			break;
		case GL_BLEND:
			RenderManager.StateSetBlendEnable(false);
			break;
		case GL_CULL_FACE:
			RenderManager.StateSetFaceCull(false);
			break;
		case GL_DEPTH_TEST:
			RenderManager.StateSetDepthTestEnable(false);
			break;
		case GL_ALPHA_TEST:
			RenderManager.StateSetAlphaTestEnable(false);
			break;
		case GL_FOG:
			RenderManager.StateSetFogEnable(false);
			break;
		case GL_LIGHTING:
			RenderManager.StateSetLightingEnable(false);
			break;
		case GL_LIGHT0:
			RenderManager.StateSetLightEnable(0,false);
			break;
		case GL_LIGHT1:
			RenderManager.StateSetLightEnable(1,false);
			break;
	}
}

void glEnable(int state)
{
	switch(state)
	{
		case GL_BLEND:
			RenderManager.StateSetBlendEnable(true);
			break;
		case GL_CULL_FACE:
			RenderManager.StateSetFaceCull(true);
			break;
		case GL_DEPTH_TEST:
			RenderManager.StateSetDepthTestEnable(true);
			break;
		case GL_ALPHA_TEST:
			RenderManager.StateSetAlphaTestEnable(true);
			break;
		case GL_FOG:
			RenderManager.StateSetFogEnable(true);
			break;
		case GL_LIGHTING:
			RenderManager.StateSetLightingEnable(true);
			break;
		case GL_LIGHT0:
			RenderManager.StateSetLightEnable(0,true);
			break;
		case GL_LIGHT1:
			RenderManager.StateSetLightEnable(1,true);
			break;
	}
}

void glDepthMask(bool enable)
{
	RenderManager.StateSetDepthMask(enable);
}

void glBlendFunc(int src, int dst)
{
	RenderManager.StateSetBlendFunc(src,dst);
}

void glAlphaFunc(int func,float param)
{
	RenderManager.StateSetAlphaFunc(func, param);
}

void glDepthFunc(int func)
{
#ifdef _XBOX
	RenderManager.StateSetDepthFunc(func);
#else
	RenderManager.StateSetDepthFunc(func);
#endif
}

void glTexParameteri(int target, int param, int value)
{
	RenderManager.TextureSetParam(param,value);
}

void glPolygonOffset(float factor, float units)
{
#ifdef __PS3__
	RenderManager.StateSetDepthSlopeAndBias(factor, units);
#else
	// DirectX specifies these offsets in z buffer 0 to 1 sort of range, whereas opengl seems to be in a 0 -> depth buffer size sort of range.
	// The slope factor is quite possibly different too. Magic factor for now anyway.
	const float magicFactor = 65536.0f;
	RenderManager.StateSetDepthSlopeAndBias(factor / magicFactor, units / magicFactor);
#endif
}

void glFogi(int param, int value)
{
	if( param == GL_FOG_MODE )
	{
		RenderManager.StateSetFogMode(value);
	}
}

void glFogf(int param, float value)
{
	switch(param)
	{
		case GL_FOG_START:
			RenderManager.StateSetFogNearDistance(value);
			break;
		case GL_FOG_END:
			RenderManager.StateSetFogFarDistance(value);
			break;
		case GL_FOG_DENSITY:
			RenderManager.StateSetFogDensity(value);
			break;
	}
}

void glFog(int param,FloatBuffer *values)
{
	if( param == GL_FOG_COLOR )
	{
		float *data = values->_getDataPointer();
		RenderManager.StateSetFogColour(data[0],data[1],data[2]);
	}
}

void glLight(int light, int mode,FloatBuffer *values)
{
	int idx;
	if( light == GL_LIGHT0 )
	{
		idx = 0;
	}
	else if( light == GL_LIGHT1 )
	{
		idx = 1;
	}
	else return;
	float *data =values->_getDataPointer();
	switch( mode )
	{
	case GL_POSITION:
		RenderManager.StateSetLightDirection(idx, data[0], data[1], data[2]);
		break;
	case GL_DIFFUSE:
		RenderManager.StateSetLightColour(idx, data[0], data[1], data[2]);
		break;
	case GL_AMBIENT:
		break;
	case GL_SPECULAR:
		break;
	}
}

void glLightModel(int mode, FloatBuffer *values)
{
	float *data =values->_getDataPointer();
	if( mode == GL_LIGHT_MODEL_AMBIENT )
	{
		RenderManager.StateSetLightAmbientColour(data[0],data[1],data[2]);
	}
}

void glLineWidth(float width)
{
	RenderManager.StateSetLineWidth(width);
}

void glColorMask(bool red, bool green, bool blue, bool alpha)
{
	RenderManager.StateSetWriteEnable(red, green, blue, alpha);
}

void glMultiTexCoord2f(int, float u , float v)
{
	// Clamp these values just to be safe - the lighting code can get broken if we pass things to StateSetVertexTextureUV that are >= 1
	if( u > 255.0f ) u = 255.0f;
	if( v > 255.0f ) v = 255.0f;

	RenderManager.StateSetVertexTextureUV( u / 256.0f, v / 256.0f);
}

void glTexGen(int coord, int mode, FloatBuffer *vec)
{
	float *data = vec->_getDataPointer();

	RenderManager.StateSetTexGenCol( coord, data[0], data[1], data[2], data[3], mode == GL_EYE_PLANE );
}

void glCullFace(int dir)
{
	RenderManager.StateSetFaceCullCW( dir == GL_BACK);
}