fzea vlnzuueck

This commit is contained in:
2017-02-22 18:31:04 -04:00
parent a2d0da2404
commit 6898dd244c
10 changed files with 59 additions and 34 deletions

View File

@@ -35,7 +35,7 @@ window
floorplan floorplan
( (
bitmap "maps/cave.png" bitmap "maps/empty.png"
size [ 16.000 16.000 1.500 ] size [ 16.000 16.000 1.500 ]
) )

View File

@@ -134,7 +134,7 @@ float IASSS_Robot::brss() {
std::map<int, float> U, V; std::map<int, float> U, V;
unsigned int i_min, i_max; unsigned int i_min, i_max;
float min, sample, prob, max, sum_uv = 0.0f, steer; float min, sample, prob, max, sum_uv = 0.0f, steer;
// std::ostringstream oss; std::ostringstream oss;
while(U.size() < (U_RATIO * NUM_PHERO_SAMPLES)) { while(U.size() < (U_RATIO * NUM_PHERO_SAMPLES)) {
min = std::numeric_limits<double>::max(); min = std::numeric_limits<double>::max();
@@ -221,12 +221,12 @@ float IASSS_Robot::brss() {
steer = (NUM_PHERO_SAMPLES / 2.0f) - i_max; steer = (NUM_PHERO_SAMPLES / 2.0f) - i_max;
// oss << "samples: " << std::endl; oss << "samples: " << std::endl;
// for(unsigned int i = 0; i < NUM_PHERO_SAMPLES; i++) 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 << "\tSAMPLE[" << i << "]: " << _phero_sensor[i] << " - " << _phero_sensor.sample_amnt[i] << " - " << _phero_sensor.probs[i] << std::endl;
// oss << "\ti_max: " << i_max << " | Steer: " << steer; oss << "\ti_max: " << i_max << " | Steer: " << steer;
// log(oss.str()); log(oss.str());
return steer; return steer;
} }

View File

@@ -74,7 +74,7 @@ int main(int argc, char **argv) {
signal(SIGINT, handler); signal(SIGINT, handler);
try { 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. // Initialize the robot objects and threads.
for(uint32_t i = 0; i < NUM_ROBOTS; ++i) { for(uint32_t i = 0; i < NUM_ROBOTS; ++i) {

BIN
maps/empty.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 867 B

BIN
maps/empty_mask.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 420 B

43
ogl.cpp
View File

@@ -28,6 +28,7 @@
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp> #include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp> #include <glm/gtc/type_ptr.hpp>
#include <pnglite.h>
#include "ogl.hpp" #include "ogl.hpp"
#include "GLSLProgram.hpp" #include "GLSLProgram.hpp"
@@ -38,6 +39,7 @@ namespace ogl
static CGLSLProgram m_program; static CGLSLProgram m_program;
static PheromoneMap * m_phero_map = NULL; static PheromoneMap * m_phero_map = NULL;
static GLuint m_textureHandle; static GLuint m_textureHandle;
static GLuint m_colorMapHandle;
// Quad definition // Quad definition
static glm::vec4 vec_points[6]; 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 ); 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) { void initialize(PheromoneMap * phero_map) {
glEnable(GL_TEXTURE_1D);
glEnable(GL_TEXTURE_2D); glEnable(GL_TEXTURE_2D);
glewInit(); glewInit();
quad(); quad();
m_phero_map = phero_map; m_phero_map = phero_map;
build_color_map();
m_program.loadShader("shaders/basic.vert", CGLSLProgram::VERTEX); m_program.loadShader("shaders/basic.vert", CGLSLProgram::VERTEX);
m_program.loadShader("shaders/basic.frag", CGLSLProgram::FRAGMENT); m_program.loadShader("shaders/basic.frag", CGLSLProgram::FRAGMENT);
m_program.create_link(); m_program.create_link();
m_program.enable(); m_program.enable();
m_program.addUniform("sTexture"); m_program.addUniform("sTexture");
m_program.addUniform("sColorMap");
m_program.disable(); m_program.disable();
std::cout << "OpenGL Version: " << (char*)glGetString(GL_VERSION) << std::endl; 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); glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
if(m_phero_map != NULL) 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(); 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); { glBegin(GL_TRIANGLES); {
for(int i = 0; i < 6; i++) { for(int i = 0; i < 6; i++) {
glTexCoord2f(vec_tex_coords[i].s, vec_tex_coords[i].t); glTexCoord2f(vec_tex_coords[i].s, vec_tex_coords[i].t);

View File

@@ -62,7 +62,6 @@ PheromoneMap::PheromoneMap(const char * file_name) {
PheromoneMap::~PheromoneMap() { PheromoneMap::~PheromoneMap() {
delete data; delete data;
delete tex_data;
sem_destroy(&map_semaphore); sem_destroy(&map_semaphore);
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
@@ -71,13 +70,12 @@ PheromoneMap::~PheromoneMap() {
} }
void PheromoneMap::load_map(const char * file_name) { void PheromoneMap::load_map(const char * file_name) {
unsigned char * _data;
png_t tex; png_t tex;
png_init(0, 0); png_init(0, 0);
png_open_file_read(&tex, file_name); png_open_file_read(&tex, file_name);
_data = new unsigned char[tex.width * tex.height * tex.bpp]; data = new unsigned char[tex.width * tex.height * tex.bpp];
png_get_data(&tex, _data); png_get_data(&tex, data);
std::cout << "Loaded map \"" << file_name << "\" :: " << tex.width << "x" << tex.height << "x" << (int)tex.bpp << std::endl; std::cout << "Loaded map \"" << file_name << "\" :: " << tex.width << "x" << tex.height << "x" << (int)tex.bpp << std::endl;
m_width = tex.width; m_width = tex.width;
@@ -85,28 +83,20 @@ void PheromoneMap::load_map(const char * file_name) {
m_bpp = tex.bpp; m_bpp = tex.bpp;
png_close_file(&tex); 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() { 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<unsigned char>(data[i]);
}
} sem_post(&map_semaphore);
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, handle); 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_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, 0);
return handle; return handle;
} }

View File

@@ -72,8 +72,7 @@ public:
void s_sample(phero_sensor_t * sensor, float x, float y, float yaw, float radius); void s_sample(phero_sensor_t * sensor, float x, float y, float yaw, float radius);
private: private:
int * data; unsigned char * data;
unsigned char * tex_data;
unsigned m_width; unsigned m_width;
unsigned m_height; unsigned m_height;
unsigned char m_bpp; unsigned char m_bpp;

View File

@@ -1,9 +1,10 @@
#version 120 #version 120
uniform sampler2D sTexture; uniform sampler2D sTexture;
uniform sampler1D sColorMap;
void main() { void main() {
vec4 tex = texture2D(sTexture, gl_TexCoord[0].st); 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); vec4 map = texture1D(sColorMap, tex.r);
gl_FragColor = clamp(color, 0.0, 1.0); gl_FragColor = map;
} }

BIN
shaders/color_map.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 346 B