Better texture handling.

This commit is contained in:
2017-02-21 17:17:35 -04:00
parent 610ab9cfa0
commit a2d0da2404
3 changed files with 17 additions and 28 deletions

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

@@ -55,8 +55,9 @@ static inline float random_n(float r) {
PheromoneMap::PheromoneMap(const char * file_name) { PheromoneMap::PheromoneMap(const char * file_name) {
load_map(file_name); load_map(file_name);
sem_init(&map_semaphore, 0, 1); sem_init(&map_semaphore, 0, 1);
tex_created = false;
then = 0; then = 0;
glGenTextures(1, &handle);
} }
PheromoneMap::~PheromoneMap() { PheromoneMap::~PheromoneMap() {
@@ -64,12 +65,10 @@ PheromoneMap::~PheromoneMap() {
delete tex_data; delete tex_data;
sem_destroy(&map_semaphore); sem_destroy(&map_semaphore);
if(tex_created) {
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, handle); glBindTexture(GL_TEXTURE_2D, handle);
glDeleteTextures(1, &handle); glDeleteTextures(1, &handle);
} }
}
void PheromoneMap::load_map(const char * file_name) { void PheromoneMap::load_map(const char * file_name) {
unsigned char * _data; unsigned char * _data;
@@ -97,26 +96,17 @@ void PheromoneMap::load_map(const char * file_name) {
GLuint PheromoneMap::s_build_texture() { GLuint PheromoneMap::s_build_texture() {
sem_wait(&map_semaphore); { sem_wait(&map_semaphore); {
if(tex_created) {
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, handle);
glDeleteTextures(1, &handle);
}
for(unsigned int i = 0; i < (m_width * m_height * m_bpp); i++) { for(unsigned int i = 0; i < (m_width * m_height * m_bpp); i++) {
tex_data[i] = static_cast<unsigned char>(data[i]); tex_data[i] = static_cast<unsigned char>(data[i]);
} }
} sem_post(&map_semaphore);
glGenTextures(1, &handle);
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); glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, m_width, m_height, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, tex_data);
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);
tex_created = true;
} sem_post(&map_semaphore);
return handle; return handle;
} }

View File

@@ -78,7 +78,6 @@ private:
unsigned m_height; unsigned m_height;
unsigned char m_bpp; unsigned char m_bpp;
sem_t map_semaphore; sem_t map_semaphore;
bool tex_created;
GLuint handle; GLuint handle;
clock_t then; clock_t then;