eqemu

diff src/material.cc @ 6:977bc1cb055b

almost done
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 18 Jul 2014 02:35:06 +0300
parents 9b5bb05ae53a
children e9ab4861536d
line diff
     1.1 --- a/src/material.cc	Fri Jul 18 00:42:15 2014 +0300
     1.2 +++ b/src/material.cc	Fri Jul 18 02:35:06 2014 +0300
     1.3 @@ -20,20 +20,23 @@
     1.4  
     1.5  void Material::setup() const
     1.6  {
     1.7 +	float amb[] = {ambient.x, ambient.y, ambient.z, 1.0};
     1.8 +	glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, amb);
     1.9 +
    1.10  	float col[] = {diffuse.x, diffuse.y, diffuse.z, alpha};
    1.11 -	glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, col);
    1.12 +	glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, col);
    1.13  
    1.14  	float spec[] = {specular.x, specular.y, specular.z, 1.0};
    1.15  	glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, spec);
    1.16  
    1.17  	glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess > 128 ? 128 : shininess);
    1.18  
    1.19 -	if(sdr) {
    1.20 -		glUseProgram(sdr);
    1.21 -	}
    1.22 +	float emit[] = {emissive.x, emissive.y, emissive.z, 1.0};
    1.23 +	glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, emit);
    1.24  
    1.25 +	int num_tex = 0;
    1.26  	if(tex[TEX_DIFFUSE]) {
    1.27 -		glActiveTexture(GL_TEXTURE0);
    1.28 +		glActiveTexture(GL_TEXTURE0 + num_tex++);
    1.29  
    1.30  		glMatrixMode(GL_TEXTURE);
    1.31  		glLoadIdentity();
    1.32 @@ -43,13 +46,14 @@
    1.33  		glBindTexture(GL_TEXTURE_2D, tex[TEX_DIFFUSE]);
    1.34  		glEnable(GL_TEXTURE_2D);
    1.35  
    1.36 -		int loc = glGetUniformLocation(sdr, "tex_diffuse");
    1.37 -		if(loc != -1) {
    1.38 -			glUniform1i(loc, 0);
    1.39 -		}
    1.40 +		glDisable(GL_TEXTURE_GEN_S);
    1.41 +		glDisable(GL_TEXTURE_GEN_T);
    1.42 +
    1.43 +		glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
    1.44  	}
    1.45 +
    1.46  	if(tex[TEX_ENVMAP]) {
    1.47 -		glActiveTexture(GL_TEXTURE1);
    1.48 +		glActiveTexture(GL_TEXTURE0 + num_tex++);
    1.49  
    1.50  		glMatrixMode(GL_TEXTURE);
    1.51  		glLoadIdentity();
    1.52 @@ -64,10 +68,13 @@
    1.53  		glEnable(GL_TEXTURE_GEN_S);
    1.54  		glEnable(GL_TEXTURE_GEN_T);
    1.55  
    1.56 -		int loc = glGetUniformLocation(sdr, "tex_envmap");
    1.57 -		if(loc != -1) {
    1.58 -			glUniform1i(loc, 1);
    1.59 -		}
    1.60 +		glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ADD);
    1.61 +	}
    1.62 +
    1.63 +
    1.64 +	for(int i=num_tex; i<4; i++) {
    1.65 +		glActiveTexture(GL_TEXTURE0 + i);
    1.66 +		glDisable(GL_TEXTURE_2D);
    1.67  	}
    1.68  
    1.69  	glActiveTexture(GL_TEXTURE0);
    1.70 @@ -98,62 +105,6 @@
    1.71  	return tex;
    1.72  }
    1.73  
    1.74 -static char *read_shader(const char *fname)
    1.75 -{
    1.76 -	FILE *fp = fopen(fname, "r");
    1.77 -	if(!fp) {
    1.78 -		return 0;
    1.79 -	}
    1.80 -	fseek(fp, 0, SEEK_END);
    1.81 -	long len = ftell();
    1.82 -	rewind(fp);
    1.83 -
    1.84 -	char *buf = new char[len + 1];
    1.85 -	fread(buf, len, fp);
    1.86 -	buf[len] = 0;
    1.87 -	fclose(fp);
    1.88 -
    1.89 -	return buf;
    1.90 -}
    1.91 -
    1.92 -unsigned int load_shader_program(const char *vname, const char *pname)
    1.93 -{
    1.94 -	unsigned int vsdr = 0, psdr = 0;
    1.95 -
    1.96 -	if(vname) {
    1.97 -		char *src = read_shader(find_path(vname));
    1.98 -		if(!src) {
    1.99 -			fprintf(stderr, "failed to load vertex shader: %s\n", vname);
   1.100 -			return 0;
   1.101 -		}
   1.102 -		vsdr = glCreateShader(GL_VERTEX_SHADER);
   1.103 -		glShaderSource(vsdr, 1, &src, 0);
   1.104 -		delete [] src;
   1.105 -
   1.106 -		int res;
   1.107 -		glGetShaderiv(vsdr, GL_INFO_LOG_LENGTH, &res);
   1.108 -		if(res > 0) {URE_2D, 0, GL_RGBA, xsz, ysz, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
   1.109 -	img_free_pixels(pixels);
   1.110 -	return tex;
   1.111 -}
   1.112 -
   1.113 -static char *read_shader(const char *fname)
   1.114 -{
   1.115 -	FILE *fp = fopen(find_path(vname), "r");
   1.116 -	if(!fp) {
   1.117 -		fprintf(stderr, "failed to load shader: %s\n", vname);
   1.118 -		return 0;
   1.119 -			char *info = new char[res];
   1.120 -			glGetShaderInfoLog(vsdr, res, &res, info);
   1.121 -			printf("shader compiler log: %s\n", info);
   1.122 -		}
   1.123 -		glGetShaderiv(vsdr, GL_COMPILE_STATUS, &res);
   1.124 -		if(!res) {
   1.125 -			return 0;
   1.126 -		}
   1.127 -	}
   1.128 -}
   1.129 -
   1.130  static const char *find_path(const char *fname)
   1.131  {
   1.132  	const char *ptr = fname + strlen(fname) - 1;