eqemu

changeset 5:9b5bb05ae53a

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 18 Jul 2014 00:42:15 +0300
parents 3d3656360a82
children 977bc1cb055b
files Makefile src/material.cc src/material.h src/objfile.cc
diffstat 4 files changed, 178 insertions(+), 4 deletions(-) [+]
line diff
     1.1 --- a/Makefile	Thu Jul 17 08:51:17 2014 +0300
     1.2 +++ b/Makefile	Fri Jul 18 00:42:15 2014 +0300
     1.3 @@ -5,7 +5,7 @@
     1.4  
     1.5  CFLAGS = -pedantic -Wall -g
     1.6  CXXFLAGS = $(CFLAGS)
     1.7 -LDFLAGS = -lGL -lGLU -lGLEW -lX11 -lm -lpthread
     1.8 +LDFLAGS = -lGL -lGLU -lGLEW -lX11 -lm -lpthread -limago
     1.9  
    1.10  $(bin): $(obj)
    1.11  	$(CXX) -o $@ $(obj) $(LDFLAGS)
     2.1 --- a/src/material.cc	Thu Jul 17 08:51:17 2014 +0300
     2.2 +++ b/src/material.cc	Fri Jul 18 00:42:15 2014 +0300
     2.3 @@ -1,11 +1,21 @@
     2.4 +#include <string.h>
     2.5  #include <GL/glew.h>
     2.6 +#include <imago2.h>
     2.7  #include "material.h"
     2.8  
     2.9 +static const char *find_path(const char *fname);
    2.10 +
    2.11  Material::Material()
    2.12  	: diffuse(1, 1, 1), specular(0, 0, 0)
    2.13  {
    2.14  	shininess = 1.0;
    2.15  	alpha = 1.0;
    2.16 +
    2.17 +	for(int i=0; i<NUM_TEXTURES; i++) {
    2.18 +		tex[i] = 0;
    2.19 +		tex_scale[i].x = tex_scale[i].y = 1.0f;
    2.20 +	}
    2.21 +	sdr = 0;
    2.22  }
    2.23  
    2.24  void Material::setup() const
    2.25 @@ -17,4 +27,150 @@
    2.26  	glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, spec);
    2.27  
    2.28  	glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess > 128 ? 128 : shininess);
    2.29 +
    2.30 +	if(sdr) {
    2.31 +		glUseProgram(sdr);
    2.32 +	}
    2.33 +
    2.34 +	if(tex[TEX_DIFFUSE]) {
    2.35 +		glActiveTexture(GL_TEXTURE0);
    2.36 +
    2.37 +		glMatrixMode(GL_TEXTURE);
    2.38 +		glLoadIdentity();
    2.39 +		glTranslatef(tex_offset[TEX_DIFFUSE].x, tex_offset[TEX_DIFFUSE].y, 0);
    2.40 +		glScalef(tex_scale[TEX_DIFFUSE].x, tex_scale[TEX_DIFFUSE].y, 1);
    2.41 +
    2.42 +		glBindTexture(GL_TEXTURE_2D, tex[TEX_DIFFUSE]);
    2.43 +		glEnable(GL_TEXTURE_2D);
    2.44 +
    2.45 +		int loc = glGetUniformLocation(sdr, "tex_diffuse");
    2.46 +		if(loc != -1) {
    2.47 +			glUniform1i(loc, 0);
    2.48 +		}
    2.49 +	}
    2.50 +	if(tex[TEX_ENVMAP]) {
    2.51 +		glActiveTexture(GL_TEXTURE1);
    2.52 +
    2.53 +		glMatrixMode(GL_TEXTURE);
    2.54 +		glLoadIdentity();
    2.55 +		glTranslatef(tex_offset[TEX_ENVMAP].x, tex_offset[TEX_ENVMAP].y, 0);
    2.56 +		glScalef(tex_scale[TEX_ENVMAP].x, tex_scale[TEX_ENVMAP].y, 1);
    2.57 +
    2.58 +		glBindTexture(GL_TEXTURE_2D, tex[TEX_ENVMAP]);
    2.59 +		glEnable(GL_TEXTURE_2D);
    2.60 +
    2.61 +		glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
    2.62 +		glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
    2.63 +		glEnable(GL_TEXTURE_GEN_S);
    2.64 +		glEnable(GL_TEXTURE_GEN_T);
    2.65 +
    2.66 +		int loc = glGetUniformLocation(sdr, "tex_envmap");
    2.67 +		if(loc != -1) {
    2.68 +			glUniform1i(loc, 1);
    2.69 +		}
    2.70 +	}
    2.71 +
    2.72 +	glActiveTexture(GL_TEXTURE0);
    2.73 +	glMatrixMode(GL_MODELVIEW);
    2.74  }
    2.75 +
    2.76 +unsigned int load_texture(const char *fname)
    2.77 +{
    2.78 +	int xsz, ysz;
    2.79 +	void *pixels;
    2.80 +	unsigned int tex;
    2.81 +
    2.82 +	const char *path = find_path(fname);
    2.83 +
    2.84 +	if(!(pixels = img_load_pixels(path, &xsz, &ysz, IMG_FMT_RGBA32))) {
    2.85 +		fprintf(stderr, "failed to load texture: %s\n", fname);
    2.86 +		return 0;
    2.87 +	}
    2.88 +
    2.89 +	glGenTextures(1, &tex);
    2.90 +	glBindTexture(GL_TEXTURE_2D, tex);
    2.91 +	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    2.92 +	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    2.93 +	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    2.94 +	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    2.95 +	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, xsz, ysz, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
    2.96 +	img_free_pixels(pixels);
    2.97 +	return tex;
    2.98 +}
    2.99 +
   2.100 +static char *read_shader(const char *fname)
   2.101 +{
   2.102 +	FILE *fp = fopen(fname, "r");
   2.103 +	if(!fp) {
   2.104 +		return 0;
   2.105 +	}
   2.106 +	fseek(fp, 0, SEEK_END);
   2.107 +	long len = ftell();
   2.108 +	rewind(fp);
   2.109 +
   2.110 +	char *buf = new char[len + 1];
   2.111 +	fread(buf, len, fp);
   2.112 +	buf[len] = 0;
   2.113 +	fclose(fp);
   2.114 +
   2.115 +	return buf;
   2.116 +}
   2.117 +
   2.118 +unsigned int load_shader_program(const char *vname, const char *pname)
   2.119 +{
   2.120 +	unsigned int vsdr = 0, psdr = 0;
   2.121 +
   2.122 +	if(vname) {
   2.123 +		char *src = read_shader(find_path(vname));
   2.124 +		if(!src) {
   2.125 +			fprintf(stderr, "failed to load vertex shader: %s\n", vname);
   2.126 +			return 0;
   2.127 +		}
   2.128 +		vsdr = glCreateShader(GL_VERTEX_SHADER);
   2.129 +		glShaderSource(vsdr, 1, &src, 0);
   2.130 +		delete [] src;
   2.131 +
   2.132 +		int res;
   2.133 +		glGetShaderiv(vsdr, GL_INFO_LOG_LENGTH, &res);
   2.134 +		if(res > 0) {URE_2D, 0, GL_RGBA, xsz, ysz, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
   2.135 +	img_free_pixels(pixels);
   2.136 +	return tex;
   2.137 +}
   2.138 +
   2.139 +static char *read_shader(const char *fname)
   2.140 +{
   2.141 +	FILE *fp = fopen(find_path(vname), "r");
   2.142 +	if(!fp) {
   2.143 +		fprintf(stderr, "failed to load shader: %s\n", vname);
   2.144 +		return 0;
   2.145 +			char *info = new char[res];
   2.146 +			glGetShaderInfoLog(vsdr, res, &res, info);
   2.147 +			printf("shader compiler log: %s\n", info);
   2.148 +		}
   2.149 +		glGetShaderiv(vsdr, GL_COMPILE_STATUS, &res);
   2.150 +		if(!res) {
   2.151 +			return 0;
   2.152 +		}
   2.153 +	}
   2.154 +}
   2.155 +
   2.156 +static const char *find_path(const char *fname)
   2.157 +{
   2.158 +	const char *ptr = fname + strlen(fname) - 1;
   2.159 +
   2.160 +	do {
   2.161 +		while(*ptr != '/' && ptr > fname - 1) {
   2.162 +			ptr--;
   2.163 +		}
   2.164 +
   2.165 +		FILE *fp = fopen(ptr + 1, "rb");
   2.166 +		if(fp) {
   2.167 +			fclose(fp);
   2.168 +			return ptr + 1;
   2.169 +		}
   2.170 +		ptr -= 1;
   2.171 +
   2.172 +	} while(ptr >= fname);
   2.173 +
   2.174 +	return fname;
   2.175 +}
     3.1 --- a/src/material.h	Thu Jul 17 08:51:17 2014 +0300
     3.2 +++ b/src/material.h	Fri Jul 18 00:42:15 2014 +0300
     3.3 @@ -3,6 +3,13 @@
     3.4  
     3.5  #include "vmath.h"
     3.6  
     3.7 +enum {
     3.8 +	TEX_DIFFUSE,
     3.9 +	TEX_ENVMAP,
    3.10 +
    3.11 +	NUM_TEXTURES
    3.12 +};
    3.13 +
    3.14  class Material {
    3.15  public:
    3.16  	Vector3 ambient;
    3.17 @@ -11,9 +18,17 @@
    3.18  	float shininess;
    3.19  	float alpha;
    3.20  
    3.21 +	unsigned int tex[NUM_TEXTURES];
    3.22 +	Vector2 tex_scale[NUM_TEXTURES], tex_offset[NUM_TEXTURES];
    3.23 +
    3.24 +	unsigned int sdr;
    3.25 +
    3.26  	Material();
    3.27  
    3.28  	void setup() const;
    3.29  };
    3.30  
    3.31 +unsigned int load_texture(const char *fname);
    3.32 +unsigned int load_shader_program(const char *vname, const char *pname);
    3.33 +
    3.34  #endif	// MATERIAL_H_
     4.1 --- a/src/objfile.cc	Thu Jul 17 08:51:17 2014 +0300
     4.2 +++ b/src/objfile.cc	Fri Jul 18 00:42:15 2014 +0300
     4.3 @@ -209,9 +209,12 @@
     4.4  					mat.shininess = vmtl[i].shininess;
     4.5  					mat.alpha = vmtl[i].alpha;
     4.6  
     4.7 -					/*if(vmtl[i].tex_dif.length()) {
     4.8 -						mat.set_texture(get_texture(vmtl[i].tex_dif.c_str()));
     4.9 -					}*/
    4.10 +					if(vmtl[i].tex_dif.length()) {
    4.11 +						mat.tex[TEX_DIFFUSE] = load_texture(vmtl[i].tex_dif.c_str());
    4.12 +					}
    4.13 +					if(vmtl[i].tex_refl.length()) {
    4.14 +						mat.tex[TEX_ENVMAP] = load_texture(vmtl[i].tex_refl.c_str());
    4.15 +					}
    4.16  
    4.17  					matlib[vmtl[i].name] = mat;
    4.18  				}