From 904ef44727b6b59b6e20fe64ebfdba0749534a95 Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Sat, 15 Jan 2011 18:08:45 +0000 Subject: [PATCH] Fix texture rendering --- include/IMesh.hpp | 13 +++++-- src/Map.cpp | 34 +++++++++++++--- src/Mesh.cpp | 98 +++++++++++++++++++++++++---------------------- 3 files changed, 90 insertions(+), 55 deletions(-) diff --git a/include/IMesh.hpp b/include/IMesh.hpp index 80bbf1f..61f7f08 100644 --- a/include/IMesh.hpp +++ b/include/IMesh.hpp @@ -47,10 +47,15 @@ struct IMeshBuffer { virtual size_t vertex_count() const = 0; virtual void add(const Vertex& vertex, const Normal& normal) = 0; - virtual void add(const Vertex& vertex, const Normal& normal, - const TexCoord& a_tex_coord) = 0; - virtual void add(const Vertex& vertex, const Normal& normal, - const Colour& colour) = 0; + + virtual void add(const Vertex& vertex, + const Normal& normal, + const TexCoord& a_tex_coord, + const Colour& colour = make_colour(1.0f, 1.0f, 1.0f)) = 0; + + virtual void add(const Vertex& vertex, + const Normal& normal, + const Colour& colour) = 0; // Convenience functions virtual void add_quad(Vertex a, Vertex b, Vertex c, Vertex d, diff --git a/src/Map.cpp b/src/Map.cpp index e1d8113..f16180c 100644 --- a/src/Map.cpp +++ b/src/Map.cpp @@ -644,6 +644,10 @@ void Map::build_mesh(int id, Point bot_left, Point top_right) }; IMeshBufferPtr buf = make_mesh_buffer(); + + Material m; + m.texture = load_texture("test.png"); + buf->bind_material(m); // Incrementing the frame counter here ensures that any track which spans // multiple sectors will be merged with each applicable mesh even when @@ -659,6 +663,18 @@ void Map::build_mesh(int id, Point bot_left, Point top_right) indexes[1], indexes[2], indexes[3], indexes[3], indexes[0], indexes[1] }; + + const IMeshBuffer::TexCoord tex_coords[4] = { + make_point(0.0f, 1.0f), + make_point(1.0f, 1.0f), + make_point(1.0f, 0.0f), + make_point(0.0f, 0.0f) + }; + + const IMeshBuffer::TexCoord tex_order[6] = { + tex_coords[1], tex_coords[2], tex_coords[3], + tex_coords[3], tex_coords[0], tex_coords[1] + }; for (int i = 0; i < 6; i++) { const HeightMap& v = height_map[order[i]]; @@ -670,13 +686,12 @@ void Map::build_mesh(int id, Point bot_left, Point top_right) hcol = colour_map[j++]; } while (get<0>(hcol) > h); - buf->add(make_vector(v.pos.x, v.pos.y, v.pos.z), - make_vector(v.normal.x, v.normal.y, v.normal.z), - get<1>(hcol)); + buf->add(v.pos, v.normal, tex_order[i], get<1>(hcol)); } } } +#if 0 // Merge any static scenery for (int x = top_right.x-1; x >= bot_left.x; x--) { for (int y = bot_left.y; y < top_right.y; y++) { @@ -795,12 +810,21 @@ void Map::build_mesh(int id, Point bot_left, Point top_right) } below_sea_levelOut: - + size_t min_size = id + 1; if (sea_sectors.size() < min_size) sea_sectors.resize(min_size); sea_sectors.at(id) = below_sea_level; - + +#else + + terrain_meshes[id] = make_mesh(buf); + + size_t min_size = id + 1; + if (sea_sectors.size() < min_size) + sea_sectors.resize(min_size); + sea_sectors.at(id) = false; +#endif // Make sure we don't rebuild this mesh if any of the tiles are dirty have_mesh(id, bot_left, top_right); } diff --git a/src/Mesh.cpp b/src/Mesh.cpp index b7495aa..cc72c6d 100644 --- a/src/Mesh.cpp +++ b/src/Mesh.cpp @@ -44,16 +44,18 @@ struct MeshBuffer : IMeshBuffer { size_t vertex_count() const { return vertices.size(); } void add(const Vertex& vertex, const Normal& normal); + void add(const Vertex& vertex, + const Normal& normal, + const TexCoord& a_tex_coord, + const Colour& colour); void add(const Vertex& vertex, const Normal& normal, - const TexCoord& a_tex_coord); - void add(const Vertex& vertex, const Normal& normal, - const Colour& colour); + const Colour& colour); void add_quad(Vertex a, Vertex b, Vertex c, Vertex d, - Colour colour); + Colour colour); void add_quad(Vertex a, Vertex b, Vertex c, Vertex d, - Normal na, Normal nb, Normal nc, Normal nd, - Colour colour); + Normal na, Normal nb, Normal nc, Normal nd, + Colour colour); void bind_material(const Material& a_material); void merge(IMeshBufferPtr other, Vector off, float y_angle); @@ -141,10 +143,6 @@ void MeshBuffer::print_stats() const void MeshBuffer::add(const Vertex& vertex, const Normal& normal) { - if (has_texture) - throw runtime_error("MeshBuffer::add called without texture coordinate " - "on a mesh which has a texture"); - if (!has_material) throw runtime_error("MeshBuffer::add called without colour on a mesh " " without a material"); @@ -180,6 +178,7 @@ void MeshBuffer::add(const Vertex& vertex, const Normal& normal) void MeshBuffer::add(const Vertex& vertex, const Normal& normal, const Colour& colour) { +#if 0 if (has_texture) throw runtime_error("MeshBuffer::add called without texture coordinate " "on a mesh which has a texture"); @@ -187,6 +186,7 @@ void MeshBuffer::add(const Vertex& vertex, const Normal& normal, if (has_material) throw runtime_error("MeshBuffer::add called with a colour on a mesh " " with a material"); +#endif // See if this vertex has already been added for (vector::iterator it = indices.begin(); @@ -217,8 +217,10 @@ void MeshBuffer::add(const Vertex& vertex, const Normal& normal, indices.push_back(index); } -void MeshBuffer::add(const Vertex& vertex, const Normal& normal, - const TexCoord& a_tex_coord) +void MeshBuffer::add(const Vertex& vertex, + const Normal& normal, + const TexCoord& a_tex_coord, + const Colour& colour) { if (!has_texture) throw runtime_error( @@ -244,6 +246,7 @@ void MeshBuffer::add(const Vertex& vertex, const Normal& normal, vertices.push_back(vertex); normals.push_back(normal); tex_coords.push_back(a_tex_coord); + colours.push_back(colour); indices.push_back(index); } @@ -341,11 +344,10 @@ static void copy_vertex_data(const MeshBuffer* buf, VertexData* vertex_data) vd->tx = buf->tex_coords[i].x; vd->ty = 1.0f - buf->tex_coords[i].y; } - else { - vd->r = buf->colours[i].r; - vd->g = buf->colours[i].g; - vd->b = buf->colours[i].b; - } + + vd->r = buf->colours[i].r; + vd->g = buf->colours[i].g; + vd->b = buf->colours[i].b; } } @@ -356,10 +358,9 @@ public: ~VertexArrayMesh(); void render() const; -private: - Material material; - bool has_material, has_texture; +private: + ITexturePtr texture; size_t my_vertex_count; VertexData* my_vertex_data; size_t my_index_count; @@ -370,9 +371,7 @@ VertexArrayMesh::VertexArrayMesh(IMeshBufferPtr a_buffer) { const MeshBuffer* buf = MeshBuffer::get(a_buffer); - material = buf->material; - has_material = buf->has_material; - has_texture = buf->has_texture; + texture = buf->material.texture; my_vertex_count = buf->vertices.size(); my_vertex_data = new VertexData[my_vertex_count]; @@ -396,24 +395,30 @@ void VertexArrayMesh::render() const glPushAttrib(GL_ENABLE_BIT); glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT); - glDisable(GL_BLEND); - - if (has_material) - material.apply(); - else { - glEnable(GL_COLOR_MATERIAL); + if (!glIsEnabled(GL_CULL_FACE)) + glEnable(GL_CULL_FACE); - glEnableClientState(GL_COLOR_ARRAY); - glColorPointer(3, GL_FLOAT, sizeof(VertexData), - reinterpret_cast(&my_vertex_data->r)); - } + if (glIsEnabled(GL_BLEND)) + glDisable(GL_BLEND); - if (has_texture) { + if (texture) { + glEnable(GL_TEXTURE_2D); + texture->bind(); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); glTexCoordPointer(2, GL_FLOAT, sizeof(VertexData), - reinterpret_cast(&my_vertex_data->tx)); + reinterpret_cast(&my_vertex_data->tx)); } - + else { + glDisable(GL_TEXTURE_2D); + } + + glEnable(GL_COLOR_MATERIAL); + + glEnableClientState(GL_COLOR_ARRAY); + glColorPointer(3, GL_FLOAT, sizeof(VertexData), + reinterpret_cast(&my_vertex_data->r)); + glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_NORMAL_ARRAY); glVertexPointer(3, GL_FLOAT, sizeof(VertexData), @@ -508,19 +513,20 @@ void VBOMesh::render() const glEnable(GL_TEXTURE_2D); texture->bind(); - glEnable(GL_COLOR_MATERIAL); - glColor3f(1.0f, 1.0f, 1.0f); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + glTexCoordPointer(2, GL_FLOAT, sizeof(VertexData), + reinterpret_cast(offsetof(VertexData, tx))); } else { glDisable(GL_TEXTURE_2D); - - glEnable(GL_COLOR_MATERIAL); - glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE); - - glEnableClientState(GL_COLOR_ARRAY); - glColorPointer(3, GL_FLOAT, sizeof(VertexData), - reinterpret_cast(offsetof(VertexData, r))); } + + glEnable(GL_COLOR_MATERIAL); + glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE); + + glEnableClientState(GL_COLOR_ARRAY); + glColorPointer(3, GL_FLOAT, sizeof(VertexData), + reinterpret_cast(offsetof(VertexData, r))); glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_NORMAL_ARRAY); @@ -545,7 +551,7 @@ IMeshPtr make_mesh(IMeshBufferPtr a_buffer) //a_buffer->print_stats(); // Prefer VBOs for all meshes - if (GLEW_ARB_vertex_buffer_object) + if (GLEW_ARB_vertex_buffer_object && false) return IMeshPtr(new VBOMesh(a_buffer)); else return IMeshPtr(new VertexArrayMesh(a_buffer)); -- 2.39.2