Added LGPL standard comment, and copyright notices where necessary.
[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 #define D3DTPRIVATE(x) mesa_d3dt_private *dtpriv = (mesa_d3dt_private*)(x)->private
34
35 void set_render_state(D3DRENDERSTATETYPE dwRenderStateType,
36                       DWORD dwRenderState, RenderState *rs)
37 {
38
39   if (TRACE_ON(ddraw))
40     _dump_renderstate(dwRenderStateType, dwRenderState);
41
42   /* First, all the stipple patterns */
43   if ((dwRenderStateType >= D3DRENDERSTATE_STIPPLEPATTERN00) && 
44       (dwRenderStateType <= D3DRENDERSTATE_STIPPLEPATTERN31)) {
45     ERR("Unhandled dwRenderStateType stipple %d!\n",dwRenderStateType);
46   } else {
47     ENTER_GL();
48     
49     /* All others state variables */
50     switch (dwRenderStateType) {
51
52     case D3DRENDERSTATE_TEXTUREHANDLE: {    /*  1 */
53       IDirect3DTexture2Impl* tex = (IDirect3DTexture2Impl*) dwRenderState;
54       
55       if (tex == NULL) {
56         glBindTexture(GL_TEXTURE_2D, 0);
57         glDisable(GL_TEXTURE_2D);
58         TRACE("disabling texturing\n");
59       } else {
60         D3DTPRIVATE(tex);
61         
62         glEnable(GL_TEXTURE_2D);
63         /* Default parameters */
64         glBindTexture(GL_TEXTURE_2D, dtpriv->tex_name);
65         /* To prevent state change, we could test here what are the parameters
66            stored in the texture */
67         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, rs->mag);
68         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, rs->min);
69         TRACE("setting OpenGL texture handle : %d\n", dtpriv->tex_name);
70       }
71     } break;
72
73     case D3DRENDERSTATE_TEXTUREPERSPECTIVE: /* 4 */
74       if (dwRenderState)
75         glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
76       else
77         glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
78       break;
79       
80     case D3DRENDERSTATE_ZENABLE:          /*  7 */
81       if (dwRenderState)
82         glEnable(GL_DEPTH_TEST);
83       else
84         glDisable(GL_DEPTH_TEST);
85       break;
86       
87     case D3DRENDERSTATE_FILLMODE:           /*  8 */
88       switch ((D3DFILLMODE) dwRenderState) {
89       case D3DFILL_SOLID:
90         break;
91
92       default:
93         ERR("Unhandled fill mode !\n");
94       }
95       break;
96
97     case D3DRENDERSTATE_SHADEMODE:          /*  9 */
98       switch ((D3DSHADEMODE) dwRenderState) {
99       case D3DSHADE_FLAT:
100         glShadeModel(GL_FLAT);
101         break;
102
103       case D3DSHADE_GOURAUD:
104         glShadeModel(GL_SMOOTH);
105         break;
106
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         
125       case D3DFILTER_LINEAR:
126         rs->mag = GL_LINEAR;
127         break;
128         
129       default:
130         ERR("Unhandled texture mag !\n");
131       }
132       break;
133
134     case D3DRENDERSTATE_TEXTUREMIN:         /* 18 */
135       switch ((D3DTEXTUREFILTER) dwRenderState) {
136       case D3DFILTER_NEAREST:
137         rs->min = GL_NEAREST;
138         break;
139         
140       case D3DFILTER_LINEAR:
141         rs->mag = GL_LINEAR;
142         break;
143         
144       default:
145         ERR("Unhandled texture min !\n");
146       }
147       break;
148       
149     case D3DRENDERSTATE_SRCBLEND:           /* 19 */
150       switch ((D3DBLEND) dwRenderState) {
151       case D3DBLEND_SRCALPHA:
152         rs->src = GL_SRC_ALPHA;
153         break;
154
155       default:
156         ERR("Unhandled blend mode !\n");
157       }
158       
159       glBlendFunc(rs->src, rs->dst);
160       break;
161       
162     case D3DRENDERSTATE_DESTBLEND:          /* 20 */
163       switch ((D3DBLEND) dwRenderState) {
164       case D3DBLEND_INVSRCALPHA:
165         rs->dst = GL_ONE_MINUS_SRC_ALPHA;
166         break;
167         
168       default:
169         ERR("Unhandled blend mode !\n");
170       }
171       
172       glBlendFunc(rs->src, rs->dst);
173       break;
174
175     case D3DRENDERSTATE_TEXTUREMAPBLEND:    /* 21 */
176       switch ((D3DTEXTUREBLEND) dwRenderState) {
177       case D3DTBLEND_MODULATE:
178       case D3DTBLEND_MODULATEALPHA:
179         glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
180         break;
181
182       default:
183         ERR("Unhandled texture environment !\n");
184       }
185       break;
186       
187     case D3DRENDERSTATE_CULLMODE:           /* 22 */
188       switch ((D3DCULL) dwRenderState) {
189       case D3DCULL_NONE:
190         glDisable(GL_CULL_FACE);
191         break;
192         
193       case D3DCULL_CW:
194         glEnable(GL_CULL_FACE);
195         glFrontFace(GL_CW);
196         break;
197         
198       case D3DCULL_CCW:
199         glEnable(GL_CULL_FACE);
200         glFrontFace(GL_CCW);
201         break;
202         
203       default:
204         ERR("Unhandled cull mode !\n");
205       }
206       break;
207       
208     case D3DRENDERSTATE_ZFUNC:            /* 23 */
209       switch ((D3DCMPFUNC) dwRenderState) {
210       case D3DCMP_NEVER:
211         glDepthFunc(GL_NEVER);
212         break;
213       case D3DCMP_LESS:
214         glDepthFunc(GL_LESS);
215         break;
216       case D3DCMP_EQUAL:
217         glDepthFunc(GL_EQUAL);
218         break;
219       case D3DCMP_LESSEQUAL:
220         glDepthFunc(GL_LEQUAL);
221         break;
222       case D3DCMP_GREATER:
223         glDepthFunc(GL_GREATER);
224         break;
225       case D3DCMP_NOTEQUAL:
226         glDepthFunc(GL_NOTEQUAL);
227         break;
228       case D3DCMP_GREATEREQUAL:
229         glDepthFunc(GL_GEQUAL);
230         break;
231       case D3DCMP_ALWAYS:
232         glDepthFunc(GL_ALWAYS);
233         break;
234
235       default:
236         ERR("Unexpected value\n");
237       }
238       break;
239       
240     case D3DRENDERSTATE_DITHERENABLE:     /* 26 */
241       if (dwRenderState)
242         glEnable(GL_DITHER);
243       else
244         glDisable(GL_DITHER);
245       break;
246       
247     case D3DRENDERSTATE_ALPHABLENDENABLE:   /* 27 */
248       if (dwRenderState)
249         glEnable(GL_BLEND);
250       else
251         glDisable(GL_BLEND);
252       break;
253
254     case D3DRENDERSTATE_COLORKEYENABLE:     /* 41 */
255       if (dwRenderState)
256         glEnable(GL_BLEND);
257       else
258         glDisable(GL_BLEND);
259       break;
260
261     case D3DRENDERSTATE_FLUSHBATCH:         /* 50 */
262       break;
263       
264     default:
265       ERR("Unhandled dwRenderStateType %d!\n",dwRenderStateType);
266       break;
267     }
268
269     LEAVE_GL();
270   }
271 }