Leaked source code of windows server 2003
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.

5860 lines
206 KiB

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