Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

5859 lines
198 KiB

  1. #include "precomp.h"
  2. #pragma warning(disable : 4273)
  3. #define D3D_OVERLOADS
  4. #include <math.h>
  5. #include <stdio.h>
  6. #include "EmulateOpenGL_opengl32.hpp"
  7. #define HOOK_WINDOW_PROC 0
  8. #define GETPARMSFORDEBUG 0
  9. #define DODPFS 0
  10. void APIENTRY glEnd (void);
  11. void APIENTRY glBegin (GLenum mode);
  12. BOOL APIENTRY wglSwapIntervalEXT(GLint interval);
  13. #define PI 3.1415926535897932384626433832795
  14. Globals * g_OpenGLValues = NULL;
  15. // The BOOL below comes from the shim command line. It can't be part of the Globals struct above
  16. // because wglCreateContext memsets g_OpenGLValues to 0, but this is after the shim command line
  17. // has been processed.
  18. BOOL g_bDoTexelAlignmentHack = FALSE;
  19. #if GETPARMSFORDEBUG || DODPFS
  20. // converts doubles to ascii for debug output. it only shows 6 place precision.
  21. void ftoa( double d, char *buf )
  22. {
  23. long l, i, j;
  24. char *s, *n;
  25. if( d < 0.0 )
  26. {
  27. d = -d;
  28. n = "-";
  29. }
  30. else
  31. {
  32. n = "";
  33. }
  34. i = (long)d;
  35. j = (long)((d - (double)i) * 100000);
  36. if( j < 10 )
  37. s = "00000";
  38. else if( j < 100 )
  39. s = "0000";
  40. else if( j < 1000 )
  41. s = "000";
  42. else if( j < 10000 )
  43. s = "00";
  44. else if( j < 100000 )
  45. s = "0";
  46. else s = "";
  47. sprintf( buf, "%s%d.%s%d", n, i, s, j );
  48. }
  49. #endif
  50. inline void VertexBufferFilled()
  51. {
  52. if(g_OpenGLValues->m_nfv + g_OpenGLValues->m_vcnt >= (VBUFSIZE - MAXVERTSPERPRIM))
  53. {
  54. if(g_OpenGLValues->m_prim == GL_TRIANGLES)
  55. {
  56. if(g_OpenGLValues->m_vcnt % 3 == 0)
  57. {
  58. glEnd();
  59. glBegin(GL_TRIANGLES);
  60. }
  61. }
  62. else if(g_OpenGLValues->m_prim == GL_QUADS)
  63. {
  64. if(g_OpenGLValues->m_vcnt % 4 == 0)
  65. {
  66. glEnd();
  67. glBegin(GL_QUADS);
  68. }
  69. }
  70. else if(g_OpenGLValues->m_prim == GL_LINES)
  71. {
  72. if(g_OpenGLValues->m_vcnt % 2 == 0)
  73. {
  74. glEnd();
  75. glBegin(GL_LINES);
  76. }
  77. }
  78. }
  79. }
  80. inline void QuakeSetVertexShader(DWORD vs)
  81. {
  82. if(g_OpenGLValues->m_curshader != vs)
  83. {
  84. g_OpenGLValues->m_d3ddev->SetVertexShader(vs);
  85. g_OpenGLValues->m_curshader = vs;
  86. }
  87. }
  88. inline void QuakeSetStreamSource(DWORD i, IDirect3DVertexBuffer8 *pBuf, DWORD stride)
  89. {
  90. if(g_OpenGLValues->m_pStreams[i] != pBuf || g_OpenGLValues->m_pStrides[i] != stride)
  91. {
  92. g_OpenGLValues->m_d3ddev->SetStreamSource(i, pBuf, stride);
  93. g_OpenGLValues->m_pStreams[i] = pBuf;
  94. g_OpenGLValues->m_pStrides[i] = stride;
  95. }
  96. }
  97. inline void MultiplyMatrix(D3DTRANSFORMSTATETYPE xfrm, const D3DMATRIX &min)
  98. {
  99. D3DMATRIX &mout = g_OpenGLValues->m_xfrm[xfrm];
  100. for(unsigned i = 0; i < 4; ++i)
  101. {
  102. float a = mout.m[0][i];
  103. float b = mout.m[1][i];
  104. float c = mout.m[2][i];
  105. float d = mout.m[3][i];
  106. for(unsigned j = 0; j < 4; ++j)
  107. {
  108. mout.m[j][i] = min.m[j][0] * a + min.m[j][1] * b + min.m[j][2] * c + min.m[j][3] * d;
  109. }
  110. }
  111. }
  112. inline void MEMCPY(VOID *dst, const VOID *src, DWORD sz)
  113. {
  114. #ifdef _X86_
  115. if((sz & 0x3) == 0)
  116. {
  117. _asm
  118. {
  119. mov ecx, sz;
  120. mov esi, src;
  121. mov edi, dst;
  122. cld;
  123. shr ecx, 2;
  124. lp1: movsd;
  125. dec ecx;
  126. jnz lp1;
  127. }
  128. }
  129. else
  130. #endif
  131. {
  132. memcpy(dst, src, sz);
  133. }
  134. }
  135. inline void Clamp(float *v)
  136. {
  137. if(*v < 0.f)
  138. {
  139. *v = 0.f;
  140. }
  141. else if(*v > 1.f)
  142. {
  143. *v = 1.f;
  144. }
  145. }
  146. inline void Clamp(double *v)
  147. {
  148. if(*v < 0.)
  149. {
  150. *v = 0.;
  151. }
  152. else if(*v > 1.)
  153. {
  154. *v = 1.;
  155. }
  156. }
  157. void QuakeSetTransform(D3DTRANSFORMSTATETYPE State, CONST D3DMATRIX *m)
  158. {
  159. #if GETPARMSFORDEBUG
  160. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "QuakeSetTransform: 0x%X 0x%X", State, m );
  161. #endif
  162. static D3DMATRIX unity = {1.f, 0.f, 0.f, 0.f, 0.f, 1.f, 0.f, 0.f, 0.f, 0.f, 1.f, 0.f, 0.f, 0.f, 0.f, 1.f};
  163. if(State == D3DTS_PROJECTION)
  164. {
  165. D3DMATRIX f = *m;
  166. f._13 = (m->_13 + m->_14) * 0.5f;
  167. f._23 = (m->_23 + m->_24) * 0.5f;
  168. f._33 = (m->_33 + m->_34) * 0.5f;
  169. f._43 = (m->_43 + m->_44) * 0.5f;
  170. if(g_OpenGLValues->m_scissoring)
  171. {
  172. FLOAT dvClipX, dvClipY, dvClipWidth, dvClipHeight;
  173. RECT scirect, vwprect, xrect;
  174. scirect.left = g_OpenGLValues->m_scix;
  175. scirect.top = g_OpenGLValues->m_sciy;
  176. scirect.right = g_OpenGLValues->m_scix + g_OpenGLValues->m_sciw;
  177. scirect.bottom = g_OpenGLValues->m_sciy + g_OpenGLValues->m_scih;
  178. vwprect.left = g_OpenGLValues->m_vwx;
  179. vwprect.top = g_OpenGLValues->m_vwy;
  180. vwprect.right = g_OpenGLValues->m_vwx + g_OpenGLValues->m_vww;
  181. vwprect.bottom = g_OpenGLValues->m_vwy + g_OpenGLValues->m_vwh;
  182. if(IntersectRect(&xrect, &scirect, &vwprect))
  183. {
  184. if(EqualRect(&xrect, &vwprect)) // Check whether viewport is completely within scissor rect
  185. {
  186. dvClipX = -1.f;
  187. dvClipY = 1.f;
  188. dvClipWidth = 2.f;
  189. dvClipHeight = 2.f;
  190. }
  191. else
  192. {
  193. // We need to use xrect rather than scirect (ie clip scissor rect to viewport)
  194. // and transform the clipped scissor rect into viewport relative coordinates
  195. // to correctly compute the clip stuff
  196. GLint scix = xrect.left - g_OpenGLValues->m_vwx;
  197. GLint sciy = xrect.top - g_OpenGLValues->m_vwy;
  198. GLsizei sciw = xrect.right - xrect.left;
  199. GLsizei scih = xrect.bottom - xrect.top;
  200. dvClipX = (2.f * scix) / g_OpenGLValues->m_vww - 1.0f;
  201. dvClipY = (2.f * (sciy + scih)) / g_OpenGLValues->m_vwh - 1.0f;
  202. dvClipWidth = (2.f * sciw) / g_OpenGLValues->m_vww;
  203. dvClipHeight = (2.f * scih) / g_OpenGLValues->m_vwh;
  204. }
  205. }
  206. else
  207. {
  208. #if DODPFS
  209. OutputDebugStringA("Wrapper: non-intersecting scissor and viewport rects not implemented\n" );
  210. #endif
  211. return;
  212. }
  213. D3DMATRIX c;
  214. // to prevent divide by zero from possibly happening (check bug #259251 in Whistler database)
  215. if(dvClipWidth == 0.f)
  216. dvClipWidth = 1.f;
  217. if(dvClipHeight == 0.f)
  218. dvClipHeight = 1.f;
  219. c._11 = 2.f / dvClipWidth;
  220. c._21 = 0.f;
  221. c._31 = 0.f;
  222. c._41 = -1.f - 2.f * (dvClipX / dvClipWidth);
  223. c._12 = 0.f;
  224. c._22 = 2.f / dvClipHeight;
  225. c._32 = 0.f;
  226. c._42 = 1.f - 2.f * (dvClipY / dvClipHeight);
  227. c._13 = 0.f;
  228. c._23 = 0.f;
  229. c._33 = 1.f;
  230. c._43 = 0.f;
  231. c._14 = 0.f;
  232. c._24 = 0.f;
  233. c._34 = 0.f;
  234. c._44 = 1.f;
  235. D3DMATRIX t = g_OpenGLValues->m_xfrm[D3DTS_PROJECTION];
  236. g_OpenGLValues->m_xfrm[D3DTS_PROJECTION] = c;
  237. #if GETPARMSFORDEBUG
  238. char buf1[40], buf2[40], buf3[40], buf4[40];
  239. ftoa( dvClipX, buf1 );
  240. ftoa( dvClipY, buf2 );
  241. ftoa( dvClipWidth, buf3 );
  242. ftoa( dvClipHeight, buf4 );
  243. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "QuakeSetTransform: DVCLIP: %s %s %s %s SCIRECT: %d %d %d %d VWPRECT: %d %d %d %d XRECT: %d %d %d %d", buf1, buf2, buf3, buf4, scirect.left, scirect.top, scirect.right, scirect.bottom, vwprect.left, vwprect.top, vwprect.right, vwprect.bottom, xrect.left, xrect.top, xrect.right, xrect.bottom );
  244. #endif
  245. MultiplyMatrix(D3DTS_PROJECTION, f);
  246. f = g_OpenGLValues->m_xfrm[D3DTS_PROJECTION];
  247. g_OpenGLValues->m_xfrm[D3DTS_PROJECTION] = t;
  248. }
  249. if( g_bDoTexelAlignmentHack )
  250. {
  251. // Translate all geometry by (-0.5 pixels in x, -0.5 pixels in y) along the screen/viewport plane.
  252. // This helps force texels that were authored to be sampled pixel-aligned on OpenGL to be pixel-aligned on D3D.
  253. float x = g_OpenGLValues->m_vport.Width ? (-1.f / g_OpenGLValues->m_vport.Width) : 0.0f;
  254. float y = g_OpenGLValues->m_vport.Height ? (1.0f / g_OpenGLValues->m_vport.Height) : 0.0f;
  255. f._11 = f._11 + f._14*x;
  256. f._12 = f._12 + f._14*y;
  257. f._21 = f._21 + f._24*x;
  258. f._22 = f._22 + f._24*y;
  259. f._31 = f._31 + f._34*x;
  260. f._32 = f._32 + f._34*y;
  261. f._41 = f._41 + f._44*x;
  262. f._42 = f._42 + f._44*y;
  263. }
  264. g_OpenGLValues->m_d3ddev->SetTransform(State, &f);
  265. }
  266. else if(State == D3DTS_TEXTURE0 || State == D3DTS_TEXTURE1)
  267. {
  268. D3DMATRIX f;
  269. f._11 = m->_11;
  270. f._12 = m->_12;
  271. f._13 = m->_14;
  272. f._14 = 0.f;
  273. f._21 = m->_21;
  274. f._22 = m->_22;
  275. f._23 = m->_24;
  276. f._24 = 0.f;
  277. f._31 = m->_41;
  278. f._32 = m->_42;
  279. f._33 = m->_44;
  280. f._34 = 0.f;
  281. f._41 = 0.f;
  282. f._42 = 0.f;
  283. f._43 = 0.f;
  284. f._44 = 0.f;
  285. g_OpenGLValues->m_d3ddev->SetTransform(State, &f);
  286. if(memcmp(&unity, m, sizeof(D3DMATRIX)) != 0)
  287. {
  288. g_OpenGLValues->m_d3ddev->SetTextureStageState((DWORD)(State - D3DTS_TEXTURE0), D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT3 | D3DTTFF_PROJECTED);
  289. }
  290. else
  291. {
  292. g_OpenGLValues->m_d3ddev->SetTextureStageState((DWORD)(State - D3DTS_TEXTURE0), D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE);
  293. }
  294. }
  295. else
  296. {
  297. g_OpenGLValues->m_d3ddev->SetTransform(State, m);
  298. }
  299. }
  300. void QuakeUpdateViewport()
  301. {
  302. #if GETPARMSFORDEBUG
  303. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "QuakeUpdateViewport" );
  304. #endif
  305. if(g_OpenGLValues->m_scissoring)
  306. {
  307. RECT scirect, vwprect, xrect;
  308. scirect.left = g_OpenGLValues->m_scix;
  309. scirect.top = g_OpenGLValues->m_sciy;
  310. scirect.right = g_OpenGLValues->m_scix + g_OpenGLValues->m_sciw;
  311. scirect.bottom = g_OpenGLValues->m_sciy + g_OpenGLValues->m_scih;
  312. vwprect.left = g_OpenGLValues->m_vwx;
  313. vwprect.top = g_OpenGLValues->m_vwy;
  314. vwprect.right = g_OpenGLValues->m_vwx + g_OpenGLValues->m_vww;
  315. vwprect.bottom = g_OpenGLValues->m_vwy + g_OpenGLValues->m_vwh;
  316. if(IntersectRect(&xrect, &scirect, &vwprect))
  317. {
  318. if(EqualRect(&xrect, &vwprect)) // Check whether viewport is completely within scissor rect
  319. {
  320. g_OpenGLValues->m_vport.X = g_OpenGLValues->m_vwx;
  321. g_OpenGLValues->m_vport.Y = g_OpenGLValues->m_winHeight - (g_OpenGLValues->m_vwy + g_OpenGLValues->m_vwh);
  322. g_OpenGLValues->m_vport.Width = g_OpenGLValues->m_vww;
  323. g_OpenGLValues->m_vport.Height = g_OpenGLValues->m_vwh;
  324. QuakeSetTransform(D3DTS_PROJECTION, &g_OpenGLValues->m_xfrm[D3DTS_PROJECTION]);
  325. }
  326. else
  327. {
  328. // We need to use xrect rather than scirect (ie clip scissor rect to viewport)
  329. g_OpenGLValues->m_vport.X = xrect.left;
  330. g_OpenGLValues->m_vport.Y = g_OpenGLValues->m_winHeight - xrect.bottom;
  331. g_OpenGLValues->m_vport.Width = xrect.right - xrect.left;
  332. g_OpenGLValues->m_vport.Height = xrect.bottom - xrect.top;
  333. QuakeSetTransform(D3DTS_PROJECTION, &g_OpenGLValues->m_xfrm[D3DTS_PROJECTION]);
  334. }
  335. }
  336. else
  337. {
  338. #if DODPFS
  339. OutputDebugStringA("Wrapper: non-intersecting scissor and viewport rects not implemented\n");
  340. #endif
  341. return;
  342. }
  343. }
  344. else
  345. {
  346. g_OpenGLValues->m_vport.X = g_OpenGLValues->m_vwx;
  347. g_OpenGLValues->m_vport.Y = g_OpenGLValues->m_winHeight - (g_OpenGLValues->m_vwy + g_OpenGLValues->m_vwh);
  348. g_OpenGLValues->m_vport.Width = g_OpenGLValues->m_vww;
  349. g_OpenGLValues->m_vport.Height = g_OpenGLValues->m_vwh;
  350. }
  351. if(g_OpenGLValues->m_polyoffset && g_OpenGLValues->m_vport.MaxZ != 0.f)
  352. {
  353. D3DVIEWPORT8 vport(g_OpenGLValues->m_vport);
  354. vport.MaxZ -= .001f;
  355. #if GETPARMSFORDEBUG
  356. char buf1[40], buf2[40];
  357. ftoa( vport.MinZ, buf1 );
  358. ftoa( vport.MaxZ, buf2 );
  359. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "QuakeUpdateViewport: vport=%d %d %d %d %s %s", vport.X, vport.Y, vport.Width, vport.Height, buf1, buf2 );
  360. #endif
  361. g_OpenGLValues->m_d3ddev->SetViewport(&vport);
  362. }
  363. else
  364. {
  365. #if GETPARMSFORDEBUG
  366. char buf1[40], buf2[40];
  367. ftoa( g_OpenGLValues->m_vport.MinZ, buf1 );
  368. ftoa( g_OpenGLValues->m_vport.MaxZ, buf2 );
  369. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "QuakeUpdateViewport g_OpenGLValues->m_vport=%d %d %d %d %s %s", g_OpenGLValues->m_vport.X, g_OpenGLValues->m_vport.Y, g_OpenGLValues->m_vport.Width, g_OpenGLValues->m_vport.Height, buf1, buf2 );
  370. #endif
  371. g_OpenGLValues->m_d3ddev->SetViewport(&g_OpenGLValues->m_vport);
  372. }
  373. g_OpenGLValues->m_updvwp = FALSE;
  374. }
  375. void QuakeUpdateLights()
  376. {
  377. #if GETPARMSFORDEBUG
  378. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "QuakeUpdateLights" );
  379. #endif
  380. for(unsigned i = 0; i < 8; ++i)
  381. {
  382. if(g_OpenGLValues->m_lightdirty & (1 << i))
  383. {
  384. D3DLIGHT8 light = g_OpenGLValues->m_light[i];
  385. if(g_OpenGLValues->m_lightPositionW[i] == 0.f)
  386. {
  387. if(light.Phi == 180.f)
  388. {
  389. light.Type = D3DLIGHT_DIRECTIONAL;
  390. }
  391. else
  392. {
  393. light.Type = D3DLIGHT_SPOT;
  394. light.Attenuation0 = 1.f;
  395. light.Attenuation1 = 0.f;
  396. light.Attenuation2 = 0.f;
  397. }
  398. }
  399. else
  400. {
  401. if(light.Phi == 180.f)
  402. {
  403. light.Type = D3DLIGHT_POINT;
  404. }
  405. else
  406. {
  407. light.Type = D3DLIGHT_SPOT;
  408. }
  409. }
  410. light.Phi *= float(PI / 90.);
  411. g_OpenGLValues->m_d3ddev->SetLight(i, &light);
  412. }
  413. }
  414. g_OpenGLValues->m_lightdirty = 0;
  415. }
  416. void QuakeSetTexturingState()
  417. {
  418. #if GETPARMSFORDEBUG
  419. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "QuakeSetTexturingState" );
  420. #endif
  421. if(g_OpenGLValues->m_lightdirty != 0)
  422. QuakeUpdateLights();
  423. if(g_OpenGLValues->m_updvwp)
  424. QuakeUpdateViewport();
  425. if(g_OpenGLValues->m_texturing == TRUE) {
  426. if(g_OpenGLValues->m_texHandleValid == FALSE) {
  427. TexInfo &ti = g_OpenGLValues->m_tex[g_OpenGLValues->m_curstagebinding[0]];
  428. if(ti.m_dwStage != 0)
  429. {
  430. g_OpenGLValues->m_d3ddev->DeleteStateBlock(ti.m_block);
  431. g_OpenGLValues->m_d3ddev->BeginStateBlock();
  432. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_ADDRESSU,ti.m_addu);
  433. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_ADDRESSV,ti.m_addv);
  434. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_MAGFILTER,ti.m_magmode);
  435. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_MINFILTER,ti.m_minmode);
  436. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_MIPFILTER,ti.m_mipmode);
  437. g_OpenGLValues->m_d3ddev->SetTexture(0, ti.m_ddsurf);
  438. g_OpenGLValues->m_d3ddev->EndStateBlock(&ti.m_block);
  439. ti.m_dwStage = 0;
  440. ti.m_capture = FALSE;
  441. }
  442. if(ti.m_capture)
  443. {
  444. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_ADDRESSU,ti.m_addu);
  445. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_ADDRESSV,ti.m_addv);
  446. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_MAGFILTER,ti.m_magmode);
  447. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_MINFILTER,ti.m_minmode);
  448. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_MIPFILTER,ti.m_mipmode);
  449. g_OpenGLValues->m_d3ddev->SetTexture(0, ti.m_ddsurf);
  450. g_OpenGLValues->m_d3ddev->CaptureStateBlock(ti.m_block);
  451. ti.m_capture = FALSE;
  452. }
  453. else
  454. {
  455. g_OpenGLValues->m_d3ddev->ApplyStateBlock(ti.m_block);
  456. }
  457. switch(g_OpenGLValues->m_blendmode[0]) {
  458. case GL_REPLACE:
  459. switch(ti.m_internalformat) {
  460. case 1:
  461. case GL_LUMINANCE:
  462. case GL_LUMINANCE8:
  463. case 3:
  464. case GL_RGB:
  465. case GL_RGB5:
  466. case GL_RGB8:
  467. g_OpenGLValues->m_d3ddev->ApplyStateBlock(g_OpenGLValues->m_shaders[0][0]);
  468. break;
  469. case 4:
  470. case GL_RGBA:
  471. case GL_RGBA4:
  472. case GL_RGBA8:
  473. g_OpenGLValues->m_d3ddev->ApplyStateBlock(g_OpenGLValues->m_shaders[0][1]);
  474. break;
  475. case GL_ALPHA:
  476. case GL_ALPHA8:
  477. g_OpenGLValues->m_d3ddev->ApplyStateBlock(g_OpenGLValues->m_shaders[0][8]);
  478. break;
  479. }
  480. break;
  481. case GL_MODULATE:
  482. switch(ti.m_internalformat) {
  483. case 1:
  484. case GL_LUMINANCE:
  485. case GL_LUMINANCE8:
  486. case 3:
  487. case GL_RGB:
  488. case GL_RGB5:
  489. case GL_RGB8:
  490. g_OpenGLValues->m_d3ddev->ApplyStateBlock(g_OpenGLValues->m_shaders[0][2]);
  491. break;
  492. case 4:
  493. case GL_RGBA:
  494. case GL_RGBA4:
  495. case GL_RGBA8:
  496. g_OpenGLValues->m_d3ddev->ApplyStateBlock(g_OpenGLValues->m_shaders[0][3]);
  497. break;
  498. case GL_ALPHA:
  499. case GL_ALPHA8:
  500. g_OpenGLValues->m_d3ddev->ApplyStateBlock(g_OpenGLValues->m_shaders[0][9]);
  501. break;
  502. }
  503. break;
  504. case GL_DECAL:
  505. switch(ti.m_internalformat) {
  506. case 3:
  507. case GL_RGB:
  508. case GL_RGB5:
  509. case GL_RGB8:
  510. g_OpenGLValues->m_d3ddev->ApplyStateBlock(g_OpenGLValues->m_shaders[0][4]);
  511. break;
  512. case 4:
  513. case GL_RGBA:
  514. case GL_RGBA4:
  515. case GL_RGBA8:
  516. g_OpenGLValues->m_d3ddev->ApplyStateBlock(g_OpenGLValues->m_shaders[0][5]);
  517. break;
  518. }
  519. break;
  520. case GL_BLEND:
  521. switch(ti.m_internalformat) {
  522. case 1:
  523. case GL_LUMINANCE:
  524. case GL_LUMINANCE8:
  525. case 3:
  526. case GL_RGB:
  527. case GL_RGB5:
  528. case GL_RGB8:
  529. g_OpenGLValues->m_d3ddev->ApplyStateBlock(g_OpenGLValues->m_shaders[0][6]);
  530. break;
  531. case 4:
  532. case GL_RGBA:
  533. case GL_RGBA4:
  534. case GL_RGBA8:
  535. g_OpenGLValues->m_d3ddev->ApplyStateBlock(g_OpenGLValues->m_shaders[0][7]);
  536. break;
  537. case GL_ALPHA:
  538. case GL_ALPHA8:
  539. g_OpenGLValues->m_d3ddev->ApplyStateBlock(g_OpenGLValues->m_shaders[0][9]);
  540. break;
  541. }
  542. break;
  543. }
  544. if(g_OpenGLValues->m_mtex != FALSE) {
  545. TexInfo &ti2 = g_OpenGLValues->m_tex[g_OpenGLValues->m_curstagebinding[1]];
  546. if(ti2.m_dwStage != 1)
  547. {
  548. g_OpenGLValues->m_d3ddev->DeleteStateBlock(ti2.m_block);
  549. g_OpenGLValues->m_d3ddev->BeginStateBlock();
  550. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_ADDRESSU,ti2.m_addu);
  551. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_ADDRESSV,ti2.m_addv);
  552. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_MAGFILTER,ti2.m_magmode);
  553. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_MINFILTER,ti2.m_minmode);
  554. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_MIPFILTER,ti2.m_mipmode);
  555. g_OpenGLValues->m_d3ddev->SetTexture(1, ti2.m_ddsurf);
  556. g_OpenGLValues->m_d3ddev->EndStateBlock(&ti2.m_block);
  557. ti2.m_dwStage = 1;
  558. ti2.m_capture = FALSE;
  559. }
  560. if(ti2.m_capture)
  561. {
  562. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_ADDRESSU,ti2.m_addu);
  563. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_ADDRESSV,ti2.m_addv);
  564. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_MAGFILTER,ti2.m_magmode);
  565. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_MINFILTER,ti2.m_minmode);
  566. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_MIPFILTER,ti2.m_mipmode);
  567. g_OpenGLValues->m_d3ddev->SetTexture(1, ti2.m_ddsurf);
  568. g_OpenGLValues->m_d3ddev->CaptureStateBlock(ti2.m_block);
  569. ti2.m_capture = FALSE;
  570. }
  571. else
  572. {
  573. g_OpenGLValues->m_d3ddev->ApplyStateBlock(ti2.m_block);
  574. }
  575. switch(g_OpenGLValues->m_blendmode[1]) {
  576. case GL_REPLACE:
  577. switch(ti2.m_internalformat) {
  578. case 1:
  579. case GL_LUMINANCE:
  580. case GL_LUMINANCE8:
  581. case 3:
  582. case GL_RGB:
  583. case GL_RGB5:
  584. case GL_RGB8:
  585. g_OpenGLValues->m_d3ddev->ApplyStateBlock(g_OpenGLValues->m_shaders[1][0]);
  586. break;
  587. case 4:
  588. case GL_RGBA:
  589. case GL_RGBA4:
  590. case GL_RGBA8:
  591. g_OpenGLValues->m_d3ddev->ApplyStateBlock(g_OpenGLValues->m_shaders[1][1]);
  592. break;
  593. case GL_ALPHA:
  594. case GL_ALPHA8:
  595. g_OpenGLValues->m_d3ddev->ApplyStateBlock(g_OpenGLValues->m_shaders[1][8]);
  596. break;
  597. }
  598. break;
  599. case GL_MODULATE:
  600. switch(ti2.m_internalformat) {
  601. case 1:
  602. case GL_LUMINANCE:
  603. case GL_LUMINANCE8:
  604. case 3:
  605. case GL_RGB:
  606. case GL_RGB5:
  607. case GL_RGB8:
  608. g_OpenGLValues->m_d3ddev->ApplyStateBlock(g_OpenGLValues->m_shaders[1][2]);
  609. break;
  610. case 4:
  611. case GL_RGBA:
  612. case GL_RGBA4:
  613. case GL_RGBA8:
  614. g_OpenGLValues->m_d3ddev->ApplyStateBlock(g_OpenGLValues->m_shaders[1][3]);
  615. break;
  616. case GL_ALPHA:
  617. case GL_ALPHA8:
  618. g_OpenGLValues->m_d3ddev->ApplyStateBlock(g_OpenGLValues->m_shaders[1][9]);
  619. break;
  620. }
  621. break;
  622. case GL_DECAL:
  623. switch(ti2.m_internalformat) {
  624. case 3:
  625. case GL_RGB:
  626. case GL_RGB5:
  627. case GL_RGB8:
  628. g_OpenGLValues->m_d3ddev->ApplyStateBlock(g_OpenGLValues->m_shaders[1][4]);
  629. break;
  630. case 4:
  631. case GL_RGBA:
  632. case GL_RGBA4:
  633. case GL_RGBA8:
  634. g_OpenGLValues->m_d3ddev->ApplyStateBlock(g_OpenGLValues->m_shaders[1][5]);
  635. break;
  636. }
  637. break;
  638. case GL_BLEND:
  639. switch(ti2.m_internalformat) {
  640. case 1:
  641. case GL_LUMINANCE:
  642. case GL_LUMINANCE8:
  643. case 3:
  644. case GL_RGB:
  645. case GL_RGB5:
  646. case GL_RGB8:
  647. g_OpenGLValues->m_d3ddev->ApplyStateBlock(g_OpenGLValues->m_shaders[1][6]);
  648. break;
  649. case 4:
  650. case GL_RGBA:
  651. case GL_RGBA4:
  652. case GL_RGBA8:
  653. g_OpenGLValues->m_d3ddev->ApplyStateBlock(g_OpenGLValues->m_shaders[1][7]);
  654. break;
  655. case GL_ALPHA:
  656. case GL_ALPHA8:
  657. g_OpenGLValues->m_d3ddev->ApplyStateBlock(g_OpenGLValues->m_shaders[1][9]);
  658. break;
  659. }
  660. break;
  661. }
  662. }
  663. g_OpenGLValues->m_texHandleValid = TRUE;
  664. }
  665. }
  666. }
  667. void RawToCanon(DWORD dwFormat, DWORD dwInternalFormat, DWORD dwWidth, DWORD dwHeight, const void *lpPixels, DWORD *lpdwCanon)
  668. {
  669. #if GETPARMSFORDEBUG
  670. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "RawToCanon: 0x%X 0x%X 0x%X 0x%X 0x%X 0x%X", dwFormat, dwInternalFormat, dwWidth, dwHeight, lpPixels, lpdwCanon );
  671. #endif
  672. if(dwFormat == GL_RGBA)
  673. {
  674. switch(dwInternalFormat) {
  675. case 1:
  676. case GL_LUMINANCE:
  677. case GL_LUMINANCE8:
  678. case 3:
  679. case 4:
  680. case GL_RGB:
  681. case GL_RGBA:
  682. case GL_RGB5:
  683. case GL_RGB8:
  684. case GL_RGBA4:
  685. case GL_RGBA8:
  686. case GL_ALPHA:
  687. case GL_ALPHA8:
  688. MEMCPY(lpdwCanon, lpPixels, dwWidth * dwHeight * sizeof(DWORD));
  689. break;
  690. #if DODPFS
  691. default:
  692. char junk[256];
  693. sprintf( junk, "Wrapper: (RawToCanon:GL_RGBA) InternalFormat not implemented (dwInternalFormat:0x%X)\n", dwInternalFormat );
  694. OutputDebugStringA( junk );
  695. #endif
  696. }
  697. }
  698. else if(dwFormat == GL_RGB)
  699. {
  700. switch(dwInternalFormat) {
  701. case 1:
  702. case GL_LUMINANCE:
  703. case GL_LUMINANCE8:
  704. case 3:
  705. case 4:
  706. case GL_RGB:
  707. case GL_RGBA:
  708. case GL_RGB5:
  709. case GL_RGB8:
  710. case GL_RGBA4:
  711. case GL_RGBA8:
  712. {
  713. int i, j = dwWidth * dwHeight;
  714. unsigned char *pixels = (unsigned char*)lpPixels;
  715. for(i = 0; i < j; ++i) {
  716. lpdwCanon[i] = (unsigned(pixels[2]) << 16) | (unsigned(pixels[1]) << 8) | unsigned(pixels[0]);
  717. pixels += 3;
  718. }
  719. }
  720. break;
  721. #if DODPFS
  722. default:
  723. char junk[256];
  724. sprintf( junk, "Wrapper: (RawToCanon:GL_RGB) InternalFormat not implemented (dwInternalFormat:0x%X)\n", dwInternalFormat );
  725. OutputDebugStringA( junk );
  726. #endif
  727. }
  728. }
  729. else if(dwFormat == GL_LUMINANCE)
  730. {
  731. switch(dwInternalFormat) {
  732. case 1:
  733. case GL_LUMINANCE:
  734. case GL_LUMINANCE8:
  735. {
  736. int i, j = dwWidth * dwHeight;
  737. for(i = 0; i < j; ++i) {
  738. DWORD t = ((UCHAR*)lpPixels)[i];
  739. lpdwCanon[i] = (t << 24) | (t << 16) | (t << 8) | t;
  740. }
  741. }
  742. break;
  743. #if DODPFS
  744. default:
  745. char junk[256];
  746. sprintf( junk, "Wrapper: (RawToCanon:GL_LUMINANCE) InternalFormat not implemented (dwInternalFormat:0x%X)\n", dwInternalFormat );
  747. OutputDebugStringA( junk );
  748. #endif
  749. }
  750. }
  751. else if(dwFormat == GL_ALPHA || dwFormat == GL_ALPHA8)
  752. {
  753. switch(dwInternalFormat) {
  754. case GL_ALPHA:
  755. case GL_ALPHA8:
  756. {
  757. int i, j = dwWidth * dwHeight;
  758. for(i = 0; i < j; ++i) {
  759. DWORD t = ((UCHAR*)lpPixels)[i];
  760. lpdwCanon[i] = (t << 24) | (t << 16) | (t << 8) | t;
  761. }
  762. }
  763. break;
  764. #if DODPFS
  765. default:
  766. char junk[256];
  767. sprintf( junk, "Wrapper: (RawToCanon:GL_ALPHA) InternalFormat not implemented (dwInternalFormat:0x%X)\n", dwInternalFormat );
  768. OutputDebugStringA( junk );
  769. #endif
  770. }
  771. }
  772. #if DODPFS
  773. else
  774. {
  775. char junk[256];
  776. sprintf( junk, "Wrapper: Format not implemented (dwFormat:0x%X dwInternalFormat:0x%X)\n", dwFormat, dwInternalFormat );
  777. OutputDebugStringA( junk );
  778. }
  779. #endif
  780. }
  781. void Resize(DWORD dwWidth, DWORD dwHeight, const DWORD *lpdwCanon,
  782. DWORD dwNewWidth, DWORD dwNewHeight, DWORD *lpdwNewCanon)
  783. {
  784. #if GETPARMSFORDEBUG
  785. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "Resize: 0x%X 0x%X 0x%X 0x%X 0x%X 0x%X", dwWidth, dwHeight, lpdwCanon, dwNewWidth, dwNewHeight, lpdwNewCanon );
  786. #endif
  787. DWORD i, j;
  788. double rx = (double)dwWidth / (double)dwNewWidth;
  789. double ry = (double)dwHeight / (double)dwNewHeight;
  790. for(i = 0; i < dwNewHeight; ++i)
  791. for(j = 0; j < dwNewWidth; ++j)
  792. lpdwNewCanon[i * dwNewWidth + j] = lpdwCanon[((DWORD)(i * ry)) * dwWidth + (DWORD)(j * rx)];
  793. }
  794. void CanonTo565(LPRECT lprect, const DWORD *lpdwCanon, D3DLOCKED_RECT* lpddsd)
  795. {
  796. #if GETPARMSFORDEBUG
  797. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "CanonTo565: 0x%X 0x%X 0x%X", lprect, lpdwCanon, lpddsd );
  798. #endif
  799. LONG i, j, k, l;
  800. USHORT *lpPixels = (USHORT*)lpddsd->pBits;
  801. for(k = lprect->top, i = 0; k < lprect->bottom; ++k, lpPixels = (USHORT*)((UCHAR*)lpPixels + lpddsd->Pitch) )
  802. {
  803. for (j = lprect->left, l = 0; j < lprect->right; ++j, ++i, ++l)
  804. {
  805. lpPixels[l] = (USHORT)(((lpdwCanon[i] & 0xF8) << 8) | ((lpdwCanon[i] & 0xFC00) >> 5) | ((lpdwCanon[i] & 0xF80000) >> 19));
  806. }
  807. }
  808. }
  809. void CanonTo555(LPRECT lprect, const DWORD *lpdwCanon, D3DLOCKED_RECT* lpddsd)
  810. {
  811. #if GETPARMSFORDEBUG
  812. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "CanonTo555: 0x%X 0x%X 0x%X", lprect, lpdwCanon, lpddsd );
  813. #endif
  814. LONG i, j, k, l;
  815. USHORT *lpPixels = (USHORT*)lpddsd->pBits;
  816. for(k = lprect->top, i = 0; k < lprect->bottom; ++k, lpPixels = (USHORT*)((UCHAR*)lpPixels + lpddsd->Pitch) )
  817. {
  818. for (j = lprect->left, l = 0; j < lprect->right; ++j, ++i, ++l)
  819. {
  820. lpPixels[l] = (USHORT)(((lpdwCanon[i] & 0xF8) << 7) | ((lpdwCanon[i] & 0xF800) >> 6) | ((lpdwCanon[i] & 0xF80000) >> 19));
  821. }
  822. }
  823. }
  824. void CanonTo4444(LPRECT lprect, const DWORD *lpdwCanon, D3DLOCKED_RECT* lpddsd)
  825. {
  826. #if GETPARMSFORDEBUG
  827. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "CanonTo4444: 0x%X 0x%X 0x%X", lprect, lpdwCanon, lpddsd );
  828. #endif
  829. LONG i, j, k, l;
  830. USHORT *lpPixels = (USHORT*)lpddsd->pBits;
  831. for(k = lprect->top, i = 0; k < lprect->bottom; ++k, lpPixels = (USHORT*)((UCHAR*)lpPixels + lpddsd->Pitch) )
  832. {
  833. for (j = lprect->left, l = 0; j < lprect->right; ++j, ++i, ++l)
  834. {
  835. lpPixels[l] = (USHORT)(((lpdwCanon[i] & 0xF0) << 4) | ((lpdwCanon[i] & 0xF000) >> 8) | ((lpdwCanon[i] & 0xF00000) >> 20) | ((lpdwCanon[i] & 0xF0000000) >> 16));
  836. }
  837. }
  838. }
  839. void CanonTo8888(LPRECT lprect, const DWORD *lpdwCanon, D3DLOCKED_RECT* lpddsd)
  840. {
  841. #if GETPARMSFORDEBUG
  842. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "CanonTo8888: 0x%X 0x%X 0x%X", lprect, lpdwCanon, lpddsd );
  843. #endif
  844. LONG i, j, k, l;
  845. DWORD *lpPixels = (DWORD*)lpddsd->pBits;
  846. for(k = lprect->top, i = 0; k < lprect->bottom; ++k, lpPixels = (DWORD*)((UCHAR*)lpPixels + lpddsd->Pitch) )
  847. {
  848. for (j = lprect->left, l = 0; j < lprect->right; ++j, ++i, ++l)
  849. {
  850. lpPixels[l] = ((lpdwCanon[i] & 0xFF00FF00) | ((lpdwCanon[i] & 0xFF) << 16) | ((lpdwCanon[i] & 0xFF0000) >> 16));
  851. }
  852. }
  853. }
  854. void CanonTo8(LPRECT lprect, const DWORD *lpdwCanon, D3DLOCKED_RECT* lpddsd)
  855. {
  856. #if GETPARMSFORDEBUG
  857. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "CanonTo8: 0x%X 0x%X 0x%X", lprect, lpdwCanon, lpddsd );
  858. #endif
  859. LONG i, j, k, l;
  860. UCHAR *lpPixels = (UCHAR*)lpddsd->pBits;
  861. for(k = lprect->top, i = 0; k < lprect->bottom; ++k, lpPixels = (UCHAR*)lpPixels + lpddsd->Pitch )
  862. {
  863. for (j = lprect->left, l = 0; j < lprect->right; ++j, ++i, ++l)
  864. {
  865. lpPixels[l] = (UCHAR)(lpdwCanon[i] >> 24);
  866. }
  867. }
  868. }
  869. void LoadSurface(LPDIRECT3DSURFACE8 lpDDS, DWORD dwFormat, DWORD dwInternalFormat,
  870. DWORD dwWidth, DWORD dwHeight, DWORD dwNewWidth, DWORD dwNewHeight,
  871. const void *pixels)
  872. {
  873. #if GETPARMSFORDEBUG
  874. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "LoadSurface: 0x%X 0x%X 0x%X 0x%X 0x%X 0x%X 0x%X 0x%X", lpDDS, dwFormat, dwInternalFormat, dwWidth, dwHeight, dwNewWidth, dwNewHeight, pixels );
  875. #endif
  876. D3DLOCKED_RECT ddsd;
  877. HRESULT ddrval;
  878. DWORD *lpdwCanon, *lpdwNewCanon;
  879. RECT rect;
  880. /*
  881. * Convert the GL texture into a canonical format (8888),
  882. * so that we can cleanly do image ops (such as resize) without
  883. * having to worry about the bit format.
  884. */
  885. lpdwCanon = (DWORD*)malloc(dwWidth * dwHeight * sizeof(DWORD));
  886. if(lpdwCanon != NULL)
  887. {
  888. RawToCanon(dwFormat, dwInternalFormat, dwWidth, dwHeight, pixels, lpdwCanon);
  889. /* Now resize the canon image */
  890. if(dwWidth != dwNewWidth || dwHeight != dwNewHeight) {
  891. lpdwNewCanon = (DWORD*)malloc(dwNewWidth * dwNewHeight * sizeof(DWORD));
  892. if(lpdwNewCanon != NULL)
  893. {
  894. Resize(dwWidth, dwHeight, lpdwCanon, dwNewWidth, dwNewHeight, lpdwNewCanon);
  895. free(lpdwCanon);
  896. }
  897. else
  898. {
  899. lpdwNewCanon = lpdwCanon;
  900. }
  901. }
  902. else
  903. lpdwNewCanon = lpdwCanon;
  904. /*
  905. * Lock the surface so it can be filled with the texture
  906. */
  907. ddrval = lpDDS->LockRect(&ddsd, NULL, D3DLOCK_NOSYSLOCK);
  908. if (FAILED(ddrval)) {
  909. #if DODPFS
  910. char junk[256];
  911. sprintf( junk, "Lock failed while loading surface (0x%X)\n", ddrval );
  912. OutputDebugStringA( junk );
  913. #endif
  914. free(lpdwNewCanon);
  915. return;
  916. }
  917. D3DSURFACE_DESC sd;
  918. lpDDS->GetDesc(&sd);
  919. SetRect(&rect, 0, 0, sd.Width, sd.Height);
  920. /* Copy the texture into the surface */
  921. if(sd.Format == D3DFMT_L8) {
  922. CanonTo8(&rect, lpdwNewCanon, &ddsd);
  923. }
  924. else if(sd.Format == D3DFMT_A8) {
  925. CanonTo8(&rect, lpdwNewCanon, &ddsd);
  926. }
  927. else if(sd.Format == D3DFMT_A4R4G4B4) {
  928. CanonTo4444(&rect, lpdwNewCanon, &ddsd);
  929. }
  930. else if(sd.Format == D3DFMT_R5G6B5) {
  931. CanonTo565(&rect, lpdwNewCanon, &ddsd);
  932. }
  933. else if(sd.Format == D3DFMT_X1R5G5B5) {
  934. CanonTo555(&rect, lpdwNewCanon, &ddsd);
  935. }
  936. else {
  937. CanonTo8888(&rect, lpdwNewCanon, &ddsd);
  938. }
  939. free(lpdwNewCanon);
  940. /*
  941. * unlock the surface
  942. */
  943. lpDDS->UnlockRect();
  944. }
  945. return;
  946. }
  947. HRESULT LoadSubSurface(LPDIRECT3DSURFACE8 lpDDS, DWORD dwFormat,
  948. DWORD dwInternalFormat, DWORD dwWidth, DWORD dwHeight,
  949. const void *pixels, LPRECT lpsubimage)
  950. {
  951. #if GETPARMSFORDEBUG
  952. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "LoadSubSurface: 0x%X 0x%X 0x%X 0x%X 0x%X 0x%X 0x%X", lpDDS, dwFormat, dwInternalFormat, dwWidth, dwHeight, pixels, lpsubimage );
  953. #endif
  954. D3DLOCKED_RECT ddsd;
  955. HRESULT ddrval;
  956. DWORD *lpdwCanon, *lpdwNewCanon;
  957. DWORD dwNewWidth=lpsubimage->right-lpsubimage->left;
  958. DWORD dwNewHeight=lpsubimage->bottom-lpsubimage->top;
  959. /*
  960. * Lock the surface so it can be filled with the texture
  961. */
  962. ddrval = lpDDS->LockRect(&ddsd, lpsubimage, D3DLOCK_NOSYSLOCK);
  963. if (FAILED(ddrval)) {
  964. #if DODPFS
  965. char junk[256];
  966. sprintf( junk, "Lock failed while loading surface (0x%X)\n", ddrval );
  967. OutputDebugStringA( junk );
  968. #endif
  969. return ddrval;
  970. }
  971. D3DSURFACE_DESC sd;
  972. lpDDS->GetDesc(&sd);
  973. if((dwInternalFormat == 3 || dwInternalFormat == GL_RGB || dwInternalFormat == GL_RGB5) && sd.Format == D3DFMT_R5G6B5 &&
  974. dwWidth == dwNewWidth && dwHeight == dwNewHeight) {
  975. CanonTo565(lpsubimage,(const unsigned long*)pixels,&ddsd);
  976. }
  977. else if((dwInternalFormat == 3 || dwInternalFormat == GL_RGB || dwInternalFormat == GL_RGB5) && sd.Format == D3DFMT_X1R5G5B5 &&
  978. dwWidth == dwNewWidth && dwHeight == dwNewHeight) {
  979. CanonTo555(lpsubimage,(const unsigned long*)pixels,&ddsd);
  980. }
  981. else if((dwInternalFormat == GL_RGB8 || dwInternalFormat == GL_RGBA || dwInternalFormat == GL_RGBA8) && (sd.Format == D3DFMT_X8R8G8B8 || sd.Format == D3DFMT_A8R8G8B8) &&
  982. dwWidth == dwNewWidth && dwHeight == dwNewHeight) {
  983. CanonTo8888(lpsubimage,(const unsigned long*)pixels,&ddsd);
  984. }
  985. else {
  986. /*
  987. * Convert the GL texture into a canonical format (8888),
  988. * so that we can cleanly do image ops (such as resize) without
  989. * having to worry about the bit format.
  990. */
  991. lpdwCanon = (DWORD*)malloc(dwWidth * dwHeight * sizeof(DWORD));
  992. if(lpdwCanon != NULL)
  993. {
  994. RawToCanon(dwFormat, dwInternalFormat, dwWidth, dwHeight, pixels, lpdwCanon);
  995. if(dwWidth != dwNewWidth || dwHeight != dwNewHeight)
  996. {
  997. /* Now resize the canon image */
  998. lpdwNewCanon = (DWORD*)malloc(dwNewWidth * dwNewHeight * sizeof(DWORD));
  999. if(lpdwNewCanon != NULL)
  1000. {
  1001. Resize(dwWidth, dwHeight, lpdwCanon, dwNewWidth, dwNewHeight, lpdwNewCanon);
  1002. free(lpdwCanon);
  1003. }
  1004. else
  1005. {
  1006. lpdwNewCanon=lpdwCanon;
  1007. }
  1008. }
  1009. else
  1010. {
  1011. lpdwNewCanon=lpdwCanon;
  1012. }
  1013. /* Copy the texture into the surface */
  1014. if(sd.Format == D3DFMT_L8) {
  1015. CanonTo8(lpsubimage,lpdwNewCanon,&ddsd);
  1016. }
  1017. else if(sd.Format == D3DFMT_A8) {
  1018. CanonTo8(lpsubimage,lpdwNewCanon,&ddsd);
  1019. }
  1020. else if(sd.Format == D3DFMT_A4R4G4B4) {
  1021. CanonTo4444(lpsubimage,lpdwNewCanon,&ddsd);
  1022. }
  1023. else if(sd.Format == D3DFMT_R5G6B5) {
  1024. CanonTo565(lpsubimage,lpdwNewCanon,&ddsd);
  1025. }
  1026. else if(sd.Format == D3DFMT_X1R5G5B5) {
  1027. CanonTo555(lpsubimage, lpdwNewCanon, &ddsd);
  1028. }
  1029. else {
  1030. CanonTo8888(lpsubimage, lpdwNewCanon, &ddsd);
  1031. }
  1032. free(lpdwNewCanon);
  1033. }
  1034. }
  1035. /*
  1036. * unlock the surface
  1037. */
  1038. lpDDS->UnlockRect();
  1039. return S_OK;
  1040. }
  1041. HRESULT GrowVB(DWORD sz)
  1042. {
  1043. #if GETPARMSFORDEBUG
  1044. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "GrowVB: 0x%X", sz );
  1045. #endif
  1046. if(sz > g_OpenGLValues->m_vbufsz)
  1047. {
  1048. HRESULT hr;
  1049. if(g_OpenGLValues->m_xyzbuf != 0)
  1050. {
  1051. g_OpenGLValues->m_xyzbuf->Release();
  1052. }
  1053. hr = g_OpenGLValues->m_d3ddev->CreateVertexBuffer(sz * 12, D3DUSAGE_WRITEONLY | D3DUSAGE_DYNAMIC, 0, D3DPOOL_DEFAULT, &g_OpenGLValues->m_xyzbuf);
  1054. if( FAILED(hr) )
  1055. {
  1056. #if DODPFS
  1057. char junk[256];
  1058. sprintf( junk, "Wrapper: GrowVB: CreateVertexBuffer(1) failed (0x%X)\n", hr );
  1059. OutputDebugStringA( junk );
  1060. #endif
  1061. return hr;
  1062. }
  1063. if(g_OpenGLValues->m_colbuf != 0)
  1064. {
  1065. g_OpenGLValues->m_colbuf->Release();
  1066. }
  1067. hr = g_OpenGLValues->m_d3ddev->CreateVertexBuffer(sz * 4, D3DUSAGE_WRITEONLY | D3DUSAGE_DYNAMIC, 0, D3DPOOL_DEFAULT, &g_OpenGLValues->m_colbuf);
  1068. if( FAILED(hr) )
  1069. {
  1070. #if DODPFS
  1071. char junk[256];
  1072. sprintf( junk, "Wrapper: GrowVB: CreateVertexBuffer(2) failed (0x%X)\n", hr );
  1073. OutputDebugStringA( junk );
  1074. #endif
  1075. return hr;
  1076. }
  1077. if(g_OpenGLValues->m_texbuf != 0)
  1078. {
  1079. g_OpenGLValues->m_texbuf->Release();
  1080. }
  1081. hr = g_OpenGLValues->m_d3ddev->CreateVertexBuffer(sz * 8, D3DUSAGE_WRITEONLY | D3DUSAGE_DYNAMIC, 0, D3DPOOL_DEFAULT, &g_OpenGLValues->m_texbuf);
  1082. if( FAILED(hr) )
  1083. {
  1084. #if DODPFS
  1085. char junk[256];
  1086. sprintf( junk, "Wrapper: GrowVB: CreateVertexBuffer(3) failed (0x%X)\n", hr );
  1087. OutputDebugStringA( junk );
  1088. #endif
  1089. return hr;
  1090. }
  1091. if(g_OpenGLValues->m_tex2buf != 0)
  1092. {
  1093. g_OpenGLValues->m_tex2buf->Release();
  1094. }
  1095. hr = g_OpenGLValues->m_d3ddev->CreateVertexBuffer(sz * 8, D3DUSAGE_WRITEONLY | D3DUSAGE_DYNAMIC, 0, D3DPOOL_DEFAULT, &g_OpenGLValues->m_tex2buf);
  1096. if( FAILED(hr) )
  1097. {
  1098. #if DODPFS
  1099. char junk[256];
  1100. sprintf( junk, "Wrapper: GrowVB: CreateVertexBuffer(4) failed (0x%X)\n", hr );
  1101. OutputDebugStringA( junk );
  1102. #endif
  1103. return hr;
  1104. }
  1105. g_OpenGLValues->m_vbufoff = 0;
  1106. g_OpenGLValues->m_vbufsz = sz;
  1107. }
  1108. return S_OK;
  1109. }
  1110. HRESULT GrowIB(DWORD sz)
  1111. {
  1112. #if GETPARMSFORDEBUG
  1113. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "GrowIB: 0x%X", sz );
  1114. #endif
  1115. if(sz > g_OpenGLValues->m_ibufsz)
  1116. {
  1117. if(g_OpenGLValues->m_ibuf != 0)
  1118. {
  1119. g_OpenGLValues->m_ibuf->Release();
  1120. }
  1121. HRESULT hr = g_OpenGLValues->m_d3ddev->CreateIndexBuffer(sz * 2, D3DUSAGE_WRITEONLY | D3DUSAGE_DYNAMIC, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &g_OpenGLValues->m_ibuf);
  1122. if( FAILED(hr) )
  1123. {
  1124. #if DODPFS
  1125. char junk[256];
  1126. sprintf( junk, "Wrapper: GrowIB: CreateIndexBuffer failed (0x%X)\n", hr );
  1127. OutputDebugStringA( junk );
  1128. #endif
  1129. return hr;
  1130. }
  1131. g_OpenGLValues->m_d3ddev->SetIndices(g_OpenGLValues->m_ibuf, 0);
  1132. g_OpenGLValues->m_ibufoff = 0;
  1133. g_OpenGLValues->m_ibufsz = sz;
  1134. }
  1135. return S_OK;
  1136. }
  1137. ///////////////////////////// BEGIN API ENTRIES ///////////////////////////////////////////////////
  1138. void APIENTRY glActiveTextureARB(GLenum texture)
  1139. {
  1140. #if GETPARMSFORDEBUG
  1141. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glActiveTextureARB: 0x%X", texture );
  1142. #endif
  1143. g_OpenGLValues->m_curtgt = texture == GL_TEXTURE0_ARB ? 0 : 1;
  1144. }
  1145. void APIENTRY glAlphaFunc (GLenum func, GLclampf ref)
  1146. {
  1147. #if GETPARMSFORDEBUG
  1148. char log[256];
  1149. char l[40];
  1150. ftoa( (double)ref, l );
  1151. sprintf( log, "glAlphaFunc: 0x%X %s", func, l );
  1152. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, log );
  1153. #endif
  1154. DWORD funcvalue;
  1155. switch(func) {
  1156. case GL_NEVER:
  1157. funcvalue=D3DCMP_NEVER;
  1158. break;
  1159. case GL_LESS:
  1160. funcvalue=D3DCMP_LESS;
  1161. break;
  1162. case GL_EQUAL:
  1163. funcvalue=D3DCMP_EQUAL;
  1164. break;
  1165. case GL_LEQUAL:
  1166. funcvalue=D3DCMP_LESSEQUAL;
  1167. break;
  1168. case GL_GREATER:
  1169. funcvalue=D3DCMP_GREATER;
  1170. break;
  1171. case GL_NOTEQUAL:
  1172. funcvalue=D3DCMP_NOTEQUAL;
  1173. break;
  1174. case GL_GEQUAL:
  1175. funcvalue=D3DCMP_GREATEREQUAL;
  1176. break;
  1177. case GL_ALWAYS:
  1178. funcvalue=D3DCMP_ALWAYS;
  1179. break;
  1180. }
  1181. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_ALPHAFUNC, funcvalue);
  1182. Clamp(&ref);
  1183. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_ALPHAREF, (DWORD)(ref * 255.f));
  1184. }
  1185. void APIENTRY glArrayElement (GLint i)
  1186. {
  1187. #if GETPARMSFORDEBUG
  1188. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glArrayElement: 0x%X", i );
  1189. #endif
  1190. if(g_OpenGLValues->m_usetexcoordary[0])
  1191. {
  1192. g_OpenGLValues->m_tu = *((FLOAT*)((BYTE*)g_OpenGLValues->m_texcoordary[0] + i * g_OpenGLValues->m_texcoordarystride[0]));
  1193. g_OpenGLValues->m_tv = *((FLOAT*)((BYTE*)g_OpenGLValues->m_texcoordary[0] + i * g_OpenGLValues->m_texcoordarystride[0]) + 1);
  1194. }
  1195. if(g_OpenGLValues->m_usetexcoordary[1])
  1196. {
  1197. g_OpenGLValues->m_tu2 = *((FLOAT*)((BYTE*)g_OpenGLValues->m_texcoordary[1] + i * g_OpenGLValues->m_texcoordarystride[1]));
  1198. g_OpenGLValues->m_tv2 = *((FLOAT*)((BYTE*)g_OpenGLValues->m_texcoordary[1] + i * g_OpenGLValues->m_texcoordarystride[1]) + 1);
  1199. }
  1200. if(g_OpenGLValues->m_usecolorary)
  1201. {
  1202. #ifdef _X86_
  1203. const void * colorary = g_OpenGLValues->m_colorary;
  1204. DWORD colorarystride = g_OpenGLValues->m_colorarystride;
  1205. D3DCOLOR color;
  1206. _asm
  1207. {
  1208. mov eax, i;
  1209. mov ebx, colorary;
  1210. mul colorarystride;
  1211. mov edx, 0x00FF00FF;
  1212. add eax, ebx;
  1213. mov ecx, eax;
  1214. and eax, edx;
  1215. not edx;
  1216. rol eax, 16;
  1217. and ecx, edx;
  1218. or eax, ecx;
  1219. mov color, eax;
  1220. }
  1221. g_OpenGLValues->m_color = color;
  1222. #else
  1223. BYTE *glcolor = (BYTE*)g_OpenGLValues->m_colorary + i * g_OpenGLValues->m_colorarystride;
  1224. g_OpenGLValues->m_color = RGBA_MAKE(glcolor[0], glcolor[1], glcolor[2], glcolor[3]);
  1225. #endif
  1226. }
  1227. VertexBufferFilled();
  1228. FLOAT *d3dv = (FLOAT*)&g_OpenGLValues->m_verts[g_OpenGLValues->m_vcnt++];
  1229. *(d3dv++) = *((FLOAT*)((BYTE*)g_OpenGLValues->m_vertexary + i * g_OpenGLValues->m_vertexarystride));
  1230. *(d3dv++) = *((FLOAT*)((BYTE*)g_OpenGLValues->m_vertexary + i * g_OpenGLValues->m_vertexarystride) + 1);
  1231. *(d3dv++) = *((FLOAT*)((BYTE*)g_OpenGLValues->m_vertexary + i * g_OpenGLValues->m_vertexarystride) + 2);
  1232. MEMCPY(d3dv, &g_OpenGLValues->m_nx, sizeof(FLOAT) * 7 + sizeof(D3DCOLOR));
  1233. }
  1234. void APIENTRY glBegin (GLenum mode)
  1235. {
  1236. #if GETPARMSFORDEBUG
  1237. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glBegin: 0x%X", mode );
  1238. #endif
  1239. g_OpenGLValues->m_prim = mode;
  1240. g_OpenGLValues->m_withinprim = TRUE;
  1241. g_OpenGLValues->m_vcnt = 0;
  1242. QuakeSetTexturingState();
  1243. if(g_OpenGLValues->m_nfv > (VBUFSIZE - MAXVERTSPERPRIM)) // check if space available
  1244. {
  1245. g_OpenGLValues->m_vbuf->Lock(0, 0, (BYTE**)&g_OpenGLValues->m_verts, D3DLOCK_DISCARD | D3DLOCK_NOSYSLOCK);
  1246. g_OpenGLValues->m_nfv = 0;
  1247. }
  1248. else
  1249. {
  1250. g_OpenGLValues->m_vbuf->Lock(0, 0, (BYTE**)&g_OpenGLValues->m_verts, D3DLOCK_NOOVERWRITE | D3DLOCK_NOSYSLOCK);
  1251. g_OpenGLValues->m_verts = &g_OpenGLValues->m_verts[g_OpenGLValues->m_nfv];
  1252. }
  1253. }
  1254. void APIENTRY glBindTexture (GLenum target, GLuint texture)
  1255. {
  1256. #if GETPARMSFORDEBUG
  1257. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glBindTexture: 0x%X 0x%X", target, texture );
  1258. #endif
  1259. g_OpenGLValues->m_curstagebinding[g_OpenGLValues->m_curtgt] = texture;
  1260. g_OpenGLValues->m_texHandleValid = FALSE;
  1261. }
  1262. void APIENTRY glBlendFunc (GLenum sfactor, GLenum dfactor)
  1263. {
  1264. #if GETPARMSFORDEBUG
  1265. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glBlendFunc: 0x%X 0x%X", sfactor, dfactor );
  1266. #endif
  1267. int svalue = -1, dvalue = -1;
  1268. switch(sfactor) {
  1269. case GL_ZERO:
  1270. svalue=D3DBLEND_ZERO;
  1271. break;
  1272. case GL_ONE:
  1273. svalue=D3DBLEND_ONE;
  1274. break;
  1275. case GL_DST_COLOR:
  1276. svalue=D3DBLEND_DESTCOLOR;
  1277. break;
  1278. case GL_ONE_MINUS_DST_COLOR:
  1279. svalue=D3DBLEND_INVDESTCOLOR;
  1280. break;
  1281. case GL_SRC_ALPHA:
  1282. svalue=D3DBLEND_SRCALPHA;
  1283. break;
  1284. case GL_ONE_MINUS_SRC_ALPHA:
  1285. svalue=D3DBLEND_INVSRCALPHA;
  1286. break;
  1287. case GL_DST_ALPHA:
  1288. svalue=D3DBLEND_DESTALPHA;
  1289. break;
  1290. case GL_ONE_MINUS_DST_ALPHA:
  1291. svalue=D3DBLEND_INVDESTALPHA;
  1292. break;
  1293. case GL_SRC_ALPHA_SATURATE:
  1294. svalue=D3DBLEND_SRCALPHASAT;
  1295. break;
  1296. }
  1297. switch(dfactor) {
  1298. case GL_ZERO:
  1299. dvalue=D3DBLEND_ZERO;
  1300. break;
  1301. case GL_ONE:
  1302. dvalue=D3DBLEND_ONE;
  1303. break;
  1304. case GL_SRC_COLOR:
  1305. dvalue=D3DBLEND_SRCCOLOR;
  1306. break;
  1307. case GL_ONE_MINUS_SRC_COLOR:
  1308. dvalue=D3DBLEND_INVSRCCOLOR;
  1309. break;
  1310. case GL_SRC_ALPHA:
  1311. dvalue=D3DBLEND_SRCALPHA;
  1312. break;
  1313. case GL_ONE_MINUS_SRC_ALPHA:
  1314. dvalue=D3DBLEND_INVSRCALPHA;
  1315. break;
  1316. case GL_DST_ALPHA:
  1317. dvalue=D3DBLEND_DESTALPHA;
  1318. break;
  1319. case GL_ONE_MINUS_DST_ALPHA:
  1320. dvalue=D3DBLEND_INVDESTALPHA;
  1321. break;
  1322. }
  1323. if (svalue >= 0) g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_SRCBLEND, (DWORD)svalue);
  1324. if (dvalue >= 0) g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_DESTBLEND, (DWORD)dvalue);
  1325. }
  1326. void APIENTRY glClearStencil (GLint s)
  1327. {
  1328. #if GETPARMSFORDEBUG
  1329. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glClearStencil: 0x%X", s );
  1330. #endif
  1331. g_OpenGLValues->m_clearStencil = (DWORD)s;
  1332. }
  1333. void APIENTRY glClear (GLbitfield mask)
  1334. {
  1335. #if GETPARMSFORDEBUG
  1336. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glClear: 0x%X", mask );
  1337. #endif
  1338. DWORD flags = 0;
  1339. if(mask & GL_COLOR_BUFFER_BIT) {
  1340. flags |= D3DCLEAR_TARGET;
  1341. }
  1342. if(mask & GL_DEPTH_BUFFER_BIT) {
  1343. flags |= D3DCLEAR_ZBUFFER;
  1344. }
  1345. if(mask & GL_STENCIL_BUFFER_BIT) {
  1346. flags |= D3DCLEAR_STENCIL;
  1347. }
  1348. if(g_OpenGLValues->m_updvwp)
  1349. QuakeUpdateViewport();
  1350. g_OpenGLValues->m_d3ddev->Clear(0, NULL, flags, g_OpenGLValues->m_clearColor, (FLOAT)g_OpenGLValues->m_clearDepth, g_OpenGLValues->m_clearStencil);
  1351. }
  1352. void APIENTRY glClearColor (GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha)
  1353. {
  1354. #if GETPARMSFORDEBUG
  1355. char log[256];
  1356. char r[40];
  1357. char g[40];
  1358. char b[40];
  1359. char a[40];
  1360. ftoa( (double)red, r );
  1361. ftoa( (double)green, g );
  1362. ftoa( (double)blue, b );
  1363. ftoa( (double)alpha, a );
  1364. sprintf( log, "glClearColor: %s %s %s %s", r, g, b, a );
  1365. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, log );
  1366. #endif
  1367. Clamp(&red);
  1368. Clamp(&green);
  1369. Clamp(&blue);
  1370. Clamp(&alpha);
  1371. unsigned int R, G, B, A;
  1372. R = (unsigned int)(red * 255.f);
  1373. G = (unsigned int)(green * 255.f);
  1374. B = (unsigned int)(blue * 255.f);
  1375. A = (unsigned int)(alpha * 255.f);
  1376. g_OpenGLValues->m_clearColor = RGBA_MAKE(R, G, B, A);
  1377. }
  1378. void APIENTRY glClearDepth (GLclampd depth)
  1379. {
  1380. #if GETPARMSFORDEBUG
  1381. char log[256];
  1382. char d[40];
  1383. ftoa( (double)depth, d );
  1384. sprintf( log, "glClearDepth: %s", d );
  1385. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, log );
  1386. #endif
  1387. Clamp(&depth);
  1388. g_OpenGLValues->m_clearDepth = depth;
  1389. }
  1390. void APIENTRY glClientActiveTextureARB(GLenum texture)
  1391. {
  1392. #if GETPARMSFORDEBUG
  1393. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glClientActiveTextureARB: 0x%X", texture );
  1394. #endif
  1395. g_OpenGLValues->m_client_active_texture_arb = texture == GL_TEXTURE0_ARB ? 0 : 1;
  1396. }
  1397. void APIENTRY glClipPlane (GLenum plane, const GLdouble *equation)
  1398. {
  1399. #if GETPARMSFORDEBUG
  1400. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glClipPlane: 0x%X 0x%X", plane, equation );
  1401. #endif
  1402. D3DMATRIX &m = g_OpenGLValues->m_xfrm[D3DTS_WORLD];
  1403. m.m[0][3] = -(m.m[0][0] * m.m[3][0] + m.m[0][1] * m.m[3][1] + m.m[0][2] * m.m[3][2]);
  1404. m.m[1][3] = -(m.m[1][0] * m.m[3][0] + m.m[1][1] * m.m[3][1] + m.m[1][2] * m.m[3][2]);
  1405. m.m[2][3] = -(m.m[2][0] * m.m[3][0] + m.m[2][1] * m.m[3][1] + m.m[2][2] * m.m[3][2]);
  1406. m.m[3][0] = 0.f; m.m[3][1] = 0.f; m.m[3][2] = 0.f;
  1407. FLOAT eqn[4];
  1408. eqn[0] = FLOAT(m.m[0][0] * equation[0] + m.m[1][0] * equation[1] + m.m[2][0] * equation[2] + m.m[3][0] * equation[3]);
  1409. eqn[1] = FLOAT(m.m[0][1] * equation[0] + m.m[1][1] * equation[1] + m.m[2][1] * equation[2] + m.m[3][1] * equation[3]);
  1410. eqn[2] = FLOAT(m.m[0][2] * equation[0] + m.m[1][2] * equation[1] + m.m[2][2] * equation[2] + m.m[3][2] * equation[3]);
  1411. eqn[3] = FLOAT(m.m[0][3] * equation[0] + m.m[1][3] * equation[1] + m.m[2][3] * equation[2] + m.m[3][3] * equation[3]);
  1412. g_OpenGLValues->m_d3ddev->SetClipPlane(plane - GL_CLIP_PLANE0, eqn);
  1413. }
  1414. void APIENTRY glColor3f (GLfloat red, GLfloat green, GLfloat blue)
  1415. {
  1416. #if GETPARMSFORDEBUG
  1417. char log[256];
  1418. char r[40];
  1419. char g[40];
  1420. char b[40];
  1421. ftoa( (double)red, r );
  1422. ftoa( (double)green, g );
  1423. ftoa( (double)blue, b );
  1424. sprintf( log, "glColor3f: %s %s %s", r, g, b );
  1425. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, log );
  1426. #endif
  1427. static float two55 = 255.f;
  1428. unsigned int R, G, B;
  1429. #ifdef _X86_
  1430. D3DCOLOR color;
  1431. _asm {
  1432. fld red;
  1433. fld green;
  1434. fld blue;
  1435. fld two55;
  1436. fmul st(1), st(0);
  1437. fmul st(2), st(0);
  1438. fmulp st(3), st(0);
  1439. fistp B;
  1440. fistp G;
  1441. fistp R;
  1442. mov eax, B;
  1443. cmp eax, 255;
  1444. jle pt1;
  1445. mov eax, 255;
  1446. pt1: mov ebx, G;
  1447. cmp ebx, 255;
  1448. jle pt2;
  1449. mov ebx, 255;
  1450. pt2: mov ecx, R;
  1451. cmp ecx, 255;
  1452. jle pt3;
  1453. mov ecx, 255;
  1454. pt3: shl ebx, 8;
  1455. shl ecx, 16;
  1456. or eax, ebx;
  1457. or ecx, 0xFF000000;
  1458. or eax, ecx;
  1459. mov color, eax;
  1460. }
  1461. g_OpenGLValues->m_color = color;
  1462. #else
  1463. R = (unsigned int)(red * two55);
  1464. G = (unsigned int)(green * two55);
  1465. B = (unsigned int)(blue * two55);
  1466. if(R > 255)
  1467. R = 255;
  1468. if(G > 255)
  1469. G = 255;
  1470. if(B > 255)
  1471. B = 255;
  1472. g_OpenGLValues->m_color = RGBA_MAKE(R, G, B, 255);
  1473. #endif
  1474. }
  1475. void APIENTRY glColor3fv (const GLfloat *v)
  1476. {
  1477. #if GETPARMSFORDEBUG
  1478. char log[256];
  1479. char v0[40];
  1480. char v1[40];
  1481. char v2[40];
  1482. ftoa( (double)v[0], v0 );
  1483. ftoa( (double)v[1], v1 );
  1484. ftoa( (double)v[2], v2 );
  1485. sprintf( log, "glColor3fv: %s %s %s", v0, v1, v2 );
  1486. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, log );
  1487. #endif
  1488. static float two55 = 255.f;
  1489. unsigned int R, G, B;
  1490. #ifdef _X86_
  1491. D3DCOLOR color;
  1492. _asm {
  1493. mov ebx, v;
  1494. fld [ebx];
  1495. fld [ebx + 4];
  1496. fld [ebx + 8];
  1497. fld two55;
  1498. fmul st(1), st(0);
  1499. fmul st(2), st(0);
  1500. fmulp st(3), st(0);
  1501. fistp B;
  1502. fistp G;
  1503. fistp R;
  1504. mov eax, B;
  1505. cmp eax, 255;
  1506. jle pt1;
  1507. mov eax, 255;
  1508. pt1: mov ebx, G;
  1509. cmp ebx, 255;
  1510. jle pt2;
  1511. mov ebx, 255;
  1512. pt2: mov ecx, R;
  1513. cmp ecx, 255;
  1514. jle pt3;
  1515. mov ecx, 255;
  1516. pt3: shl ebx, 8;
  1517. shl ecx, 16;
  1518. or eax, ebx;
  1519. or ecx, 0xFF000000;
  1520. or eax, ecx;
  1521. mov color, eax;
  1522. }
  1523. g_OpenGLValues->m_color = color;
  1524. #else
  1525. R = (unsigned int)(v[0] * two55);
  1526. G = (unsigned int)(v[1] * two55);
  1527. B = (unsigned int)(v[2] * two55);
  1528. if(R > 255)
  1529. R = 255;
  1530. if(G > 255)
  1531. G = 255;
  1532. if(B > 255)
  1533. B = 255;
  1534. g_OpenGLValues->m_color = RGBA_MAKE(R, G, B, 255);
  1535. #endif
  1536. }
  1537. void APIENTRY glColor3ubv (const GLubyte *v)
  1538. {
  1539. #if GETPARMSFORDEBUG
  1540. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glColor3ubv: 0x%X 0x%X 0x%X", v[0], v[1], v[2] );
  1541. #endif
  1542. g_OpenGLValues->m_color = RGBA_MAKE(v[0], v[1], v[2], 255);
  1543. }
  1544. void APIENTRY glColor4ub (GLubyte red, GLubyte green, GLubyte blue, GLubyte alpha)
  1545. {
  1546. #if GETPARMSFORDEBUG
  1547. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glColor4ub: 0x%X 0x%X 0x%X 0x%X", red, green, blue, alpha );
  1548. #endif
  1549. g_OpenGLValues->m_color = RGBA_MAKE(red, green, blue, alpha);
  1550. }
  1551. void APIENTRY glColor4ubv (const GLubyte *v)
  1552. {
  1553. #if GETPARMSFORDEBUG
  1554. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glColor4ubv: 0x%X 0x%X 0x%X 0x%X", v[0], v[1], v[2], v[3] );
  1555. #endif
  1556. #ifdef _X86_
  1557. D3DCOLOR color;
  1558. _asm
  1559. {
  1560. mov ebx, v;
  1561. mov edx, 0x00FF00FF;
  1562. mov eax, [ebx];
  1563. mov ecx, eax;
  1564. and eax, edx;
  1565. not edx;
  1566. rol eax, 16;
  1567. and ecx, edx;
  1568. or eax, ecx;
  1569. mov color, eax;
  1570. }
  1571. g_OpenGLValues->m_color = color;
  1572. #else
  1573. g_OpenGLValues->m_color = RGBA_MAKE(v[0], v[1], v[2], v[3]);
  1574. #endif
  1575. }
  1576. void APIENTRY glColor4f (GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha)
  1577. {
  1578. #if GETPARMSFORDEBUG
  1579. char log[256];
  1580. char r[40];
  1581. char g[40];
  1582. char b[40];
  1583. char a[40];
  1584. ftoa( (double)red, r );
  1585. ftoa( (double)green, g );
  1586. ftoa( (double)blue, b );
  1587. ftoa( (double)alpha, a );
  1588. sprintf( log, "glColor4f: %s %s %s %s", r, g, b, a );
  1589. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, log );
  1590. #endif
  1591. static float two55 = 255.f;
  1592. unsigned int R, G, B, A;
  1593. #ifdef _X86_
  1594. D3DCOLOR color;
  1595. __asm {
  1596. fld red;
  1597. fld green;
  1598. fld blue;
  1599. fld alpha;
  1600. fld two55;
  1601. fmul st(1), st(0);
  1602. fmul st(2), st(0);
  1603. fmul st(3), st(0);
  1604. fmulp st(4), st(0);
  1605. fistp A;
  1606. fistp B;
  1607. fistp G;
  1608. fistp R;
  1609. mov edx, A;
  1610. cmp edx, 255;
  1611. jle pt1;
  1612. mov edx, 255;
  1613. pt1: mov eax, B;
  1614. cmp eax, 255;
  1615. jle pt2;
  1616. mov eax, 255;
  1617. pt2: mov ebx, G;
  1618. cmp ebx, 255;
  1619. jle pt3;
  1620. mov ebx, 255;
  1621. pt3: mov ecx, R;
  1622. cmp ecx, 255;
  1623. jle pt4;
  1624. mov ecx, 255;
  1625. pt4: shl ebx, 8;
  1626. shl ecx, 16;
  1627. shl edx, 24;
  1628. or eax, ebx;
  1629. or ecx, edx;
  1630. or eax, ecx;
  1631. mov color, eax;
  1632. }
  1633. g_OpenGLValues->m_color = color;
  1634. #else
  1635. R = (unsigned int)(red * two55);
  1636. G = (unsigned int)(green * two55);
  1637. B = (unsigned int)(blue * two55);
  1638. A = (unsigned int)(alpha * two55);
  1639. if(R > 255)
  1640. R = 255;
  1641. if(G > 255)
  1642. G = 255;
  1643. if(B > 255)
  1644. B = 255;
  1645. if(A > 255)
  1646. A = 255;
  1647. g_OpenGLValues->m_color = RGBA_MAKE(R, G, B, A);
  1648. #endif
  1649. }
  1650. void APIENTRY glColor4fv (const GLfloat *v)
  1651. {
  1652. #if GETPARMSFORDEBUG
  1653. char log[256];
  1654. char v0[40];
  1655. char v1[40];
  1656. char v2[40];
  1657. char v3[40];
  1658. ftoa( (double)v[0], v0 );
  1659. ftoa( (double)v[1], v1 );
  1660. ftoa( (double)v[2], v2 );
  1661. ftoa( (double)v[3], v3 );
  1662. sprintf( log, "glColor4fv: %s %s %s %s", v0, v1, v2, v3 );
  1663. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, log );
  1664. #endif
  1665. static float two55 = 255.f;
  1666. unsigned int R, G, B, A;
  1667. #ifdef _X86_
  1668. D3DCOLOR color;
  1669. _asm {
  1670. mov ebx, v;
  1671. fld [ebx];
  1672. fld [ebx + 4];
  1673. fld [ebx + 8];
  1674. fld [ebx + 12];
  1675. fld two55;
  1676. fmul st(1), st(0);
  1677. fmul st(2), st(0);
  1678. fmul st(3), st(0);
  1679. fmulp st(4), st(0);
  1680. fistp A;
  1681. fistp B;
  1682. fistp G;
  1683. fistp R;
  1684. mov edx, A;
  1685. cmp edx, 255;
  1686. jle pt1;
  1687. mov edx, 255;
  1688. pt1: mov eax, B;
  1689. cmp eax, 255;
  1690. jle pt2;
  1691. mov eax, 255;
  1692. pt2: mov ebx, G;
  1693. cmp ebx, 255;
  1694. jle pt3;
  1695. mov ebx, 255;
  1696. pt3: mov ecx, R;
  1697. cmp ecx, 255;
  1698. jle pt4;
  1699. mov ecx, 255;
  1700. pt4: shl ebx, 8;
  1701. shl ecx, 16;
  1702. shl edx, 24;
  1703. or eax, ebx;
  1704. or ecx, edx;
  1705. or eax, ecx;
  1706. mov color, eax;
  1707. }
  1708. g_OpenGLValues->m_color = color;
  1709. #else
  1710. R = (unsigned int)(v[0] * two55);
  1711. G = (unsigned int)(v[1] * two55);
  1712. B = (unsigned int)(v[2] * two55);
  1713. A = (unsigned int)(v[3] * two55);
  1714. if(R > 255)
  1715. R = 255;
  1716. if(G > 255)
  1717. G = 255;
  1718. if(B > 255)
  1719. B = 255;
  1720. if(A > 255)
  1721. A = 255;
  1722. g_OpenGLValues->m_color = RGBA_MAKE(R, G, B, A);
  1723. #endif
  1724. }
  1725. void APIENTRY glColorPointer (GLint size, GLenum type, GLsizei stride, const GLvoid *pointer)
  1726. {
  1727. #if GETPARMSFORDEBUG
  1728. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glColorPointer: 0x%X 0x%X 0x%X 0x%X", size, type, stride, pointer );
  1729. #endif
  1730. if(size == 4 && (type == GL_BYTE || type == GL_UNSIGNED_BYTE))
  1731. g_OpenGLValues->m_colorary = (GLubyte*)pointer;
  1732. #if DODPFS
  1733. else
  1734. {
  1735. char junk[256];
  1736. sprintf( junk, "Color array not supported (size:0x%X type:0x%X)\n", size, type );
  1737. OutputDebugStringA( junk );
  1738. }
  1739. #endif
  1740. if(stride == 0)
  1741. {
  1742. stride = 4;
  1743. }
  1744. g_OpenGLValues->m_colorarystride = stride;
  1745. }
  1746. void APIENTRY glCullFace (GLenum mode)
  1747. {
  1748. #if GETPARMSFORDEBUG
  1749. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glCullFace: 0x%X", mode );
  1750. #endif
  1751. g_OpenGLValues->m_cullMode = mode;
  1752. if(g_OpenGLValues->m_cullEnabled == TRUE){
  1753. DWORD statevalue;
  1754. if(mode == GL_BACK)
  1755. statevalue = g_OpenGLValues->m_FrontFace == GL_CCW ? D3DCULL_CW : D3DCULL_CCW;
  1756. else
  1757. statevalue = g_OpenGLValues->m_FrontFace == GL_CCW ? D3DCULL_CCW : D3DCULL_CW;
  1758. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_CULLMODE, statevalue);
  1759. }
  1760. }
  1761. void APIENTRY glDeleteTextures (GLsizei n, const GLuint *textures)
  1762. {
  1763. #if GETPARMSFORDEBUG
  1764. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glDeleteTextures: 0x%X 0x%X", n, textures );
  1765. #endif
  1766. for(int i = 0; i < n; ++i) {
  1767. TexInfo &ti = g_OpenGLValues->m_tex[textures[i]];
  1768. if(ti.m_ddsurf != 0) {
  1769. ti.m_ddsurf->Release();
  1770. ti.m_ddsurf = 0;
  1771. }
  1772. if(ti.m_block != 0)
  1773. {
  1774. g_OpenGLValues->m_d3ddev->DeleteStateBlock(ti.m_block);
  1775. ti.m_block = 0;
  1776. }
  1777. ti.m_capture = FALSE;
  1778. ti.m_dwStage = 0;
  1779. ti.m_minmode = D3DTEXF_POINT;
  1780. ti.m_magmode = D3DTEXF_LINEAR;
  1781. ti.m_mipmode = D3DTEXF_LINEAR;
  1782. ti.m_addu = D3DTADDRESS_WRAP;
  1783. ti.m_addv = D3DTADDRESS_WRAP;
  1784. ti.m_next = g_OpenGLValues->m_free;
  1785. ti.m_prev = -1;
  1786. g_OpenGLValues->m_tex[g_OpenGLValues->m_free].m_prev = textures[i];
  1787. g_OpenGLValues->m_free = textures[i];
  1788. }
  1789. }
  1790. void APIENTRY glDepthFunc (GLenum func)
  1791. {
  1792. #if GETPARMSFORDEBUG
  1793. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glDepthFunc: 0x%X", func );
  1794. #endif
  1795. int state = -1;
  1796. switch(func) {
  1797. case GL_NEVER:
  1798. state=D3DCMP_NEVER;
  1799. break;
  1800. case GL_LESS:
  1801. state=D3DCMP_LESS;
  1802. break;
  1803. case GL_EQUAL:
  1804. state=D3DCMP_EQUAL;
  1805. break;
  1806. case GL_LEQUAL:
  1807. state=D3DCMP_LESSEQUAL;
  1808. break;
  1809. case GL_GREATER:
  1810. state=D3DCMP_GREATER;
  1811. break;
  1812. case GL_NOTEQUAL:
  1813. state=D3DCMP_NOTEQUAL;
  1814. break;
  1815. case GL_GEQUAL:
  1816. state=D3DCMP_GREATEREQUAL;
  1817. break;
  1818. case GL_ALWAYS:
  1819. state=D3DCMP_ALWAYS;
  1820. break;
  1821. }
  1822. if(state >= 0)
  1823. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_ZFUNC, state);
  1824. }
  1825. void APIENTRY glDepthMask (GLboolean flag)
  1826. {
  1827. #if GETPARMSFORDEBUG
  1828. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glDepthMask: 0x%X", flag );
  1829. #endif
  1830. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_ZWRITEENABLE, flag);
  1831. }
  1832. void APIENTRY glDepthRange (GLclampd zNear, GLclampd zFar)
  1833. {
  1834. #if GETPARMSFORDEBUG
  1835. char log[256];
  1836. char zn[40];
  1837. char zf[40];
  1838. ftoa( (double)zNear, zn );
  1839. ftoa( (double)zFar, zf );
  1840. sprintf( log, "glDepthRange: %s %s", zn, zf );
  1841. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, log );
  1842. #endif
  1843. Clamp(&zNear);
  1844. Clamp(&zFar);
  1845. g_OpenGLValues->m_vport.MinZ = (FLOAT)zNear;
  1846. g_OpenGLValues->m_vport.MaxZ = (FLOAT)zFar;
  1847. if(g_OpenGLValues->m_polyoffset && g_OpenGLValues->m_vport.MaxZ != 0.f)
  1848. {
  1849. D3DVIEWPORT8 vport(g_OpenGLValues->m_vport);
  1850. vport.MaxZ -= .001f;
  1851. g_OpenGLValues->m_d3ddev->SetViewport(&vport);
  1852. }
  1853. else
  1854. {
  1855. g_OpenGLValues->m_d3ddev->SetViewport(&g_OpenGLValues->m_vport);
  1856. }
  1857. }
  1858. void APIENTRY glEnd (void)
  1859. {
  1860. #if GETPARMSFORDEBUG
  1861. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glEnd" );
  1862. #endif
  1863. if(!g_OpenGLValues->m_withinprim)
  1864. return;
  1865. g_OpenGLValues->m_vbuf->Unlock();
  1866. QuakeSetVertexShader(QUAKEVFMT);
  1867. QuakeSetStreamSource(0, g_OpenGLValues->m_vbuf, sizeof(QuakeVertex));
  1868. unsigned vcnt;
  1869. vcnt = g_OpenGLValues->m_vcnt;
  1870. switch(g_OpenGLValues->m_prim)
  1871. {
  1872. case GL_TRIANGLES:
  1873. if(vcnt >= 3)
  1874. {
  1875. g_OpenGLValues->m_d3ddev->DrawPrimitive(D3DPT_TRIANGLELIST, g_OpenGLValues->m_nfv, vcnt / 3);
  1876. }
  1877. #if DODPFS
  1878. else
  1879. {
  1880. char junk[256];
  1881. sprintf( junk, "Wrapper: glEnd: GL_TRIANGLES cnt=%d NOT STORED\n", vcnt );
  1882. OutputDebugStringA( junk );
  1883. }
  1884. #endif
  1885. g_OpenGLValues->m_nfv += vcnt;
  1886. break;
  1887. case GL_QUADS:
  1888. if(vcnt >= 4)
  1889. {
  1890. unsigned i;
  1891. for(i = 0; i < vcnt; i += 4)
  1892. {
  1893. g_OpenGLValues->m_d3ddev->DrawPrimitive(D3DPT_TRIANGLEFAN, g_OpenGLValues->m_nfv, 2);
  1894. g_OpenGLValues->m_nfv += 4;
  1895. }
  1896. }
  1897. #if DODPFS
  1898. else
  1899. {
  1900. char junk[256];
  1901. sprintf( junk, "Wrapper: glEnd: GL_QUADS cnt=%d NOT STORED\n", vcnt );
  1902. OutputDebugStringA( junk );
  1903. }
  1904. #endif
  1905. break;
  1906. case GL_LINES:
  1907. if(vcnt >= 2)
  1908. {
  1909. g_OpenGLValues->m_d3ddev->DrawPrimitive(D3DPT_LINELIST, g_OpenGLValues->m_nfv, vcnt / 2);
  1910. }
  1911. #if DODPFS
  1912. else
  1913. {
  1914. char junk[256];
  1915. sprintf( junk, "Wrapper: glEnd: GL_LINES cnt=%d NOT STORED\n", vcnt );
  1916. OutputDebugStringA( junk );
  1917. }
  1918. #endif
  1919. g_OpenGLValues->m_nfv += vcnt;
  1920. break;
  1921. case GL_TRIANGLE_STRIP:
  1922. case GL_QUAD_STRIP:
  1923. if(vcnt > 2)
  1924. {
  1925. g_OpenGLValues->m_d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, g_OpenGLValues->m_nfv, vcnt-2 );
  1926. }
  1927. #if DODPFS
  1928. else
  1929. {
  1930. char junk[256];
  1931. sprintf( junk, "Wrapper: glEnd: GL_TRIANGLE_STRIP or GL_QUAD_STRIP cnt=%d NOT STORED\n", vcnt );
  1932. OutputDebugStringA( junk );
  1933. }
  1934. #endif
  1935. g_OpenGLValues->m_nfv += vcnt;
  1936. break;
  1937. case GL_POLYGON:
  1938. case GL_TRIANGLE_FAN:
  1939. if(vcnt > 2)
  1940. {
  1941. g_OpenGLValues->m_d3ddev->DrawPrimitive(D3DPT_TRIANGLEFAN, g_OpenGLValues->m_nfv, vcnt-2);
  1942. }
  1943. #if DODPFS
  1944. else
  1945. {
  1946. char junk[256];
  1947. sprintf( junk, "Wrapper: glEnd: GL_POLYGON or GL_TRIANGLE_FAN cnt=%d NOT STORED\n", vcnt );
  1948. OutputDebugStringA( junk );
  1949. }
  1950. #endif
  1951. g_OpenGLValues->m_nfv += vcnt;
  1952. break;
  1953. case GL_POINTS:
  1954. if(vcnt > 0)
  1955. {
  1956. g_OpenGLValues->m_d3ddev->DrawPrimitive(D3DPT_POINTLIST, g_OpenGLValues->m_nfv, vcnt);
  1957. }
  1958. #if DODPFS
  1959. else
  1960. {
  1961. char junk[256];
  1962. sprintf( junk, "Wrapper: glEnd: GL_POINTS cnt=%d NOT STORED\n", vcnt );
  1963. OutputDebugStringA( junk );
  1964. }
  1965. #endif
  1966. g_OpenGLValues->m_nfv += vcnt;
  1967. break;
  1968. #if DODPFS
  1969. default:
  1970. char junk[256];
  1971. sprintf( junk, "Wrapper: unimplemented primitive type=0x%X cnt=%d\n", g_OpenGLValues->m_prim, vcnt );
  1972. OutputDebugStringA( junk );
  1973. #endif
  1974. }
  1975. g_OpenGLValues->m_withinprim = FALSE;
  1976. }
  1977. void APIENTRY glDrawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices)
  1978. {
  1979. #if GETPARMSFORDEBUG
  1980. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glDrawElements: 0x%X 0x%X 0x%X 0x%X", mode, count, type, indices );
  1981. #endif
  1982. QuakeSetTexturingState();
  1983. if((DWORD)count > g_OpenGLValues->m_ibufsz)
  1984. {
  1985. GrowIB(count);
  1986. }
  1987. unsigned min, max, LockFlags;
  1988. GLsizei i;
  1989. if(g_OpenGLValues->m_lckcount != 0)
  1990. {
  1991. WORD *pIndices;
  1992. min = g_OpenGLValues->m_lckfirst;
  1993. max = g_OpenGLValues->m_lckfirst + g_OpenGLValues->m_lckcount - 1;
  1994. if(max - min + 1 > g_OpenGLValues->m_vbufsz)
  1995. {
  1996. GrowVB(max - min + 1);
  1997. }
  1998. if(g_OpenGLValues->m_vbufoff + max - min + 1 > g_OpenGLValues->m_vbufsz)
  1999. {
  2000. LockFlags = D3DLOCK_DISCARD | D3DLOCK_NOSYSLOCK;
  2001. g_OpenGLValues->m_vbufoff = 0;
  2002. }
  2003. else
  2004. {
  2005. LockFlags = D3DLOCK_NOOVERWRITE | D3DLOCK_NOSYSLOCK;
  2006. }
  2007. if(g_OpenGLValues->m_ibufoff + count > g_OpenGLValues->m_ibufsz)
  2008. {
  2009. g_OpenGLValues->m_ibufoff = 0;
  2010. g_OpenGLValues->m_ibuf->Lock(g_OpenGLValues->m_ibufoff * 2, count * 2, (BYTE**)&pIndices, D3DLOCK_DISCARD | D3DLOCK_NOSYSLOCK);
  2011. }
  2012. else
  2013. {
  2014. g_OpenGLValues->m_ibuf->Lock(g_OpenGLValues->m_ibufoff * 2, count * 2, (BYTE**)&pIndices, D3DLOCK_NOOVERWRITE | D3DLOCK_NOSYSLOCK);
  2015. }
  2016. switch(type)
  2017. {
  2018. case GL_UNSIGNED_BYTE:
  2019. for(i = 0; i < count; ++i)
  2020. pIndices[i] = (WORD)(((unsigned char*)indices)[i] - min + g_OpenGLValues->m_vbufoff);
  2021. break;
  2022. case GL_UNSIGNED_SHORT:
  2023. for(i = 0; i < count; ++i)
  2024. pIndices[i] = (WORD)(((unsigned short*)indices)[i] - min + g_OpenGLValues->m_vbufoff);
  2025. break;
  2026. case GL_UNSIGNED_INT:
  2027. for(i = 0; i < count; ++i)
  2028. pIndices[i] = (WORD)(((unsigned int*)indices)[i] - min + g_OpenGLValues->m_vbufoff);
  2029. break;
  2030. }
  2031. g_OpenGLValues->m_ibuf->Unlock();
  2032. }
  2033. else
  2034. {
  2035. WORD *pIndices;
  2036. min = 65535;
  2037. max = 0;
  2038. switch(type)
  2039. {
  2040. case GL_UNSIGNED_BYTE:
  2041. for(i = 0; i < count; ++i)
  2042. {
  2043. unsigned t = ((unsigned char*)indices)[i];
  2044. if(t < min)
  2045. min = t;
  2046. if(t > max)
  2047. max = t;
  2048. }
  2049. break;
  2050. case GL_UNSIGNED_SHORT:
  2051. for(i = 0; i < count; ++i)
  2052. {
  2053. unsigned t = ((unsigned short*)indices)[i];
  2054. if(t < min)
  2055. min = t;
  2056. if(t > max)
  2057. max = t;
  2058. }
  2059. break;
  2060. case GL_UNSIGNED_INT:
  2061. for(i = 0; i < count; ++i)
  2062. {
  2063. unsigned t = ((unsigned int*)indices)[i];
  2064. if(t < min)
  2065. min = t;
  2066. if(t > max)
  2067. max = t;
  2068. }
  2069. break;
  2070. }
  2071. if(max - min + 1 > g_OpenGLValues->m_vbufsz)
  2072. {
  2073. GrowVB(max - min + 1);
  2074. }
  2075. if(g_OpenGLValues->m_vbufoff + max - min + 1 > g_OpenGLValues->m_vbufsz)
  2076. {
  2077. LockFlags = D3DLOCK_DISCARD | D3DLOCK_NOSYSLOCK;
  2078. g_OpenGLValues->m_vbufoff = 0;
  2079. }
  2080. else
  2081. {
  2082. LockFlags = D3DLOCK_NOOVERWRITE | D3DLOCK_NOSYSLOCK;
  2083. }
  2084. if(g_OpenGLValues->m_ibufoff + count > g_OpenGLValues->m_ibufsz)
  2085. {
  2086. g_OpenGLValues->m_ibufoff = 0;
  2087. g_OpenGLValues->m_ibuf->Lock(g_OpenGLValues->m_ibufoff * 2, count * 2, (BYTE**)&pIndices, D3DLOCK_DISCARD | D3DLOCK_NOSYSLOCK);
  2088. }
  2089. else
  2090. {
  2091. g_OpenGLValues->m_ibuf->Lock(g_OpenGLValues->m_ibufoff * 2, count * 2, (BYTE**)&pIndices, D3DLOCK_NOOVERWRITE | D3DLOCK_NOSYSLOCK);
  2092. }
  2093. switch(type)
  2094. {
  2095. case GL_UNSIGNED_BYTE:
  2096. for(i = 0; i < count; ++i)
  2097. {
  2098. pIndices[i] = (WORD)(((unsigned char*)indices)[i] - min + g_OpenGLValues->m_vbufoff);
  2099. }
  2100. break;
  2101. case GL_UNSIGNED_SHORT:
  2102. for(i = 0; i < count; ++i)
  2103. {
  2104. pIndices[i] = (WORD)(((unsigned short*)indices)[i] - min + g_OpenGLValues->m_vbufoff);
  2105. }
  2106. break;
  2107. case GL_UNSIGNED_INT:
  2108. for(i = 0; i < count; ++i)
  2109. {
  2110. pIndices[i] = (WORD)(((unsigned int*)indices)[i] - min + g_OpenGLValues->m_vbufoff);
  2111. }
  2112. break;
  2113. }
  2114. g_OpenGLValues->m_ibuf->Unlock();
  2115. }
  2116. if(g_OpenGLValues->m_usetexcoordary[1])
  2117. {
  2118. BYTE *pTex, *pSrcTex;
  2119. g_OpenGLValues->m_texbuf->Lock(g_OpenGLValues->m_vbufoff * 8, 8 * (max - min + 1), &pTex, LockFlags);
  2120. pSrcTex = (BYTE*)g_OpenGLValues->m_texcoordary[0] + min * g_OpenGLValues->m_texcoordarystride[0];
  2121. for(unsigned i = min; i <= max; ++i)
  2122. {
  2123. MEMCPY(pTex, pSrcTex, 8);
  2124. pTex += 8;
  2125. pSrcTex += g_OpenGLValues->m_texcoordarystride[0];
  2126. }
  2127. g_OpenGLValues->m_texbuf->Unlock();
  2128. BYTE *pTex2, *pSrcTex2;
  2129. g_OpenGLValues->m_tex2buf->Lock(g_OpenGLValues->m_vbufoff * 8, 8 * (max - min + 1), &pTex2, LockFlags);
  2130. pSrcTex2 = (BYTE*)g_OpenGLValues->m_texcoordary[1] + min * g_OpenGLValues->m_texcoordarystride[1];
  2131. for(unsigned i = min; i <= max; ++i)
  2132. {
  2133. MEMCPY(pTex2, pSrcTex2, 8);
  2134. pTex2 += 8;
  2135. pSrcTex2 += g_OpenGLValues->m_texcoordarystride[1];
  2136. }
  2137. g_OpenGLValues->m_tex2buf->Unlock();
  2138. }
  2139. else if(g_OpenGLValues->m_usetexcoordary[0])
  2140. {
  2141. BYTE *pTex, *pSrcTex;
  2142. g_OpenGLValues->m_texbuf->Lock(g_OpenGLValues->m_vbufoff * 8, 8 * (max - min + 1), &pTex, LockFlags);
  2143. pSrcTex = (BYTE*)g_OpenGLValues->m_texcoordary[0] + min * g_OpenGLValues->m_texcoordarystride[0];
  2144. for(unsigned i = min; i <= max; ++i)
  2145. {
  2146. MEMCPY(pTex, pSrcTex, 8);
  2147. pTex += 8;
  2148. pSrcTex += g_OpenGLValues->m_texcoordarystride[0];
  2149. }
  2150. g_OpenGLValues->m_texbuf->Unlock();
  2151. }
  2152. if(g_OpenGLValues->m_usecolorary)
  2153. {
  2154. if(max - min + 1 > VBUFSIZE)
  2155. {
  2156. #if DODPFS
  2157. char junk[256];
  2158. sprintf( junk, "Insufficient color buffer (amnt:0x%X size:0x%X)\n", (max-min+1), VBUFSIZE );
  2159. OutputDebugStringA( junk );
  2160. #endif
  2161. return;
  2162. }
  2163. DWORD *pColor;
  2164. BYTE *pSrcColor;
  2165. g_OpenGLValues->m_colbuf->Lock(g_OpenGLValues->m_vbufoff * 4, 4 * (max - min + 1), (BYTE**)&pColor, LockFlags);
  2166. #ifdef _X86_
  2167. const void *colorary = g_OpenGLValues->m_colorary;
  2168. DWORD colorarystride = g_OpenGLValues->m_colorarystride;
  2169. _asm
  2170. {
  2171. mov esi, min;
  2172. mov ecx, colorarystride;
  2173. mov ebx, colorary;
  2174. mov edi, pColor;
  2175. mov eax, esi;
  2176. mul ecx;
  2177. mov edx, max;
  2178. sub edx, esi;
  2179. lea esi, [ebx + eax];
  2180. inc edx;
  2181. lp1: mov eax, [esi];
  2182. add esi, ecx;
  2183. mov ebx, eax;
  2184. and eax, 0x00FF00FF;
  2185. rol eax, 16;
  2186. and ebx, 0xFF00FF00;
  2187. or eax, ebx;
  2188. mov [edi], eax;
  2189. add edi, 4;
  2190. dec edx;
  2191. jnz lp1;
  2192. }
  2193. #else
  2194. pSrcColor = (BYTE*)g_OpenGLValues->m_colorary + min * g_OpenGLValues->m_colorarystride;
  2195. for(unsigned i = min; i <= max; ++i)
  2196. {
  2197. *(pColor++) = RGBA_MAKE(pSrcColor[0], pSrcColor[1], pSrcColor[2], pSrcColor[3]);
  2198. pSrcColor += g_OpenGLValues->m_colorarystride;
  2199. }
  2200. #endif
  2201. g_OpenGLValues->m_colbuf->Unlock();
  2202. }
  2203. if(g_OpenGLValues->m_usevertexary)
  2204. {
  2205. BYTE *pXYZ, *pSrcXYZ;
  2206. g_OpenGLValues->m_xyzbuf->Lock(g_OpenGLValues->m_vbufoff * 12, 12 * (max - min + 1), &pXYZ, LockFlags);
  2207. pSrcXYZ = (BYTE*)g_OpenGLValues->m_vertexary + min * g_OpenGLValues->m_vertexarystride;
  2208. for(unsigned i = min; i <= max; ++i)
  2209. {
  2210. MEMCPY(pXYZ, pSrcXYZ, 12);
  2211. pXYZ += 12;
  2212. pSrcXYZ += g_OpenGLValues->m_vertexarystride;
  2213. }
  2214. g_OpenGLValues->m_xyzbuf->Unlock();
  2215. }
  2216. QuakeSetStreamSource(0, g_OpenGLValues->m_xyzbuf, 12);
  2217. if(g_OpenGLValues->m_usecolorary)
  2218. {
  2219. QuakeSetStreamSource(1, g_OpenGLValues->m_colbuf, 4);
  2220. if(g_OpenGLValues->m_usetexcoordary[1])
  2221. {
  2222. QuakeSetStreamSource(2, g_OpenGLValues->m_texbuf, 8);
  2223. QuakeSetStreamSource(3, g_OpenGLValues->m_tex2buf, 8);
  2224. QuakeSetVertexShader(g_OpenGLValues->m_vshader[5]);
  2225. }
  2226. else if(g_OpenGLValues->m_usetexcoordary[0])
  2227. {
  2228. QuakeSetStreamSource(2, g_OpenGLValues->m_texbuf, 8);
  2229. QuakeSetVertexShader(g_OpenGLValues->m_vshader[4]);
  2230. }
  2231. else
  2232. {
  2233. QuakeSetVertexShader(g_OpenGLValues->m_vshader[1]);
  2234. }
  2235. }
  2236. else if(g_OpenGLValues->m_usetexcoordary[1])
  2237. {
  2238. QuakeSetStreamSource(1, g_OpenGLValues->m_texbuf, 8);
  2239. QuakeSetStreamSource(2, g_OpenGLValues->m_tex2buf, 8);
  2240. QuakeSetVertexShader(g_OpenGLValues->m_vshader[3]);
  2241. }
  2242. else if(g_OpenGLValues->m_usetexcoordary[0])
  2243. {
  2244. QuakeSetStreamSource(2, g_OpenGLValues->m_texbuf, 8);
  2245. QuakeSetVertexShader(g_OpenGLValues->m_vshader[2]);
  2246. }
  2247. else
  2248. {
  2249. QuakeSetVertexShader(g_OpenGLValues->m_vshader[0]);
  2250. }
  2251. switch(mode)
  2252. {
  2253. case GL_LINES:
  2254. if(count >= 2)
  2255. {
  2256. g_OpenGLValues->m_d3ddev->DrawIndexedPrimitive(D3DPT_LINELIST, g_OpenGLValues->m_vbufoff, max - min + 1, g_OpenGLValues->m_ibufoff, count / 2);
  2257. }
  2258. break;
  2259. case GL_TRIANGLES:
  2260. if(count >= 3)
  2261. {
  2262. g_OpenGLValues->m_d3ddev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, g_OpenGLValues->m_vbufoff, max - min + 1, g_OpenGLValues->m_ibufoff, count / 3);
  2263. }
  2264. break;
  2265. case GL_TRIANGLE_STRIP:
  2266. case GL_QUAD_STRIP:
  2267. if(count > 2)
  2268. {
  2269. g_OpenGLValues->m_d3ddev->DrawIndexedPrimitive(D3DPT_TRIANGLESTRIP, g_OpenGLValues->m_vbufoff, max - min + 1, g_OpenGLValues->m_ibufoff, count - 2);
  2270. }
  2271. break;
  2272. case GL_POLYGON:
  2273. case GL_TRIANGLE_FAN:
  2274. if(count > 2)
  2275. {
  2276. g_OpenGLValues->m_d3ddev->DrawIndexedPrimitive(D3DPT_TRIANGLEFAN, g_OpenGLValues->m_vbufoff, max - min + 1, g_OpenGLValues->m_ibufoff, count - 2);
  2277. }
  2278. break;
  2279. case GL_QUADS:
  2280. if(count >= 4)
  2281. {
  2282. for(i = 0; i < count; i += 4)
  2283. g_OpenGLValues->m_d3ddev->DrawIndexedPrimitive(D3DPT_TRIANGLEFAN, g_OpenGLValues->m_vbufoff, max - min + 1, g_OpenGLValues->m_ibufoff, 2);
  2284. }
  2285. break;
  2286. #if DODPFS
  2287. default:
  2288. char junk[256];
  2289. sprintf( junk, "Wrapper (4): unimplemented primitive type (0x%X)\n", mode );
  2290. OutputDebugStringA( junk );
  2291. #endif
  2292. }
  2293. g_OpenGLValues->m_vbufoff += (max - min + 1);
  2294. g_OpenGLValues->m_ibufoff += count;
  2295. }
  2296. void APIENTRY glFrontFace (GLenum mode)
  2297. {
  2298. #if GETPARMSFORDEBUG
  2299. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glFrontFace: 0x%X", mode );
  2300. #endif
  2301. g_OpenGLValues->m_FrontFace = mode;
  2302. }
  2303. void APIENTRY glViewport (GLint x, GLint y, GLsizei width, GLsizei height)
  2304. {
  2305. #if GETPARMSFORDEBUG
  2306. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glViewport: 0x%X 0x%X 0x%X 0x%X", x, y, width, height );
  2307. #endif
  2308. g_OpenGLValues->m_vwx = x;
  2309. g_OpenGLValues->m_vwy = y;
  2310. g_OpenGLValues->m_vww = width;
  2311. g_OpenGLValues->m_vwh = height;
  2312. g_OpenGLValues->m_updvwp = TRUE;
  2313. }
  2314. void APIENTRY glLineWidth (GLfloat width)
  2315. {
  2316. #if GETPARMSFORDEBUG
  2317. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glLineWidth: 0x%X", width );
  2318. #endif
  2319. }
  2320. void APIENTRY glLoadIdentity (void)
  2321. {
  2322. #if GETPARMSFORDEBUG
  2323. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glLoadIdentity" );
  2324. #endif
  2325. D3DMATRIX unity;
  2326. unity._11 = 1.0f; unity._12 = 0.0f; unity._13 = 0.0f; unity._14 = 0.0f;
  2327. unity._21 = 0.0f; unity._22 = 1.0f; unity._23 = 0.0f; unity._24 = 0.0f;
  2328. unity._31 = 0.0f; unity._32 = 0.0f; unity._33 = 1.0f; unity._34 = 0.0f;
  2329. unity._41 = 0.0f; unity._42 = 0.0f; unity._43 = 0.0f; unity._44 = 1.0f;
  2330. if(g_OpenGLValues->m_matrixMode == D3DTS_TEXTURE0)
  2331. {
  2332. g_OpenGLValues->m_xfrm[g_OpenGLValues->m_matrixMode + g_OpenGLValues->m_curtgt] = unity;
  2333. QuakeSetTransform((D3DTRANSFORMSTATETYPE)(g_OpenGLValues->m_matrixMode + g_OpenGLValues->m_curtgt), &unity);
  2334. }
  2335. else
  2336. {
  2337. g_OpenGLValues->m_xfrm[g_OpenGLValues->m_matrixMode] = unity;
  2338. QuakeSetTransform(g_OpenGLValues->m_matrixMode, &unity);
  2339. }
  2340. }
  2341. void APIENTRY glMatrixMode (GLenum mode)
  2342. {
  2343. #if GETPARMSFORDEBUG
  2344. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glMatrixMode: 0x%X", mode );
  2345. #endif
  2346. switch(mode)
  2347. {
  2348. case GL_MODELVIEW:
  2349. g_OpenGLValues->m_matrixMode = D3DTS_WORLD;
  2350. break;
  2351. case GL_PROJECTION:
  2352. g_OpenGLValues->m_matrixMode = D3DTS_PROJECTION;
  2353. break;
  2354. case GL_TEXTURE:
  2355. g_OpenGLValues->m_matrixMode = D3DTS_TEXTURE0;
  2356. break;
  2357. }
  2358. }
  2359. void APIENTRY glColorMaterial (GLenum face, GLenum mode)
  2360. {
  2361. #if GETPARMSFORDEBUG
  2362. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glColorMaterial: 0x%X 0x%X", face, mode );
  2363. #endif
  2364. if(face == GL_FRONT || face == GL_FRONT_AND_BACK)
  2365. {
  2366. switch(mode)
  2367. {
  2368. case GL_EMISSION:
  2369. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_DIFFUSEMATERIALSOURCE, D3DMCS_MATERIAL);
  2370. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_SPECULARMATERIALSOURCE, D3DMCS_MATERIAL);
  2371. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_AMBIENTMATERIALSOURCE, D3DMCS_MATERIAL);
  2372. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_EMISSIVEMATERIALSOURCE, D3DMCS_COLOR1);
  2373. break;
  2374. case GL_AMBIENT:
  2375. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_DIFFUSEMATERIALSOURCE, D3DMCS_MATERIAL);
  2376. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_SPECULARMATERIALSOURCE, D3DMCS_MATERIAL);
  2377. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_AMBIENTMATERIALSOURCE, D3DMCS_COLOR1);
  2378. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_EMISSIVEMATERIALSOURCE, D3DMCS_MATERIAL);
  2379. break;
  2380. case GL_DIFFUSE:
  2381. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_DIFFUSEMATERIALSOURCE, D3DMCS_COLOR1);
  2382. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_SPECULARMATERIALSOURCE, D3DMCS_MATERIAL);
  2383. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_AMBIENTMATERIALSOURCE, D3DMCS_MATERIAL);
  2384. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_EMISSIVEMATERIALSOURCE, D3DMCS_MATERIAL);
  2385. break;
  2386. case GL_SPECULAR:
  2387. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_DIFFUSEMATERIALSOURCE, D3DMCS_MATERIAL);
  2388. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_SPECULARMATERIALSOURCE, D3DMCS_COLOR1);
  2389. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_AMBIENTMATERIALSOURCE, D3DMCS_MATERIAL);
  2390. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_EMISSIVEMATERIALSOURCE, D3DMCS_MATERIAL);
  2391. break;
  2392. case GL_AMBIENT_AND_DIFFUSE:
  2393. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_DIFFUSEMATERIALSOURCE, D3DMCS_COLOR1);
  2394. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_SPECULARMATERIALSOURCE, D3DMCS_MATERIAL);
  2395. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_AMBIENTMATERIALSOURCE, D3DMCS_COLOR1);
  2396. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_EMISSIVEMATERIALSOURCE, D3DMCS_MATERIAL);
  2397. break;
  2398. }
  2399. }
  2400. #if DODPFS
  2401. else
  2402. {
  2403. OutputDebugStringA("Wrapper: Back face ColorMaterial ignored\n");
  2404. }
  2405. #endif
  2406. }
  2407. void APIENTRY glDisable (GLenum cap)
  2408. {
  2409. switch(cap) {
  2410. case GL_LIGHTING:
  2411. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_LIGHTING, FALSE);
  2412. break;
  2413. case GL_COLOR_MATERIAL:
  2414. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_COLORVERTEX, FALSE);
  2415. break;
  2416. case GL_DEPTH_TEST:
  2417. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_ZENABLE, FALSE);
  2418. break;
  2419. case GL_CULL_FACE:
  2420. g_OpenGLValues->m_cullEnabled = FALSE;
  2421. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
  2422. break;
  2423. case GL_FOG:
  2424. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_FOGTABLEMODE, D3DFOG_NONE);
  2425. break;
  2426. case GL_BLEND:
  2427. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
  2428. break;
  2429. case GL_CLIP_PLANE0:
  2430. g_OpenGLValues->m_clippstate &= ~0x01;
  2431. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_CLIPPLANEENABLE, g_OpenGLValues->m_clippstate);
  2432. break;
  2433. case GL_CLIP_PLANE1:
  2434. g_OpenGLValues->m_clippstate &= ~0x02;
  2435. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_CLIPPLANEENABLE, g_OpenGLValues->m_clippstate);
  2436. break;
  2437. case GL_CLIP_PLANE2:
  2438. g_OpenGLValues->m_clippstate &= ~0x04;
  2439. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_CLIPPLANEENABLE, g_OpenGLValues->m_clippstate);
  2440. break;
  2441. case GL_CLIP_PLANE3:
  2442. g_OpenGLValues->m_clippstate &= ~0x08;
  2443. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_CLIPPLANEENABLE, g_OpenGLValues->m_clippstate);
  2444. break;
  2445. case GL_CLIP_PLANE4:
  2446. g_OpenGLValues->m_clippstate &= ~0x10;
  2447. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_CLIPPLANEENABLE, g_OpenGLValues->m_clippstate);
  2448. break;
  2449. case GL_CLIP_PLANE5:
  2450. g_OpenGLValues->m_clippstate &= ~0x20;
  2451. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_CLIPPLANEENABLE, g_OpenGLValues->m_clippstate);
  2452. break;
  2453. case GL_POLYGON_OFFSET_FILL:
  2454. g_OpenGLValues->m_polyoffset = FALSE;
  2455. glDepthRange(g_OpenGLValues->m_vport.MinZ, g_OpenGLValues->m_vport.MaxZ);
  2456. break;
  2457. case GL_STENCIL_TEST:
  2458. break;
  2459. case GL_SCISSOR_TEST:
  2460. g_OpenGLValues->m_scissoring = FALSE;
  2461. glViewport(g_OpenGLValues->m_vwx, g_OpenGLValues->m_vwy, g_OpenGLValues->m_vww, g_OpenGLValues->m_vwh);
  2462. QuakeSetTransform(D3DTS_PROJECTION, &g_OpenGLValues->m_xfrm[D3DTS_PROJECTION]);
  2463. break;
  2464. case GL_TEXTURE_2D:
  2465. if(g_OpenGLValues->m_curtgt == 0) {
  2466. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_COLOROP, D3DTOP_DISABLE);
  2467. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_ALPHAOP, D3DTOP_DISABLE);
  2468. g_OpenGLValues->m_texturing = FALSE;
  2469. }
  2470. else {
  2471. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_COLOROP, D3DTOP_DISABLE);
  2472. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_ALPHAOP, D3DTOP_DISABLE);
  2473. g_OpenGLValues->m_mtex = FALSE;
  2474. }
  2475. g_OpenGLValues->m_texHandleValid = FALSE;
  2476. break;
  2477. case GL_ALPHA_TEST:
  2478. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
  2479. break;
  2480. case GL_LIGHT0:
  2481. g_OpenGLValues->m_d3ddev->LightEnable(0, FALSE);
  2482. break;
  2483. case GL_LIGHT1:
  2484. g_OpenGLValues->m_d3ddev->LightEnable(1, FALSE);
  2485. break;
  2486. case GL_LIGHT2:
  2487. g_OpenGLValues->m_d3ddev->LightEnable(2, FALSE);
  2488. break;
  2489. case GL_LIGHT3:
  2490. g_OpenGLValues->m_d3ddev->LightEnable(3, FALSE);
  2491. break;
  2492. case GL_LIGHT4:
  2493. g_OpenGLValues->m_d3ddev->LightEnable(4, FALSE);
  2494. break;
  2495. case GL_LIGHT5:
  2496. g_OpenGLValues->m_d3ddev->LightEnable(5, FALSE);
  2497. break;
  2498. case GL_LIGHT6:
  2499. g_OpenGLValues->m_d3ddev->LightEnable(6, FALSE);
  2500. break;
  2501. case GL_LIGHT7:
  2502. g_OpenGLValues->m_d3ddev->LightEnable(7, FALSE);
  2503. break;
  2504. case GL_TEXTURE_GEN_S:
  2505. g_OpenGLValues->m_texgen = FALSE;
  2506. g_OpenGLValues->m_d3ddev->SetTextureStageState(0, D3DTSS_TEXCOORDINDEX, 0);
  2507. if(g_OpenGLValues->m_usemtex == TRUE)
  2508. {
  2509. g_OpenGLValues->m_d3ddev->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, 1);
  2510. }
  2511. break;
  2512. case GL_TEXTURE_GEN_T:
  2513. break;
  2514. #if DODPFS
  2515. default:
  2516. char junk[256];
  2517. sprintf( junk, "Wrapper: glDisable on this cap not supported (0x%X)\n", cap );
  2518. OutputDebugStringA( junk );
  2519. #endif
  2520. }
  2521. }
  2522. void APIENTRY glDisableClientState (GLenum array)
  2523. {
  2524. #if GETPARMSFORDEBUG
  2525. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glDisableClientState: 0x%X", array );
  2526. #endif
  2527. switch(array)
  2528. {
  2529. case GL_COLOR_ARRAY:
  2530. g_OpenGLValues->m_usecolorary = FALSE;
  2531. break;
  2532. case GL_TEXTURE_COORD_ARRAY:
  2533. g_OpenGLValues->m_usetexcoordary[g_OpenGLValues->m_client_active_texture_arb] = FALSE;
  2534. break;
  2535. case GL_VERTEX_ARRAY:
  2536. g_OpenGLValues->m_usevertexary = FALSE;
  2537. break;
  2538. #if DODPFS
  2539. default:
  2540. char junk[256];
  2541. sprintf( junk, "Wrapper: Array not supported (0x%X)\n", array );
  2542. OutputDebugStringA( junk );
  2543. #endif
  2544. }
  2545. }
  2546. void APIENTRY glDrawBuffer (GLenum mode)
  2547. {
  2548. #if GETPARMSFORDEBUG
  2549. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glDrawBuffer: 0x%X", mode );
  2550. #endif
  2551. }
  2552. void APIENTRY glEnable (GLenum cap)
  2553. {
  2554. #if GETPARMSFORDEBUG
  2555. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glEnable: 0x%X", cap );
  2556. #endif
  2557. switch(cap) {
  2558. case GL_LIGHTING:
  2559. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_LIGHTING, TRUE);
  2560. break;
  2561. case GL_COLOR_MATERIAL:
  2562. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_COLORVERTEX, TRUE);
  2563. break;
  2564. case GL_DEPTH_TEST:
  2565. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_ZENABLE, TRUE);
  2566. break;
  2567. case GL_CULL_FACE:
  2568. g_OpenGLValues->m_cullEnabled = TRUE;
  2569. glCullFace(g_OpenGLValues->m_cullMode);
  2570. break;
  2571. case GL_FOG:
  2572. break;
  2573. case GL_BLEND:
  2574. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
  2575. break;
  2576. case GL_CLIP_PLANE0:
  2577. g_OpenGLValues->m_clippstate |= 0x01;
  2578. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_CLIPPLANEENABLE, g_OpenGLValues->m_clippstate);
  2579. break;
  2580. case GL_CLIP_PLANE1:
  2581. g_OpenGLValues->m_clippstate |= 0x02;
  2582. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_CLIPPLANEENABLE, g_OpenGLValues->m_clippstate);
  2583. break;
  2584. case GL_CLIP_PLANE2:
  2585. g_OpenGLValues->m_clippstate |= 0x04;
  2586. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_CLIPPLANEENABLE, g_OpenGLValues->m_clippstate);
  2587. break;
  2588. case GL_CLIP_PLANE3:
  2589. g_OpenGLValues->m_clippstate |= 0x08;
  2590. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_CLIPPLANEENABLE, g_OpenGLValues->m_clippstate);
  2591. break;
  2592. case GL_CLIP_PLANE4:
  2593. g_OpenGLValues->m_clippstate |= 0x10;
  2594. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_CLIPPLANEENABLE, g_OpenGLValues->m_clippstate);
  2595. break;
  2596. case GL_CLIP_PLANE5:
  2597. g_OpenGLValues->m_clippstate |= 0x20;
  2598. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_CLIPPLANEENABLE, g_OpenGLValues->m_clippstate);
  2599. break;
  2600. case GL_POLYGON_OFFSET_FILL:
  2601. g_OpenGLValues->m_polyoffset = TRUE;
  2602. glDepthRange(g_OpenGLValues->m_vport.MinZ, g_OpenGLValues->m_vport.MaxZ);
  2603. break;
  2604. case GL_SCISSOR_TEST:
  2605. g_OpenGLValues->m_scissoring = TRUE;
  2606. g_OpenGLValues->m_updvwp = TRUE;
  2607. break;
  2608. case GL_TEXTURE_2D:
  2609. if(g_OpenGLValues->m_curtgt == 0)
  2610. g_OpenGLValues->m_texturing = TRUE;
  2611. else
  2612. g_OpenGLValues->m_mtex = TRUE;
  2613. g_OpenGLValues->m_texHandleValid = FALSE;
  2614. break;
  2615. case GL_ALPHA_TEST:
  2616. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_ALPHATESTENABLE, TRUE);
  2617. break;
  2618. case GL_LIGHT0:
  2619. g_OpenGLValues->m_d3ddev->LightEnable(0, TRUE);
  2620. break;
  2621. case GL_LIGHT1:
  2622. g_OpenGLValues->m_d3ddev->LightEnable(1, TRUE);
  2623. break;
  2624. case GL_LIGHT2:
  2625. g_OpenGLValues->m_d3ddev->LightEnable(2, TRUE);
  2626. break;
  2627. case GL_LIGHT3:
  2628. g_OpenGLValues->m_d3ddev->LightEnable(3, TRUE);
  2629. break;
  2630. case GL_LIGHT4:
  2631. g_OpenGLValues->m_d3ddev->LightEnable(4, TRUE);
  2632. break;
  2633. case GL_LIGHT5:
  2634. g_OpenGLValues->m_d3ddev->LightEnable(5, TRUE);
  2635. break;
  2636. case GL_LIGHT6:
  2637. g_OpenGLValues->m_d3ddev->LightEnable(6, TRUE);
  2638. break;
  2639. case GL_LIGHT7:
  2640. g_OpenGLValues->m_d3ddev->LightEnable(7, TRUE);
  2641. break;
  2642. case GL_TEXTURE_GEN_S:
  2643. g_OpenGLValues->m_texgen = TRUE;
  2644. g_OpenGLValues->m_d3ddev->SetTextureStageState(0, D3DTSS_TEXCOORDINDEX, 0 | D3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR);
  2645. if(g_OpenGLValues->m_usemtex == TRUE)
  2646. {
  2647. g_OpenGLValues->m_d3ddev->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, 1 | D3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR);
  2648. }
  2649. break;
  2650. case GL_TEXTURE_GEN_T:
  2651. break;
  2652. #if DODPFS
  2653. default:
  2654. char junk[256];
  2655. sprintf( junk, "Wrapper: glEnable on this cap not supported (0x%X)\n", cap );
  2656. OutputDebugStringA( junk );
  2657. #endif
  2658. }
  2659. }
  2660. void APIENTRY glEnableClientState (GLenum array)
  2661. {
  2662. #if GETPARMSFORDEBUG
  2663. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glEnableClientState: 0x%X", array );
  2664. #endif
  2665. switch(array)
  2666. {
  2667. case GL_COLOR_ARRAY:
  2668. g_OpenGLValues->m_usecolorary = TRUE;
  2669. break;
  2670. case GL_TEXTURE_COORD_ARRAY:
  2671. g_OpenGLValues->m_usetexcoordary[g_OpenGLValues->m_client_active_texture_arb] = TRUE;
  2672. break;
  2673. case GL_VERTEX_ARRAY:
  2674. g_OpenGLValues->m_usevertexary = TRUE;
  2675. break;
  2676. #if DODPFS
  2677. default:
  2678. char junk[256];
  2679. sprintf( junk, "Wrapper: Array not supported (0x%X)\n", array );
  2680. OutputDebugStringA( junk );
  2681. #endif
  2682. }
  2683. }
  2684. void APIENTRY glFogf (GLenum pname, GLfloat param)
  2685. {
  2686. #if GETPARMSFORDEBUG
  2687. char log[256];
  2688. char p[40];
  2689. ftoa( (double)param, p );
  2690. sprintf( log, "glFogf: 0x%X %s", pname, p );
  2691. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, log );
  2692. #endif
  2693. FLOAT start, end;
  2694. switch(pname)
  2695. {
  2696. case GL_FOG_MODE:
  2697. switch((int)param)
  2698. {
  2699. case GL_LINEAR:
  2700. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_FOGTABLEMODE, D3DFOG_LINEAR);
  2701. break;
  2702. case GL_EXP:
  2703. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_FOGTABLEMODE, D3DFOG_EXP);
  2704. break;
  2705. case GL_EXP2:
  2706. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_FOGTABLEMODE, D3DFOG_EXP2);
  2707. break;
  2708. }
  2709. break;
  2710. case GL_FOG_START:
  2711. start = param;
  2712. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_FOGSTART, *(DWORD*)(&start));
  2713. break;
  2714. case GL_FOG_END:
  2715. end = param;
  2716. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_FOGEND, *(DWORD*)(&end));
  2717. break;
  2718. #if DODPFS
  2719. default:
  2720. char junk[256];
  2721. sprintf( junk, "Wrapper: Fog pname not supported (0x%X)\n", pname );
  2722. OutputDebugStringA( junk );
  2723. #endif
  2724. }
  2725. }
  2726. void APIENTRY glFogi (GLenum pname, GLint param)
  2727. {
  2728. #if GETPARMSFORDEBUG
  2729. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glFogi: 0x%X 0x%X", pname, param );
  2730. #endif
  2731. glFogf(pname, (GLfloat)param);
  2732. }
  2733. void APIENTRY glFrustum (GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble zNear, GLdouble zFar)
  2734. {
  2735. #if GETPARMSFORDEBUG
  2736. char log[256];
  2737. char l[40];
  2738. char r[40];
  2739. char b[40];
  2740. char t[40];
  2741. char zn[40];
  2742. char zf[40];
  2743. ftoa( (double)left, l );
  2744. ftoa( (double)right, r );
  2745. ftoa( (double)bottom, b );
  2746. ftoa( (double)top, t );
  2747. ftoa( (double)zNear, zn );
  2748. ftoa( (double)zFar, zf );
  2749. sprintf( log, "glFrustum: %s %s %s %s %s %s", l, r, b, t, zn, zf );
  2750. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, log );
  2751. #endif
  2752. D3DMATRIX f;
  2753. f._11 = (FLOAT)((2.0 * zNear) / (right - left));
  2754. f._21 = 0.f;
  2755. f._31 = (FLOAT)((right + left) / (right - left));
  2756. f._41 = 0.f;
  2757. f._12 = 0.f;
  2758. f._22 = (FLOAT)((2.0 * zNear) / (top - bottom));
  2759. f._32 = (FLOAT)((top + bottom) / (top - bottom));
  2760. f._42 = 0.f;
  2761. f._13 = 0.f;
  2762. f._23 = 0.f;
  2763. f._33 = (FLOAT)(-(zFar + zNear) / (zFar - zNear));
  2764. f._43 = (FLOAT)(-(2.0 * zFar * zNear) / (zFar - zNear));
  2765. f._14 = 0.f;
  2766. f._24 = 0.f;
  2767. f._34 = -1.f;
  2768. f._44 = 0.f;
  2769. if(g_OpenGLValues->m_matrixMode == D3DTS_TEXTURE0)
  2770. {
  2771. MultiplyMatrix((D3DTRANSFORMSTATETYPE)(g_OpenGLValues->m_matrixMode + g_OpenGLValues->m_curtgt), f);
  2772. QuakeSetTransform((D3DTRANSFORMSTATETYPE)(g_OpenGLValues->m_matrixMode + g_OpenGLValues->m_curtgt), &g_OpenGLValues->m_xfrm[g_OpenGLValues->m_matrixMode + g_OpenGLValues->m_curtgt]);
  2773. }
  2774. else
  2775. {
  2776. MultiplyMatrix(g_OpenGLValues->m_matrixMode, f);
  2777. QuakeSetTransform(g_OpenGLValues->m_matrixMode, &g_OpenGLValues->m_xfrm[g_OpenGLValues->m_matrixMode]);
  2778. }
  2779. }
  2780. void APIENTRY glGenTextures (GLsizei n, GLuint *textures)
  2781. {
  2782. #if GETPARMSFORDEBUG
  2783. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glGenTextures: 0x%X 0x%X", n, textures );
  2784. #endif
  2785. for(GLsizei i = 0; i < n; ++i)
  2786. {
  2787. textures[i] = g_OpenGLValues->m_free;
  2788. int t = g_OpenGLValues->m_free;
  2789. g_OpenGLValues->m_free = g_OpenGLValues->m_tex[g_OpenGLValues->m_free].m_next;
  2790. g_OpenGLValues->m_tex[g_OpenGLValues->m_free].m_prev = -1;
  2791. g_OpenGLValues->m_tex[t].m_next = g_OpenGLValues->m_tex[t].m_prev = -1;
  2792. }
  2793. }
  2794. GLenum APIENTRY glGetError (void)
  2795. {
  2796. #if GETPARMSFORDEBUG
  2797. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glGetError" );
  2798. #endif
  2799. return GL_NO_ERROR;
  2800. }
  2801. void APIENTRY glGetFloatv (GLenum pname, GLfloat *params)
  2802. {
  2803. #if GETPARMSFORDEBUG
  2804. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glGetFloatv: 0x%X 0x%X", pname, params );
  2805. #endif
  2806. switch (pname) {
  2807. case GL_MODELVIEW_MATRIX:
  2808. *((D3DMATRIX*)params) = g_OpenGLValues->m_xfrm[D3DTS_WORLD];
  2809. break;
  2810. case GL_PROJECTION_MATRIX:
  2811. *((D3DMATRIX*)params) = g_OpenGLValues->m_xfrm[D3DTS_PROJECTION];
  2812. break;
  2813. #if DODPFS
  2814. default:
  2815. char junk[256];
  2816. sprintf( junk, "Wrapper: Unimplemented GetFloatv query (0x%X)\n", pname );
  2817. OutputDebugStringA( junk );
  2818. #endif
  2819. }
  2820. }
  2821. void APIENTRY glGetIntegerv (GLenum pname, GLint *params)
  2822. {
  2823. #if GETPARMSFORDEBUG
  2824. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glGetIntegerv: 0x%X 0x%X", pname, params );
  2825. #endif
  2826. switch(pname)
  2827. {
  2828. case GL_MAX_TEXTURE_SIZE:
  2829. *params = max(g_OpenGLValues->m_dd.MaxTextureWidth, g_OpenGLValues->m_dd.MaxTextureHeight);
  2830. break;
  2831. case GL_MAX_ACTIVE_TEXTURES_ARB:
  2832. *params = g_OpenGLValues->m_usemtex ? 2 : 1;
  2833. break;
  2834. case GL_PACK_LSB_FIRST:
  2835. case GL_PACK_SWAP_BYTES:
  2836. case GL_UNPACK_SWAP_BYTES:
  2837. case GL_UNPACK_LSB_FIRST:
  2838. *params = GL_FALSE;
  2839. break;
  2840. case GL_PACK_ROW_LENGTH:
  2841. case GL_UNPACK_ROW_LENGTH:
  2842. case GL_PACK_SKIP_ROWS:
  2843. case GL_PACK_SKIP_PIXELS:
  2844. case GL_UNPACK_SKIP_ROWS:
  2845. case GL_UNPACK_SKIP_PIXELS:
  2846. *params = 0;
  2847. break;
  2848. case GL_PACK_ALIGNMENT:
  2849. case GL_UNPACK_ALIGNMENT:
  2850. *params = 4;
  2851. break;
  2852. case GL_TEXTURE_1D:
  2853. *params = FALSE;
  2854. break;
  2855. case GL_TEXTURE_2D:
  2856. *params = TRUE;
  2857. break;
  2858. #if DODPFS
  2859. default:
  2860. char junk[256];
  2861. sprintf( junk, "Wrapper: Unimplemented GetIntegerv query (0x%X)\n", pname );
  2862. OutputDebugStringA( junk );
  2863. #endif
  2864. }
  2865. }
  2866. const GLubyte* APIENTRY glGetString (GLenum name)
  2867. {
  2868. #if GETPARMSFORDEBUG
  2869. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glGetString: 0x%X", name );
  2870. #endif
  2871. if (!g_OpenGLValues->m_d3ddev)
  2872. return NULL; // No current RC!
  2873. switch(name) {
  2874. case GL_VENDOR:
  2875. return (const GLubyte*)"Microsoft Corp.";
  2876. case GL_RENDERER:
  2877. return (const GLubyte*)"Direct3D";
  2878. case GL_VERSION:
  2879. return (const GLubyte*)"1.1";
  2880. case GL_EXTENSIONS:
  2881. if(g_OpenGLValues->m_usemtex != FALSE)
  2882. return (const GLubyte*)"GL_ARB_multitexture GL_EXT_compiled_vertex_array GL_SGIS_multitexture";
  2883. else
  2884. return (const GLubyte*)"GL_EXT_compiled_vertex_array";
  2885. #if DODPFS
  2886. default:
  2887. char junk[256];
  2888. sprintf( junk, "Wrapper: Unimplemented GetString query (0x%X)\n", name );
  2889. OutputDebugStringA( junk );
  2890. #endif
  2891. }
  2892. return (const GLubyte*)"";
  2893. }
  2894. GLboolean APIENTRY glIsTexture (GLuint texture)
  2895. {
  2896. #if GETPARMSFORDEBUG
  2897. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glIsTexture: 0x%X", texture );
  2898. #endif
  2899. if(texture != 0 && texture < MAXGLTEXHANDLES && g_OpenGLValues->m_tex[texture].m_block != 0)
  2900. {
  2901. return TRUE;
  2902. }
  2903. return FALSE;
  2904. }
  2905. void APIENTRY glLightf (GLenum light, GLenum pname, GLfloat param)
  2906. {
  2907. #if GETPARMSFORDEBUG
  2908. char log[256];
  2909. char p[40];
  2910. ftoa( (double)param, p );
  2911. sprintf( log, "glLightf: 0x%X 0x%X %s", light, pname, p );
  2912. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, log );
  2913. #endif
  2914. unsigned i = (DWORD)light - GL_LIGHT0;
  2915. switch(pname)
  2916. {
  2917. case GL_SPOT_EXPONENT:
  2918. g_OpenGLValues->m_light[i].Falloff = param;
  2919. break;
  2920. case GL_SPOT_CUTOFF:
  2921. g_OpenGLValues->m_light[i].Phi = param;
  2922. break;
  2923. case GL_CONSTANT_ATTENUATION:
  2924. g_OpenGLValues->m_light[i].Attenuation0 = param;
  2925. break;
  2926. case GL_LINEAR_ATTENUATION:
  2927. g_OpenGLValues->m_light[i].Attenuation1 = param;
  2928. break;
  2929. case GL_QUADRATIC_ATTENUATION:
  2930. g_OpenGLValues->m_light[i].Attenuation2 = param;
  2931. break;
  2932. }
  2933. g_OpenGLValues->m_lightdirty |= (1 << i);
  2934. }
  2935. void APIENTRY glLightfv (GLenum light, GLenum pname, const GLfloat *params)
  2936. {
  2937. #if GETPARMSFORDEBUG
  2938. char log[256];
  2939. char p[40];
  2940. ftoa( (double)*params, p );
  2941. sprintf( log, "glLightfv: 0x%X 0x%X %s", light, pname, p );
  2942. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, log );
  2943. #endif
  2944. unsigned i = (DWORD)light - GL_LIGHT0;
  2945. switch(pname)
  2946. {
  2947. case GL_AMBIENT:
  2948. MEMCPY(&g_OpenGLValues->m_light[i].Ambient, params, sizeof(D3DCOLORVALUE));
  2949. break;
  2950. case GL_DIFFUSE:
  2951. MEMCPY(&g_OpenGLValues->m_light[i].Diffuse, params, sizeof(D3DCOLORVALUE));
  2952. break;
  2953. case GL_SPECULAR:
  2954. MEMCPY(&g_OpenGLValues->m_light[i].Specular, params, sizeof(D3DCOLORVALUE));
  2955. break;
  2956. case GL_POSITION:
  2957. MEMCPY(&g_OpenGLValues->m_light[i].Position, params, sizeof(D3DVECTOR));
  2958. g_OpenGLValues->m_lightPositionW[i] = params[3];
  2959. break;
  2960. case GL_SPOT_DIRECTION:
  2961. MEMCPY(&g_OpenGLValues->m_light[i].Direction, params, sizeof(D3DVECTOR));
  2962. break;
  2963. case GL_SPOT_EXPONENT:
  2964. g_OpenGLValues->m_light[i].Falloff = *params;
  2965. break;
  2966. case GL_SPOT_CUTOFF:
  2967. g_OpenGLValues->m_light[i].Phi = *params;
  2968. break;
  2969. case GL_CONSTANT_ATTENUATION:
  2970. g_OpenGLValues->m_light[i].Attenuation0 = *params;
  2971. break;
  2972. case GL_LINEAR_ATTENUATION:
  2973. g_OpenGLValues->m_light[i].Attenuation1 = *params;
  2974. break;
  2975. case GL_QUADRATIC_ATTENUATION:
  2976. g_OpenGLValues->m_light[i].Attenuation2 = *params;
  2977. break;
  2978. }
  2979. g_OpenGLValues->m_lightdirty |= (1 << i);
  2980. }
  2981. void APIENTRY glLightModelfv (GLenum pname, const GLfloat *params)
  2982. {
  2983. #if GETPARMSFORDEBUG
  2984. char log[256];
  2985. char p[40];
  2986. ftoa( (double)*params, p );
  2987. sprintf( log, "glLightModelfv: 0x%X %s", pname, p );
  2988. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, log );
  2989. #endif
  2990. static float two55 = 255.f;
  2991. unsigned int R, G, B, A;
  2992. switch(pname)
  2993. {
  2994. case GL_LIGHT_MODEL_AMBIENT:
  2995. R = (unsigned int)(params[0] * two55);
  2996. G = (unsigned int)(params[1] * two55);
  2997. B = (unsigned int)(params[2] * two55);
  2998. A = (unsigned int)(params[3] * two55);
  2999. if(R > 255)
  3000. R = 255;
  3001. if(G > 255)
  3002. G = 255;
  3003. if(B > 255)
  3004. B = 255;
  3005. if(A > 255)
  3006. A = 255;
  3007. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_AMBIENT, RGBA_MAKE(R, G, B, A));
  3008. break;
  3009. case GL_LIGHT_MODEL_TWO_SIDE:
  3010. #if DODPFS
  3011. if(*params != 0.f)
  3012. {
  3013. OutputDebugStringA("Wrapper: Two sided lighting not supported\n");
  3014. }
  3015. #endif
  3016. break;
  3017. case GL_LIGHT_MODEL_LOCAL_VIEWER:
  3018. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_LOCALVIEWER, (DWORD)(*params));
  3019. break;
  3020. #if DODPFS
  3021. default:
  3022. OutputDebugStringA("Wrapper: LIGHT_MODEL_COLOR_CONTROL not supported\n" );
  3023. #endif
  3024. }
  3025. }
  3026. void APIENTRY glLightModeli (GLenum pname, GLint param)
  3027. {
  3028. #if GETPARMSFORDEBUG
  3029. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glLightModeli: 0x%X 0x%X", pname, param );
  3030. #endif
  3031. switch(pname)
  3032. {
  3033. case GL_LIGHT_MODEL_TWO_SIDE:
  3034. #if DODPFS
  3035. if(param != 0)
  3036. {
  3037. OutputDebugStringA("Wrapper: Two sided lighting not supported\n" );
  3038. }
  3039. #endif
  3040. break;
  3041. case GL_LIGHT_MODEL_LOCAL_VIEWER:
  3042. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_LOCALVIEWER, param);
  3043. break;
  3044. #if DODPFS
  3045. default:
  3046. OutputDebugStringA("Wrapper: LIGHT_MODEL_COLOR_CONTROL not supported\n");
  3047. #endif
  3048. }
  3049. }
  3050. void APIENTRY glLoadMatrixf (const GLfloat *m)
  3051. {
  3052. #if GETPARMSFORDEBUG
  3053. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glLoadMatrixf: 0x%X", m );
  3054. #endif
  3055. if(g_OpenGLValues->m_matrixMode == D3DTS_TEXTURE0)
  3056. {
  3057. g_OpenGLValues->m_xfrm[g_OpenGLValues->m_matrixMode + g_OpenGLValues->m_curtgt] = *((const D3DMATRIX*)m);
  3058. QuakeSetTransform((D3DTRANSFORMSTATETYPE)(g_OpenGLValues->m_matrixMode + g_OpenGLValues->m_curtgt), (const D3DMATRIX*)m);
  3059. }
  3060. else
  3061. {
  3062. g_OpenGLValues->m_xfrm[g_OpenGLValues->m_matrixMode] = *((const D3DMATRIX*)m);
  3063. QuakeSetTransform(g_OpenGLValues->m_matrixMode, (const D3DMATRIX*)m);
  3064. }
  3065. }
  3066. void APIENTRY glLockArraysEXT(GLint first, GLsizei count)
  3067. {
  3068. #if GETPARMSFORDEBUG
  3069. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glLockArraysEXT: 0x%X 0x%X", first, count );
  3070. #endif
  3071. g_OpenGLValues->m_lckfirst = first;
  3072. g_OpenGLValues->m_lckcount = count;
  3073. }
  3074. void APIENTRY glMaterialf (GLenum face, GLenum pname, GLfloat param)
  3075. {
  3076. #if GETPARMSFORDEBUG
  3077. char log[256];
  3078. char p[40];
  3079. ftoa( (double)param, p );
  3080. sprintf( log, "glMaterialf: 0x%X 0x%X %s", face, pname, p );
  3081. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, log );
  3082. #endif
  3083. if(face == GL_FRONT || face == GL_FRONT_AND_BACK)
  3084. {
  3085. switch(pname)
  3086. {
  3087. case GL_SHININESS:
  3088. g_OpenGLValues->m_material.Power = param;
  3089. break;
  3090. #if DODPFS
  3091. default:
  3092. char junk[256];
  3093. sprintf( junk, "Wrapper: Unimplemented Material (0x%X)\n", pname );
  3094. OutputDebugStringA( junk );
  3095. #endif
  3096. }
  3097. g_OpenGLValues->m_d3ddev->SetMaterial(&g_OpenGLValues->m_material);
  3098. }
  3099. #if DODPFS
  3100. else
  3101. {
  3102. OutputDebugStringA("Wrapper: Back face material properties ignored\n");
  3103. }
  3104. #endif
  3105. }
  3106. void APIENTRY glMaterialfv (GLenum face, GLenum pname, const GLfloat *params)
  3107. {
  3108. #if GETPARMSFORDEBUG
  3109. char log[256];
  3110. char p[40];
  3111. ftoa( (double)*params, p );
  3112. sprintf( log, "glMaterialfv: 0x%X 0x%X %s", face, pname, p );
  3113. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, log );
  3114. #endif
  3115. if(face == GL_FRONT || face == GL_FRONT_AND_BACK)
  3116. {
  3117. switch(pname)
  3118. {
  3119. case GL_AMBIENT:
  3120. MEMCPY(&g_OpenGLValues->m_material.Ambient, params, sizeof(D3DCOLORVALUE));
  3121. break;
  3122. case GL_DIFFUSE:
  3123. MEMCPY(&g_OpenGLValues->m_material.Diffuse, params, sizeof(D3DCOLORVALUE));
  3124. break;
  3125. case GL_SPECULAR:
  3126. MEMCPY(&g_OpenGLValues->m_material.Specular, params, sizeof(D3DCOLORVALUE));
  3127. break;
  3128. case GL_EMISSION:
  3129. MEMCPY(&g_OpenGLValues->m_material.Emissive, params, sizeof(D3DCOLORVALUE));
  3130. break;
  3131. case GL_SHININESS:
  3132. g_OpenGLValues->m_material.Power = *params;
  3133. break;
  3134. case GL_AMBIENT_AND_DIFFUSE:
  3135. MEMCPY(&g_OpenGLValues->m_material.Ambient, params, sizeof(D3DCOLORVALUE));
  3136. MEMCPY(&g_OpenGLValues->m_material.Diffuse, params, sizeof(D3DCOLORVALUE));
  3137. break;
  3138. #if DODPFS
  3139. default:
  3140. char junk[256];
  3141. sprintf( junk, "Wrapper: Unimplemented Material (0x%X)\n", pname );
  3142. OutputDebugStringA( junk );
  3143. #endif
  3144. }
  3145. g_OpenGLValues->m_d3ddev->SetMaterial(&g_OpenGLValues->m_material);
  3146. }
  3147. #if DODPFS
  3148. else
  3149. {
  3150. OutputDebugStringA("Wrapper: Back face material properties ignored\n");
  3151. }
  3152. #endif
  3153. }
  3154. void APIENTRY glMTexCoord2fSGIS(GLenum target, GLfloat s, GLfloat t)
  3155. {
  3156. #if GETPARMSFORDEBUG
  3157. char log[256];
  3158. char ps[40];
  3159. char pt[40];
  3160. ftoa( (double)s, ps );
  3161. ftoa( (double)t, pt );
  3162. sprintf( log, "glMTexCoord2fSGIS: 0x%X %s %s", target, ps, pt );
  3163. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, log );
  3164. #endif
  3165. if(target == GL_TEXTURE0_SGIS) {
  3166. g_OpenGLValues->m_tu = s;
  3167. g_OpenGLValues->m_tv = t;
  3168. }
  3169. else {
  3170. g_OpenGLValues->m_tu2 = s;
  3171. g_OpenGLValues->m_tv2 = t;
  3172. }
  3173. }
  3174. void APIENTRY glMultiTexCoord2fARB (GLenum texture, GLfloat s, GLfloat t)
  3175. {
  3176. #if GETPARMSFORDEBUG
  3177. char log[256];
  3178. char ps[40];
  3179. char pt[40];
  3180. ftoa( (double)s, ps );
  3181. ftoa( (double)t, pt );
  3182. sprintf( log, "glMultiTexCoord2fARB: 0x%X %s %s", texture, ps, pt );
  3183. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, log );
  3184. #endif
  3185. if(texture == GL_TEXTURE0_ARB)
  3186. {
  3187. g_OpenGLValues->m_tu = s;
  3188. g_OpenGLValues->m_tv = t;
  3189. }
  3190. else
  3191. {
  3192. g_OpenGLValues->m_tu2 = s;
  3193. g_OpenGLValues->m_tv2 = t;
  3194. }
  3195. }
  3196. void APIENTRY glMultiTexCoord2fvARB (GLenum texture, const GLfloat *t)
  3197. {
  3198. #if GETPARMSFORDEBUG
  3199. char log[256];
  3200. char ps[40];
  3201. char pt[40];
  3202. ftoa( (double)t[0], ps );
  3203. ftoa( (double)t[1], pt );
  3204. sprintf( log, "glMultiTexCoord2fvARB: 0x%X %s %s", texture, ps, pt );
  3205. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, log );
  3206. #endif
  3207. if(texture == GL_TEXTURE0_ARB)
  3208. {
  3209. g_OpenGLValues->m_tu = t[0];
  3210. g_OpenGLValues->m_tv = t[1];
  3211. }
  3212. else
  3213. {
  3214. g_OpenGLValues->m_tu2 = t[0];
  3215. g_OpenGLValues->m_tv2 = t[1];
  3216. }
  3217. }
  3218. void APIENTRY glMultMatrixd (const GLdouble *m)
  3219. {
  3220. #if GETPARMSFORDEBUG
  3221. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glMultMatrixd: 0x%X", m );
  3222. #endif
  3223. D3DMATRIX f;
  3224. for(unsigned i = 0; i < 16; ++i)
  3225. {
  3226. ((FLOAT*)&f)[i] = (FLOAT)m[i];
  3227. }
  3228. if(g_OpenGLValues->m_matrixMode == D3DTS_TEXTURE0)
  3229. {
  3230. MultiplyMatrix((D3DTRANSFORMSTATETYPE)(g_OpenGLValues->m_matrixMode + g_OpenGLValues->m_curtgt), f);
  3231. QuakeSetTransform((D3DTRANSFORMSTATETYPE)(g_OpenGLValues->m_matrixMode + g_OpenGLValues->m_curtgt), &g_OpenGLValues->m_xfrm[g_OpenGLValues->m_matrixMode + g_OpenGLValues->m_curtgt]);
  3232. }
  3233. else
  3234. {
  3235. MultiplyMatrix(g_OpenGLValues->m_matrixMode, f);
  3236. QuakeSetTransform(g_OpenGLValues->m_matrixMode, &g_OpenGLValues->m_xfrm[g_OpenGLValues->m_matrixMode]);
  3237. }
  3238. }
  3239. void APIENTRY glMultMatrixf (const GLfloat *m)
  3240. {
  3241. #if GETPARMSFORDEBUG
  3242. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glMultMatrixf: 0x%X", m );
  3243. #endif
  3244. if(g_OpenGLValues->m_matrixMode == D3DTS_TEXTURE0)
  3245. {
  3246. MultiplyMatrix((D3DTRANSFORMSTATETYPE)(g_OpenGLValues->m_matrixMode + g_OpenGLValues->m_curtgt), *((const D3DMATRIX*)m));
  3247. QuakeSetTransform((D3DTRANSFORMSTATETYPE)(g_OpenGLValues->m_matrixMode + g_OpenGLValues->m_curtgt), &g_OpenGLValues->m_xfrm[g_OpenGLValues->m_matrixMode + g_OpenGLValues->m_curtgt]);
  3248. }
  3249. else
  3250. {
  3251. MultiplyMatrix(g_OpenGLValues->m_matrixMode, *((const D3DMATRIX*)m));
  3252. QuakeSetTransform(g_OpenGLValues->m_matrixMode, &g_OpenGLValues->m_xfrm[g_OpenGLValues->m_matrixMode]);
  3253. }
  3254. }
  3255. void APIENTRY glNormal3bv (const GLbyte *v)
  3256. {
  3257. #if GETPARMSFORDEBUG
  3258. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glNormal3bv: 0x%X 0x%X 0x%X", v[0], v[1], v[2] );
  3259. #endif
  3260. g_OpenGLValues->m_nx = v[0];
  3261. g_OpenGLValues->m_ny = v[1];
  3262. g_OpenGLValues->m_nz = v[2];
  3263. }
  3264. void APIENTRY glNormal3fv (const GLfloat *v)
  3265. {
  3266. #if GETPARMSFORDEBUG
  3267. char log[256];
  3268. char v0[40];
  3269. char v1[40];
  3270. char v2[40];
  3271. ftoa( (double)v[0], v0 );
  3272. ftoa( (double)v[1], v1 );
  3273. ftoa( (double)v[2], v2 );
  3274. sprintf( log, "glNormal3fv: %s %s %s", v0, v1, v2 );
  3275. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, log );
  3276. #endif
  3277. g_OpenGLValues->m_nx = v[0];
  3278. g_OpenGLValues->m_ny = v[1];
  3279. g_OpenGLValues->m_nz = v[2];
  3280. }
  3281. void APIENTRY glOrtho (GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble zNear, GLdouble zFar)
  3282. {
  3283. #if GETPARMSFORDEBUG
  3284. char log[256];
  3285. char l[40], r[40], b[40], t[40], zn[40], zf[40];
  3286. ftoa( (double)left, l );
  3287. ftoa( (double)right, r );
  3288. ftoa( (double)bottom, b );
  3289. ftoa( (double)top, t );
  3290. ftoa( (double)zNear, zn );
  3291. ftoa( (double)zFar, zf );
  3292. sprintf( log, "glOrtho: %s %s %s %s %s %s", l, r, b, t, zn, zf );
  3293. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, log );
  3294. #endif
  3295. D3DMATRIX f;
  3296. f._11 = (FLOAT)(2.0 / (right - left));
  3297. f._21 = 0.f;
  3298. f._31 = 0.f;
  3299. f._41 = (FLOAT)(-(right + left) / (right - left));
  3300. f._12 = 0.f;
  3301. f._22 = (FLOAT)(2.0 / (top - bottom));
  3302. f._32 = 0.f;
  3303. f._42 = (FLOAT)(-(top + bottom) / (top - bottom));
  3304. f._13 = 0.f;
  3305. f._23 = 0.f;
  3306. f._33 = (FLOAT)(-2.0 / (zFar - zNear));
  3307. f._43 = (FLOAT)(-(zFar + zNear) / (zFar - zNear));
  3308. f._14 = 0.f;
  3309. f._24 = 0.f;
  3310. f._34 = 0.f;
  3311. f._44 = 1.f;
  3312. if(g_OpenGLValues->m_matrixMode == D3DTS_TEXTURE0)
  3313. {
  3314. MultiplyMatrix((D3DTRANSFORMSTATETYPE)(g_OpenGLValues->m_matrixMode + g_OpenGLValues->m_curtgt), f);
  3315. QuakeSetTransform((D3DTRANSFORMSTATETYPE)(g_OpenGLValues->m_matrixMode + g_OpenGLValues->m_curtgt), &g_OpenGLValues->m_xfrm[g_OpenGLValues->m_matrixMode + g_OpenGLValues->m_curtgt]);
  3316. }
  3317. else
  3318. {
  3319. MultiplyMatrix(g_OpenGLValues->m_matrixMode, f);
  3320. QuakeSetTransform(g_OpenGLValues->m_matrixMode, &g_OpenGLValues->m_xfrm[g_OpenGLValues->m_matrixMode]);
  3321. }
  3322. }
  3323. void APIENTRY glPolygonMode (GLenum face, GLenum mode)
  3324. {
  3325. #if GETPARMSFORDEBUG
  3326. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glPolygonMode: 0x%X 0x%X", face, mode );
  3327. #endif
  3328. int statevalue=-1;
  3329. switch(mode) {
  3330. case GL_POINT:
  3331. statevalue=D3DFILL_POINT;
  3332. break;
  3333. case GL_LINE:
  3334. statevalue=D3DFILL_WIREFRAME;
  3335. break;
  3336. case GL_FILL:
  3337. statevalue=D3DFILL_SOLID;
  3338. break;
  3339. }
  3340. if(statevalue >= 0) {
  3341. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_FILLMODE, (DWORD)statevalue);
  3342. }
  3343. }
  3344. void APIENTRY glPolygonOffset (GLfloat factor, GLfloat units)
  3345. {
  3346. #if GETPARMSFORDEBUG
  3347. char log[256];
  3348. char f[40];
  3349. char u[40];
  3350. ftoa( (double)factor, f );
  3351. ftoa( (double)units, u );
  3352. sprintf( log, "glPolygonOffset: %s %s", f, u );
  3353. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, log );
  3354. #endif
  3355. }
  3356. void APIENTRY glPopAttrib (void)
  3357. {
  3358. #if GETPARMSFORDEBUG
  3359. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glPopAttrib" );
  3360. #endif
  3361. g_OpenGLValues->m_d3ddev->ApplyStateBlock(g_OpenGLValues->m_cbufbit);
  3362. }
  3363. void APIENTRY glPopMatrix (void)
  3364. {
  3365. #if GETPARMSFORDEBUG
  3366. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glPopMatrix" );
  3367. #endif
  3368. if(g_OpenGLValues->m_matrixMode == D3DTS_WORLD) {
  3369. if(g_OpenGLValues->m_matrixStack[0].length() == 0)
  3370. return;
  3371. LListManip<D3DMATRIX> m(&g_OpenGLValues->m_matrixStack[0]);
  3372. g_OpenGLValues->m_xfrm[g_OpenGLValues->m_matrixMode] = m();
  3373. QuakeSetTransform(g_OpenGLValues->m_matrixMode, &(m()));
  3374. m.remove();
  3375. }
  3376. else if(g_OpenGLValues->m_matrixMode == D3DTS_PROJECTION) {
  3377. if(g_OpenGLValues->m_matrixStack[1].length() == 0)
  3378. return;
  3379. LListManip<D3DMATRIX> m(&g_OpenGLValues->m_matrixStack[1]);
  3380. g_OpenGLValues->m_xfrm[g_OpenGLValues->m_matrixMode] = m();
  3381. QuakeSetTransform(g_OpenGLValues->m_matrixMode, &(m()));
  3382. m.remove();
  3383. }
  3384. else {
  3385. if(g_OpenGLValues->m_matrixStack[2 + g_OpenGLValues->m_curtgt].length() == 0)
  3386. return;
  3387. LListManip<D3DMATRIX> m(&g_OpenGLValues->m_matrixStack[2 + g_OpenGLValues->m_curtgt]);
  3388. g_OpenGLValues->m_xfrm[g_OpenGLValues->m_matrixMode + g_OpenGLValues->m_curtgt] = m();
  3389. QuakeSetTransform((D3DTRANSFORMSTATETYPE)(g_OpenGLValues->m_matrixMode + g_OpenGLValues->m_curtgt), &(m()));
  3390. m.remove();
  3391. }
  3392. }
  3393. void APIENTRY glPushAttrib (GLbitfield mask)
  3394. {
  3395. #if GETPARMSFORDEBUG
  3396. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glPushAttrib: 0x%X", mask );
  3397. #endif
  3398. if(mask & GL_COLOR_BUFFER_BIT)
  3399. {
  3400. g_OpenGLValues->m_d3ddev->CaptureStateBlock(g_OpenGLValues->m_cbufbit);
  3401. }
  3402. #if DODPFS
  3403. else
  3404. {
  3405. char junk[256];
  3406. sprintf( junk, "Wrapper: Attrib push not implemented (0x%X)\n", mask );
  3407. OutputDebugStringA( junk );
  3408. }
  3409. #endif
  3410. }
  3411. void APIENTRY glPushMatrix (void)
  3412. {
  3413. #if GETPARMSFORDEBUG
  3414. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glPushMatrix" );
  3415. #endif
  3416. if(g_OpenGLValues->m_matrixMode == D3DTS_WORLD)
  3417. {
  3418. g_OpenGLValues->m_matrixStack[0].prepend(g_OpenGLValues->m_xfrm[g_OpenGLValues->m_matrixMode]);
  3419. }
  3420. else if(g_OpenGLValues->m_matrixMode == D3DTS_PROJECTION)
  3421. {
  3422. g_OpenGLValues->m_matrixStack[1].prepend(g_OpenGLValues->m_xfrm[g_OpenGLValues->m_matrixMode]);
  3423. }
  3424. else
  3425. {
  3426. g_OpenGLValues->m_matrixStack[2 + g_OpenGLValues->m_curtgt].prepend(g_OpenGLValues->m_xfrm[g_OpenGLValues->m_matrixMode + g_OpenGLValues->m_curtgt]);
  3427. }
  3428. }
  3429. void APIENTRY glRotatef (GLfloat angle, GLfloat x, GLfloat y, GLfloat z)
  3430. {
  3431. #if GETPARMSFORDEBUG
  3432. char log[256];
  3433. char va[40];
  3434. char vx[40];
  3435. char vy[40];
  3436. char vz[40];
  3437. ftoa( (double)angle, va );
  3438. ftoa( (double)x, vx );
  3439. ftoa( (double)y, vy );
  3440. ftoa( (double)z, vz );
  3441. sprintf( log, "glRotatef: %s %s %s %s", va, vx, vy, vz );
  3442. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, log );
  3443. #endif
  3444. if(angle == 0.f)
  3445. {
  3446. // Early out for Quake II engine since angle = 0 does not prevent the matrix from getting bad when x, y & z are 0.
  3447. return;
  3448. }
  3449. float u[3];
  3450. double norm = sqrt(x * x + y * y + z * z);
  3451. u[0] = (float)(x / norm);
  3452. u[1] = (float)(y / norm);
  3453. u[2] = (float)(z / norm);
  3454. double ra = angle * PI / 180.f;
  3455. float ca = (float)cos(ra);
  3456. float sa = (float)sin(ra);
  3457. D3DMATRIX s;
  3458. s._11 = 0.f; s._21 = -u[2]; s._31 = u[1];
  3459. s._12 = u[2]; s._22 = 0.f; s._32 = -u[0];
  3460. s._13 = -u[1]; s._23 = u[0]; s._33 = 0.f;
  3461. D3DMATRIX uu;
  3462. uu._11 = u[0] * u[0]; uu._21 = u[0] * u[1]; uu._31 = u[0] * u[2];
  3463. uu._12 = u[1] * u[0]; uu._22 = u[1] * u[1]; uu._32 = u[1] * u[2];
  3464. uu._13 = u[2] * u[0]; uu._23 = u[2] * u[1]; uu._33 = u[2] * u[2];
  3465. D3DMATRIX r;
  3466. r._11 = uu._11 + ca * (1.f - uu._11) + sa * s._11;
  3467. r._21 = uu._21 + ca * (0.f - uu._21) + sa * s._21;
  3468. r._31 = uu._31 + ca * (0.f - uu._31) + sa * s._31;
  3469. r._41 = 0.f;
  3470. r._12 = uu._12 + ca * (0.f - uu._12) + sa * s._12;
  3471. r._22 = uu._22 + ca * (1.f - uu._22) + sa * s._22;
  3472. r._32 = uu._32 + ca * (0.f - uu._32) + sa * s._32;
  3473. r._42 = 0.f;
  3474. r._13 = uu._13 + ca * (0.f - uu._13) + sa * s._13;
  3475. r._23 = uu._23 + ca * (0.f - uu._23) + sa * s._23;
  3476. r._33 = uu._33 + ca * (1.f - uu._33) + sa * s._33;
  3477. r._43 = 0.f;
  3478. r._14 = 0.f;
  3479. r._24 = 0.f;
  3480. r._34 = 0.f;
  3481. r._44 = 1.f;
  3482. if(g_OpenGLValues->m_matrixMode == D3DTS_TEXTURE0)
  3483. {
  3484. MultiplyMatrix((D3DTRANSFORMSTATETYPE)(g_OpenGLValues->m_matrixMode + g_OpenGLValues->m_curtgt), r);
  3485. QuakeSetTransform((D3DTRANSFORMSTATETYPE)(g_OpenGLValues->m_matrixMode + g_OpenGLValues->m_curtgt), &g_OpenGLValues->m_xfrm[g_OpenGLValues->m_matrixMode + g_OpenGLValues->m_curtgt]);
  3486. }
  3487. else
  3488. {
  3489. MultiplyMatrix(g_OpenGLValues->m_matrixMode, r);
  3490. QuakeSetTransform(g_OpenGLValues->m_matrixMode, &g_OpenGLValues->m_xfrm[g_OpenGLValues->m_matrixMode]);
  3491. }
  3492. }
  3493. void APIENTRY glScalef (GLfloat x, GLfloat y, GLfloat z)
  3494. {
  3495. #if GETPARMSFORDEBUG
  3496. char log[256];
  3497. char vx[40];
  3498. char vy[40];
  3499. char vz[40];
  3500. ftoa( (double)x, vx );
  3501. ftoa( (double)y, vy );
  3502. ftoa( (double)z, vz );
  3503. sprintf( log, "glScalef: %s %s %s", vx, vy, vz );
  3504. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, log );
  3505. #endif
  3506. D3DMATRIX f;
  3507. f._11 = x; f._21 = 0.f; f._31 = 0.f; f._41 = 0.f;
  3508. f._12 = 0.f; f._22 = y; f._32 = 0.f; f._42 = 0.f;
  3509. f._13 = 0.f; f._23 = 0.f; f._33 = z; f._43 = 0.f;
  3510. f._14 = 0.f; f._24 = 0.f; f._34 = 0.f; f._44 = 1.f;
  3511. if(g_OpenGLValues->m_matrixMode == D3DTS_TEXTURE0)
  3512. {
  3513. MultiplyMatrix((D3DTRANSFORMSTATETYPE)(g_OpenGLValues->m_matrixMode + g_OpenGLValues->m_curtgt), f);
  3514. QuakeSetTransform((D3DTRANSFORMSTATETYPE)(g_OpenGLValues->m_matrixMode + g_OpenGLValues->m_curtgt), &g_OpenGLValues->m_xfrm[g_OpenGLValues->m_matrixMode + g_OpenGLValues->m_curtgt]);
  3515. }
  3516. else
  3517. {
  3518. MultiplyMatrix(g_OpenGLValues->m_matrixMode, f);
  3519. QuakeSetTransform(g_OpenGLValues->m_matrixMode, &g_OpenGLValues->m_xfrm[g_OpenGLValues->m_matrixMode]);
  3520. }
  3521. }
  3522. void APIENTRY glScissor (GLint x, GLint y, GLsizei width, GLsizei height)
  3523. {
  3524. #if GETPARMSFORDEBUG
  3525. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glScissor: 0x%X 0x%X 0x%X 0x%X", x, y, width, height );
  3526. #endif
  3527. RECT wrect, screct, xrect;
  3528. wrect.left = 0;
  3529. wrect.top = 0;
  3530. wrect.right = g_OpenGLValues->m_winWidth;
  3531. wrect.bottom = g_OpenGLValues->m_winHeight;
  3532. screct.left = x;
  3533. screct.top = y;
  3534. screct.right = x + width;
  3535. screct.bottom = y + height;
  3536. IntersectRect(&xrect, &wrect, &screct);
  3537. // LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glScissor: %d %d %d %d WRECT: %d %d %d %d SCRECT: %d %d %d %d XRECT %d %d %d %d", x, y, width, height, wrect.left, wrect.top, wrect.right, wrect.bottom, screct.left, screct.top, screct.right, screct.bottom, xrect.left, xrect.top, xrect.right, xrect.bottom );
  3538. g_OpenGLValues->m_scix = xrect.left;
  3539. g_OpenGLValues->m_sciy = xrect.top;
  3540. g_OpenGLValues->m_sciw = xrect.right - xrect.left;
  3541. g_OpenGLValues->m_scih = xrect.bottom - xrect.top;
  3542. g_OpenGLValues->m_updvwp = TRUE;
  3543. }
  3544. void APIENTRY glSelectTextureSGIS(GLenum target)
  3545. {
  3546. #if GETPARMSFORDEBUG
  3547. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glSelectTextureSGIS: 0x%X", target );
  3548. #endif
  3549. g_OpenGLValues->m_curtgt = target == GL_TEXTURE0_SGIS ? 0 : 1;
  3550. }
  3551. void APIENTRY glShadeModel (GLenum mode)
  3552. {
  3553. #if GETPARMSFORDEBUG
  3554. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glShadeModel: 0x%X", mode );
  3555. #endif
  3556. if(mode == GL_SMOOTH)
  3557. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_GOURAUD);
  3558. else
  3559. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_FLAT);
  3560. }
  3561. void APIENTRY glTexCoord2f (GLfloat s, GLfloat t)
  3562. {
  3563. #if GETPARMSFORDEBUG
  3564. char log[256];
  3565. char ps[40];
  3566. char pt[40];
  3567. ftoa( (double)s, ps );
  3568. ftoa( (double)t, pt );
  3569. sprintf( log, "glTexCoord2f: %s %s", ps, pt );
  3570. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, log );
  3571. #endif
  3572. g_OpenGLValues->m_tu = s;
  3573. g_OpenGLValues->m_tv = t;
  3574. }
  3575. void APIENTRY glTexCoord2fv (const GLfloat *t)
  3576. {
  3577. #if GETPARMSFORDEBUG
  3578. char log[256];
  3579. char ps[40];
  3580. char pt[40];
  3581. ftoa( (double)t[0], ps );
  3582. ftoa( (double)t[1], pt );
  3583. sprintf( log, "glTexCoord2fv: %s %s", ps, pt );
  3584. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, log );
  3585. #endif
  3586. g_OpenGLValues->m_tu = t[0];
  3587. g_OpenGLValues->m_tv = t[1];
  3588. }
  3589. void APIENTRY glTexCoordPointer (GLint size, GLenum type, GLsizei stride, const GLvoid *pointer)
  3590. {
  3591. #if GETPARMSFORDEBUG
  3592. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glTexCoordPointer: 0x%X 0x%X 0x%X 0x%X", size, type, stride, pointer );
  3593. #endif
  3594. if(size == 2 && type == GL_FLOAT)
  3595. g_OpenGLValues->m_texcoordary[g_OpenGLValues->m_client_active_texture_arb] = (GLfloat*)pointer;
  3596. #if DODPFS
  3597. else
  3598. {
  3599. char junk[256];
  3600. sprintf( junk, "TexCoord array not supported (size:0x%X type:0x%X)\n", size, type );
  3601. OutputDebugStringA( junk );
  3602. }
  3603. #endif
  3604. if(stride == 0)
  3605. {
  3606. stride = 8;
  3607. }
  3608. g_OpenGLValues->m_texcoordarystride[g_OpenGLValues->m_client_active_texture_arb] = stride;
  3609. }
  3610. void APIENTRY glTexEnvf (GLenum target, GLenum pname, GLfloat param)
  3611. {
  3612. #if GETPARMSFORDEBUG
  3613. char log[256];
  3614. sprintf( log, "glTexEnvf: 0x%X 0x%X 0x%X", target, pname, (int)param );
  3615. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, log );
  3616. #endif
  3617. if(pname == GL_TEXTURE_ENV_MODE) {
  3618. g_OpenGLValues->m_blendmode[g_OpenGLValues->m_curtgt] = (int)param;
  3619. g_OpenGLValues->m_texHandleValid = FALSE;
  3620. }
  3621. #if DODPFS
  3622. else
  3623. {
  3624. char junk[256];
  3625. sprintf( junk, "Wrapper: GL_TEXTURE_ENV_COLOR not implemented (0x%X)\n", pname );
  3626. OutputDebugStringA( junk );
  3627. }
  3628. #endif
  3629. }
  3630. void APIENTRY glTexEnvi (GLenum target, GLenum pname, GLint param)
  3631. {
  3632. #if GETPARMSFORDEBUG
  3633. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glTexEnvi: 0x%X 0x%X 0x%X", target, pname, param );
  3634. #endif
  3635. if(pname == GL_TEXTURE_ENV_MODE) {
  3636. g_OpenGLValues->m_blendmode[g_OpenGLValues->m_curtgt] = param;
  3637. g_OpenGLValues->m_texHandleValid = FALSE;
  3638. }
  3639. #if DODPFS
  3640. else
  3641. {
  3642. char junk[256];
  3643. sprintf( junk, "Wrapper: GL_TEXTURE_ENV_COLOR not implemented (0x%X)\n", pname );
  3644. OutputDebugStringA( junk );
  3645. }
  3646. #endif
  3647. }
  3648. void APIENTRY glTexGeni (GLenum coord, GLenum pname, GLint param)
  3649. {
  3650. #if GETPARMSFORDEBUG
  3651. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glTexGeni: 0x%X 0x%X 0x%X", coord, pname, param );
  3652. #endif
  3653. if(coord == GL_S)
  3654. {
  3655. if(pname == GL_TEXTURE_GEN_MODE)
  3656. {
  3657. if(param == GL_SPHERE_MAP)
  3658. {
  3659. g_OpenGLValues->m_d3ddev->SetTextureStageState(g_OpenGLValues->m_curtgt, D3DTSS_TEXCOORDINDEX, g_OpenGLValues->m_curtgt | D3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR);
  3660. }
  3661. #if DODPFS
  3662. else
  3663. {
  3664. char junk[256];
  3665. sprintf( junk, "Wrapper: TexGen param not implemented (0x%X)\n", param );
  3666. OutputDebugStringA( junk );
  3667. }
  3668. #endif
  3669. }
  3670. #if DODPFS
  3671. else
  3672. {
  3673. char junk[256];
  3674. sprintf( junk, "Wrapper: TexGen pname not implemented (0x%X)\n", pname );
  3675. OutputDebugStringA( junk );
  3676. }
  3677. #endif
  3678. }
  3679. }
  3680. void APIENTRY glTexImage2D (GLenum target, GLint level, GLint internalformat, GLsizei glwidth, GLsizei glheight, GLint border, GLenum format, GLenum type, const GLvoid *pixels)
  3681. {
  3682. #if GETPARMSFORDEBUG
  3683. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glTexImage2D: 0x%X 0x%X 0x%X 0x%X 0x%X 0x%X 0x%X 0x%X 0x%X", target, level, internalformat, glwidth, glheight, border, format, type, pixels );
  3684. #endif
  3685. DWORD width, height;
  3686. TexInfo &ti = g_OpenGLValues->m_tex[g_OpenGLValues->m_curstagebinding[g_OpenGLValues->m_curtgt]];
  3687. if(ti.m_next >= 0)
  3688. {
  3689. if(ti.m_prev < 0)
  3690. {
  3691. g_OpenGLValues->m_free = ti.m_next;
  3692. g_OpenGLValues->m_tex[ti.m_next].m_prev = ti.m_prev;
  3693. ti.m_next = ti.m_prev = -1;
  3694. }
  3695. else
  3696. {
  3697. g_OpenGLValues->m_tex[ti.m_prev].m_next = ti.m_next;
  3698. g_OpenGLValues->m_tex[ti.m_next].m_prev = ti.m_prev;
  3699. ti.m_next = ti.m_prev = -1;
  3700. }
  3701. }
  3702. /* See if texture needs to be subsampled */
  3703. if(g_OpenGLValues->m_subsample) {
  3704. if(glwidth > 256 || glheight > 256) {
  3705. if(glwidth > glheight) {
  3706. width = 256;
  3707. height = (glheight * 256) / glwidth;
  3708. }
  3709. else {
  3710. height = 256;
  3711. width = (glwidth * 256) / glheight;
  3712. }
  3713. }
  3714. else {
  3715. width = glwidth;
  3716. height = glheight;
  3717. }
  3718. }
  3719. else {
  3720. width = glwidth;
  3721. height = glheight;
  3722. }
  3723. /* See if texture needs to be square */
  3724. if(g_OpenGLValues->m_makeSquare) {
  3725. if(height > width) {
  3726. width = height;
  3727. }
  3728. else {
  3729. height = width;
  3730. }
  3731. }
  3732. if(level == 0) {
  3733. IDirect3DTexture8 *ddsurf;
  3734. D3DFORMAT fmt;
  3735. switch(internalformat) {
  3736. case 1:
  3737. case GL_LUMINANCE:
  3738. case GL_LUMINANCE8:
  3739. fmt = g_OpenGLValues->m_ddLuminanceSurfFormat;
  3740. break;
  3741. case 3:
  3742. case GL_RGB:
  3743. case GL_RGB5:
  3744. fmt = g_OpenGLValues->m_ddFiveBitSurfFormat;
  3745. break;
  3746. case 4:
  3747. case GL_RGBA:
  3748. case GL_RGBA4:
  3749. fmt = g_OpenGLValues->m_ddFourBitAlphaSurfFormat;
  3750. break;
  3751. case GL_RGB8:
  3752. fmt = g_OpenGLValues->m_ddEightBitSurfFormat;
  3753. break;
  3754. case GL_RGBA8:
  3755. fmt = g_OpenGLValues->m_ddEightBitAlphaSurfFormat;
  3756. break;
  3757. case GL_ALPHA:
  3758. case GL_ALPHA8:
  3759. fmt = g_OpenGLValues->m_ddEightBitAlphaOnlySurfFormat;
  3760. break;
  3761. default:
  3762. #if DODPFS
  3763. char junk[256];
  3764. sprintf( junk, "Wrapper: Unimplemented internalformat (0x%X)\n", internalformat );
  3765. OutputDebugStringA( junk );
  3766. #endif
  3767. return;
  3768. }
  3769. HRESULT ddrval = g_OpenGLValues->m_d3ddev->CreateTexture(width,
  3770. height,
  3771. 1, // levels
  3772. 0,
  3773. fmt,
  3774. D3DPOOL_MANAGED,
  3775. &ddsurf);
  3776. if (FAILED(ddrval))
  3777. {
  3778. #if DODPFS
  3779. char junk[256];
  3780. sprintf( junk, "Wrapper: CreateTexture failed (0x%X)\n", ddrval );
  3781. OutputDebugStringA( junk );
  3782. #endif
  3783. return;
  3784. }
  3785. LPDIRECT3DSURFACE8 pLevel;
  3786. ddrval = ddsurf->GetSurfaceLevel(0, &pLevel);
  3787. if (FAILED(ddrval))
  3788. {
  3789. #if DODPFS
  3790. char junk[256];
  3791. sprintf( junk, "Wrapper: Failed to retrieve surface (0x%X)\n", ddrval );
  3792. OutputDebugStringA( junk );
  3793. #endif
  3794. return;
  3795. }
  3796. LoadSurface(pLevel, format, internalformat, glwidth, glheight, width, height, (const DWORD*)pixels);
  3797. pLevel->Release();
  3798. if(ti.m_ddsurf != 0) {
  3799. ti.m_ddsurf->Release();
  3800. }
  3801. ti.m_dwStage = g_OpenGLValues->m_curtgt;
  3802. ti.m_fmt = fmt;
  3803. ti.m_internalformat = internalformat;
  3804. ti.m_width = width;
  3805. ti.m_height = height;
  3806. ti.m_ddsurf = ddsurf;
  3807. ti.m_oldwidth = glwidth;
  3808. ti.m_oldheight = glheight;
  3809. if(ti.m_block == 0)
  3810. {
  3811. g_OpenGLValues->m_d3ddev->BeginStateBlock();
  3812. g_OpenGLValues->m_d3ddev->SetTextureStageState (g_OpenGLValues->m_curtgt, D3DTSS_ADDRESSU,ti.m_addu);
  3813. g_OpenGLValues->m_d3ddev->SetTextureStageState (g_OpenGLValues->m_curtgt, D3DTSS_ADDRESSV,ti.m_addv);
  3814. g_OpenGLValues->m_d3ddev->SetTextureStageState (g_OpenGLValues->m_curtgt, D3DTSS_MAGFILTER,ti.m_magmode);
  3815. g_OpenGLValues->m_d3ddev->SetTextureStageState (g_OpenGLValues->m_curtgt, D3DTSS_MINFILTER,ti.m_minmode);
  3816. g_OpenGLValues->m_d3ddev->SetTextureStageState (g_OpenGLValues->m_curtgt, D3DTSS_MIPFILTER,ti.m_mipmode);
  3817. g_OpenGLValues->m_d3ddev->SetTexture(g_OpenGLValues->m_curtgt, ti.m_ddsurf);
  3818. g_OpenGLValues->m_d3ddev->EndStateBlock(&ti.m_block);
  3819. ti.m_capture = FALSE;
  3820. }
  3821. else
  3822. {
  3823. ti.m_capture = TRUE;
  3824. }
  3825. }
  3826. else if(level == 1 && g_OpenGLValues->m_usemipmap) { // oops, a mipmap
  3827. IDirect3DTexture8 *ddsurf;
  3828. LPDIRECT3DSURFACE8 pLevel, pSrcLevel;
  3829. HRESULT ddrval = g_OpenGLValues->m_d3ddev->CreateTexture(ti.m_width,
  3830. ti.m_height,
  3831. miplevels(ti.m_width, ti.m_height),
  3832. 0,
  3833. ti.m_fmt,
  3834. D3DPOOL_MANAGED,
  3835. &ddsurf);
  3836. if (FAILED(ddrval)) {
  3837. #if DODPFS
  3838. char junk[256];
  3839. sprintf( junk, "Wrapper: CreateTexture failed (0x%X)\n", ddrval );
  3840. OutputDebugStringA( junk );
  3841. #endif
  3842. return;
  3843. }
  3844. ddsurf->GetSurfaceLevel(0, &pLevel);
  3845. ti.m_ddsurf->GetSurfaceLevel(0, &pSrcLevel);
  3846. ddrval = g_OpenGLValues->m_d3ddev->CopyRects(pSrcLevel, NULL, 0, pLevel, NULL);
  3847. if(FAILED(ddrval))
  3848. {
  3849. #if DODPFS
  3850. char junk[256];
  3851. sprintf( junk, "Wrapper: CopyRects failed (0x%X)\n", ddrval );
  3852. OutputDebugStringA( junk );
  3853. #endif
  3854. return;
  3855. }
  3856. pLevel->Release();
  3857. pSrcLevel->Release();
  3858. ti.m_ddsurf->Release();
  3859. ti.m_ddsurf = ddsurf;
  3860. ti.m_ddsurf->GetSurfaceLevel(1, &pLevel);
  3861. LoadSurface(pLevel, format, internalformat, glwidth, glheight, width, height, (const DWORD*)pixels);
  3862. pLevel->Release();
  3863. ti.m_capture = TRUE;
  3864. }
  3865. else if(g_OpenGLValues->m_usemipmap) {
  3866. if (ti.m_ddsurf!=NULL)
  3867. {
  3868. LPDIRECT3DSURFACE8 pLevel;
  3869. ti.m_ddsurf->GetSurfaceLevel(level, &pLevel);
  3870. LoadSurface(pLevel, format, internalformat, glwidth, glheight, width, height, (const DWORD*)pixels);
  3871. pLevel->Release();
  3872. }
  3873. #if DODPFS
  3874. else
  3875. {
  3876. char junk[256];
  3877. sprintf( junk, "NULL surface pointer %d %d %d %d %d %d %d\n", level, ti.m_width, ti.m_height, glwidth, glheight, width, height );
  3878. OutputDebugStringA( junk );
  3879. }
  3880. #endif
  3881. }
  3882. g_OpenGLValues->m_texHandleValid = FALSE;
  3883. }
  3884. void APIENTRY glTexSubImage2D (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid *pixels)
  3885. {
  3886. #if GETPARMSFORDEBUG
  3887. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glTexSubImage2D: 0x%X 0x%X 0x%X 0x%X 0x%X 0x%X 0x%X 0x%X 0x%X", target, level, xoffset, yoffset, width, height, format, type, pixels );
  3888. #endif
  3889. if(level > 1 && !g_OpenGLValues->m_usemipmap)
  3890. return;
  3891. TexInfo &ti = g_OpenGLValues->m_tex[g_OpenGLValues->m_curstagebinding[g_OpenGLValues->m_curtgt]];
  3892. RECT subimage;
  3893. LPDIRECT3DSURFACE8 pLevel;
  3894. HRESULT ddrval = ti.m_ddsurf->GetSurfaceLevel(level, &pLevel);
  3895. if (FAILED(ddrval))
  3896. {
  3897. #if DODPFS
  3898. char junk[256];
  3899. sprintf( junk, "Wrapper: Failed to retrieve surface (0x%X)\n", ddrval );
  3900. OutputDebugStringA( junk );
  3901. #endif
  3902. return;
  3903. }
  3904. xoffset = (xoffset * ti.m_width) / ti.m_oldwidth;
  3905. yoffset = (yoffset * ti.m_height) / ti.m_oldheight;
  3906. SetRect(&subimage, xoffset, yoffset,
  3907. (width * ti.m_width) / ti.m_oldwidth + xoffset,
  3908. (height * ti.m_height) / ti.m_oldheight + yoffset);
  3909. ddrval = LoadSubSurface( pLevel, format, ti.m_internalformat, width, height, (const DWORD*)pixels, &subimage );
  3910. if (FAILED(ddrval)) {
  3911. #if DODPFS
  3912. char junk[256];
  3913. sprintf( junk, "Wrapper: LoadSubSurface Failure (0x%X)\n", ddrval );
  3914. OutputDebugStringA( junk );
  3915. #endif
  3916. // return;
  3917. }
  3918. pLevel->Release();
  3919. }
  3920. void APIENTRY glTexParameterf (GLenum target, GLenum pname, GLfloat param)
  3921. {
  3922. #if GETPARMSFORDEBUG
  3923. char log[256];
  3924. sprintf( log, "glTexParameterf: 0x%X 0x%X 0x%X", target, pname, (int)param );
  3925. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, log );
  3926. #endif
  3927. switch(pname) {
  3928. case GL_TEXTURE_MIN_FILTER:
  3929. switch((int)param) {
  3930. case GL_NEAREST:
  3931. g_OpenGLValues->m_tex[g_OpenGLValues->m_curstagebinding[g_OpenGLValues->m_curtgt]].m_minmode = D3DTEXF_POINT;
  3932. g_OpenGLValues->m_tex[g_OpenGLValues->m_curstagebinding[g_OpenGLValues->m_curtgt]].m_mipmode = D3DTEXF_NONE;
  3933. break;
  3934. case GL_LINEAR:
  3935. g_OpenGLValues->m_tex[g_OpenGLValues->m_curstagebinding[g_OpenGLValues->m_curtgt]].m_minmode = D3DTEXF_LINEAR;
  3936. g_OpenGLValues->m_tex[g_OpenGLValues->m_curstagebinding[g_OpenGLValues->m_curtgt]].m_mipmode = D3DTEXF_NONE;
  3937. break;
  3938. case GL_NEAREST_MIPMAP_NEAREST:
  3939. g_OpenGLValues->m_tex[g_OpenGLValues->m_curstagebinding[g_OpenGLValues->m_curtgt]].m_minmode = D3DTEXF_POINT;
  3940. g_OpenGLValues->m_tex[g_OpenGLValues->m_curstagebinding[g_OpenGLValues->m_curtgt]].m_mipmode = D3DTEXF_POINT;
  3941. break;
  3942. case GL_NEAREST_MIPMAP_LINEAR:
  3943. g_OpenGLValues->m_tex[g_OpenGLValues->m_curstagebinding[g_OpenGLValues->m_curtgt]].m_minmode = D3DTEXF_POINT;
  3944. g_OpenGLValues->m_tex[g_OpenGLValues->m_curstagebinding[g_OpenGLValues->m_curtgt]].m_mipmode = D3DTEXF_LINEAR;
  3945. break;
  3946. case GL_LINEAR_MIPMAP_NEAREST:
  3947. g_OpenGLValues->m_tex[g_OpenGLValues->m_curstagebinding[g_OpenGLValues->m_curtgt]].m_minmode = D3DTEXF_LINEAR;
  3948. g_OpenGLValues->m_tex[g_OpenGLValues->m_curstagebinding[g_OpenGLValues->m_curtgt]].m_mipmode = D3DTEXF_POINT;
  3949. break;
  3950. case GL_LINEAR_MIPMAP_LINEAR:
  3951. g_OpenGLValues->m_tex[g_OpenGLValues->m_curstagebinding[g_OpenGLValues->m_curtgt]].m_minmode = D3DTEXF_LINEAR;
  3952. g_OpenGLValues->m_tex[g_OpenGLValues->m_curstagebinding[g_OpenGLValues->m_curtgt]].m_mipmode = D3DTEXF_LINEAR;
  3953. break;
  3954. }
  3955. break;
  3956. case GL_TEXTURE_MAG_FILTER:
  3957. if((int)param == GL_NEAREST)
  3958. g_OpenGLValues->m_tex[g_OpenGLValues->m_curstagebinding[g_OpenGLValues->m_curtgt]].m_magmode = D3DTEXF_POINT;
  3959. else
  3960. g_OpenGLValues->m_tex[g_OpenGLValues->m_curstagebinding[g_OpenGLValues->m_curtgt]].m_magmode = D3DTEXF_LINEAR;
  3961. break;
  3962. case GL_TEXTURE_WRAP_S:
  3963. if((int)param == GL_CLAMP)
  3964. g_OpenGLValues->m_tex[g_OpenGLValues->m_curstagebinding[g_OpenGLValues->m_curtgt]].m_addu = D3DTADDRESS_CLAMP;
  3965. else
  3966. //GL_REPEAT falls here
  3967. g_OpenGLValues->m_tex[g_OpenGLValues->m_curstagebinding[g_OpenGLValues->m_curtgt]].m_addu = D3DTADDRESS_WRAP;
  3968. break;
  3969. case GL_TEXTURE_WRAP_T:
  3970. if((int)param == GL_CLAMP)
  3971. g_OpenGLValues->m_tex[g_OpenGLValues->m_curstagebinding[g_OpenGLValues->m_curtgt]].m_addv = D3DTADDRESS_CLAMP;
  3972. else
  3973. //GL_REPEAT falls here
  3974. g_OpenGLValues->m_tex[g_OpenGLValues->m_curstagebinding[g_OpenGLValues->m_curtgt]].m_addv = D3DTADDRESS_WRAP;
  3975. break;
  3976. }
  3977. g_OpenGLValues->m_tex[g_OpenGLValues->m_curstagebinding[g_OpenGLValues->m_curtgt]].m_capture = TRUE;
  3978. g_OpenGLValues->m_texHandleValid = FALSE;
  3979. }
  3980. void APIENTRY glTexParameteri (GLenum target, GLenum pname, GLint param)
  3981. {
  3982. #if GETPARMSFORDEBUG
  3983. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glTexParameteri: 0x%X 0x%X 0x%X", target, pname, param );
  3984. #endif
  3985. glTexParameterf(target, pname, (GLfloat)param);
  3986. }
  3987. void APIENTRY glTranslatef (GLfloat x, GLfloat y, GLfloat z)
  3988. {
  3989. #if GETPARMSFORDEBUG
  3990. char log[256];
  3991. char vx[40];
  3992. char vy[40];
  3993. char vz[40];
  3994. ftoa( (double)x, vx );
  3995. ftoa( (double)y, vy );
  3996. ftoa( (double)z, vz );
  3997. sprintf( log, "glTranslatef: %s %s %s", vx, vy, vz );
  3998. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, log );
  3999. #endif
  4000. D3DMATRIX f;
  4001. f._11 = 1.f; f._21 = 0.f; f._31 = 0.f; f._41 = x;
  4002. f._12 = 0.f; f._22 = 1.f; f._32 = 0.f; f._42 = y;
  4003. f._13 = 0.f; f._23 = 0.f; f._33 = 1.f; f._43 = z;
  4004. f._14 = 0.f; f._24 = 0.f; f._34 = 0.f; f._44 = 1.f;
  4005. if(g_OpenGLValues->m_matrixMode == D3DTS_TEXTURE0)
  4006. {
  4007. MultiplyMatrix((D3DTRANSFORMSTATETYPE)(g_OpenGLValues->m_matrixMode + g_OpenGLValues->m_curtgt), f);
  4008. QuakeSetTransform((D3DTRANSFORMSTATETYPE)(g_OpenGLValues->m_matrixMode + g_OpenGLValues->m_curtgt), &g_OpenGLValues->m_xfrm[g_OpenGLValues->m_matrixMode + g_OpenGLValues->m_curtgt]);
  4009. }
  4010. else
  4011. {
  4012. MultiplyMatrix(g_OpenGLValues->m_matrixMode, f);
  4013. QuakeSetTransform(g_OpenGLValues->m_matrixMode, &g_OpenGLValues->m_xfrm[g_OpenGLValues->m_matrixMode]);
  4014. }
  4015. }
  4016. void APIENTRY glUnlockArraysEXT()
  4017. {
  4018. #if GETPARMSFORDEBUG
  4019. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glUnlockArraysEXT" );
  4020. #endif
  4021. g_OpenGLValues->m_lckfirst = 0;
  4022. g_OpenGLValues->m_lckcount = 0;
  4023. }
  4024. void APIENTRY glVertex2d (GLdouble x, GLdouble y)
  4025. {
  4026. #if GETPARMSFORDEBUG
  4027. char log[256];
  4028. char vx[40];
  4029. char vy[40];
  4030. ftoa( (double)x, vx );
  4031. ftoa( (double)y, vy );
  4032. sprintf( log, "glVertex2d: %s %s", vx, vy );
  4033. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, log );
  4034. #endif
  4035. VertexBufferFilled();
  4036. FLOAT *d3dv = (FLOAT*)&g_OpenGLValues->m_verts[g_OpenGLValues->m_vcnt++];
  4037. *(d3dv++) = (GLfloat)x;
  4038. *(d3dv++) = (GLfloat)y;
  4039. *(d3dv++) = 0.f;
  4040. MEMCPY(d3dv, &g_OpenGLValues->m_nx, sizeof(FLOAT) * 7 + sizeof(D3DCOLOR));
  4041. }
  4042. void APIENTRY glVertex2dv (const GLdouble *v)
  4043. {
  4044. #if GETPARMSFORDEBUG
  4045. char log[256];
  4046. char vx[40];
  4047. char vy[40];
  4048. ftoa( (double)v[0], vx );
  4049. ftoa( (double)v[1], vy );
  4050. sprintf( log, "glVertex2dv: %s %s", vx, vy );
  4051. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, log );
  4052. #endif
  4053. VertexBufferFilled();
  4054. FLOAT *d3dv = (FLOAT*)&g_OpenGLValues->m_verts[g_OpenGLValues->m_vcnt++];
  4055. *(d3dv++) = (GLfloat)*(v++);
  4056. *(d3dv++) = (GLfloat)*(v++);
  4057. *(d3dv++) = 0.f;
  4058. MEMCPY(d3dv, &g_OpenGLValues->m_nx, sizeof(FLOAT) * 7 + sizeof(D3DCOLOR));
  4059. }
  4060. void APIENTRY glVertex2f (GLfloat x, GLfloat y)
  4061. {
  4062. #if GETPARMSFORDEBUG
  4063. char log[256];
  4064. char vx[40];
  4065. char vy[40];
  4066. ftoa( (double)x, vx );
  4067. ftoa( (double)y, vy );
  4068. sprintf( log, "glVertex2f: %s %s", vx, vy );
  4069. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, log );
  4070. #endif
  4071. VertexBufferFilled();
  4072. FLOAT *d3dv = (FLOAT*)&g_OpenGLValues->m_verts[g_OpenGLValues->m_vcnt++];
  4073. *(d3dv++) = x;
  4074. *(d3dv++) = y;
  4075. *(d3dv++) = 0.f;
  4076. MEMCPY(d3dv, &g_OpenGLValues->m_nx, sizeof(FLOAT) * 7 + sizeof(D3DCOLOR));
  4077. }
  4078. void APIENTRY glVertex2fv (const GLfloat *v)
  4079. {
  4080. #if GETPARMSFORDEBUG
  4081. char log[256];
  4082. char vx[40];
  4083. char vy[40];
  4084. ftoa( (double)v[0], vx );
  4085. ftoa( (double)v[1], vy );
  4086. sprintf( log, "glVertex2fv: %s %s", vx, vy );
  4087. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, log );
  4088. #endif
  4089. VertexBufferFilled();
  4090. FLOAT *d3dv = (FLOAT*)&g_OpenGLValues->m_verts[g_OpenGLValues->m_vcnt++];
  4091. *(d3dv++) = *(v++);
  4092. *(d3dv++) = *(v++);
  4093. *(d3dv++) = 0.f;
  4094. MEMCPY(d3dv, &g_OpenGLValues->m_nx, sizeof(FLOAT) * 7 + sizeof(D3DCOLOR));
  4095. }
  4096. void APIENTRY glVertex2i (GLint x, GLint y)
  4097. {
  4098. #if GETPARMSFORDEBUG
  4099. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glVertex2i: %d %d", x, y );
  4100. #endif
  4101. VertexBufferFilled();
  4102. FLOAT *d3dv = (FLOAT*)&g_OpenGLValues->m_verts[g_OpenGLValues->m_vcnt++];
  4103. *(d3dv++) = (GLfloat)x;
  4104. *(d3dv++) = (GLfloat)y;
  4105. *(d3dv++) = 0.f;
  4106. MEMCPY(d3dv, &g_OpenGLValues->m_nx, sizeof(FLOAT) * 7 + sizeof(D3DCOLOR));
  4107. }
  4108. void APIENTRY glVertex2iv (const GLint *v)
  4109. {
  4110. #if GETPARMSFORDEBUG
  4111. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glVertex2iv: %d %d", v[0], v[1] );
  4112. #endif
  4113. VertexBufferFilled();
  4114. FLOAT *d3dv = (FLOAT*)&g_OpenGLValues->m_verts[g_OpenGLValues->m_vcnt++];
  4115. *(d3dv++) = (GLfloat) *(v++);
  4116. *(d3dv++) = (GLfloat) *(v++);
  4117. *(d3dv++) = 0.f;
  4118. MEMCPY(d3dv, &g_OpenGLValues->m_nx, sizeof(FLOAT) * 7 + sizeof(D3DCOLOR));
  4119. }
  4120. void APIENTRY glVertex2s (GLshort x, GLshort y)
  4121. {
  4122. #if GETPARMSFORDEBUG
  4123. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glVertex2s: %d %d", x, y );
  4124. #endif
  4125. VertexBufferFilled();
  4126. FLOAT *d3dv = (FLOAT*)&g_OpenGLValues->m_verts[g_OpenGLValues->m_vcnt++];
  4127. *(d3dv++) = (GLfloat)x;
  4128. *(d3dv++) = (GLfloat)y;
  4129. *(d3dv++) = 0.f;
  4130. MEMCPY(d3dv, &g_OpenGLValues->m_nx, sizeof(FLOAT) * 7 + sizeof(D3DCOLOR));
  4131. }
  4132. void APIENTRY glVertex2sv (const GLshort *v)
  4133. {
  4134. #if GETPARMSFORDEBUG
  4135. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glVertex2sv: %s %s", v[0], v[1] );
  4136. #endif
  4137. VertexBufferFilled();
  4138. FLOAT *d3dv = (FLOAT*)&g_OpenGLValues->m_verts[g_OpenGLValues->m_vcnt++];
  4139. *(d3dv++) = (GLfloat) *(v++);
  4140. *(d3dv++) = (GLfloat) *(v++);
  4141. *(d3dv++) = 0.f;
  4142. MEMCPY(d3dv, &g_OpenGLValues->m_nx, sizeof(FLOAT) * 7 + sizeof(D3DCOLOR));
  4143. }
  4144. void APIENTRY glVertex3d (GLdouble x, GLdouble y, GLdouble z)
  4145. {
  4146. #if GETPARMSFORDEBUG
  4147. char log[256];
  4148. char vx[40];
  4149. char vy[40];
  4150. char vz[40];
  4151. ftoa( (double)x, vx );
  4152. ftoa( (double)y, vy );
  4153. ftoa( (double)z, vz );
  4154. sprintf( log, "glVertex3d: %s %s %s", vx, vy, vz );
  4155. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, log );
  4156. #endif
  4157. VertexBufferFilled();
  4158. FLOAT *d3dv = (FLOAT*)&g_OpenGLValues->m_verts[g_OpenGLValues->m_vcnt++];
  4159. *(d3dv++) = (GLfloat) x;
  4160. *(d3dv++) = (GLfloat) y;
  4161. *(d3dv++) = (GLfloat) z;
  4162. MEMCPY(d3dv, &g_OpenGLValues->m_nx, sizeof(FLOAT) * 7 + sizeof(D3DCOLOR));
  4163. }
  4164. void APIENTRY glVertex3dv (const GLdouble *v)
  4165. {
  4166. #if GETPARMSFORDEBUG
  4167. char log[256];
  4168. char vx[40];
  4169. char vy[40];
  4170. char vz[40];
  4171. ftoa( (double)v[0], vx );
  4172. ftoa( (double)v[1], vy );
  4173. ftoa( (double)v[2], vz );
  4174. sprintf( log, "glVertex3dv: %s %s %s", vx, vy, vz );
  4175. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, log );
  4176. #endif
  4177. VertexBufferFilled();
  4178. FLOAT *d3dv = (FLOAT*)&g_OpenGLValues->m_verts[g_OpenGLValues->m_vcnt++];
  4179. *(d3dv++) = (GLfloat) *(v++);
  4180. *(d3dv++) = (GLfloat) *(v++);
  4181. *(d3dv++) = (GLfloat) *(v++);
  4182. MEMCPY(d3dv, &g_OpenGLValues->m_nx, sizeof(FLOAT) * 7 + sizeof(D3DCOLOR));
  4183. }
  4184. void APIENTRY glVertex3f (GLfloat x, GLfloat y, GLfloat z)
  4185. {
  4186. #if GETPARMSFORDEBUG
  4187. char log[256];
  4188. char vx[40];
  4189. char vy[40];
  4190. char vz[40];
  4191. ftoa( (double)x, vx );
  4192. ftoa( (double)y, vy );
  4193. ftoa( (double)z, vz );
  4194. sprintf( log, "glVertex3f: %s %s %s", vx, vy, vz );
  4195. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, log );
  4196. #endif
  4197. VertexBufferFilled();
  4198. FLOAT *d3dv = (FLOAT*)&g_OpenGLValues->m_verts[g_OpenGLValues->m_vcnt++];
  4199. *(d3dv++) = x;
  4200. *(d3dv++) = y;
  4201. *(d3dv++) = z;
  4202. MEMCPY(d3dv, &g_OpenGLValues->m_nx, sizeof(FLOAT) * 7 + sizeof(D3DCOLOR));
  4203. }
  4204. void APIENTRY glVertex3fv (const GLfloat *v)
  4205. {
  4206. #if GETPARMSFORDEBUG
  4207. char log[256];
  4208. char vx[40];
  4209. char vy[40];
  4210. char vz[40];
  4211. ftoa( (double)v[0], vx );
  4212. ftoa( (double)v[1], vy );
  4213. ftoa( (double)v[2], vz );
  4214. sprintf( log, "glVertex3fv: %s %s %s", vx, vy, vz );
  4215. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, log );
  4216. #endif
  4217. VertexBufferFilled();
  4218. FLOAT *d3dv = (FLOAT*)&g_OpenGLValues->m_verts[g_OpenGLValues->m_vcnt++];
  4219. *(d3dv++) = *(v++);
  4220. *(d3dv++) = *(v++);
  4221. *(d3dv++) = *(v++);
  4222. MEMCPY(d3dv, &g_OpenGLValues->m_nx, sizeof(FLOAT) * 7 + sizeof(D3DCOLOR));
  4223. }
  4224. void APIENTRY glVertex3i (GLint x, GLint y, GLint z)
  4225. {
  4226. #if GETPARMSFORDEBUG
  4227. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glVertex3i: %d %d %d", x, y, z );
  4228. #endif
  4229. VertexBufferFilled();
  4230. FLOAT *d3dv = (FLOAT*)&g_OpenGLValues->m_verts[g_OpenGLValues->m_vcnt++];
  4231. *(d3dv++) = (GLfloat)x;
  4232. *(d3dv++) = (GLfloat)y;
  4233. *(d3dv++) = (GLfloat)z;
  4234. MEMCPY(d3dv, &g_OpenGLValues->m_nx, sizeof(FLOAT) * 7 + sizeof(D3DCOLOR));
  4235. }
  4236. void APIENTRY glVertex3iv (const GLint *v)
  4237. {
  4238. #if GETPARMSFORDEBUG
  4239. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glVertex3iv: %d %d %d", v[0], v[1], v[2] );
  4240. #endif
  4241. VertexBufferFilled();
  4242. FLOAT *d3dv = (FLOAT*)&g_OpenGLValues->m_verts[g_OpenGLValues->m_vcnt++];
  4243. *(d3dv++) = (GLfloat) *(v++);
  4244. *(d3dv++) = (GLfloat) *(v++);
  4245. *(d3dv++) = (GLfloat) *(v++);
  4246. MEMCPY(d3dv, &g_OpenGLValues->m_nx, sizeof(FLOAT) * 7 + sizeof(D3DCOLOR));
  4247. }
  4248. void APIENTRY glVertex3s (GLshort x, GLshort y, GLshort z)
  4249. {
  4250. #if GETPARMSFORDEBUG
  4251. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glVertex3s: %d %d %d", x, y, z );
  4252. #endif
  4253. VertexBufferFilled();
  4254. FLOAT *d3dv = (FLOAT*)&g_OpenGLValues->m_verts[g_OpenGLValues->m_vcnt++];
  4255. *(d3dv++) = (GLfloat)x;
  4256. *(d3dv++) = (GLfloat)y;
  4257. *(d3dv++) = (GLfloat)z;
  4258. MEMCPY(d3dv, &g_OpenGLValues->m_nx, sizeof(FLOAT) * 7 + sizeof(D3DCOLOR));
  4259. }
  4260. void APIENTRY glVertex3sv (const GLshort *v)
  4261. {
  4262. #if GETPARMSFORDEBUG
  4263. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glVertex3sv: %d %d %d", v[0], v[1], v[2] );
  4264. #endif
  4265. VertexBufferFilled();
  4266. FLOAT *d3dv = (FLOAT*)&g_OpenGLValues->m_verts[g_OpenGLValues->m_vcnt++];
  4267. *(d3dv++) = (GLfloat) *(v++);
  4268. *(d3dv++) = (GLfloat) *(v++);
  4269. *(d3dv++) = (GLfloat) *(v++);
  4270. MEMCPY(d3dv, &g_OpenGLValues->m_nx, sizeof(FLOAT) * 7 + sizeof(D3DCOLOR));
  4271. }
  4272. void APIENTRY glVertex4d (GLdouble x, GLdouble y, GLdouble z, GLdouble w)
  4273. {
  4274. #if GETPARMSFORDEBUG
  4275. char log[256];
  4276. char vx[40];
  4277. char vy[40];
  4278. char vz[40];
  4279. char vw[40];
  4280. ftoa( (double)x, vx );
  4281. ftoa( (double)y, vy );
  4282. ftoa( (double)z, vz );
  4283. ftoa( (double)w, vw );
  4284. sprintf( log, "glVertex4d: %s %s %s %s", vx, vy, vz, vw );
  4285. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, log );
  4286. #endif
  4287. VertexBufferFilled();
  4288. FLOAT *d3dv = (FLOAT*)&g_OpenGLValues->m_verts[g_OpenGLValues->m_vcnt++];
  4289. *(d3dv++) = (GLfloat)x;
  4290. *(d3dv++) = (GLfloat)y;
  4291. *(d3dv++) = (GLfloat)z;
  4292. MEMCPY(d3dv, &g_OpenGLValues->m_nx, sizeof(FLOAT) * 7 + sizeof(D3DCOLOR));
  4293. }
  4294. void APIENTRY glVertex4dv (const GLdouble *v)
  4295. {
  4296. #if GETPARMSFORDEBUG
  4297. char log[256];
  4298. char vx[40];
  4299. char vy[40];
  4300. char vz[40];
  4301. char vw[40];
  4302. ftoa( (double)v[0], vx );
  4303. ftoa( (double)v[1], vy );
  4304. ftoa( (double)v[2], vz );
  4305. ftoa( (double)v[3], vw );
  4306. sprintf( log, "glVertex4dv: %s %s %s %s", vx, vy, vz, vw );
  4307. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, log );
  4308. #endif
  4309. VertexBufferFilled();
  4310. FLOAT *d3dv = (FLOAT*)&g_OpenGLValues->m_verts[g_OpenGLValues->m_vcnt++];
  4311. *(d3dv++) = (GLfloat) *(v++);
  4312. *(d3dv++) = (GLfloat) *(v++);
  4313. *(d3dv++) = (GLfloat) *(v++);
  4314. MEMCPY(d3dv, &g_OpenGLValues->m_nx, sizeof(FLOAT) * 7 + sizeof(D3DCOLOR));
  4315. }
  4316. void APIENTRY glVertex4f (GLfloat x, GLfloat y, GLfloat z, GLfloat w)
  4317. {
  4318. #if GETPARMSFORDEBUG
  4319. char log[256];
  4320. char vx[40];
  4321. char vy[40];
  4322. char vz[40];
  4323. char vw[40];
  4324. ftoa( (double)x, vx );
  4325. ftoa( (double)x, vy );
  4326. ftoa( (double)y, vz );
  4327. ftoa( (double)w, vw );
  4328. sprintf( log, "glVertex4f: %s %s %s %s", vx, vy, vz, vw );
  4329. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, log );
  4330. #endif
  4331. VertexBufferFilled();
  4332. FLOAT *d3dv = (FLOAT*)&g_OpenGLValues->m_verts[g_OpenGLValues->m_vcnt++];
  4333. *(d3dv++) = x;
  4334. *(d3dv++) = y;
  4335. *(d3dv++) = z;
  4336. MEMCPY(d3dv, &g_OpenGLValues->m_nx, sizeof(FLOAT) * 7 + sizeof(D3DCOLOR));
  4337. }
  4338. void APIENTRY glVertex4fv (const GLfloat *v)
  4339. {
  4340. #if GETPARMSFORDEBUG
  4341. char log[256];
  4342. char vx[40];
  4343. char vy[40];
  4344. char vz[40];
  4345. char vw[40];
  4346. ftoa( (double)v[0], vx );
  4347. ftoa( (double)v[1], vy );
  4348. ftoa( (double)v[2], vz );
  4349. ftoa( (double)v[3], vw );
  4350. sprintf( log, "glVertex4fv: %s %s %s %s", vx, vy, vz, vw );
  4351. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, log );
  4352. #endif
  4353. VertexBufferFilled();
  4354. FLOAT *d3dv = (FLOAT*)&g_OpenGLValues->m_verts[g_OpenGLValues->m_vcnt++];
  4355. *(d3dv++) = *(v++);
  4356. *(d3dv++) = *(v++);
  4357. *(d3dv++) = *(v++);
  4358. MEMCPY(d3dv, &g_OpenGLValues->m_nx, sizeof(FLOAT) * 7 + sizeof(D3DCOLOR));
  4359. }
  4360. void APIENTRY glVertex4i (GLint x, GLint y, GLint z, GLint w)
  4361. {
  4362. #if GETPARMSFORDEBUG
  4363. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glVertex4i: %d %d %d %d", x, y, z, w );
  4364. #endif
  4365. VertexBufferFilled();
  4366. FLOAT *d3dv = (FLOAT*)&g_OpenGLValues->m_verts[g_OpenGLValues->m_vcnt++];
  4367. *(d3dv++) = (GLfloat)x;
  4368. *(d3dv++) = (GLfloat)y;
  4369. *(d3dv++) = (GLfloat)z;
  4370. MEMCPY(d3dv, &g_OpenGLValues->m_nx, sizeof(FLOAT) * 7 + sizeof(D3DCOLOR));
  4371. }
  4372. void APIENTRY glVertex4iv (const GLint *v)
  4373. {
  4374. #if GETPARMSFORDEBUG
  4375. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glVertex4iv: %d %d %d %d", v[0], v[1], v[2], v[3] );
  4376. #endif
  4377. VertexBufferFilled();
  4378. FLOAT *d3dv = (FLOAT*)&g_OpenGLValues->m_verts[g_OpenGLValues->m_vcnt++];
  4379. *(d3dv++) = (GLfloat) *(v++);
  4380. *(d3dv++) = (GLfloat) *(v++);
  4381. *(d3dv++) = (GLfloat) *(v++);
  4382. MEMCPY(d3dv, &g_OpenGLValues->m_nx, sizeof(FLOAT) * 7 + sizeof(D3DCOLOR));
  4383. }
  4384. void APIENTRY glVertex4s (GLshort x, GLshort y, GLshort z, GLshort w)
  4385. {
  4386. #if GETPARMSFORDEBUG
  4387. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glVertex4s: %d %d %d %d", x, y, z, w );
  4388. #endif
  4389. VertexBufferFilled();
  4390. FLOAT *d3dv = (FLOAT*)&g_OpenGLValues->m_verts[g_OpenGLValues->m_vcnt++];
  4391. *(d3dv++) = (GLfloat)x;
  4392. *(d3dv++) = (GLfloat)y;
  4393. *(d3dv++) = (GLfloat)z;
  4394. MEMCPY(d3dv, &g_OpenGLValues->m_nx, sizeof(FLOAT) * 7 + sizeof(D3DCOLOR));
  4395. }
  4396. void APIENTRY glVertex4sv (const GLshort *v)
  4397. {
  4398. #if GETPARMSFORDEBUG
  4399. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glVertex4sv: %d %d %d %d", v[0], v[1], v[2], v[3] );
  4400. #endif
  4401. VertexBufferFilled();
  4402. FLOAT *d3dv = (FLOAT*)&g_OpenGLValues->m_verts[g_OpenGLValues->m_vcnt++];
  4403. *(d3dv++) = (GLfloat) *(v++);
  4404. *(d3dv++) = (GLfloat) *(v++);
  4405. *(d3dv++) = (GLfloat) *(v++);
  4406. MEMCPY(d3dv, &g_OpenGLValues->m_nx, sizeof(FLOAT) * 7 + sizeof(D3DCOLOR));
  4407. }
  4408. void APIENTRY glVertexPointer (GLint size, GLenum type, GLsizei stride, const GLvoid *pointer)
  4409. {
  4410. #if GETPARMSFORDEBUG
  4411. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "glVertexPointer: 0x%X 0x%X 0x%X 0x%X", size, type, stride, pointer );
  4412. #endif
  4413. if(size == 3 && type == GL_FLOAT)
  4414. g_OpenGLValues->m_vertexary = (GLfloat*)pointer;
  4415. #if DODPFS
  4416. else
  4417. {
  4418. char junk[256];
  4419. sprintf( junk, "Vertex array not supported (size:0x%X type:0x%X)\n", size, type );
  4420. OutputDebugStringA( junk );
  4421. }
  4422. #endif
  4423. if(stride == 0)
  4424. {
  4425. stride = 12;
  4426. }
  4427. g_OpenGLValues->m_vertexarystride = stride;
  4428. }
  4429. int WINAPI wglChoosePixelFormat(HDC hdc, CONST PIXELFORMATDESCRIPTOR *ppfd)
  4430. {
  4431. #if GETPARMSFORDEBUG
  4432. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "wglChoosePixelFormat: 0x%X 0x%X", hdc, ppfd );
  4433. #endif
  4434. return 1;
  4435. }
  4436. #if HOOK_WINDOW_PROC // Custom message loop to test SetLOD
  4437. LRESULT CALLBACK MyMsgHandler(
  4438. HWND hwnd, // handle to window
  4439. UINT uMsg, // message identifier
  4440. WPARAM wParam, // first message parameter
  4441. LPARAM lParam // second message parameter
  4442. )
  4443. {
  4444. if(uMsg == WM_CHAR)
  4445. {
  4446. if(wParam == 0x2F)
  4447. {
  4448. ++g_OpenGLValues->m_lod;
  4449. if(g_OpenGLValues->m_lod == 8)
  4450. g_OpenGLValues->m_lod = 0;
  4451. int changedLOD = 0;
  4452. static char str[256];
  4453. for(int i = 0; i < MAXGLTEXHANDLES; ++i)
  4454. {
  4455. if(g_OpenGLValues->m_tex[i].m_ddsurf != 0)
  4456. {
  4457. DWORD mipcount = g_OpenGLValues->m_tex[i].m_ddsurf->GetLevelCount();
  4458. if(mipcount > 1)
  4459. {
  4460. if(g_OpenGLValues->m_lod >= mipcount)
  4461. g_OpenGLValues->m_tex[i].m_ddsurf->SetLOD(mipcount - 1);
  4462. else
  4463. {
  4464. g_OpenGLValues->m_tex[i].m_ddsurf->SetLOD(g_OpenGLValues->m_lod);
  4465. ++changedLOD;
  4466. }
  4467. }
  4468. }
  4469. }
  4470. _itoa(changedLOD, str, 10);
  4471. #if DODPFS
  4472. char junk[256];
  4473. sprintf( junk, "MyMsgHandler:%s", str );
  4474. OutputDebugStringA( junk );
  4475. #endif
  4476. return 0;
  4477. }
  4478. }
  4479. return CallWindowProc(g_OpenGLValues->m_wndproc, hwnd, uMsg, wParam, lParam);
  4480. }
  4481. #endif
  4482. HGLRC WINAPI wglCreateContext(HDC hdc)
  4483. {
  4484. #if GETPARMSFORDEBUG
  4485. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "wglCreateContext: 0x%X", hdc );
  4486. #endif
  4487. /*
  4488. This IF/ELSE block is necessary in order to allow apps to delete a
  4489. context and then create a new one (as in changing screen resolution
  4490. or changing from a software renderer to using OpenGL32.DLL). We need
  4491. to make sure that the pre-existing DirectX8Device is freed and that
  4492. the g_OpenGLValues data structure is cleared as it would be when
  4493. created. If we do not the results can range from rendering the screen
  4494. inaccurately or not at all to crashing the app. (a-brienw 03/07/2001)
  4495. */
  4496. if (g_OpenGLValues != NULL)
  4497. {
  4498. if (g_OpenGLValues->m_d3ddev != NULL)
  4499. {
  4500. g_OpenGLValues->m_d3ddev->Release();
  4501. g_OpenGLValues->m_d3ddev = 0;
  4502. }
  4503. }
  4504. else
  4505. {
  4506. g_OpenGLValues = new Globals;
  4507. }
  4508. if (g_OpenGLValues == NULL)
  4509. return 0;
  4510. memset( g_OpenGLValues, 0, sizeof(Globals) );
  4511. g_OpenGLValues->m_hdc = hdc;
  4512. g_OpenGLValues->m_hwnd = WindowFromDC(g_OpenGLValues->m_hdc);
  4513. RECT rect;
  4514. GetClientRect(g_OpenGLValues->m_hwnd, &rect);
  4515. g_OpenGLValues->m_winWidth = (USHORT)rect.right;
  4516. g_OpenGLValues->m_winHeight = (USHORT)rect.bottom;
  4517. g_OpenGLValues->m_vwx = rect.left;
  4518. g_OpenGLValues->m_vwy = rect.top;
  4519. g_OpenGLValues->m_vww = rect.right - rect.left;
  4520. g_OpenGLValues->m_vwh = rect.bottom - rect.top;
  4521. g_OpenGLValues->m_vport.X = g_OpenGLValues->m_vwx;
  4522. g_OpenGLValues->m_vport.Y = g_OpenGLValues->m_winHeight - (g_OpenGLValues->m_vwy + g_OpenGLValues->m_vwh);
  4523. g_OpenGLValues->m_vport.Width = g_OpenGLValues->m_vww;
  4524. g_OpenGLValues->m_vport.Height = g_OpenGLValues->m_vwh;
  4525. g_OpenGLValues->m_vport.MinZ = 0.f;
  4526. g_OpenGLValues->m_vport.MaxZ = 1.f;
  4527. D3DDEVTYPE DeviceType = D3DDEVTYPE_HAL;
  4528. // Check registry key to see if we need to do software emulation
  4529. HKEY hKey;
  4530. if(ERROR_SUCCESS == RegOpenKey(HKEY_LOCAL_MACHINE, RESPATH_QUAKE, &hKey)) {
  4531. DWORD dwType;
  4532. DWORD dwValue;
  4533. DWORD dwSize = 4;
  4534. if (ERROR_SUCCESS == RegQueryValueEx( hKey, L"Emulation", NULL, &dwType, (LPBYTE) &dwValue, &dwSize) &&
  4535. dwType == REG_DWORD &&
  4536. dwValue != 0) {
  4537. DeviceType = D3DDEVTYPE_REF;
  4538. }
  4539. RegCloseKey( hKey );
  4540. }
  4541. //
  4542. // get the current display settings so they can be restored
  4543. // after the call to Direct3DCreate8.
  4544. //
  4545. DEVMODEW dm;
  4546. dm.dmSize = sizeof(DEVMODEW);
  4547. dm.dmDriverExtra = 0;
  4548. if (!EnumDisplaySettings(0, ENUM_CURRENT_SETTINGS, &dm))
  4549. {
  4550. return 0;
  4551. }
  4552. IDirect3D8 *pEnum = Direct3DCreate8(D3D_SDK_VERSION);
  4553. if (pEnum == NULL)
  4554. {
  4555. #if DODPFS
  4556. OutputDebugStringA("Wrapper: Direct3DCreate8 failed\n");
  4557. #endif
  4558. return 0;
  4559. }
  4560. D3DPRESENT_PARAMETERS d3dpp;
  4561. memset(&d3dpp, 0, sizeof(D3DPRESENT_PARAMETERS));
  4562. // See if the window is full screen
  4563. if( rect.right == GetDeviceCaps(hdc, HORZRES) && rect.bottom == GetDeviceCaps(hdc, VERTRES) )
  4564. {
  4565. // We are full screen
  4566. d3dpp.Windowed = FALSE;
  4567. }
  4568. else
  4569. {
  4570. d3dpp.Windowed = TRUE;
  4571. }
  4572. int bpp = GetDeviceCaps(hdc, BITSPIXEL);
  4573. /*
  4574. If this device is a 3dfx Voodoo then make sure we don't allow the
  4575. display to be set to more than 16bpp because of the bug in the drivers
  4576. that the 3dfx team is (hopefully) going to get to at a later date.
  4577. */
  4578. if (wcsstr(dm.dmDeviceName,L"3dfx"))
  4579. {
  4580. if (bpp > 16)
  4581. {
  4582. bpp = 16;
  4583. dm.dmBitsPerPel = 16;
  4584. }
  4585. }
  4586. d3dpp.hDeviceWindow = g_OpenGLValues->m_hwnd;
  4587. d3dpp.BackBufferWidth = rect.right;
  4588. d3dpp.BackBufferHeight = rect.bottom;
  4589. d3dpp.BackBufferFormat = bpp == 16 ? D3DFMT_R5G6B5 : D3DFMT_X8R8G8B8;
  4590. d3dpp.BackBufferCount = 1;
  4591. d3dpp.EnableAutoDepthStencil = TRUE;
  4592. d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
  4593. d3dpp.SwapEffect = D3DSWAPEFFECT_COPY;
  4594. // check to see if the current format is supported
  4595. // if not and we are in 32 bpp change to 16 bpp
  4596. HRESULT hr = pEnum->CheckDeviceType( D3DADAPTER_DEFAULT, DeviceType, d3dpp.BackBufferFormat, d3dpp.BackBufferFormat, d3dpp.Windowed );
  4597. if(FAILED(hr))
  4598. {
  4599. if( bpp == 32 )
  4600. {
  4601. d3dpp.BackBufferFormat = D3DFMT_R5G6B5;
  4602. dm.dmBitsPerPel = 16;
  4603. }
  4604. }
  4605. // restore to the saved display settings.
  4606. ChangeDisplaySettingsExW(0, &dm, 0, CDS_FULLSCREEN, 0);
  4607. hr = pEnum->GetDeviceCaps( D3DADAPTER_DEFAULT, DeviceType, &g_OpenGLValues->m_dd );
  4608. if(FAILED(hr))
  4609. {
  4610. pEnum->Release();
  4611. #if DODPFS
  4612. char junk[256];
  4613. sprintf( junk, "Wrapper: GetDeviceCaps failed (0x%X)\n", hr );
  4614. OutputDebugStringA( junk );
  4615. #endif
  4616. return 0;
  4617. }
  4618. DWORD Behaviour;
  4619. #if 1
  4620. if(g_OpenGLValues->m_dd.MaxStreams >= 4 &&
  4621. (g_OpenGLValues->m_dd.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT) != 0 &&
  4622. g_OpenGLValues->m_dd.MaxActiveLights >= 8 &&
  4623. (g_OpenGLValues->m_dd.TextureCaps & D3DPTEXTURECAPS_PROJECTED) != 0)
  4624. {
  4625. Behaviour = D3DCREATE_HARDWARE_VERTEXPROCESSING | ((g_OpenGLValues->m_dd.DevCaps & D3DDEVCAPS_PUREDEVICE) != 0 ? D3DCREATE_PUREDEVICE : 0);
  4626. #if DODPFS
  4627. OutputDebugStringA("Wrapper: Using T&L hardware\n");
  4628. #endif
  4629. }
  4630. else
  4631. #endif
  4632. {
  4633. Behaviour = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
  4634. #if DODPFS
  4635. OutputDebugStringA("Wrapper: Using software pipeline\n");
  4636. #endif
  4637. }
  4638. hr = pEnum->CreateDevice(D3DADAPTER_DEFAULT, DeviceType, g_OpenGLValues->m_hwnd, Behaviour, &d3dpp, &g_OpenGLValues->m_d3ddev);
  4639. if(FAILED(hr))
  4640. {
  4641. #if DODPFS
  4642. char junk[256];
  4643. sprintf( junk, "Wrapper: CreateDevice failed (hr:0x%X windowed:%d)\n", hr, d3dpp.Windowed );
  4644. OutputDebugStringA( junk );
  4645. #endif
  4646. pEnum->Release();
  4647. return 0;
  4648. }
  4649. // Check registry key to see if we need to turn off mipmapping
  4650. if(ERROR_SUCCESS == RegOpenKey(HKEY_LOCAL_MACHINE, RESPATH_QUAKE, &hKey)) {
  4651. DWORD dwType;
  4652. DWORD dwValue;
  4653. DWORD dwSize = 4;
  4654. if (ERROR_SUCCESS == RegQueryValueEx( hKey, L"DisableMipMap", NULL, &dwType, (LPBYTE) &dwValue, &dwSize) &&
  4655. dwType == REG_DWORD &&
  4656. dwValue != 0) {
  4657. g_OpenGLValues->m_usemipmap = FALSE;
  4658. #if DODPFS
  4659. OutputDebugStringA("Wrapper: Mipmapping disabled\n");
  4660. #endif
  4661. }
  4662. else {
  4663. g_OpenGLValues->m_usemipmap = TRUE;
  4664. }
  4665. RegCloseKey( hKey );
  4666. }
  4667. else {
  4668. g_OpenGLValues->m_usemipmap = TRUE;
  4669. }
  4670. // Enumerate texture formats and find the right ones to use
  4671. // Look for a four bit alpha surface
  4672. hr = pEnum->CheckDeviceFormat(D3DADAPTER_DEFAULT, DeviceType, d3dpp.BackBufferFormat, 0, D3DRTYPE_TEXTURE, D3DFMT_A4R4G4B4);
  4673. if ( FAILED(hr) )
  4674. {
  4675. #if DODPFS
  4676. char junk[256];
  4677. sprintf( junk, "Wrapper: Unable to find 4444 texture (0x%X)\n", hr );
  4678. OutputDebugStringA( junk );
  4679. #endif
  4680. g_OpenGLValues->m_d3ddev->Release();
  4681. pEnum->Release();
  4682. return 0;
  4683. }
  4684. g_OpenGLValues->m_ddFourBitAlphaSurfFormat = D3DFMT_A4R4G4B4;
  4685. // Look for an eight bit alpha surface
  4686. hr = pEnum->CheckDeviceFormat(D3DADAPTER_DEFAULT, DeviceType, d3dpp.BackBufferFormat, 0, D3DRTYPE_TEXTURE, D3DFMT_A8R8G8B8);
  4687. if ( FAILED(hr) )
  4688. {
  4689. #if DODPFS
  4690. char junk[256];
  4691. sprintf( junk, "Wrapper: Not using 8888 texture (0x%X)\n", hr );
  4692. OutputDebugStringA( junk );
  4693. #endif
  4694. g_OpenGLValues->m_ddEightBitAlphaSurfFormat = g_OpenGLValues->m_ddFourBitAlphaSurfFormat;
  4695. }
  4696. else
  4697. {
  4698. g_OpenGLValues->m_ddEightBitAlphaSurfFormat = D3DFMT_A8R8G8B8;
  4699. }
  4700. // Look for a surface
  4701. hr = pEnum->CheckDeviceFormat(D3DADAPTER_DEFAULT, DeviceType, d3dpp.BackBufferFormat, 0, D3DRTYPE_TEXTURE, D3DFMT_R5G6B5);
  4702. if ( FAILED(hr) )
  4703. {
  4704. hr = pEnum->CheckDeviceFormat(D3DADAPTER_DEFAULT, DeviceType, d3dpp.BackBufferFormat, 0, D3DRTYPE_TEXTURE, D3DFMT_X1R5G5B5);
  4705. if ( FAILED(hr) )
  4706. {
  4707. #if DODPFS
  4708. char junk[256];
  4709. sprintf( junk, "Wrapper: Unable to find 555 or 565 texture (0x%X)\n", hr );
  4710. OutputDebugStringA( junk );
  4711. #endif
  4712. g_OpenGLValues->m_d3ddev->Release();
  4713. pEnum->Release();
  4714. return 0;
  4715. }
  4716. g_OpenGLValues->m_ddFiveBitSurfFormat = D3DFMT_X1R5G5B5;
  4717. }
  4718. else
  4719. {
  4720. g_OpenGLValues->m_ddFiveBitSurfFormat = D3DFMT_R5G6B5;
  4721. }
  4722. // Look for an 8-bit surface
  4723. hr = pEnum->CheckDeviceFormat(D3DADAPTER_DEFAULT, DeviceType, d3dpp.BackBufferFormat, 0, D3DRTYPE_TEXTURE, D3DFMT_X8R8G8B8);
  4724. if ( FAILED(hr) )
  4725. {
  4726. #if DODPFS
  4727. char junk[256];
  4728. sprintf( junk, "Wrapper: Not using 888 texture (0x%X)\n", hr );
  4729. OutputDebugStringA( junk );
  4730. #endif
  4731. g_OpenGLValues->m_ddEightBitSurfFormat = g_OpenGLValues->m_ddFiveBitSurfFormat;
  4732. }
  4733. else
  4734. {
  4735. g_OpenGLValues->m_ddEightBitSurfFormat = D3DFMT_X8R8G8B8;
  4736. }
  4737. // Look for a luminance surface
  4738. hr = pEnum->CheckDeviceFormat(D3DADAPTER_DEFAULT, DeviceType, d3dpp.BackBufferFormat, 0, D3DRTYPE_TEXTURE, D3DFMT_L8);
  4739. if ( FAILED(hr) )
  4740. {
  4741. #if DODPFS
  4742. char junk[256];
  4743. sprintf( junk, "Wrapper: Not using luminance texture (0x%X)\n", hr );
  4744. OutputDebugStringA( junk );
  4745. #endif
  4746. g_OpenGLValues->m_ddLuminanceSurfFormat = g_OpenGLValues->m_ddEightBitSurfFormat;
  4747. }
  4748. else
  4749. {
  4750. g_OpenGLValues->m_ddLuminanceSurfFormat = D3DFMT_L8;
  4751. }
  4752. // Look for a alpha surface
  4753. hr = pEnum->CheckDeviceFormat(D3DADAPTER_DEFAULT, DeviceType, d3dpp.BackBufferFormat, 0, D3DRTYPE_TEXTURE, D3DFMT_A8);
  4754. if ( FAILED(hr) )
  4755. {
  4756. #if DODPFS
  4757. char junk[256];
  4758. sprintf( junk, "Wrapper: Not using alpha-only texture (0x%X)\n", hr );
  4759. OutputDebugStringA( junk );
  4760. #endif
  4761. g_OpenGLValues->m_ddEightBitAlphaOnlySurfFormat = g_OpenGLValues->m_ddEightBitAlphaSurfFormat;
  4762. }
  4763. else
  4764. {
  4765. g_OpenGLValues->m_ddEightBitAlphaOnlySurfFormat = D3DFMT_A8;
  4766. }
  4767. // Done with enumerator
  4768. pEnum->Release();
  4769. // Do misc init stuff
  4770. if(g_OpenGLValues->m_dd.MaxTextureWidth < 512 || g_OpenGLValues->m_dd.MaxTextureHeight < 512) {
  4771. g_OpenGLValues->m_subsample = TRUE;
  4772. #if DODPFS
  4773. OutputDebugStringA("Wrapper: Subsampling textures to 256 x 256\n");
  4774. #endif
  4775. }
  4776. else
  4777. g_OpenGLValues->m_subsample = FALSE;
  4778. if(g_OpenGLValues->m_dd.TextureCaps & D3DPTEXTURECAPS_SQUAREONLY) {
  4779. g_OpenGLValues->m_makeSquare = TRUE;
  4780. #if DODPFS
  4781. OutputDebugStringA("Wrapper: Forcing all textures to be square\n");
  4782. #endif
  4783. }
  4784. else
  4785. g_OpenGLValues->m_makeSquare = FALSE;
  4786. if(g_OpenGLValues->m_dd.MaxSimultaneousTextures > 1) {
  4787. g_OpenGLValues->m_usemtex = TRUE;
  4788. #if DODPFS
  4789. OutputDebugStringA("Wrapper: Multitexturing enabled\n");
  4790. #endif
  4791. }
  4792. else {
  4793. g_OpenGLValues->m_usemtex = FALSE;
  4794. #if DODPFS
  4795. OutputDebugStringA("Wrapper: Multitexturing not available with this driver\n");
  4796. #endif
  4797. }
  4798. if(!(g_OpenGLValues->m_dd.TextureFilterCaps & D3DPTFILTERCAPS_MIPFPOINT) &&
  4799. !(g_OpenGLValues->m_dd.TextureFilterCaps & D3DPTFILTERCAPS_MIPFLINEAR)) {
  4800. g_OpenGLValues->m_usemipmap = FALSE;
  4801. #if DODPFS
  4802. OutputDebugStringA("Wrapper: Mipmapping disabled\n");
  4803. #endif
  4804. }
  4805. if(ERROR_SUCCESS == RegOpenKey(HKEY_LOCAL_MACHINE, RESPATH_QUAKE, &hKey)) {
  4806. DWORD dwType;
  4807. DWORD dwValue;
  4808. DWORD dwSize = 4;
  4809. if (ERROR_SUCCESS == RegQueryValueEx( hKey, L"DoFlip", NULL, &dwType, (LPBYTE) &dwValue, &dwSize) &&
  4810. dwType == REG_DWORD &&
  4811. dwValue != 0) {
  4812. g_OpenGLValues->m_doFlip = TRUE;
  4813. }
  4814. else {
  4815. g_OpenGLValues->m_doFlip = FALSE;
  4816. }
  4817. RegCloseKey( hKey );
  4818. }
  4819. else {
  4820. g_OpenGLValues->m_doFlip = FALSE;
  4821. }
  4822. // Create shaders
  4823. DWORD decl0[] =
  4824. {
  4825. D3DVSD_STREAM(0),
  4826. D3DVSD_REG( D3DVSDE_POSITION, D3DVSDT_FLOAT3),
  4827. D3DVSD_END()
  4828. };
  4829. hr = g_OpenGLValues->m_d3ddev->CreateVertexShader( decl0, NULL, &g_OpenGLValues->m_vshader[0], 0 );
  4830. if( FAILED(hr) )
  4831. {
  4832. #if DODPFS
  4833. char junk[256];
  4834. sprintf( junk, "Wrapper: CreateVertexShader(1) failed (0x%X)\n", hr );
  4835. OutputDebugStringA( junk );
  4836. #endif
  4837. g_OpenGLValues->m_d3ddev->Release();
  4838. return 0;
  4839. }
  4840. DWORD decl1[] =
  4841. {
  4842. D3DVSD_STREAM(0),
  4843. D3DVSD_REG( D3DVSDE_POSITION, D3DVSDT_FLOAT3),
  4844. D3DVSD_STREAM(1),
  4845. D3DVSD_REG( D3DVSDE_DIFFUSE, D3DVSDT_D3DCOLOR),
  4846. D3DVSD_END()
  4847. };
  4848. hr = g_OpenGLValues->m_d3ddev->CreateVertexShader( decl1, NULL, &g_OpenGLValues->m_vshader[1], 0 );
  4849. if( FAILED(hr) )
  4850. {
  4851. #if DODPFS
  4852. char junk[256];
  4853. sprintf( junk, "Wrapper: CreateVertexShader(2) failed (0x%X)\n", hr );
  4854. OutputDebugStringA( junk );
  4855. #endif
  4856. g_OpenGLValues->m_d3ddev->Release();
  4857. return 0;
  4858. }
  4859. DWORD decl2[] =
  4860. {
  4861. D3DVSD_STREAM(0),
  4862. D3DVSD_REG( D3DVSDE_POSITION, D3DVSDT_FLOAT3),
  4863. D3DVSD_STREAM(1),
  4864. D3DVSD_REG( D3DVSDE_TEXCOORD0, D3DVSDT_FLOAT2),
  4865. D3DVSD_END()
  4866. };
  4867. hr = g_OpenGLValues->m_d3ddev->CreateVertexShader( decl2, NULL, &g_OpenGLValues->m_vshader[2], 0 );
  4868. if( FAILED(hr) )
  4869. {
  4870. #if DODPFS
  4871. char junk[256];
  4872. sprintf( junk, "Wrapper: CreateVertexShader(3) failed (0x%X)\n", hr );
  4873. OutputDebugStringA( junk );
  4874. #endif
  4875. g_OpenGLValues->m_d3ddev->Release();
  4876. return 0;
  4877. }
  4878. DWORD decl3[] =
  4879. {
  4880. D3DVSD_STREAM(0),
  4881. D3DVSD_REG( D3DVSDE_POSITION, D3DVSDT_FLOAT3),
  4882. D3DVSD_STREAM(1),
  4883. D3DVSD_REG( D3DVSDE_TEXCOORD0, D3DVSDT_FLOAT2),
  4884. D3DVSD_STREAM(2),
  4885. D3DVSD_REG( D3DVSDE_TEXCOORD1, D3DVSDT_FLOAT2),
  4886. D3DVSD_END()
  4887. };
  4888. hr = g_OpenGLValues->m_d3ddev->CreateVertexShader( decl3, NULL, &g_OpenGLValues->m_vshader[3], 0 );
  4889. if( FAILED(hr) )
  4890. {
  4891. #if DODPFS
  4892. char junk[256];
  4893. sprintf( junk, "Wrapper: CreateVertexShader(4) failed (0x%X)\n", hr );
  4894. OutputDebugStringA( junk );
  4895. #endif
  4896. g_OpenGLValues->m_d3ddev->Release();
  4897. return 0;
  4898. }
  4899. DWORD decl4[] =
  4900. {
  4901. D3DVSD_STREAM(0),
  4902. D3DVSD_REG( D3DVSDE_POSITION, D3DVSDT_FLOAT3),
  4903. D3DVSD_STREAM(1),
  4904. D3DVSD_REG( D3DVSDE_DIFFUSE, D3DVSDT_D3DCOLOR),
  4905. D3DVSD_STREAM(2),
  4906. D3DVSD_REG( D3DVSDE_TEXCOORD0, D3DVSDT_FLOAT2),
  4907. D3DVSD_END()
  4908. };
  4909. hr = g_OpenGLValues->m_d3ddev->CreateVertexShader( decl4, NULL, &g_OpenGLValues->m_vshader[4], 0 );
  4910. if( FAILED(hr) )
  4911. {
  4912. #if DODPFS
  4913. char junk[256];
  4914. sprintf( junk, "Wrapper: CreateVertexShader(5) failed (0x%X)\n", hr );
  4915. OutputDebugStringA( junk );
  4916. #endif
  4917. g_OpenGLValues->m_d3ddev->Release();
  4918. return 0;
  4919. }
  4920. DWORD decl5[] =
  4921. {
  4922. D3DVSD_STREAM(0),
  4923. D3DVSD_REG( D3DVSDE_POSITION, D3DVSDT_FLOAT3),
  4924. D3DVSD_STREAM(1),
  4925. D3DVSD_REG( D3DVSDE_DIFFUSE, D3DVSDT_D3DCOLOR),
  4926. D3DVSD_STREAM(2),
  4927. D3DVSD_REG( D3DVSDE_TEXCOORD0, D3DVSDT_FLOAT2),
  4928. D3DVSD_STREAM(3),
  4929. D3DVSD_REG( D3DVSDE_TEXCOORD1, D3DVSDT_FLOAT2),
  4930. D3DVSD_END()
  4931. };
  4932. hr = g_OpenGLValues->m_d3ddev->CreateVertexShader( decl5, NULL, &g_OpenGLValues->m_vshader[5], 0 );
  4933. if( FAILED(hr) )
  4934. {
  4935. #if DODPFS
  4936. char junk[256];
  4937. sprintf( junk, "Wrapper: CreateVertexShader(6) failed (0x%X)\n", hr );
  4938. OutputDebugStringA( junk );
  4939. #endif
  4940. g_OpenGLValues->m_d3ddev->Release();
  4941. return 0;
  4942. }
  4943. // Create vertex buffers
  4944. hr = g_OpenGLValues->m_d3ddev->CreateVertexBuffer(sizeof(QuakeVertex) * VBUFSIZE, D3DUSAGE_WRITEONLY | D3DUSAGE_DYNAMIC, QUAKEVFMT, D3DPOOL_DEFAULT, &g_OpenGLValues->m_vbuf);
  4945. if( FAILED(hr) )
  4946. {
  4947. #if DODPFS
  4948. char junk[256];
  4949. sprintf( junk, "Wrapper: CreateVertexBuffer failed (0x%X)\n", hr );
  4950. OutputDebugStringA( junk );
  4951. #endif
  4952. g_OpenGLValues->m_d3ddev->Release();
  4953. return 0;
  4954. }
  4955. g_OpenGLValues->m_vertexarystride = 12;
  4956. g_OpenGLValues->m_colorarystride = 4;
  4957. g_OpenGLValues->m_texcoordarystride[0] = 8;
  4958. g_OpenGLValues->m_texcoordarystride[1] = 8;
  4959. g_OpenGLValues->m_xyzbuf = 0;
  4960. g_OpenGLValues->m_colbuf = 0;
  4961. g_OpenGLValues->m_texbuf = 0;
  4962. g_OpenGLValues->m_tex2buf = 0;
  4963. g_OpenGLValues->m_vbufsz = 0;
  4964. hr = GrowVB(VBUFSIZE);
  4965. if( FAILED(hr) )
  4966. {
  4967. #if DODPFS
  4968. char junk[256];
  4969. sprintf( junk, "Wrapper: GrowVB failed (0x%X)\n", hr );
  4970. OutputDebugStringA( junk );
  4971. #endif
  4972. g_OpenGLValues->m_vbuf->Release();
  4973. g_OpenGLValues->m_d3ddev->Release();
  4974. return 0;
  4975. }
  4976. g_OpenGLValues->m_ibuf = 0;
  4977. g_OpenGLValues->m_ibufsz = 0;
  4978. hr = GrowIB(VBUFSIZE);
  4979. if( FAILED(hr) )
  4980. {
  4981. #if DODPFS
  4982. char junk[256];
  4983. sprintf( junk, "Wrapper: GrowIB failed (0x%X)\n", hr );
  4984. OutputDebugStringA( junk );
  4985. #endif
  4986. g_OpenGLValues->m_vbuf->Release();
  4987. g_OpenGLValues->m_d3ddev->Release();
  4988. return 0;
  4989. }
  4990. // Some more init stuff
  4991. g_OpenGLValues->m_cullMode = GL_BACK;
  4992. g_OpenGLValues->m_FrontFace = GL_CCW;
  4993. g_OpenGLValues->m_cullEnabled = FALSE;
  4994. g_OpenGLValues->m_texHandleValid = FALSE;
  4995. g_OpenGLValues->m_texturing = FALSE;
  4996. g_OpenGLValues->m_updvwp = TRUE;
  4997. g_OpenGLValues->m_blendmode[0] = GL_MODULATE;
  4998. g_OpenGLValues->m_blendmode[1] = GL_MODULATE;
  4999. g_OpenGLValues->m_nfv = 0;
  5000. g_OpenGLValues->m_curtgt = 0;
  5001. g_OpenGLValues->m_client_active_texture_arb = 0;
  5002. g_OpenGLValues->m_tu = g_OpenGLValues->m_tv = g_OpenGLValues->m_tu2 = g_OpenGLValues->m_tv2 = 0.f;
  5003. g_OpenGLValues->m_color = 0xFFFFFFFF;
  5004. g_OpenGLValues->m_material.Ambient.r = 0.2f;
  5005. g_OpenGLValues->m_material.Ambient.g = 0.2f;
  5006. g_OpenGLValues->m_material.Ambient.b = 0.2f;
  5007. g_OpenGLValues->m_material.Ambient.a = 1.0f;
  5008. g_OpenGLValues->m_material.Diffuse.r = 0.8f;
  5009. g_OpenGLValues->m_material.Diffuse.g = 0.8f;
  5010. g_OpenGLValues->m_material.Diffuse.b = 0.8f;
  5011. g_OpenGLValues->m_material.Diffuse.a = 1.0f;
  5012. g_OpenGLValues->m_material.Specular.r = 0.0f;
  5013. g_OpenGLValues->m_material.Specular.g = 0.0f;
  5014. g_OpenGLValues->m_material.Specular.b = 0.0f;
  5015. g_OpenGLValues->m_material.Specular.a = 1.0f;
  5016. g_OpenGLValues->m_material.Emissive.r = 0.0f;
  5017. g_OpenGLValues->m_material.Emissive.g = 0.0f;
  5018. g_OpenGLValues->m_material.Emissive.b = 0.0f;
  5019. g_OpenGLValues->m_material.Emissive.a = 1.0f;
  5020. g_OpenGLValues->m_material.Power = 0.f;
  5021. g_OpenGLValues->m_clearColor = 0;
  5022. g_OpenGLValues->m_clearDepth = 1.f;
  5023. g_OpenGLValues->m_usecolorary = FALSE;
  5024. g_OpenGLValues->m_usetexcoordary[0] = FALSE;
  5025. g_OpenGLValues->m_usetexcoordary[1] = FALSE;
  5026. g_OpenGLValues->m_usevertexary = FALSE;
  5027. g_OpenGLValues->m_polyoffset = FALSE;
  5028. g_OpenGLValues->m_withinprim = FALSE;
  5029. g_OpenGLValues->m_scix = 0;
  5030. g_OpenGLValues->m_sciy = 0;
  5031. g_OpenGLValues->m_sciw = g_OpenGLValues->m_winWidth;
  5032. g_OpenGLValues->m_scih = g_OpenGLValues->m_winHeight;
  5033. g_OpenGLValues->m_lckfirst = 0;
  5034. g_OpenGLValues->m_lckcount = 0;
  5035. g_OpenGLValues->m_clippstate = 0;
  5036. g_OpenGLValues->m_ibufoff = 0;
  5037. g_OpenGLValues->m_vbufoff = 0;
  5038. g_OpenGLValues->m_lightdirty = 0;
  5039. g_OpenGLValues->m_pStreams[0] = 0;
  5040. g_OpenGLValues->m_pStreams[1] = 0;
  5041. g_OpenGLValues->m_pStreams[2] = 0;
  5042. g_OpenGLValues->m_pStreams[3] = 0;
  5043. g_OpenGLValues->m_pStrides[0] = 0;
  5044. g_OpenGLValues->m_pStrides[1] = 0;
  5045. g_OpenGLValues->m_pStrides[2] = 0;
  5046. g_OpenGLValues->m_pStrides[3] = 0;
  5047. for(unsigned i = 0; i < MAXGLTEXHANDLES; ++i) {
  5048. g_OpenGLValues->m_tex[i].m_ddsurf = 0;
  5049. g_OpenGLValues->m_tex[i].m_block = 0;
  5050. g_OpenGLValues->m_tex[i].m_capture = FALSE;
  5051. g_OpenGLValues->m_tex[i].m_dwStage = 0;
  5052. g_OpenGLValues->m_tex[i].m_minmode = D3DTEXF_POINT;
  5053. g_OpenGLValues->m_tex[i].m_magmode = D3DTEXF_LINEAR;
  5054. g_OpenGLValues->m_tex[i].m_mipmode = D3DTEXF_LINEAR;
  5055. g_OpenGLValues->m_tex[i].m_addu = D3DTADDRESS_WRAP;
  5056. g_OpenGLValues->m_tex[i].m_addv = D3DTADDRESS_WRAP;
  5057. g_OpenGLValues->m_tex[i].m_prev = (int)i - 1;
  5058. g_OpenGLValues->m_tex[i].m_next = (int)i + 1;
  5059. }
  5060. for(i = 0; i < 8; ++i)
  5061. {
  5062. g_OpenGLValues->m_light[i].Ambient.r = 0.0f;
  5063. g_OpenGLValues->m_light[i].Ambient.g = 0.0f;
  5064. g_OpenGLValues->m_light[i].Ambient.b = 0.0f;
  5065. g_OpenGLValues->m_light[i].Ambient.a = 1.0f;
  5066. if(i == 0)
  5067. {
  5068. g_OpenGLValues->m_light[i].Diffuse.r = 1.0f;
  5069. g_OpenGLValues->m_light[i].Diffuse.g = 1.0f;
  5070. g_OpenGLValues->m_light[i].Diffuse.b = 1.0f;
  5071. g_OpenGLValues->m_light[i].Diffuse.a = 1.0f;
  5072. g_OpenGLValues->m_light[i].Specular.r = 1.0f;
  5073. g_OpenGLValues->m_light[i].Specular.g = 1.0f;
  5074. g_OpenGLValues->m_light[i].Specular.b = 1.0f;
  5075. g_OpenGLValues->m_light[i].Specular.a = 1.0f;
  5076. }
  5077. else
  5078. {
  5079. g_OpenGLValues->m_light[i].Diffuse.r = 0.0f;
  5080. g_OpenGLValues->m_light[i].Diffuse.g = 0.0f;
  5081. g_OpenGLValues->m_light[i].Diffuse.b = 0.0f;
  5082. g_OpenGLValues->m_light[i].Diffuse.a = 1.0f;
  5083. g_OpenGLValues->m_light[i].Specular.r = 0.0f;
  5084. g_OpenGLValues->m_light[i].Specular.g = 0.0f;
  5085. g_OpenGLValues->m_light[i].Specular.b = 0.0f;
  5086. g_OpenGLValues->m_light[i].Specular.a = 1.0f;
  5087. }
  5088. g_OpenGLValues->m_light[i].Position.x = 0.f;
  5089. g_OpenGLValues->m_light[i].Position.y = 0.f;
  5090. g_OpenGLValues->m_light[i].Position.z = 1.f;
  5091. g_OpenGLValues->m_lightPositionW[i] = 0.f;
  5092. g_OpenGLValues->m_light[i].Direction.x = 0.f;
  5093. g_OpenGLValues->m_light[i].Direction.y = 0.f;
  5094. g_OpenGLValues->m_light[i].Direction.z = -1.f;
  5095. g_OpenGLValues->m_light[i].Range = (float)sqrt(FLT_MAX);
  5096. g_OpenGLValues->m_light[i].Falloff = 0.f;
  5097. g_OpenGLValues->m_light[i].Attenuation0 = 1.f;
  5098. g_OpenGLValues->m_light[i].Attenuation1 = 0.f;
  5099. g_OpenGLValues->m_light[i].Attenuation2 = 0.f;
  5100. g_OpenGLValues->m_light[i].Theta = 0.f;
  5101. g_OpenGLValues->m_light[i].Phi = 180.f;
  5102. g_OpenGLValues->m_lightdirty |= (1 << i);
  5103. }
  5104. g_OpenGLValues->m_free = 0;
  5105. D3DMATRIX unity;
  5106. unity._11 = 1.0f; unity._12 = 0.0f; unity._13 = 0.0f; unity._14 = 0.0f;
  5107. unity._21 = 0.0f; unity._22 = 1.0f; unity._23 = 0.0f; unity._24 = 0.0f;
  5108. unity._31 = 0.0f; unity._32 = 0.0f; unity._33 = 1.0f; unity._34 = 0.0f;
  5109. unity._41 = 0.0f; unity._42 = 0.0f; unity._43 = 0.0f; unity._44 = 1.0f;
  5110. g_OpenGLValues->m_xfrm[D3DTS_WORLD] = unity;
  5111. QuakeSetTransform(D3DTS_WORLD, &unity);
  5112. g_OpenGLValues->m_xfrm[D3DTS_PROJECTION] = unity;
  5113. QuakeSetTransform(D3DTS_PROJECTION, &unity);
  5114. g_OpenGLValues->m_xfrm[D3DTS_TEXTURE0] = unity;
  5115. QuakeSetTransform(D3DTS_TEXTURE0, &unity);
  5116. if(g_OpenGLValues->m_usemtex == TRUE)
  5117. {
  5118. g_OpenGLValues->m_xfrm[D3DTS_TEXTURE1] = unity;
  5119. QuakeSetTransform(D3DTS_TEXTURE1, &unity);
  5120. }
  5121. g_OpenGLValues->m_matrixMode = D3DTS_WORLD;
  5122. g_OpenGLValues->m_d3ddev->SetViewport(&g_OpenGLValues->m_vport);
  5123. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_SPECULARENABLE, TRUE);
  5124. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_DITHERENABLE, TRUE);
  5125. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_CLIPPING, TRUE);
  5126. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_LIGHTING, FALSE);
  5127. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);
  5128. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_TEXCOORDINDEX,0);
  5129. if(g_OpenGLValues->m_usemtex == TRUE)
  5130. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_TEXCOORDINDEX,1);
  5131. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_AMBIENT, RGBA_MAKE(51, 51, 51, 255));
  5132. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_DIFFUSEMATERIALSOURCE, D3DMCS_COLOR1);
  5133. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_SPECULARMATERIALSOURCE, D3DMCS_MATERIAL);
  5134. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_AMBIENTMATERIALSOURCE, D3DMCS_COLOR1);
  5135. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_EMISSIVEMATERIALSOURCE, D3DMCS_MATERIAL);
  5136. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_NORMALIZENORMALS, FALSE);
  5137. g_OpenGLValues->m_d3ddev->SetMaterial(&g_OpenGLValues->m_material);
  5138. // State block for capturing color buffer bit
  5139. g_OpenGLValues->m_d3ddev->BeginStateBlock();
  5140. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
  5141. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_ALPHAREF, 0);
  5142. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_ALWAYS);
  5143. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
  5144. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE);
  5145. g_OpenGLValues->m_d3ddev->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ZERO);
  5146. g_OpenGLValues->m_d3ddev->EndStateBlock(&g_OpenGLValues->m_cbufbit);
  5147. // Create shaders
  5148. g_OpenGLValues->m_d3ddev->BeginStateBlock();
  5149. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
  5150. // following stage state to speedup software rasterizer
  5151. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
  5152. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
  5153. // following stage state to speedup software rasterizer
  5154. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
  5155. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);
  5156. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG2);
  5157. g_OpenGLValues->m_d3ddev->EndStateBlock(&g_OpenGLValues->m_shaders[0][0]);
  5158. g_OpenGLValues->m_d3ddev->BeginStateBlock();
  5159. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
  5160. // following stage state to speedup software rasterizer
  5161. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
  5162. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
  5163. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
  5164. // following stage state to speedup software rasterizer
  5165. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);
  5166. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
  5167. g_OpenGLValues->m_d3ddev->EndStateBlock(&g_OpenGLValues->m_shaders[0][1]);
  5168. g_OpenGLValues->m_d3ddev->BeginStateBlock();
  5169. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
  5170. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
  5171. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_COLOROP, D3DTOP_MODULATE);
  5172. // following stage state to speedup software rasterizer
  5173. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
  5174. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);
  5175. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG2);
  5176. g_OpenGLValues->m_d3ddev->EndStateBlock(&g_OpenGLValues->m_shaders[0][2]);
  5177. g_OpenGLValues->m_d3ddev->BeginStateBlock();
  5178. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
  5179. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
  5180. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_COLOROP, D3DTOP_MODULATE);
  5181. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
  5182. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);
  5183. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
  5184. g_OpenGLValues->m_d3ddev->EndStateBlock(&g_OpenGLValues->m_shaders[0][3]);
  5185. g_OpenGLValues->m_d3ddev->BeginStateBlock();
  5186. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
  5187. // following stage state to speedup software rasterizer
  5188. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
  5189. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
  5190. // following stage state to speedup software rasterizer
  5191. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
  5192. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);
  5193. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG2);
  5194. g_OpenGLValues->m_d3ddev->EndStateBlock(&g_OpenGLValues->m_shaders[0][4]);
  5195. g_OpenGLValues->m_d3ddev->BeginStateBlock();
  5196. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
  5197. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
  5198. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_COLOROP, D3DTOP_BLENDTEXTUREALPHA);
  5199. // following stage state to speedup software rasterizer
  5200. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
  5201. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);
  5202. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG2);
  5203. g_OpenGLValues->m_d3ddev->EndStateBlock(&g_OpenGLValues->m_shaders[0][5]);
  5204. g_OpenGLValues->m_d3ddev->BeginStateBlock();
  5205. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_COLORARG1, D3DTA_TEXTURE | D3DTA_COMPLEMENT);
  5206. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
  5207. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_COLOROP, D3DTOP_MODULATE);
  5208. // following stage state to speedup software rasterizer
  5209. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
  5210. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);
  5211. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG2);
  5212. g_OpenGLValues->m_d3ddev->EndStateBlock(&g_OpenGLValues->m_shaders[0][6]);
  5213. g_OpenGLValues->m_d3ddev->BeginStateBlock();
  5214. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_COLORARG1, D3DTA_TEXTURE | D3DTA_COMPLEMENT);
  5215. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
  5216. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_COLOROP, D3DTOP_MODULATE);
  5217. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
  5218. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);
  5219. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
  5220. g_OpenGLValues->m_d3ddev->EndStateBlock(&g_OpenGLValues->m_shaders[0][7]);
  5221. g_OpenGLValues->m_d3ddev->BeginStateBlock();
  5222. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
  5223. // following stage state to speedup software rasterizer
  5224. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
  5225. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_COLOROP, D3DTOP_SELECTARG2);
  5226. // following stage state to speedup software rasterizer
  5227. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
  5228. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);
  5229. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
  5230. g_OpenGLValues->m_d3ddev->EndStateBlock(&g_OpenGLValues->m_shaders[0][8]);
  5231. g_OpenGLValues->m_d3ddev->BeginStateBlock();
  5232. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
  5233. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
  5234. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_COLOROP, D3DTOP_SELECTARG2);
  5235. // following stage state to speedup software rasterizer
  5236. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
  5237. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);
  5238. g_OpenGLValues->m_d3ddev->SetTextureStageState (0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
  5239. g_OpenGLValues->m_d3ddev->EndStateBlock(&g_OpenGLValues->m_shaders[0][9]);
  5240. if(g_OpenGLValues->m_usemtex)
  5241. {
  5242. g_OpenGLValues->m_d3ddev->BeginStateBlock();
  5243. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_COLORARG1, D3DTA_TEXTURE);
  5244. // following stage state to speedup software rasterizer
  5245. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_COLORARG2, D3DTA_CURRENT);
  5246. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
  5247. // following stage state to speedup software rasterizer
  5248. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
  5249. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_ALPHAARG2, D3DTA_CURRENT);
  5250. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_ALPHAOP, D3DTOP_SELECTARG2);
  5251. g_OpenGLValues->m_d3ddev->EndStateBlock(&g_OpenGLValues->m_shaders[1][0]);
  5252. g_OpenGLValues->m_d3ddev->BeginStateBlock();
  5253. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_COLORARG1, D3DTA_TEXTURE);
  5254. // following stage state to speedup software rasterizer
  5255. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_COLORARG2, D3DTA_CURRENT);
  5256. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
  5257. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
  5258. // following stage state to speedup software rasterizer
  5259. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_ALPHAARG2, D3DTA_CURRENT);
  5260. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
  5261. g_OpenGLValues->m_d3ddev->EndStateBlock(&g_OpenGLValues->m_shaders[1][1]);
  5262. g_OpenGLValues->m_d3ddev->BeginStateBlock();
  5263. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_COLORARG1, D3DTA_TEXTURE);
  5264. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_COLORARG2, D3DTA_CURRENT);
  5265. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_COLOROP, D3DTOP_MODULATE);
  5266. // following stage state to speedup software rasterizer
  5267. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
  5268. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_ALPHAARG2, D3DTA_CURRENT);
  5269. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_ALPHAOP, D3DTOP_SELECTARG2);
  5270. g_OpenGLValues->m_d3ddev->EndStateBlock(&g_OpenGLValues->m_shaders[1][2]);
  5271. g_OpenGLValues->m_d3ddev->BeginStateBlock();
  5272. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_COLORARG1, D3DTA_TEXTURE);
  5273. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_COLORARG2, D3DTA_CURRENT);
  5274. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_COLOROP, D3DTOP_MODULATE);
  5275. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
  5276. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_ALPHAARG2, D3DTA_CURRENT);
  5277. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
  5278. g_OpenGLValues->m_d3ddev->EndStateBlock(&g_OpenGLValues->m_shaders[1][3]);
  5279. g_OpenGLValues->m_d3ddev->BeginStateBlock();
  5280. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_COLORARG1, D3DTA_TEXTURE);
  5281. // following stage state to speedup software rasterizer
  5282. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_COLORARG2, D3DTA_CURRENT);
  5283. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
  5284. // following stage state to speedup software rasterizer
  5285. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
  5286. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_ALPHAARG2, D3DTA_CURRENT);
  5287. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_ALPHAOP, D3DTOP_SELECTARG2);
  5288. g_OpenGLValues->m_d3ddev->EndStateBlock(&g_OpenGLValues->m_shaders[1][4]);
  5289. g_OpenGLValues->m_d3ddev->BeginStateBlock();
  5290. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_COLORARG1, D3DTA_TEXTURE);
  5291. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_COLORARG2, D3DTA_CURRENT);
  5292. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_COLOROP, D3DTOP_BLENDTEXTUREALPHA);
  5293. // following stage state to speedup software rasterizer
  5294. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
  5295. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_ALPHAARG2, D3DTA_CURRENT);
  5296. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_ALPHAOP, D3DTOP_SELECTARG2);
  5297. g_OpenGLValues->m_d3ddev->EndStateBlock(&g_OpenGLValues->m_shaders[1][5]);
  5298. g_OpenGLValues->m_d3ddev->BeginStateBlock();
  5299. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_COLORARG1, D3DTA_TEXTURE | D3DTA_COMPLEMENT);
  5300. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_COLORARG2, D3DTA_CURRENT);
  5301. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_COLOROP, D3DTOP_MODULATE);
  5302. // following stage state to speedup software rasterizer
  5303. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
  5304. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_ALPHAARG2, D3DTA_CURRENT);
  5305. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_ALPHAOP, D3DTOP_SELECTARG2);
  5306. g_OpenGLValues->m_d3ddev->EndStateBlock(&g_OpenGLValues->m_shaders[1][6]);
  5307. g_OpenGLValues->m_d3ddev->BeginStateBlock();
  5308. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_COLORARG1, D3DTA_TEXTURE | D3DTA_COMPLEMENT);
  5309. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_COLORARG2, D3DTA_CURRENT);
  5310. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_COLOROP, D3DTOP_MODULATE);
  5311. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
  5312. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_ALPHAARG2, D3DTA_CURRENT);
  5313. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
  5314. g_OpenGLValues->m_d3ddev->EndStateBlock(&g_OpenGLValues->m_shaders[1][7]);
  5315. g_OpenGLValues->m_d3ddev->BeginStateBlock();
  5316. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_COLORARG1, D3DTA_TEXTURE);
  5317. // following stage state to speedup software rasterizer
  5318. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_COLORARG2, D3DTA_CURRENT);
  5319. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_COLOROP, D3DTOP_SELECTARG2);
  5320. // following stage state to speedup software rasterizer
  5321. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
  5322. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_ALPHAARG2, D3DTA_CURRENT);
  5323. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
  5324. g_OpenGLValues->m_d3ddev->EndStateBlock(&g_OpenGLValues->m_shaders[1][8]);
  5325. g_OpenGLValues->m_d3ddev->BeginStateBlock();
  5326. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_COLORARG1, D3DTA_TEXTURE);
  5327. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_COLORARG2, D3DTA_CURRENT);
  5328. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_COLOROP, D3DTOP_SELECTARG2);
  5329. // following stage state to speedup software rasterizer
  5330. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
  5331. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_ALPHAARG2, D3DTA_CURRENT);
  5332. g_OpenGLValues->m_d3ddev->SetTextureStageState (1, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
  5333. g_OpenGLValues->m_d3ddev->EndStateBlock(&g_OpenGLValues->m_shaders[1][9]);
  5334. }
  5335. #if HOOK_WINDOW_PROC
  5336. // Hook into message loop
  5337. g_OpenGLValues->m_wndproc = (WNDPROC)SetWindowLong(g_OpenGLValues->m_hwnd, GWL_WNDPROC, (LONG)MyMsgHandler);
  5338. g_OpenGLValues->m_lod = 0;
  5339. #endif
  5340. // Start a scene
  5341. g_OpenGLValues->m_d3ddev->BeginScene();
  5342. return (HGLRC)1;
  5343. }
  5344. BOOL WINAPI wglDeleteContext(HGLRC hglrc)
  5345. {
  5346. #if GETPARMSFORDEBUG
  5347. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "wglDeleteContext: 0x%X", hglrc );
  5348. #endif
  5349. if (g_OpenGLValues != NULL)
  5350. {
  5351. g_OpenGLValues->m_d3ddev->EndScene();
  5352. #if HOOK_WINDOW_PROC
  5353. SetWindowLong(g_OpenGLValues->m_hwnd, GWL_WNDPROC, (LONG)g_OpenGLValues->m_wndproc);
  5354. #endif
  5355. for(int i = 0; i < MAXGLTEXHANDLES; ++i){
  5356. if(g_OpenGLValues->m_tex[i].m_ddsurf != 0) {
  5357. g_OpenGLValues->m_tex[i].m_ddsurf->Release();
  5358. g_OpenGLValues->m_tex[i].m_ddsurf = 0;
  5359. if(g_OpenGLValues->m_tex[i].m_block != 0)
  5360. {
  5361. g_OpenGLValues->m_d3ddev->DeleteStateBlock(g_OpenGLValues->m_tex[i].m_block);
  5362. g_OpenGLValues->m_tex[i].m_block = 0;
  5363. }
  5364. g_OpenGLValues->m_tex[i].m_capture = FALSE;
  5365. }
  5366. }
  5367. for(i = 0; i < 8; ++i)
  5368. g_OpenGLValues->m_d3ddev->DeleteStateBlock(g_OpenGLValues->m_shaders[0][i]);
  5369. if(g_OpenGLValues->m_usemtex)
  5370. {
  5371. for(i = 0; i < 8; ++i)
  5372. g_OpenGLValues->m_d3ddev->DeleteStateBlock(g_OpenGLValues->m_shaders[1][i]);
  5373. }
  5374. g_OpenGLValues->m_ibuf->Release();
  5375. g_OpenGLValues->m_ibuf = 0;
  5376. g_OpenGLValues->m_tex2buf->Release();
  5377. g_OpenGLValues->m_tex2buf = 0;
  5378. g_OpenGLValues->m_texbuf->Release();
  5379. g_OpenGLValues->m_texbuf = 0;
  5380. g_OpenGLValues->m_colbuf->Release();
  5381. g_OpenGLValues->m_colbuf = 0;
  5382. g_OpenGLValues->m_xyzbuf->Release();
  5383. g_OpenGLValues->m_xyzbuf = 0;
  5384. g_OpenGLValues->m_vbuf->Release();
  5385. g_OpenGLValues->m_vbuf = 0;
  5386. /*
  5387. Although this is the correct location to release the m_d3ddev object
  5388. we aren't going to do it here and instead we will do it in the
  5389. NOTIFY_FUNCTION when the DLL is detached from the process. I am
  5390. doing this to prevent apps (such as MDK2) from crashing on exit due
  5391. to the fact that they continue to call GL functions after deleting
  5392. the context which causes an access violation. (a-brienw 03/02/2001)
  5393. g_OpenGLValues->m_d3ddev->Release();
  5394. g_OpenGLValues->m_d3ddev = 0;
  5395. */
  5396. }
  5397. return TRUE;
  5398. }
  5399. int WINAPI wglDescribePixelFormat(HDC hdc, INT iPixelFormat, UINT nBytes, PIXELFORMATDESCRIPTOR *ppfd)
  5400. {
  5401. #if GETPARMSFORDEBUG
  5402. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "wglDescribePixelFormat: 0x%X 0x%X 0x%X 0x%X", hdc, iPixelFormat, nBytes, ppfd );
  5403. #endif
  5404. if (ppfd != NULL)
  5405. {
  5406. ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
  5407. ppfd->nVersion = 1;
  5408. ppfd->dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_GENERIC_ACCELERATED | PFD_DOUBLEBUFFER;
  5409. ppfd->iPixelType = PFD_TYPE_RGBA;
  5410. ppfd->cColorBits = (unsigned char)GetDeviceCaps(hdc, BITSPIXEL);
  5411. ppfd->cAccumBits = 0;
  5412. ppfd->cAccumRedBits = 0;
  5413. ppfd->cAccumGreenBits = 0;
  5414. ppfd->cAccumBlueBits = 0;
  5415. ppfd->cAccumAlphaBits = 0;
  5416. ppfd->cStencilBits = 0;
  5417. ppfd->cAuxBuffers = 0;
  5418. ppfd->iLayerType = 0;
  5419. ppfd->bReserved = 0;
  5420. ppfd->dwLayerMask = 0;
  5421. ppfd->dwVisibleMask = 0;
  5422. ppfd->dwDamageMask = 0;
  5423. if(GetDeviceCaps(hdc, BITSPIXEL) == 16)
  5424. {
  5425. ppfd->cRedBits = 5;
  5426. ppfd->cRedShift = 11;
  5427. ppfd->cGreenBits = 6;
  5428. ppfd->cGreenShift = 5;
  5429. ppfd->cBlueBits = 5;
  5430. ppfd->cBlueShift = 0;
  5431. ppfd->cAlphaBits = 0;
  5432. ppfd->cAlphaShift = 0;
  5433. ppfd->cDepthBits = 16;
  5434. }
  5435. else if(GetDeviceCaps(hdc, BITSPIXEL) == 24 || GetDeviceCaps(hdc, BITSPIXEL) == 32)
  5436. {
  5437. ppfd->cRedBits = 8;
  5438. ppfd->cRedShift = 16;
  5439. ppfd->cGreenBits = 8;
  5440. ppfd->cGreenShift = 8;
  5441. ppfd->cBlueBits = 8;
  5442. ppfd->cBlueShift = 0;
  5443. ppfd->cAlphaBits = 0;
  5444. ppfd->cAlphaShift = 0;
  5445. ppfd->cDepthBits = 16;
  5446. }
  5447. else
  5448. {
  5449. return 0;
  5450. }
  5451. }
  5452. return 1;
  5453. }
  5454. HGLRC WINAPI wglGetCurrentContext(VOID)
  5455. {
  5456. #if GETPARMSFORDEBUG
  5457. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "wglGetCurrentContext" );
  5458. #endif
  5459. return (HGLRC)1;
  5460. }
  5461. HDC WINAPI wglGetCurrentDC(VOID)
  5462. {
  5463. #if GETPARMSFORDEBUG
  5464. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "wglGetCurrentDC" );
  5465. #endif
  5466. return g_OpenGLValues->m_hdc;
  5467. }
  5468. int WINAPI wglGetPixelFormat(HDC hdc)
  5469. {
  5470. #if GETPARMSFORDEBUG
  5471. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "wglGetPixelFormat: 0x%X", hdc );
  5472. #endif
  5473. return 1;
  5474. }
  5475. PROC WINAPI wglGetProcAddress(LPCSTR str)
  5476. {
  5477. if(strcmp(str, "glMTexCoord2fSGIS") == 0)
  5478. return (PROC)glMTexCoord2fSGIS;
  5479. else if(strcmp(str, "glSelectTextureSGIS") == 0)
  5480. return (PROC)glSelectTextureSGIS;
  5481. else if(strcmp(str, "glActiveTextureARB") == 0)
  5482. return (PROC)glActiveTextureARB;
  5483. else if(strcmp(str, "glClientActiveTextureARB") == 0)
  5484. return (PROC)glClientActiveTextureARB;
  5485. else if(strcmp(str, "glMultiTexCoord2fARB") == 0)
  5486. return (PROC)glMultiTexCoord2fARB;
  5487. else if(strcmp(str, "glMultiTexCoord2fvARB") == 0)
  5488. return (PROC)glMultiTexCoord2fvARB;
  5489. else if(strcmp(str, "glLockArraysEXT") == 0)
  5490. return (PROC)glLockArraysEXT;
  5491. else if(strcmp(str, "glUnlockArraysEXT") == 0)
  5492. return (PROC)glUnlockArraysEXT;
  5493. else if(strcmp(str, "wglSwapIntervalEXT") == 0)
  5494. return (PROC)wglSwapIntervalEXT;
  5495. #if DODPFS
  5496. else
  5497. {
  5498. char junk[256];
  5499. sprintf( junk, "Wrapper: Unimplemented function (%s)\n", str );
  5500. OutputDebugStringA( junk );
  5501. }
  5502. #endif
  5503. return NULL;
  5504. }
  5505. BOOL wglMakeCurrent(HDC hdc, HGLRC hglrc)
  5506. {
  5507. #if GETPARMSFORDEBUG
  5508. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "wglMakeCurrent: 0x%X 0x%X", hdc, hglrc );
  5509. #endif
  5510. return g_OpenGLValues->m_d3ddev != NULL; // Fail if no device
  5511. }
  5512. BOOL WINAPI wglSetPixelFormat(HDC hdc, int iPixelFormat, CONST PIXELFORMATDESCRIPTOR *ppfd)
  5513. {
  5514. #if GETPARMSFORDEBUG
  5515. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "wglSetPixelFormat: 0x%X 0x%X 0x%X", hdc, iPixelFormat, ppfd );
  5516. #endif
  5517. return TRUE;
  5518. }
  5519. BOOL WINAPI wglSwapBuffers(HDC hdc)
  5520. {
  5521. #if GETPARMSFORDEBUG
  5522. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "wglSwapBuffers: 0x%X", hdc );
  5523. #endif
  5524. if( g_OpenGLValues->m_withinprim == TRUE )
  5525. {
  5526. #if DODPFS
  5527. char junk[256];
  5528. sprintf( junk, "Wrapper: wglSwapBuffers primitive=%d cnt=%d\n", g_OpenGLValues->m_prim, g_OpenGLValues->m_vcnt );
  5529. OutputDebugStringA( junk );
  5530. #endif
  5531. glEnd();
  5532. }
  5533. g_OpenGLValues->m_d3ddev->EndScene();
  5534. g_OpenGLValues->m_d3ddev->Present(NULL, NULL, NULL, NULL);
  5535. g_OpenGLValues->m_d3ddev->BeginScene();
  5536. return TRUE;
  5537. }
  5538. BOOL WINAPI wglSwapIntervalEXT(GLint interval)
  5539. {
  5540. #if GETPARMSFORDEBUG
  5541. LOG("EmulateOpenGL - PARMS", eDbgLevelInfo, "wglSwapIntervalEXT: 0x%X", interval );
  5542. #endif
  5543. return TRUE;
  5544. }