home *** CD-ROM | disk | FTP | other *** search
-
- /*
- * texture.c -- standard manipulations for textures
- *
- * (c) 1993, 1994 by Han-Wen Nienhuys <hanwen@stack.urc.tue.nl>
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by the
- * Free Software Foundation;
- *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 675 Mass Ave, Cambridge, MA 02139, USA.
- */
-
- #include <stdlib.h>
- #include "ray.h"
- #include "proto.h"
- #include "extern.h"
-
- PRIVATE void
- init_texture(struct texture_data *td)
- {
- color black;
-
- /* initialize a texture to black, riennada f.. nothing */
- td->PoVcolor = NULL;
- td->type = T_EGAL;
- td->shadingtype = PHONG_SHADING;
-
- td->refl_diffuse =
- td->refr_diffuse =
- td->roughness = 0.00;
-
- td->brilliance = td->index = 1.0;
-
- setcolor(black, 0, 0, 0);
-
- td->refl_color = black;
- td->refr_color = black;
- td->ambient = black;
- td->diffuse = black;
- td->specular = black;
-
- unit_matrix(td->inv_trans);
- }
-
- PUBLIC struct texture_data *
- get_new_texture(void)
- {
- struct texture_data *td;
-
- td = ALLOC(struct texture_data);
-
- CHECK_MEM(td, "texture");
- init_texture(td);
-
- return td;
- }
-
- /* standard routine for a texture */
- PUBLIC struct texture_data *
- copy_texture(struct texture_data *dst, struct texture_data *src)
- {
- assert(dst != NULL && src != NULL);
-
- *dst = *src;
- if (src->PoVcolor != NULL) {
- dst->PoVcolor = get_new_color();
- copy_color(dst->PoVcolor, src->PoVcolor);
- }
- if (src->type == T_IMAGEMAP) {
- dst->exttext.image = get_new_image_map();
- copy_image_map(dst->exttext.image, src->exttext.image);
- }
- return dst;
- }
-
- /* translate a texture. */
- PUBLIC void
- translate_texture(struct texture_data *t, vector v)
- {
- matrix m;
-
- if (t->type == T_EGAL)
- return;
-
- inv_translate_matrix(m, v);
-
- mmproduct(t->inv_trans, t->inv_trans, m);
- }
-
- /* rotation */
- PUBLIC void
- rotate_texture(struct texture_data *t, matrix rotmat)
- {
- matrix m;
-
- if (t->type == T_EGAL)
- return;
-
- transpose_matrix(m, rotmat);
- mmproduct(t->inv_trans, t->inv_trans, m);
- }
-
- /* scale. Not needed for egal textures */
- PUBLIC void
- scale_texture(struct texture_data *t, vector s)
- {
- matrix m;
-
- if (t->type == T_EGAL)
- return;
-
- inv_scale_matrix(m, s);
- mmproduct(t->inv_trans, t->inv_trans, m);
- }
-
- /* zapit. Don't forget to free imagemaps as well */
- PUBLIC void
- free_texture(struct texture_data *t)
- {
- if (t->PoVcolor != NULL)
- free_color(t->PoVcolor);
- switch (t->type) {
- case T_EGAL:
- break;
- case T_IMAGEMAP:
- free_image_map(t->exttext.image);
- break;
- default:
- assert(FALSE);
- }
-
- free((void *) t);
- }
-
- /*
- * get homogenous equivalent of of orgtext at point x
- */
- PUBLIC void
- fill_texture(struct texture_data *tp, struct texture_data *orgtext, struct intersect i)
- {
- color c;
- vector x = i.x;
-
- *tp = *orgtext;
- if (orgtext->type == T_EGAL) {
-
- /* pov compatibility */
- if (orgtext->PoVcolor != NULL) {
- c = *orgtext->PoVcolor;
- tp->ambient = scolorproduct(color_length(orgtext->ambient), c);
- tp->diffuse = scolorproduct(color_length(orgtext->diffuse), c);
-
- /* specular always looks white in PoV */
- setcolor(tp->specular, 1, 1, 1);
- scale_color(&tp->specular, color_length(orgtext->specular));
-
- tp->refl_color = scolorproduct(color_length(orgtext->refl_color), c);
- tp->refr_color = scolorproduct(color_length(orgtext->refr_color), c);
- return;
- }
- } else if (orgtext->type == T_IMAGEMAP) {
- x = mvproduct(orgtext->inv_trans, x);
- c = get_image_map_color(orgtext->exttext.image, i);
- tp->ambient = scolorproduct(color_length(orgtext->ambient), c);
- tp->diffuse = scolorproduct(color_length(orgtext->diffuse), c);
-
- /* specular always looks white in PoV */
- setcolor(tp->specular, 1, 1, 1);
- scale_color(&tp->specular, color_length(orgtext->specular));
-
- tp->refl_color = scolorproduct(color_length(orgtext->refl_color), c);
- tp->refr_color = scolorproduct(color_length(orgtext->refr_color), c);
- return;
- } else
- assert(FALSE);
- }
-
- PUBLIC void
- print_texture(struct texture_data *tx)
- {
- #ifdef DEBUG
- printf("\n\tTEXTURE TYPE %d\n", tx->type);
- if (tx->type == T_IMAGEMAP) {
- print_image_map(tx->exttext.image);
- printf("transformation:\n");
- print_matrix(tx->inv_trans);
- }
- if (tx->PoVcolor != NULL) {
- printf("POV compatibility\n");
- print_c("POV color", *tx->PoVcolor);
- }
- print_c("ambient", tx->ambient);
- print_c("diffuse", tx->diffuse);
- printf("brilliance %lf\n", tx->brilliance);
-
- print_c("specular", tx->specular);
- printf("roughness %lf\n", tx->roughness);
-
- printf("reflang %lf ", tx->refl_diffuse);
- print_c("reflcolor", tx->refl_color);
-
- printf("ior %lf refrang %lf, ", tx->index, tx->refr_diffuse);
- print_c("refrcolor", tx->refr_color);
- #endif
- }
-