- fixed D3DFVF_VERTEX case
[wine] / dlls / ddraw / mesa.c
1 /* Direct3D Common functions
2  * Copyright (c) 1998 Lionel ULMER
3  *
4  * This file contains all MESA common code
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22
23 #include "windef.h"
24 #include "wine/obj_base.h"
25 #include "ddraw.h"
26 #include "d3d.h"
27 #include "wine/debug.h"
28
29 #include "mesa_private.h"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
32
33 void set_render_state(D3DRENDERSTATETYPE dwRenderStateType,
34                       DWORD dwRenderState, RenderState *rs)
35 {
36     if (TRACE_ON(ddraw))
37         TRACE("%s = %08lx\n", _get_renderstate(dwRenderStateType), dwRenderState);
38
39     /* First, all the stipple patterns */
40     if ((dwRenderStateType >= D3DRENDERSTATE_STIPPLEPATTERN00) &&
41         (dwRenderStateType <= D3DRENDERSTATE_STIPPLEPATTERN31)) {
42         ERR("Unhandled dwRenderStateType stipple %d!\n",dwRenderStateType);
43     } else {
44         ENTER_GL();
45
46         /* All others state variables */
47         switch (dwRenderStateType) {
48             case D3DRENDERSTATE_TEXTUREHANDLE: {    /*  1 */
49                 IDirect3DTextureImpl *tex = (IDirect3DTextureImpl*) dwRenderState;
50                 
51                 if (tex == NULL) {
52                     glBindTexture(GL_TEXTURE_2D, 0);
53                     glDisable(GL_TEXTURE_2D);
54                     TRACE("disabling texturing\n");
55                 } else {
56                     IDirect3DTextureGLImpl *gl_tex = (IDirect3DTextureGLImpl *) tex;
57                     
58                     glEnable(GL_TEXTURE_2D);
59                     /* Default parameters */
60                     glBindTexture(GL_TEXTURE_2D, gl_tex->tex_name);
61                     /* To prevent state change, we could test here what are the parameters
62                        stored in the texture */
63                     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, rs->mag);
64                     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, rs->min);
65                     TRACE("setting OpenGL texture handle : %d\n", gl_tex->tex_name);
66                 }
67             } break;
68
69             case D3DRENDERSTATE_TEXTUREPERSPECTIVE: /* 4 */
70                 if (dwRenderState)
71                     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
72                 else
73                     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
74                 break;
75
76             case D3DRENDERSTATE_ZENABLE:          /*  7 */
77                 if (dwRenderState)
78                     glEnable(GL_DEPTH_TEST);
79                 else
80                     glDisable(GL_DEPTH_TEST);
81                 break;
82
83             case D3DRENDERSTATE_FILLMODE:           /*  8 */
84                 switch ((D3DFILLMODE) dwRenderState) {
85                     case D3DFILL_POINT:
86                         glPolygonMode(GL_FRONT_AND_BACK,GL_POINT);        
87                         break;
88                     case D3DFILL_WIREFRAME:
89                         glPolygonMode(GL_FRONT_AND_BACK,GL_LINE); 
90                         break;
91                     case D3DFILL_SOLID:
92                         glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); 
93                         break;
94                     default:
95                         ERR("Unhandled fill mode !\n");
96                  }
97                  break;
98
99             case D3DRENDERSTATE_SHADEMODE:          /*  9 */
100                 switch ((D3DSHADEMODE) dwRenderState) {
101                     case D3DSHADE_FLAT:
102                         glShadeModel(GL_FLAT);
103                         break;
104                     case D3DSHADE_GOURAUD:
105                         glShadeModel(GL_SMOOTH);
106                         break;
107                     default:
108                         ERR("Unhandled shade mode !\n");
109                 }
110                 break;
111
112             case D3DRENDERSTATE_ZWRITEENABLE:     /* 14 */
113                 if (dwRenderState)
114                     glDepthMask(GL_TRUE);
115                 else
116                     glDepthMask(GL_FALSE);
117                 break;
118
119             case D3DRENDERSTATE_TEXTUREMAG:         /* 17 */
120                 switch ((D3DTEXTUREFILTER) dwRenderState) {
121                     case D3DFILTER_NEAREST:
122                         rs->mag = GL_NEAREST;
123                         break;
124                     case D3DFILTER_LINEAR:
125                         rs->mag = GL_LINEAR;
126                         break;
127                     default:
128                         ERR("Unhandled texture mag !\n");
129                 }
130                 break;
131
132             case D3DRENDERSTATE_TEXTUREMIN:         /* 18 */
133                 switch ((D3DTEXTUREFILTER) dwRenderState) {
134                     case D3DFILTER_NEAREST:
135                         rs->min = GL_NEAREST;
136                         break;
137                     case D3DFILTER_LINEAR:
138                         rs->mag = GL_LINEAR;
139                         break;
140                     default:
141                         ERR("Unhandled texture min !\n");
142                 }
143                 break;
144
145             case D3DRENDERSTATE_SRCBLEND:           /* 19 */
146                 switch ((D3DBLEND) dwRenderState) {
147                     case D3DBLEND_SRCALPHA:
148                           rs->src = GL_SRC_ALPHA;
149                           break;
150                     default:
151                           ERR("Unhandled blend mode !\n");
152                 }
153                 glBlendFunc(rs->src, rs->dst);
154                 break;
155
156             case D3DRENDERSTATE_DESTBLEND:          /* 20 */
157                 switch ((D3DBLEND) dwRenderState) {
158                     case D3DBLEND_INVSRCALPHA:
159                         rs->dst = GL_ONE_MINUS_SRC_ALPHA;
160                         break;
161                     default:
162                         ERR("Unhandled blend mode !\n");
163                 }
164                 glBlendFunc(rs->src, rs->dst);
165                 break;
166
167             case D3DRENDERSTATE_TEXTUREMAPBLEND:    /* 21 */
168                 switch ((D3DTEXTUREBLEND) dwRenderState) {
169                     case D3DTBLEND_MODULATE:
170                     case D3DTBLEND_MODULATEALPHA:
171                           glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
172                           break;
173                     default:
174                           ERR("Unhandled texture environment !\n");
175                 }
176                 break;
177
178             case D3DRENDERSTATE_CULLMODE:           /* 22 */
179                 switch ((D3DCULL) dwRenderState) {
180                     case D3DCULL_NONE:
181                          glDisable(GL_CULL_FACE);
182                          break;
183                     case D3DCULL_CW:
184                          glEnable(GL_CULL_FACE);
185                          glFrontFace(GL_CW);
186                          break;
187                     case D3DCULL_CCW:
188                          glEnable(GL_CULL_FACE);
189                          glFrontFace(GL_CCW);
190                          break;
191                     default:
192                          ERR("Unhandled cull mode !\n");
193                 }
194                 break;
195
196             case D3DRENDERSTATE_ZFUNC:            /* 23 */
197                 switch ((D3DCMPFUNC) dwRenderState) {
198                     case D3DCMP_NEVER:
199                         glDepthFunc(GL_NEVER);
200                         break;
201                     case D3DCMP_LESS:
202                         glDepthFunc(GL_LESS);
203                         break;
204                     case D3DCMP_EQUAL:
205                         glDepthFunc(GL_EQUAL);
206                         break;
207                     case D3DCMP_LESSEQUAL:
208                         glDepthFunc(GL_LEQUAL);
209                         break;
210                     case D3DCMP_GREATER:
211                         glDepthFunc(GL_GREATER);
212                         break;
213                     case D3DCMP_NOTEQUAL:
214                         glDepthFunc(GL_NOTEQUAL);
215                         break;
216                     case D3DCMP_GREATEREQUAL:
217                         glDepthFunc(GL_GEQUAL);
218                         break;
219                     case D3DCMP_ALWAYS:
220                         glDepthFunc(GL_ALWAYS);
221                         break;
222                     default:
223                         ERR("Unexpected value\n");
224                 }
225                 break;
226
227             case D3DRENDERSTATE_DITHERENABLE:     /* 26 */
228                 if (dwRenderState)
229                     glEnable(GL_DITHER);
230                 else
231                     glDisable(GL_DITHER);
232                 break;
233
234             case D3DRENDERSTATE_ALPHABLENDENABLE:   /* 27 */
235                 if (dwRenderState)
236                     glEnable(GL_BLEND);
237                 else
238                     glDisable(GL_BLEND);
239                 break;
240
241             case D3DRENDERSTATE_COLORKEYENABLE:     /* 41 */
242                 if (dwRenderState)
243                     glEnable(GL_BLEND);
244                 else
245                     glDisable(GL_BLEND);
246                 break;
247
248             case D3DRENDERSTATE_FLUSHBATCH:         /* 50 */
249                 break;
250
251             default:
252                 ERR("Unhandled dwRenderStateType %s !\n", _get_renderstate(dwRenderStateType));
253         }
254         LEAVE_GL();
255     }
256 }