# HG changeset patch # User John Tsiombikas # Date 1435281726 -10800 # Node ID a0d30f6f20d491d860b930eb94844dd5d9842fe9 # Parent e48b40a3c82a4ed44ebdf6b78441816e0dce864c - texture coordinate generation in class Mesh - wood texture diff -r e48b40a3c82a -r a0d30f6f20d4 Makefile --- a/Makefile Thu Jun 25 20:43:34 2015 +0300 +++ b/Makefile Fri Jun 26 04:22:06 2015 +0300 @@ -2,6 +2,7 @@ src = $(wildcard src/*.cc) obj = $(src:.cc=.o) +dep = $(obj:.o=.d) bin = tavli @@ -17,10 +18,19 @@ $(bin): $(obj) $(CXX) -o $@ $(obj) $(LDFLAGS) +-include $(dep) + +%.d: %.cc + @$(CPP) $(CXXFLAGS) $< -MM -MT $(@:.d=.o) >$@ + .PHONY: clean clean: rm -f $(obj) $(bin) +.PHONY: cleandep +cleandep: + rm -f $(dep) + .PHONY: install install: $(bin) mkdir -p $(PREFIX)/bin diff -r e48b40a3c82a -r a0d30f6f20d4 src/board.cc --- a/src/board.cc Thu Jun 25 20:43:34 2015 +0300 +++ b/src/board.cc Fri Jun 26 04:22:06 2015 +0300 @@ -1,7 +1,7 @@ #include "opengl.h" #include "board.h" #include "meshgen.h" - +#include "pnoise.h" Board::Board() { @@ -109,9 +109,15 @@ sides->append(tmp); tmp.clear(); + // generate texture coordinates + sides->texcoord_gen_box(); + Object *osides = new Object; osides->set_mesh(sides); osides->xform() = obottom->xform(); + osides->set_texture(img_wood.texture()); + osides->tex_xform().set_scaling(Vector3(2, 2, 2)); + osides->tex_xform().rotate(-Vector3(1, 0, 0.5), M_PI / 4.0); obj.push_back(osides); } @@ -137,6 +143,17 @@ ohinges->set_mesh(hinges); obj.push_back(ohinges); + // debug object + /*Mesh *dbgmesh = new Mesh; + gen_box(dbgmesh, 0.5, 0.5, 0.5); + xform.set_translation(Vector3(0, 0.4, 0)); + xform.set_scaling(Vector3(4, 1, 4)); + dbgmesh->apply_xform(xform); + Object *dbgobj = new Object; + dbgobj->set_mesh(dbgmesh); + dbgobj->set_texture(img_wood.texture()); + dbgobj->tex_xform().set_scaling(Vector3(3, 3, 3)); + obj.push_back(dbgobj);*/ return true; } @@ -157,6 +174,20 @@ return val < 0.0 ? 0.0 : (val > 1.0 ? 1.0 : val); } +static float wood_tile(float x, float y) +{ + float u = x; + float v = y; + x *= 10.0; + y *= 10.0; + + float val = x + pnoise2(u * 6.0, v, 6, 1) * 3.0 + + pturbulence2(u * 4, v * 2, 4, 2, 2) * 1.5 + pturbulence2(u * 8, v * 8, 8, 8, 2) * 0.5; + + val = fmod(val, 1.0); + return val < 0.0 ? 0.0 : (val > 1.0 ? 1.0 : val); +} + static bool spike(float x, float y) { x = fmod(x * 5.0, 1.0); @@ -185,20 +216,18 @@ bool Board::generate_textures() { + // ---- board field texture ---- static const Vector3 wcol1 = Vector3(0.6, 0.4, 0.2); - static const Vector3 wcol2 = Vector3(0.53, 0.32, 0.1);//Vector3(0.38, 0.25, 0.08); + static const Vector3 wcol2 = Vector3(0.53, 0.32, 0.1); + static const Vector3 wcol3 = Vector3(0.38, 0.25, 0.08); - const int xsz = 512; - const int ysz = 1024; + img_field.create(512, 1024); + unsigned char *pptr = img_field.pixels; + for(int i=0; i 255 ? 255 : r; + pptr[1] = g > 255 ? 255 : g; + pptr[2] = b > 255 ? 255 : b; + pptr += 3; + } + } + img_wood.texture(); return true; } diff -r e48b40a3c82a -r a0d30f6f20d4 src/game.cc --- a/src/game.cc Thu Jun 25 20:43:34 2015 +0300 +++ b/src/game.cc Fri Jun 26 04:22:06 2015 +0300 @@ -52,8 +52,10 @@ glRotatef(cam_phi, 1, 0, 0); glRotatef(cam_theta, 0, 1, 0); + float ldir[] = {-1, 2, 1, 0}; + glLightfv(GL_LIGHT0, GL_POSITION, ldir); + draw_backdrop(); - board.draw(); } diff -r e48b40a3c82a -r a0d30f6f20d4 src/mesh.cc --- a/src/mesh.cc Thu Jun 25 20:43:34 2015 +0300 +++ b/src/mesh.cc Fri Jun 26 04:22:06 2015 +0300 @@ -931,6 +931,72 @@ } +// texture coordinate manipulation +void Mesh::texcoord_apply_xform(const Matrix4x4 &xform) +{ + if(!has_attrib(MESH_ATTR_TEXCOORD)) { + return; + } + + for(unsigned int i=0; i abs_ny && abs_nx > abs_nz ? 0 : (abs_ny > abs_nz ? 1 : 2); + + float uv[2], *uvptr = uv; + for(int j=0; j<3; j++) { + if(j == dom) continue; // skip dominant axis + + *uvptr++ = pos[j]; + } + set_attrib(MESH_ATTR_TEXCOORD, i, Vector4(uv[0], uv[1], 0, 1)); + } +} + // ------ private member functions ------ void Mesh::calc_aabb() diff -r e48b40a3c82a -r a0d30f6f20d4 src/mesh.h --- a/src/mesh.h Thu Jun 25 20:43:34 2015 +0300 +++ b/src/mesh.h Fri Jun 26 04:22:06 2015 +0300 @@ -220,6 +220,11 @@ * If you intend to use it in a speed-critical part of the code, you'll *have* to optimize it! */ bool intersect(const Ray &ray, HitPoint *hit = 0) const; + + // texture coordinate manipulation + void texcoord_apply_xform(const Matrix4x4 &xform); + void texcoord_gen_plane(const Vector3 &norm, const Vector3 &tang); + void texcoord_gen_box(); }; #endif // MESH_H_ diff -r e48b40a3c82a -r a0d30f6f20d4 src/object.cc --- a/src/object.cc Thu Jun 25 20:43:34 2015 +0300 +++ b/src/object.cc Fri Jun 26 04:22:06 2015 +0300 @@ -5,6 +5,11 @@ { mesh = 0; tex = 0; + + mtl.diffuse = Vector3(1, 1, 1); + mtl.specular = Vector3(0, 0, 0); + mtl.shininess = 60.0; + mtl.alpha = 1.0; } Object::~Object() @@ -22,6 +27,16 @@ return matrix; } +Matrix4x4 &Object::tex_xform() +{ + return tex_matrix; +} + +const Matrix4x4 &Object::tex_xform() const +{ + return tex_matrix; +} + void Object::set_mesh(Mesh *m) { this->mesh = m; @@ -44,6 +59,10 @@ if(tex) { glBindTexture(GL_TEXTURE_2D, tex); glEnable(GL_TEXTURE_2D); + + glMatrixMode(GL_TEXTURE); + glPushMatrix(); + glLoadTransposeMatrixf(tex_matrix[0]); } else { glDisable(GL_TEXTURE_2D); } @@ -52,13 +71,23 @@ glPushMatrix(); glMultTransposeMatrixf(matrix[0]); + float dcol[] = {mtl.diffuse.x, mtl.diffuse.y, mtl.diffuse.z, mtl.alpha}; + glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, dcol); + float scol[] = {mtl.specular.x, mtl.specular.y, mtl.specular.z, 1.0f}; + glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, scol); + glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, mtl.shininess); + mesh->draw(); - glPopMatrix(); - if(tex) { glDisable(GL_TEXTURE_2D); + + glMatrixMode(GL_TEXTURE); + glPopMatrix(); } + + glMatrixMode(GL_MODELVIEW); + glPopMatrix(); } bool Object::intersect(const Ray &ray, HitPoint *hit) const diff -r e48b40a3c82a -r a0d30f6f20d4 src/object.h --- a/src/object.h Thu Jun 25 20:43:34 2015 +0300 +++ b/src/object.h Fri Jun 26 04:22:06 2015 +0300 @@ -3,20 +3,34 @@ #include "mesh.h" #include "geom.h" +#include "vmath/vmath.h" + +struct Material { + Vector3 diffuse; + Vector3 specular; + float shininess; + float alpha; +}; class Object { private: Mesh *mesh; Matrix4x4 matrix; unsigned int tex; + Matrix4x4 tex_matrix; public: + Material mtl; + Object(); ~Object(); Matrix4x4 &xform(); const Matrix4x4 &xform() const; + Matrix4x4 &tex_xform(); + const Matrix4x4 &tex_xform() const; + void set_mesh(Mesh *m); Mesh *get_mesh() const; diff -r e48b40a3c82a -r a0d30f6f20d4 src/pnoise.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/pnoise.cc Fri Jun 26 04:22:06 2015 +0300 @@ -0,0 +1,149 @@ +#include +#include "pnoise.h" +#include "vmath/vmath.h" + +#define B 0x100 +#define BM 0xff +#define N 0x1000 +#define NP 12 +#define NM 0xfff + +#define s_curve(t) ((t) * (t) * (3.0f - 2.0f * (t))) +#define setup(elem, b0, b1, r0, r1) \ + do { \ + float t = elem + N; \ + b0 = ((int)t) & BM; \ + b1 = (b0 + 1) & BM; \ + r0 = t - (int)t; \ + r1 = r0 - 1.0f; \ + } while(0) + +#define setup_p(elem, b0, b1, r0, r1, p) \ + do { \ + float t = elem + N; \ + b0 = (((int)t) & BM) % p; \ + b1 = ((b0 + 1) & BM) % p; \ + r0 = t - (int)t; \ + r1 = r0 - 1.0f; \ + } while(0) + +static int perm[B + B + 2]; +static vec2_t grad2[B + B + 2]; +static bool tables_valid; + +static void init_noise() +{ + for(int i=0; i