dbf-halloween2015

diff src/object.cc @ 0:50683c78264e

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 01 Nov 2015 00:09:12 +0200
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/object.cc	Sun Nov 01 00:09:12 2015 +0200
     1.3 @@ -0,0 +1,226 @@
     1.4 +#include "object.h"
     1.5 +#include "opengl.h"
     1.6 +#include "sdr.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 +unsigned int Object::get_texture() const
    1.83 +{
    1.84 +	return tex;
    1.85 +}
    1.86 +
    1.87 +void Object::set_shader(unsigned int sdr)
    1.88 +{
    1.89 +	if(GLEW_ARB_vertex_shader && GLEW_ARB_fragment_shader) {
    1.90 +		this->sdr = sdr;
    1.91 +	}
    1.92 +}
    1.93 +
    1.94 +unsigned int Object::get_shader() const
    1.95 +{
    1.96 +	return sdr;
    1.97 +}
    1.98 +
    1.99 +void Object::draw() const
   1.100 +{
   1.101 +	if(!mesh) return;
   1.102 +
   1.103 +	/*
   1.104 +	if(shadow_pass && !rop.cast_shadows) {
   1.105 +		return;
   1.106 +	}
   1.107 +	*/
   1.108 +
   1.109 +	glPushAttrib(GL_ENABLE_BIT | GL_DEPTH_BUFFER_BIT);
   1.110 +	rop.setup();
   1.111 +
   1.112 +	glUseProgram(sdr);
   1.113 +
   1.114 +	if(tex) {
   1.115 +		glBindTexture(GL_TEXTURE_2D, tex);
   1.116 +		glEnable(GL_TEXTURE_2D);
   1.117 +
   1.118 +		glMatrixMode(GL_TEXTURE);
   1.119 +		glPushMatrix();
   1.120 +		glLoadTransposeMatrixf(tex_matrix[0]);
   1.121 +	} else {
   1.122 +		glDisable(GL_TEXTURE_2D);
   1.123 +	}
   1.124 +
   1.125 +	glMatrixMode(GL_MODELVIEW);
   1.126 +	glPushMatrix();
   1.127 +	glMultTransposeMatrixf(matrix[0]);
   1.128 +
   1.129 +	if(sdr) {
   1.130 +		set_uniform_matrix4_transposed(sdr, "world_matrix", (float*)matrix[0]);
   1.131 +	}
   1.132 +
   1.133 +	float dcol[] = {mtl.diffuse.x, mtl.diffuse.y, mtl.diffuse.z, mtl.alpha};
   1.134 +	glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, dcol);
   1.135 +	float scol[] = {mtl.specular.x, mtl.specular.y, mtl.specular.z, 1.0f};
   1.136 +	glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, scol);
   1.137 +	glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, mtl.shininess);
   1.138 +
   1.139 +	mesh->draw();
   1.140 +
   1.141 +	if(tex) {
   1.142 +		glDisable(GL_TEXTURE_2D);
   1.143 +
   1.144 +		glMatrixMode(GL_TEXTURE);
   1.145 +		glPopMatrix();
   1.146 +	}
   1.147 +
   1.148 +	if(sdr) {
   1.149 +		glUseProgram(0);
   1.150 +	}
   1.151 +
   1.152 +	glMatrixMode(GL_MODELVIEW);
   1.153 +	glPopMatrix();
   1.154 +
   1.155 +	glPopAttrib();
   1.156 +}
   1.157 +
   1.158 +void Object::draw_wire(const Vector4 &col) const
   1.159 +{
   1.160 +	glPushAttrib(GL_ENABLE_BIT);
   1.161 +	glDisable(GL_LIGHTING);
   1.162 +	glUseProgram(0);
   1.163 +
   1.164 +	glMatrixMode(GL_MODELVIEW);
   1.165 +	glPushMatrix();
   1.166 +	glMultTransposeMatrixf(matrix[0]);
   1.167 +
   1.168 +	glColor4f(col.x, col.y, col.z, col.w);
   1.169 +	mesh->draw_wire();
   1.170 +
   1.171 +	glPopMatrix();
   1.172 +	glPopAttrib();
   1.173 +}
   1.174 +
   1.175 +void Object::draw_vertices(const Vector4 &col) const
   1.176 +{
   1.177 +	glPushAttrib(GL_ENABLE_BIT);
   1.178 +	glDisable(GL_LIGHTING);
   1.179 +	glUseProgram(0);
   1.180 +
   1.181 +	glMatrixMode(GL_MODELVIEW);
   1.182 +	glPushMatrix();
   1.183 +	glMultTransposeMatrixf(matrix[0]);
   1.184 +
   1.185 +	glColor4f(col.x, col.y, col.z, col.w);
   1.186 +	mesh->draw_vertices();
   1.187 +
   1.188 +	glPopMatrix();
   1.189 +	glPopAttrib();
   1.190 +}
   1.191 +
   1.192 +void Object::draw_normals(float len, const Vector4 &col) const
   1.193 +{
   1.194 +	glPushAttrib(GL_ENABLE_BIT);
   1.195 +	glDisable(GL_LIGHTING);
   1.196 +
   1.197 +	glMatrixMode(GL_MODELVIEW);
   1.198 +	glPushMatrix();
   1.199 +	glMultTransposeMatrixf(matrix[0]);
   1.200 +
   1.201 +	glColor4f(col.x, col.y, col.z, col.w);
   1.202 +	mesh->set_vis_vecsize(len);
   1.203 +	mesh->draw_normals();
   1.204 +
   1.205 +	glPopMatrix();
   1.206 +	glPopAttrib();
   1.207 +}
   1.208 +
   1.209 +void Object::draw_tangents(float len, const Vector4 &col) const
   1.210 +{
   1.211 +	glPushAttrib(GL_ENABLE_BIT);
   1.212 +	glDisable(GL_LIGHTING);
   1.213 +
   1.214 +	glMatrixMode(GL_MODELVIEW);
   1.215 +	glPushMatrix();
   1.216 +	glMultTransposeMatrixf(matrix[0]);
   1.217 +
   1.218 +	glColor4f(col.x, col.y, col.z, col.w);
   1.219 +	mesh->set_vis_vecsize(len);
   1.220 +	mesh->draw_tangents();
   1.221 +
   1.222 +	glPopMatrix();
   1.223 +	glPopAttrib();
   1.224 +}
   1.225 +
   1.226 +bool Object::intersect(const Ray &ray, HitPoint *hit) const
   1.227 +{
   1.228 +	return false;	// TODO
   1.229 +}