dbf_amiga

diff src/object.cc @ 0:87dfe0e10235

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