ld33_umonster

diff src/object.cc @ 0:4a6683050e29

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 22 Aug 2015 07:15:00 +0300
parents
children 35349df5392d
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/object.cc	Sat Aug 22 07:15:00 2015 +0300
     1.3 @@ -0,0 +1,225 @@
     1.4 +#include "object.h"
     1.5 +#include "opengl.h"
     1.6 +#include "shadow.h"
     1.7 +
     1.8 +Material::Material()
     1.9 +	: diffuse(1, 1, 1), specular(0, 0, 0)
    1.10 +{
    1.11 +	shininess = 60.0;
    1.12 +	alpha = 1.0;
    1.13 +}
    1.14 +
    1.15 +RenderOps::RenderOps()
    1.16 +{
    1.17 +	zwrite = true;
    1.18 +	cast_shadows = true;
    1.19 +	transparent = false;
    1.20 +}
    1.21 +
    1.22 +void RenderOps::setup() const
    1.23 +{
    1.24 +	if(!zwrite) {
    1.25 +		glDepthMask(0);
    1.26 +	}
    1.27 +	if(transparent) {
    1.28 +		glEnable(GL_BLEND);
    1.29 +		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    1.30 +	} else {
    1.31 +		glDisable(GL_BLEND);
    1.32 +	}
    1.33 +}
    1.34 +
    1.35 +Object::Object()
    1.36 +{
    1.37 +	mesh = 0;
    1.38 +	tex = 0;
    1.39 +	sdr = 0;
    1.40 +}
    1.41 +
    1.42 +Object::~Object()
    1.43 +{
    1.44 +	delete mesh;
    1.45 +}
    1.46 +
    1.47 +Matrix4x4 &Object::xform()
    1.48 +{
    1.49 +	return matrix;
    1.50 +}
    1.51 +
    1.52 +const Matrix4x4 &Object::xform() const
    1.53 +{
    1.54 +	return matrix;
    1.55 +}
    1.56 +
    1.57 +Matrix4x4 &Object::tex_xform()
    1.58 +{
    1.59 +	return tex_matrix;
    1.60 +}
    1.61 +
    1.62 +const Matrix4x4 &Object::tex_xform() const
    1.63 +{
    1.64 +	return tex_matrix;
    1.65 +}
    1.66 +
    1.67 +void Object::set_mesh(Mesh *m)
    1.68 +{
    1.69 +	this->mesh = m;
    1.70 +}
    1.71 +
    1.72 +Mesh *Object::get_mesh() const
    1.73 +{
    1.74 +	return mesh;
    1.75 +}
    1.76 +
    1.77 +void Object::set_texture(unsigned int tex)
    1.78 +{
    1.79 +	this->tex = tex;
    1.80 +}
    1.81 +
    1.82 +void Object::set_shader(unsigned int sdr)
    1.83 +{
    1.84 +	if(GLEW_ARB_vertex_shader && GLEW_ARB_fragment_shader) {
    1.85 +		this->sdr = sdr;
    1.86 +	}
    1.87 +}
    1.88 +
    1.89 +unsigned int Object::get_shader() const
    1.90 +{
    1.91 +	return sdr;
    1.92 +}
    1.93 +
    1.94 +void Object::draw() const
    1.95 +{
    1.96 +	if(!mesh) return;
    1.97 +
    1.98 +	if(shadow_pass && !rop.cast_shadows) {
    1.99 +		return;
   1.100 +	}
   1.101 +
   1.102 +	glPushAttrib(GL_ENABLE_BIT | GL_DEPTH_BUFFER_BIT);
   1.103 +	rop.setup();
   1.104 +
   1.105 +	if(glcaps.shaders) {
   1.106 +		if(sdr) {
   1.107 +			if(!shadow_pass) {
   1.108 +				glUseProgram(sdr);
   1.109 +			}
   1.110 +		} else {
   1.111 +			glUseProgram(0);
   1.112 +		}
   1.113 +	}
   1.114 +
   1.115 +	if(tex) {
   1.116 +		glBindTexture(GL_TEXTURE_2D, tex);
   1.117 +		glEnable(GL_TEXTURE_2D);
   1.118 +
   1.119 +		glMatrixMode(GL_TEXTURE);
   1.120 +		glPushMatrix();
   1.121 +		glLoadTransposeMatrixf(tex_matrix[0]);
   1.122 +	} else {
   1.123 +		glDisable(GL_TEXTURE_2D);
   1.124 +	}
   1.125 +
   1.126 +	glMatrixMode(GL_MODELVIEW);
   1.127 +	glPushMatrix();
   1.128 +	glMultTransposeMatrixf(matrix[0]);
   1.129 +
   1.130 +	float dcol[] = {mtl.diffuse.x, mtl.diffuse.y, mtl.diffuse.z, mtl.alpha};
   1.131 +	glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, dcol);
   1.132 +	float scol[] = {mtl.specular.x, mtl.specular.y, mtl.specular.z, 1.0f};
   1.133 +	glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, scol);
   1.134 +	glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, mtl.shininess);
   1.135 +
   1.136 +	mesh->draw();
   1.137 +
   1.138 +	if(tex) {
   1.139 +		glDisable(GL_TEXTURE_2D);
   1.140 +
   1.141 +		glMatrixMode(GL_TEXTURE);
   1.142 +		glPopMatrix();
   1.143 +	}
   1.144 +
   1.145 +	if(sdr) {
   1.146 +		glUseProgram(0);
   1.147 +	}
   1.148 +
   1.149 +	glMatrixMode(GL_MODELVIEW);
   1.150 +	glPopMatrix();
   1.151 +
   1.152 +	glPopAttrib();
   1.153 +}
   1.154 +
   1.155 +void Object::draw_wire(const Vector4 &col) const
   1.156 +{
   1.157 +	if(shadow_pass) return;
   1.158 +
   1.159 +	glPushAttrib(GL_ENABLE_BIT);
   1.160 +	glDisable(GL_LIGHTING);
   1.161 +	glUseProgram(0);
   1.162 +
   1.163 +	glMatrixMode(GL_MODELVIEW);
   1.164 +	glPushMatrix();
   1.165 +	glMultTransposeMatrixf(matrix[0]);
   1.166 +
   1.167 +	glColor4f(col.x, col.y, col.z, col.w);
   1.168 +	mesh->draw_wire();
   1.169 +
   1.170 +	glPopMatrix();
   1.171 +	glPopAttrib();
   1.172 +}
   1.173 +
   1.174 +void Object::draw_vertices(const Vector4 &col) const
   1.175 +{
   1.176 +	glPushAttrib(GL_ENABLE_BIT);
   1.177 +	glDisable(GL_LIGHTING);
   1.178 +	glUseProgram(0);
   1.179 +
   1.180 +	glMatrixMode(GL_MODELVIEW);
   1.181 +	glPushMatrix();
   1.182 +	glMultTransposeMatrixf(matrix[0]);
   1.183 +
   1.184 +	glColor4f(col.x, col.y, col.z, col.w);
   1.185 +	mesh->draw_vertices();
   1.186 +
   1.187 +	glPopMatrix();
   1.188 +	glPopAttrib();
   1.189 +}
   1.190 +
   1.191 +void Object::draw_normals(float len, const Vector4 &col) const
   1.192 +{
   1.193 +	glPushAttrib(GL_ENABLE_BIT);
   1.194 +	glDisable(GL_LIGHTING);
   1.195 +
   1.196 +	glMatrixMode(GL_MODELVIEW);
   1.197 +	glPushMatrix();
   1.198 +	glMultTransposeMatrixf(matrix[0]);
   1.199 +
   1.200 +	glColor4f(col.x, col.y, col.z, col.w);
   1.201 +	mesh->set_vis_vecsize(len);
   1.202 +	mesh->draw_normals();
   1.203 +
   1.204 +	glPopMatrix();
   1.205 +	glPopAttrib();
   1.206 +}
   1.207 +
   1.208 +void Object::draw_tangents(float len, const Vector4 &col) const
   1.209 +{
   1.210 +	glPushAttrib(GL_ENABLE_BIT);
   1.211 +	glDisable(GL_LIGHTING);
   1.212 +
   1.213 +	glMatrixMode(GL_MODELVIEW);
   1.214 +	glPushMatrix();
   1.215 +	glMultTransposeMatrixf(matrix[0]);
   1.216 +
   1.217 +	glColor4f(col.x, col.y, col.z, col.w);
   1.218 +	mesh->set_vis_vecsize(len);
   1.219 +	mesh->draw_tangents();
   1.220 +
   1.221 +	glPopMatrix();
   1.222 +	glPopAttrib();
   1.223 +}
   1.224 +
   1.225 +bool Object::intersect(const Ray &ray, HitPoint *hit) const
   1.226 +{
   1.227 +	return false;	// TODO
   1.228 +}