1 /************************************************************************
3 * voxelands - 3d voxel world sandbox game
4 * Copyright (C) Lisa 'darkrose' Milne 2016 <lisa@ltmnet.com>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 * See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>
18 ************************************************************************/
28 material_t
*materials
;
30 GLuint current_texture
[8];
43 material_t
*mat_find(char* name
)
45 material_t
*m
= mat_data
.materials
;
48 if (!strcmp(m
->name
,name
))
56 /* create a new blank material */
57 material_t
*mat_create()
59 material_t
*mat
= malloc(sizeof(material_t
));
60 mat
->id
= mat_data
.ids
++;
62 mat
->options
= MATOPT_BFCULL
;
63 array_init(&mat
->textures
,ARRAY_TYPE_PTR
);
66 mat
->reflectivity
= 0.0;
69 mat_data
.materials
= list_push(&mat_data
.materials
,mat
);
75 void mat_free(material_t
*mat
)
81 mat_data
.materials
= list_remove(&mat_data
.materials
,mat
);
83 while ((tex
= array_pop_ptr(&mat
->textures
))) {
87 array_free(&mat
->textures
,0);
92 /* create a material from a texture */
93 material_t
*mat_from_tex(texture_t
*tex
)
95 material_t
*mat
= mat_create();
96 array_push_ptr(&mat
->textures
,tex
);
97 strcpy(mat
->name
,tex
->name
);
102 /* create a material from pixel data */
103 material_t
*mat_from_pixels(image_t
*p
)
106 material_t
*mat
= mat_create();
107 tex
= tex_from_pixels(p
);
108 array_push_ptr(&mat
->textures
,tex
);
109 strcpy(mat
->name
,tex
->name
);
114 /* create a material from an image */
115 material_t
*mat_from_image(char* type
, char* file
)
120 tex
= tex_from_image(type
,file
);
125 array_push_ptr(&mat
->textures
,tex
);
127 strcpy(mat
->name
,tex
->name
);
132 /* combine size images into a texture cube */
133 material_t
*mat_from_box(char* front
, char* back
, char* left
, char* right
, char* top
, char* bottom
)
138 tex
= tex_from_box(front
,back
,left
,right
,top
,bottom
);
143 array_push_ptr(&mat
->textures
,tex
);
145 strcpy(mat
->name
,tex
->name
);
150 /* add a second texture to a material from an image */
151 int mat_add_image(material_t
*mat
, char* type
, char* file
)
154 tex
= tex_from_image(type
,file
);
158 array_push_ptr(&mat
->textures
,tex
);
163 /* add a new texture to a material */
164 int mat_add_tex(material_t
*mat
, texture_t
*tex
)
169 array_push_ptr(&mat
->textures
,tex
);
174 /* create a material from a colour */
175 material_t
*mat_from_colour(colour_t
*c
)
180 snprintf(buff
,256,"rgba-%d-%d-%d-%d",c
->r
,c
->g
,c
->b
,c
->a
);
182 tex
= tex_from_rgba(1,1,c
->r
,c
->g
,c
->b
,c
->a
);
187 array_push_ptr(&mat
->textures
,tex
);
189 strcpy(mat
->name
,buff
);
194 static void mat_apply_opts(uint32_t options
)
196 /* back face culling */
197 if ((options
&MATOPT_BFCULL
) == MATOPT_BFCULL
) {
198 if (!mat_data
.culling
) {
199 glEnable(GL_CULL_FACE
);
201 mat_data
.culling
= 1;
203 }else if (mat_data
.culling
) {
204 glDisable(GL_CULL_FACE
);
205 mat_data
.culling
= 0;
209 if ((options
&MATOPT_ADDITIVE_BLEND
) == MATOPT_ADDITIVE_BLEND
) {
210 if (!mat_data
.blended
) {
212 glEnable(GL_LINE_SMOOTH
);
213 mat_data
.blended
= 1;
215 if (mat_data
.blend_mode
!= GL_ONE
) {
216 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
217 mat_data
.blend_mode
= GL_ONE
;
219 }else if ((options
&MATOPT_ALPHA_BLEND
) == MATOPT_ALPHA_BLEND
) {
220 if (!mat_data
.blended
) {
222 glEnable(GL_LINE_SMOOTH
);
223 mat_data
.blended
= 1;
225 if (mat_data
.blend_mode
!= GL_ONE_MINUS_SRC_ALPHA
) {
226 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
227 mat_data
.blend_mode
= GL_ONE_MINUS_SRC_ALPHA
;
229 }else if (mat_data
.blended
) {
231 mat_data
.blended
= 0;
232 mat_data
.blend_mode
= GL_ZERO
;
235 if ((options
&MATOPT_SDF_ALPHA
) == MATOPT_SDF_ALPHA
) {
236 if (opengl_has_psdf()) {
237 shader_uniform_float(NULL
,"alphafunc",2.0);
239 shader_uniform_float(NULL
,"alphafunc",1.0);
241 }else if ((options
&MATOPT_ALPHA_TEST
) == MATOPT_ALPHA_TEST
) {
242 shader_uniform_float(NULL
,"alphafunc",1.0);
244 shader_uniform_float(NULL
,"alphafunc",0.0);
248 static void mat_bind(GLuint tex
, GLenum type
, uint8_t slot
)
251 if (slot
< 0 || slot
> 7)
254 if (mat_data
.current_texture
[slot
] == tex
)
259 glActiveTexture(GL_TEXTURE0
);
262 glActiveTexture(GL_TEXTURE1
);
265 glActiveTexture(GL_TEXTURE2
);
268 glActiveTexture(GL_TEXTURE3
);
271 glActiveTexture(GL_TEXTURE4
);
274 glActiveTexture(GL_TEXTURE5
);
277 glActiveTexture(GL_TEXTURE6
);
280 glActiveTexture(GL_TEXTURE7
);
286 snprintf(buff
,32,"texture%u",slot
);
288 glBindTexture(type
,tex
);
290 mat_data
.current_texture
[slot
] = tex
;
292 shader_uniform_int(NULL
,buff
,slot
);
295 /* bind a texture and apply options */
296 int mat_bind_with_opts(GLuint tex
, uint32_t options
)
299 mat_bind(tex
,GL_TEXTURE_2D
,0);
300 mat_apply_opts(options
);
302 for (i
=1; i
<8; i
++) {
303 mat_bind(0,GL_TEXTURE_2D
,i
);
311 void mat_use(material_t
*mat
, shader_t
*shader
)
313 if (mat
->textures
.length
) {
316 for (i
=0; i
<8; i
++) {
317 tex
= array_get_ptr(&mat
->textures
,i
);
321 mat_bind(tex
->glid
,tex
->type
,i
);
322 }else if (mat_data
.current_texture
[i
]) {
323 mat_bind(0,GL_TEXTURE_2D
,i
);
326 mat_apply_opts(mat
->options
);
328 mat_bind_with_opts(0,mat
->options
);
331 /* specular values */
332 shader_uniform_float(shader
,"shininess",mat
->shininess
);
333 shader_uniform_float(shader
,"reflectivity",mat
->reflectivity
);
334 shader_uniform_float(shader
,"bumpiness",mat
->bumpiness
);
337 /* set the shininess and reflectivity of a material */
338 void mat_shininess(material_t
*mat
, float shininess
, float reflectivity
)
340 mat
->shininess
= shininess
;
341 mat
->reflectivity
= reflectivity
;
344 /* set the bumpiness (for bump mapping) of a material */
345 void mat_bumpiness(material_t
*mat
, float bumpiness
)
347 mat
->bumpiness
= bumpiness
;
350 /* set the name of a material */
351 void mat_name(material_t
*mat
, char* name
)
353 strncpy(mat
->name
,name
,256);
356 /* set the options for a material, return the previous options */
357 uint32_t mat_options(material_t
*mat
, uint32_t opt
)
359 uint32_t o
= mat
->options
;
360 if (opt
!= MATOPT_GET
)