From ea73765a5f4e11694f889f8b2c21560e3ccda9b2 Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Sun, 16 Jan 2011 15:38:21 +0000 Subject: [PATCH] Simple noise texture --- include/ITexture.hpp | 7 ++-- src/Map.cpp | 7 ++-- src/NoiseTexture.cpp | 77 ++++++++++++++++++++++++++++++++++++++++++++ src/Texture.cpp | 6 ++-- 4 files changed, 87 insertions(+), 10 deletions(-) create mode 100644 src/NoiseTexture.cpp diff --git a/include/ITexture.hpp b/include/ITexture.hpp index 67e1866..3c298ef 100644 --- a/include/ITexture.hpp +++ b/include/ITexture.hpp @@ -1,5 +1,5 @@ // -// Copyright (C) 2009 Nick Gasson +// Copyright (C) 2009-2011 Nick Gasson // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by @@ -28,7 +28,7 @@ struct ITexture { virtual ~ITexture() {} // Bind this texture to the current OpenGL texture - virtual void bind() const = 0; + virtual void bind() = 0; virtual int width() const = 0; virtual int height() const = 0; @@ -44,4 +44,7 @@ ITexturePtr load_texture(const string& a_file_name); // Load a texture from a resource ITexturePtr load_texture(IResourcePtr a_res, const string& a_file_name); +// Generate Perlin noise +ITexturePtr make_noise_texture(int width, int height); + #endif diff --git a/src/Map.cpp b/src/Map.cpp index dc0a7e5..c47ad7b 100644 --- a/src/Map.cpp +++ b/src/Map.cpp @@ -650,6 +650,8 @@ void Map::build_mesh(int id, Point bot_left, Point top_right) // the meshes are built on the same frame ++frame_num; + buf->bind(make_noise_texture(256, 256)); + for (int x = top_right.x-1; x >= bot_left.x; x--) { for (int y = bot_left.y; y < top_right.y; y++) { int indexes[4]; @@ -671,11 +673,6 @@ void Map::build_mesh(int id, Point bot_left, Point top_right) tex_coords[1], tex_coords[2], tex_coords[3], tex_coords[3], tex_coords[0], tex_coords[1] }; - - if (random() % 2 == 0) - buf->bind(load_texture("test2.png")); - else - buf->bind(load_texture("test.png")); for (int i = 0; i < 6; i++) { const HeightMap& v = height_map[order[i]]; diff --git a/src/NoiseTexture.cpp b/src/NoiseTexture.cpp new file mode 100644 index 0000000..19320aa --- /dev/null +++ b/src/NoiseTexture.cpp @@ -0,0 +1,77 @@ +// +// Copyright (C) 2011 Nick Gasson +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// + +#include "ITexture.hpp" +#include "ILogger.hpp" +#include "OpenGLHelper.hpp" + +#include // XXX + +class NoiseTexture : public ITexture { +public: + NoiseTexture(int w, int h); + ~NoiseTexture(); + + // ITexture interface + void bind(); + int width() const { return width_; } + int height() const { return height_; } + +private: + const int width_, height_; + GLuint texture; +}; + +NoiseTexture::NoiseTexture(int w, int h) + : width_(w), height_(h) +{ + GLubyte* pixels = new GLubyte[w * h]; + + for (int x = 0; x < w; x++) { + for (int y = 0; y < h; y++) + pixels[x + (y*w)] = 127 + (random() % 32) - 16; + } + + glGenTextures(1, &texture); + glBindTexture(GL_TEXTURE_2D, texture); + + // Use GL_NEAREST here for better performance + // Or GL_LINEAR for better apppearance + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + + // Load the surface's data into the texture + glTexImage2D(GL_TEXTURE_2D, 0, 3, w, h, 0, + GL_LUMINANCE, GL_UNSIGNED_BYTE, pixels); + + delete[] pixels; +} + +NoiseTexture::~NoiseTexture() +{ + glDeleteTextures(1, &texture); +} + +void NoiseTexture::bind() +{ + glBindTexture(GL_TEXTURE_2D, texture); +} + +ITexturePtr make_noise_texture(int width, int height) +{ + return ITexturePtr(new NoiseTexture(width, height)); +} diff --git a/src/Texture.cpp b/src/Texture.cpp index 48e2f5f..c13b59d 100644 --- a/src/Texture.cpp +++ b/src/Texture.cpp @@ -33,10 +33,10 @@ using namespace std::tr1; class Texture : public ITexture { public: Texture(const string &file); - virtual ~Texture(); + ~Texture(); GLuint texture() const { return my_texture; } - void bind() const; + void bind(); int width() const { return my_width; } int height() const { return my_height; } @@ -168,7 +168,7 @@ bool Texture::is_texture_sizeSupported(int width, int height, int ncols, GLenum return height != 0 && width != 0; } -void Texture::bind() const +void Texture::bind() { glBindTexture(GL_TEXTURE_2D, my_texture); } -- 2.39.2