From 6898dd244c16892ddbbfd04752f9fc0ee7200dda Mon Sep 17 00:00:00 2001 From: Miguel Angel Astor Romero Date: Wed, 22 Feb 2017 18:31:04 -0400 Subject: [PATCH] fzea vlnzuueck --- ant.world | 2 +- ias_robot.cpp | 12 ++++++------ ias_ss.cpp | 2 +- maps/empty.png | Bin 0 -> 867 bytes maps/empty_mask.png | Bin 0 -> 420 bytes ogl.cpp | 43 ++++++++++++++++++++++++++++++++++++++---- pheromone.cpp | 26 ++++++++----------------- pheromone.hpp | 3 +-- shaders/basic.frag | 5 +++-- shaders/color_map.png | Bin 0 -> 346 bytes 10 files changed, 59 insertions(+), 34 deletions(-) create mode 100644 maps/empty.png create mode 100644 maps/empty_mask.png create mode 100644 shaders/color_map.png diff --git a/ant.world b/ant.world index 12e00db..e517218 100644 --- a/ant.world +++ b/ant.world @@ -35,7 +35,7 @@ window floorplan ( - bitmap "maps/cave.png" + bitmap "maps/empty.png" size [ 16.000 16.000 1.500 ] ) diff --git a/ias_robot.cpp b/ias_robot.cpp index 2bd1d79..aae3552 100644 --- a/ias_robot.cpp +++ b/ias_robot.cpp @@ -134,7 +134,7 @@ float IASSS_Robot::brss() { std::map U, V; unsigned int i_min, i_max; float min, sample, prob, max, sum_uv = 0.0f, steer; - // std::ostringstream oss; + std::ostringstream oss; while(U.size() < (U_RATIO * NUM_PHERO_SAMPLES)) { min = std::numeric_limits::max(); @@ -221,12 +221,12 @@ float IASSS_Robot::brss() { steer = (NUM_PHERO_SAMPLES / 2.0f) - i_max; - // oss << "samples: " << std::endl; - // for(unsigned int i = 0; i < NUM_PHERO_SAMPLES; i++) - // oss << "\tSAMPLE[" << i << "]: " << _phero_sensor[i] << " - " << _phero_sensor.sample_amnt[i] << " - " << _phero_sensor.probs[i] << std::endl; - // oss << "\ti_max: " << i_max << " | Steer: " << steer; + oss << "samples: " << std::endl; + for(unsigned int i = 0; i < NUM_PHERO_SAMPLES; i++) + oss << "\tSAMPLE[" << i << "]: " << _phero_sensor[i] << " - " << _phero_sensor.sample_amnt[i] << " - " << _phero_sensor.probs[i] << std::endl; + oss << "\ti_max: " << i_max << " | Steer: " << steer; - // log(oss.str()); + log(oss.str()); return steer; } diff --git a/ias_ss.cpp b/ias_ss.cpp index e60b59d..bd134b1 100644 --- a/ias_ss.cpp +++ b/ias_ss.cpp @@ -74,7 +74,7 @@ int main(int argc, char **argv) { signal(SIGINT, handler); try { - phero_map = new PheromoneMap("maps/cave_mask.png"); + phero_map = new PheromoneMap(argc > 1 ? argv[1] : "maps/cave_mask.png"); // Initialize the robot objects and threads. for(uint32_t i = 0; i < NUM_ROBOTS; ++i) { diff --git a/maps/empty.png b/maps/empty.png new file mode 100644 index 0000000000000000000000000000000000000000..c9d5aa58d2a1e7b293bf42a14f17a1bc5e06cd66 GIT binary patch literal 867 zcmeAS@N?(olHy`uVBq!ia0y~yVEh8Y96$kvcY7PcfD~teM`SSr1Gf+eGhVt|_XjA* zUgGKN%Knf^OjMRh^kXPL0|PUYr;B4q#jUp&85tQEIGPQ%)<2hiqRe!==av=s0`6Vd zk+XP-BUaR|_o4|a;)}h&iWSM?u!7sFWYqrA@TYnD+q;`ROr7QV)T3{DLAl4%)z4*} HQ$iB}uaQ@P literal 0 HcmV?d00001 diff --git a/maps/empty_mask.png b/maps/empty_mask.png new file mode 100644 index 0000000000000000000000000000000000000000..0557eeb3c149208560cf4f3f0584c4066bb36e80 GIT binary patch literal 420 zcmeAS@N?(olHy`uVBq!ia0y~yU<5K5890C>L#5>RT|kPnz$3Dlfq`2Hgc&d0t@{HM zWH0gbb!C6ZBql1S@ihIs6i}11r;B4q#jUrO4)QV>@Guz$mHwZ4IluhCc1E$yFW<2^ sl$)QtvM!zFVdQ&MBb@0P&$K3jhEB literal 0 HcmV?d00001 diff --git a/ogl.cpp b/ogl.cpp index a7b6edb..67b4e82 100644 --- a/ogl.cpp +++ b/ogl.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #include "ogl.hpp" #include "GLSLProgram.hpp" @@ -38,6 +39,7 @@ namespace ogl static CGLSLProgram m_program; static PheromoneMap * m_phero_map = NULL; static GLuint m_textureHandle; + static GLuint m_colorMapHandle; // Quad definition static glm::vec4 vec_points[6]; @@ -52,19 +54,43 @@ namespace ogl vec_tex_coords[5] = glm::vec2( 1.0f, 0.0f); vec_points[5] = glm::vec4( 0.5f, 0.5f, 0.0f, 1.0f ); } + void build_color_map() { + png_t tex; + + png_init(0, 0); + png_open_file_read(&tex, "shaders/color_map.png"); + unsigned char * data = new unsigned char[tex.width * tex.height * tex.bpp]; + png_get_data(&tex, data); + + glEnable(GL_TEXTURE_1D); + glGenTextures(1, &m_colorMapHandle); + glEnable(GL_TEXTURE_1D); + glBindTexture(GL_TEXTURE_1D, m_colorMapHandle); + glTexImage1D(GL_TEXTURE_1D, 0, GL_RGB, tex.width, 0, GL_RGB, GL_UNSIGNED_BYTE, data); + glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + + png_close_file(&tex); + delete data; + } + void initialize(PheromoneMap * phero_map) { + glEnable(GL_TEXTURE_1D); glEnable(GL_TEXTURE_2D); glewInit(); quad(); m_phero_map = phero_map; + + build_color_map(); m_program.loadShader("shaders/basic.vert", CGLSLProgram::VERTEX); m_program.loadShader("shaders/basic.frag", CGLSLProgram::FRAGMENT); m_program.create_link(); m_program.enable(); m_program.addUniform("sTexture"); + m_program.addUniform("sColorMap"); m_program.disable(); std::cout << "OpenGL Version: " << (char*)glGetString(GL_VERSION) << std::endl; @@ -76,13 +102,22 @@ namespace ogl glClearColor(0.0f, 0.0f, 0.0f, 1.0f); if(m_phero_map != NULL) - m_textureHandle = m_phero_map->s_build_texture(); // Good grief! + m_textureHandle = m_phero_map->s_build_texture(); m_program.enable(); - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, m_textureHandle); - glUniform1i(m_program.getLocation("sTexture"), GL_TEXTURE0); + glActiveTexture(GL_TEXTURE0); + glEnable(GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D, m_textureHandle); + glUniform1i(m_program.getLocation("sTexture"), 0); + + glActiveTexture(GL_TEXTURE1); + glEnable(GL_TEXTURE_1D); + glBindTexture(GL_TEXTURE_1D, m_colorMapHandle); + glUniform1i(m_program.getLocation("sColorMap"), 1); + + glActiveTexture(GL_TEXTURE0); + glBegin(GL_TRIANGLES); { for(int i = 0; i < 6; i++) { glTexCoord2f(vec_tex_coords[i].s, vec_tex_coords[i].t); diff --git a/pheromone.cpp b/pheromone.cpp index 9fa3658..cd26dbb 100644 --- a/pheromone.cpp +++ b/pheromone.cpp @@ -62,7 +62,6 @@ PheromoneMap::PheromoneMap(const char * file_name) { PheromoneMap::~PheromoneMap() { delete data; - delete tex_data; sem_destroy(&map_semaphore); glActiveTexture(GL_TEXTURE0); @@ -71,13 +70,12 @@ PheromoneMap::~PheromoneMap() { } void PheromoneMap::load_map(const char * file_name) { - unsigned char * _data; png_t tex; png_init(0, 0); png_open_file_read(&tex, file_name); - _data = new unsigned char[tex.width * tex.height * tex.bpp]; - png_get_data(&tex, _data); + data = new unsigned char[tex.width * tex.height * tex.bpp]; + png_get_data(&tex, data); std::cout << "Loaded map \"" << file_name << "\" :: " << tex.width << "x" << tex.height << "x" << (int)tex.bpp << std::endl; m_width = tex.width; @@ -85,28 +83,20 @@ void PheromoneMap::load_map(const char * file_name) { m_bpp = tex.bpp; png_close_file(&tex); - - data = new int[tex.width * tex.height * tex.bpp]; - tex_data = new unsigned char[tex.width * tex.height * tex.bpp]; - for(unsigned int i = 0; i < (tex.width * tex.height * tex.bpp); i++) { - data[i] = _data[i]; - } - delete _data; } GLuint PheromoneMap::s_build_texture() { - sem_wait(&map_semaphore); { - for(unsigned int i = 0; i < (m_width * m_height * m_bpp); i++) { - tex_data[i] = static_cast(data[i]); - } - } sem_post(&map_semaphore); - glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, handle); - glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, m_width, m_height, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, tex_data); + sem_wait(&map_semaphore); { + glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, m_width, m_height, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, data); + } sem_post(&map_semaphore); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, 0); + return handle; } diff --git a/pheromone.hpp b/pheromone.hpp index 72a6e70..27fa54f 100644 --- a/pheromone.hpp +++ b/pheromone.hpp @@ -72,8 +72,7 @@ public: void s_sample(phero_sensor_t * sensor, float x, float y, float yaw, float radius); private: - int * data; - unsigned char * tex_data; + unsigned char * data; unsigned m_width; unsigned m_height; unsigned char m_bpp; diff --git a/shaders/basic.frag b/shaders/basic.frag index 29599cb..7a979e9 100644 --- a/shaders/basic.frag +++ b/shaders/basic.frag @@ -1,9 +1,10 @@ #version 120 uniform sampler2D sTexture; +uniform sampler1D sColorMap; void main() { vec4 tex = texture2D(sTexture, gl_TexCoord[0].st); - vec4 color = mix(vec4(0.0, 0.0, 0.3, 1.0), vec4(1.0, 1.0, 0.0, 1.0), tex.r); - gl_FragColor = clamp(color, 0.0, 1.0); + vec4 map = texture1D(sColorMap, tex.r); + gl_FragColor = map; } diff --git a/shaders/color_map.png b/shaders/color_map.png new file mode 100644 index 0000000000000000000000000000000000000000..98f2d2b4c42c755e0969507d64b96ad53319e11f GIT binary patch literal 346 zcmV-g0j2(lP)|#d&kZAj=schE_{o1fZ`^m`1N-~C-~a#s07*qoM6N<$f=(ER!T