nuclear@12: /* nuclear@12: eqemu - electronic queue system emulator nuclear@12: Copyright (C) 2014 John Tsiombikas , nuclear@12: Eleni-Maria Stea nuclear@12: nuclear@12: This program is free software: you can redistribute it and/or modify nuclear@12: it under the terms of the GNU General Public License as published by nuclear@12: the Free Software Foundation, either version 3 of the License, or nuclear@12: (at your option) any later version. nuclear@12: nuclear@12: This program is distributed in the hope that it will be useful, nuclear@12: but WITHOUT ANY WARRANTY; without even the implied warranty of nuclear@12: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the nuclear@12: GNU General Public License for more details. nuclear@12: nuclear@12: You should have received a copy of the GNU General Public License nuclear@12: along with this program. If not, see . nuclear@12: */ nuclear@5: #include nuclear@3: #include nuclear@5: #include nuclear@3: #include "material.h" nuclear@3: nuclear@5: static const char *find_path(const char *fname); nuclear@5: nuclear@3: Material::Material() nuclear@4: : diffuse(1, 1, 1), specular(0, 0, 0) nuclear@3: { nuclear@3: shininess = 1.0; nuclear@3: alpha = 1.0; nuclear@5: nuclear@5: for(int i=0; i 128 ? 128 : shininess); nuclear@5: nuclear@6: float emit[] = {emissive.x, emissive.y, emissive.z, 1.0}; nuclear@6: glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, emit); nuclear@5: nuclear@6: int num_tex = 0; nuclear@5: if(tex[TEX_DIFFUSE]) { nuclear@6: glActiveTexture(GL_TEXTURE0 + num_tex++); nuclear@5: nuclear@5: glMatrixMode(GL_TEXTURE); nuclear@5: glLoadIdentity(); nuclear@5: glTranslatef(tex_offset[TEX_DIFFUSE].x, tex_offset[TEX_DIFFUSE].y, 0); nuclear@5: glScalef(tex_scale[TEX_DIFFUSE].x, tex_scale[TEX_DIFFUSE].y, 1); nuclear@5: nuclear@5: glBindTexture(GL_TEXTURE_2D, tex[TEX_DIFFUSE]); nuclear@5: glEnable(GL_TEXTURE_2D); nuclear@5: nuclear@6: glDisable(GL_TEXTURE_GEN_S); nuclear@6: glDisable(GL_TEXTURE_GEN_T); nuclear@6: nuclear@6: glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); nuclear@5: } nuclear@6: nuclear@5: if(tex[TEX_ENVMAP]) { nuclear@6: glActiveTexture(GL_TEXTURE0 + num_tex++); nuclear@5: nuclear@5: glMatrixMode(GL_TEXTURE); nuclear@5: glLoadIdentity(); nuclear@5: glTranslatef(tex_offset[TEX_ENVMAP].x, tex_offset[TEX_ENVMAP].y, 0); nuclear@5: glScalef(tex_scale[TEX_ENVMAP].x, tex_scale[TEX_ENVMAP].y, 1); nuclear@5: nuclear@5: glBindTexture(GL_TEXTURE_2D, tex[TEX_ENVMAP]); nuclear@5: glEnable(GL_TEXTURE_2D); nuclear@5: nuclear@5: glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP); nuclear@5: glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP); nuclear@5: glEnable(GL_TEXTURE_GEN_S); nuclear@5: glEnable(GL_TEXTURE_GEN_T); nuclear@5: nuclear@6: glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ADD); nuclear@6: } nuclear@6: nuclear@6: nuclear@7: glMatrixMode(GL_TEXTURE); nuclear@6: for(int i=num_tex; i<4; i++) { nuclear@6: glActiveTexture(GL_TEXTURE0 + i); nuclear@6: glDisable(GL_TEXTURE_2D); nuclear@7: glLoadIdentity(); nuclear@5: } nuclear@5: nuclear@5: glActiveTexture(GL_TEXTURE0); nuclear@5: glMatrixMode(GL_MODELVIEW); nuclear@3: } nuclear@5: nuclear@5: unsigned int load_texture(const char *fname) nuclear@5: { nuclear@5: int xsz, ysz; nuclear@5: void *pixels; nuclear@5: unsigned int tex; nuclear@5: nuclear@5: const char *path = find_path(fname); nuclear@5: nuclear@5: if(!(pixels = img_load_pixels(path, &xsz, &ysz, IMG_FMT_RGBA32))) { nuclear@5: fprintf(stderr, "failed to load texture: %s\n", fname); nuclear@5: return 0; nuclear@5: } nuclear@5: nuclear@5: glGenTextures(1, &tex); nuclear@5: glBindTexture(GL_TEXTURE_2D, tex); nuclear@5: glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); nuclear@5: glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); nuclear@5: glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); nuclear@5: glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); nuclear@5: glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, xsz, ysz, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels); nuclear@5: img_free_pixels(pixels); nuclear@5: return tex; nuclear@5: } nuclear@5: nuclear@5: static const char *find_path(const char *fname) nuclear@5: { nuclear@5: const char *ptr = fname + strlen(fname) - 1; nuclear@5: nuclear@5: do { nuclear@5: while(*ptr != '/' && ptr > fname - 1) { nuclear@5: ptr--; nuclear@5: } nuclear@5: nuclear@5: FILE *fp = fopen(ptr + 1, "rb"); nuclear@5: if(fp) { nuclear@5: fclose(fp); nuclear@5: return ptr + 1; nuclear@5: } nuclear@5: ptr -= 1; nuclear@5: nuclear@5: } while(ptr >= fname); nuclear@5: nuclear@5: return fname; nuclear@5: }