i dont care
This commit is contained in:
19
gui.cpp
19
gui.cpp
@@ -28,44 +28,35 @@
|
|||||||
#include "gui.hpp"
|
#include "gui.hpp"
|
||||||
#include "ogl.hpp"
|
#include "ogl.hpp"
|
||||||
|
|
||||||
static const int REFRESH_MS = 0.016; // for 60 fps.
|
|
||||||
|
|
||||||
static void redraw_callback(void * arg) {
|
static void redraw_callback(void * arg) {
|
||||||
GlGui * window = static_cast<GlGui *>(arg);
|
GlGui * window = static_cast<GlGui *>(arg);
|
||||||
window->redraw();
|
window->redraw();
|
||||||
if(!window->isFinished())
|
|
||||||
Fl::repeat_timeout(REFRESH_MS, redraw_callback, window);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
GlGui::GlGui(Fl_Window * parent, int x, int y, int w, int h, const char * l, PheromoneMap * phero_map) : Fl_Gl_Window(x, y, w, h, l) {
|
GlGui::GlGui(Fl_Window * parent, int x, int y, int w, int h, const char * l, PheromoneMap * phero_map) : Fl_Gl_Window(x, y, w, h, l) {
|
||||||
|
mode(FL_RGB | FL_DOUBLE);
|
||||||
|
|
||||||
this->parent = parent;
|
this->parent = parent;
|
||||||
this->phero_map = phero_map;
|
this->phero_map = phero_map;
|
||||||
title += l;
|
title += l;
|
||||||
initialized = false;
|
initialized = false;
|
||||||
done = false;
|
|
||||||
|
Fl::add_idle(redraw_callback, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void GlGui::draw() {
|
void GlGui::draw() {
|
||||||
if(!valid()) {
|
if(!valid()) {
|
||||||
if(!initialized) {
|
if(!initialized) {
|
||||||
ogl::initialize(phero_map);
|
ogl::initialize(phero_map);
|
||||||
Fl::add_timeout(REFRESH_MS, redraw_callback, this);
|
|
||||||
initialized = true;
|
initialized = true;
|
||||||
}
|
}
|
||||||
ogl::reshape(w(), h());
|
ogl::reshape(w(), h());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
phero_map->s_update();
|
||||||
ogl::display();
|
ogl::display();
|
||||||
}
|
}
|
||||||
|
|
||||||
int GlGui::handle(int event) {
|
int GlGui::handle(int event) {
|
||||||
return Fl_Gl_Window::handle(event);
|
return Fl_Gl_Window::handle(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
void GlGui::finish() {
|
|
||||||
done = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool GlGui::isFinished() {
|
|
||||||
return done;
|
|
||||||
}
|
|
||||||
|
3
gui.hpp
3
gui.hpp
@@ -37,8 +37,6 @@
|
|||||||
class GlGui : public Fl_Gl_Window {
|
class GlGui : public Fl_Gl_Window {
|
||||||
public:
|
public:
|
||||||
GlGui(Fl_Window * parent, int x, int y, int w, int h, const char * l, PheromoneMap * phero_map);
|
GlGui(Fl_Window * parent, int x, int y, int w, int h, const char * l, PheromoneMap * phero_map);
|
||||||
void finish();
|
|
||||||
bool isFinished();
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void draw();
|
virtual void draw();
|
||||||
@@ -48,7 +46,6 @@ private:
|
|||||||
Fl_Window * parent;
|
Fl_Window * parent;
|
||||||
std::string title;
|
std::string title;
|
||||||
bool initialized;
|
bool initialized;
|
||||||
bool done;
|
|
||||||
PheromoneMap * phero_map;
|
PheromoneMap * phero_map;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@@ -32,7 +32,8 @@ static const float TURN_DEG_PER_SEC = 40.0f;
|
|||||||
static const float METERS_PER_SEC = 0.4f;
|
static const float METERS_PER_SEC = 0.4f;
|
||||||
static const long HALF_SECOND_USEC = 500000;
|
static const long HALF_SECOND_USEC = 500000;
|
||||||
static const double MIN_DIST_M = 1.5;
|
static const double MIN_DIST_M = 1.5;
|
||||||
static const double CRIT_DIST_M = 1.0;
|
static const double CRIT_DIST_M = 1.0;
|
||||||
|
static const float MAP_SIZE = 16.0f;
|
||||||
|
|
||||||
IASSS_Robot::IASSS_Robot(std::string hostname, uint32_t port, PheromoneMap * phero_map) : Robot(hostname, port) {
|
IASSS_Robot::IASSS_Robot(std::string hostname, uint32_t port, PheromoneMap * phero_map) : Robot(hostname, port) {
|
||||||
_phero_map = phero_map;
|
_phero_map = phero_map;
|
||||||
@@ -45,6 +46,7 @@ IASSS_Robot::~IASSS_Robot() {
|
|||||||
|
|
||||||
void IASSS_Robot::run() {
|
void IASSS_Robot::run() {
|
||||||
int rv;
|
int rv;
|
||||||
|
float x, y;
|
||||||
long then, now, delta, wait;
|
long then, now, delta, wait;
|
||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
double dist = std::numeric_limits<double>::infinity();
|
double dist = std::numeric_limits<double>::infinity();
|
||||||
@@ -72,6 +74,12 @@ void IASSS_Robot::run() {
|
|||||||
now = tv.tv_usec;
|
now = tv.tv_usec;
|
||||||
delta = now - then;
|
delta = now - then;
|
||||||
|
|
||||||
|
if(_phero_map != NULL) {
|
||||||
|
x = (_p_proxy->GetXPos() + (MAP_SIZE / 2)) / MAP_SIZE;
|
||||||
|
y = (_p_proxy->GetYPos() + (MAP_SIZE / 2)) / MAP_SIZE;
|
||||||
|
_phero_map->s_draw_point(x, y);
|
||||||
|
}
|
||||||
|
|
||||||
// Sleep for a bit before finishing this control iteration.
|
// Sleep for a bit before finishing this control iteration.
|
||||||
wait = rv == 0 ? HALF_SECOND_USEC - delta : HALF_SECOND_USEC;
|
wait = rv == 0 ? HALF_SECOND_USEC - delta : HALF_SECOND_USEC;
|
||||||
usleep(wait);
|
usleep(wait);
|
||||||
|
14
ias_ss.cpp
14
ias_ss.cpp
@@ -44,11 +44,6 @@ static PheromoneMap * phero_map = NULL;
|
|||||||
|
|
||||||
extern "C" void handler(int signal) {
|
extern "C" void handler(int signal) {
|
||||||
done = true;
|
done = true;
|
||||||
|
|
||||||
if(window != NULL) {
|
|
||||||
glWindow->finish();
|
|
||||||
window->hide();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
extern "C" void * robot_thread(void * arg) {
|
extern "C" void * robot_thread(void * arg) {
|
||||||
@@ -56,8 +51,10 @@ extern "C" void * robot_thread(void * arg) {
|
|||||||
|
|
||||||
std::cout << "Running robot thread." << std::endl;
|
std::cout << "Running robot thread." << std::endl;
|
||||||
|
|
||||||
while(!done)
|
while(!done) {
|
||||||
robot->run();
|
robot->run();
|
||||||
|
pthread_testcancel();
|
||||||
|
}
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@@ -92,7 +89,10 @@ int main(int argc, char **argv) {
|
|||||||
create_gui(argc, argv);
|
create_gui(argc, argv);
|
||||||
Fl::run();
|
Fl::run();
|
||||||
|
|
||||||
// Wait for all the robots to finish.
|
// Finish all the robots.
|
||||||
|
for(uint32_t i = 0; i < NUM_ROBOTS; ++i)
|
||||||
|
pthread_cancel(robot_threads[i]);
|
||||||
|
|
||||||
for(uint32_t i = 0; i < NUM_ROBOTS; ++i) {
|
for(uint32_t i = 0; i < NUM_ROBOTS; ++i) {
|
||||||
if(pthread_join(robot_threads[i], NULL) != 0) {
|
if(pthread_join(robot_threads[i], NULL) != 0) {
|
||||||
perror("Could not join robot thread");
|
perror("Could not join robot thread");
|
||||||
|
Binary file not shown.
Before Width: | Height: | Size: 1.1 KiB After Width: | Height: | Size: 911 B |
Binary file not shown.
Before Width: | Height: | Size: 212 B |
20
ogl.cpp
20
ogl.cpp
@@ -28,7 +28,6 @@
|
|||||||
#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"
|
||||||
@@ -41,21 +40,24 @@ 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_gradientHandle;
|
||||||
|
|
||||||
// Quad definition
|
// Quad definition
|
||||||
static glm::vec4 vec_points[6];
|
static glm::vec4 vec_points[6];
|
||||||
static glm::vec2 vec_tex_coords[6];
|
static glm::vec2 vec_tex_coords[6];
|
||||||
|
|
||||||
static void quad() {
|
static void quad() {
|
||||||
vec_tex_coords[0] = glm::vec2( 0.0f, 0.0f); vec_points[0] = glm::vec4( -0.5f, -0.5f, 0.0f, 1.0f );
|
vec_tex_coords[0] = glm::vec2( 0.0f, 1.0f); vec_points[0] = glm::vec4( -0.5f, -0.5f, 0.0f, 1.0f );
|
||||||
vec_tex_coords[1] = glm::vec2( 0.0f, 1.0f); vec_points[1] = glm::vec4( -0.5f, 0.5f, 0.0f, 1.0f );
|
vec_tex_coords[1] = glm::vec2( 0.0f, 0.0f); vec_points[1] = glm::vec4( -0.5f, 0.5f, 0.0f, 1.0f );
|
||||||
vec_tex_coords[2] = glm::vec2( 1.0f, 0.0f); vec_points[2] = glm::vec4( 0.5f, -0.5f, 0.0f, 1.0f );
|
vec_tex_coords[2] = glm::vec2( 1.0f, 1.0f); vec_points[2] = glm::vec4( 0.5f, -0.5f, 0.0f, 1.0f );
|
||||||
vec_tex_coords[3] = glm::vec2( 1.0f, 0.0f); vec_points[3] = glm::vec4( 0.5f, -0.5f, 0.0f, 1.0f );
|
vec_tex_coords[3] = glm::vec2( 1.0f, 1.0f); vec_points[3] = glm::vec4( 0.5f, -0.5f, 0.0f, 1.0f );
|
||||||
vec_tex_coords[4] = glm::vec2( 0.0f, 1.0f); vec_points[4] = glm::vec4( -0.5f, 0.5f, 0.0f, 1.0f );
|
vec_tex_coords[4] = glm::vec2( 0.0f, 0.0f); vec_points[4] = glm::vec4( -0.5f, 0.5f, 0.0f, 1.0f );
|
||||||
vec_tex_coords[5] = glm::vec2( 1.0f, 1.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 initialize(PheromoneMap * phero_map) {
|
void initialize(PheromoneMap * phero_map) {
|
||||||
|
glEnable(GL_TEXTURE_2D);
|
||||||
|
|
||||||
glewInit();
|
glewInit();
|
||||||
|
|
||||||
quad();
|
quad();
|
||||||
@@ -74,7 +76,7 @@ namespace ogl
|
|||||||
|
|
||||||
void display() {
|
void display() {
|
||||||
glClear(GL_COLOR_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
glClearColor(0.15f, 0.15f, 0.15f, 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(); // Good grief!
|
||||||
@@ -82,7 +84,7 @@ namespace ogl
|
|||||||
m_program.enable();
|
m_program.enable();
|
||||||
glActiveTexture(GL_TEXTURE0);
|
glActiveTexture(GL_TEXTURE0);
|
||||||
glBindTexture(GL_TEXTURE_2D, m_textureHandle);
|
glBindTexture(GL_TEXTURE_2D, m_textureHandle);
|
||||||
glUniform1i(m_program.getLocation("sTexture"), 0);
|
glUniform1i(m_program.getLocation("sTexture"), GL_TEXTURE0);
|
||||||
|
|
||||||
glBegin(GL_TRIANGLES); {
|
glBegin(GL_TRIANGLES); {
|
||||||
for(int i = 0; i < 6; i++) {
|
for(int i = 0; i < 6; i++) {
|
||||||
|
@@ -8,6 +8,7 @@ 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;
|
tex_created = false;
|
||||||
|
then = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
PheromoneMap::~PheromoneMap() {
|
PheromoneMap::~PheromoneMap() {
|
||||||
@@ -26,7 +27,7 @@ void PheromoneMap::load_map(const char * file_name) {
|
|||||||
|
|
||||||
png_init(0, 0);
|
png_init(0, 0);
|
||||||
png_open_file_read(&tex, file_name);
|
png_open_file_read(&tex, file_name);
|
||||||
data = (unsigned char*) malloc(tex.width * tex.height * tex.bpp);
|
data = static_cast<unsigned char *>(malloc(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;
|
||||||
@@ -48,7 +49,7 @@ GLuint PheromoneMap::s_build_texture() {
|
|||||||
glGenTextures(1, &handle);
|
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_RGB, m_width, m_height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, m_width, m_height, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, data);
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||||
|
|
||||||
@@ -57,3 +58,56 @@ GLuint PheromoneMap::s_build_texture() {
|
|||||||
|
|
||||||
return handle;
|
return handle;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void PheromoneMap::s_draw_point(float x, float y) {
|
||||||
|
int _x = m_width * x;
|
||||||
|
int _y = m_height - (m_height * y);
|
||||||
|
|
||||||
|
sem_wait(&map_semaphore); {
|
||||||
|
data[(_y * m_height) + _x] = 249;
|
||||||
|
} sem_post(&map_semaphore);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PheromoneMap::s_update() {
|
||||||
|
clock_t now = clock();
|
||||||
|
|
||||||
|
if(static_cast<float>(now - then) / CLOCKS_PER_SEC < 0.005) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
then = now;
|
||||||
|
|
||||||
|
sem_wait(&map_semaphore); {
|
||||||
|
for(int i = 1; i < m_height - 1; i++) {
|
||||||
|
for(int j = 1; j < m_width - 1; j++) {
|
||||||
|
if(data[(i * m_height) + j] > 10 && data[(i * m_height) + j] < 250){
|
||||||
|
data[(i * m_height) + j] -= 1;
|
||||||
|
|
||||||
|
if(data[((i - 1) * m_height) + (j - 1)] < 250)
|
||||||
|
data[((i - 1) * m_height) + (j - 1)] = data[(i * m_height) + j];
|
||||||
|
|
||||||
|
if(data[((i - 1) * m_height) + j] < 250)
|
||||||
|
data[((i - 1) * m_height) + j] = data[(i * m_height) + j];
|
||||||
|
|
||||||
|
if(data[((i - 1) * m_height) + (j + 1)] < 250)
|
||||||
|
data[((i - 1) * m_height) + (j + 1)] = data[(i * m_height) + j];
|
||||||
|
|
||||||
|
if(data[(i * m_height) + (j - 1)] < 250)
|
||||||
|
data[(i * m_height) + (j - 1)] = data[(i * m_height) + j];
|
||||||
|
|
||||||
|
if(data[(i * m_height) + (j + 1)] < 250)
|
||||||
|
data[(i * m_height) + (j + 1)] = data[(i * m_height) + j];
|
||||||
|
|
||||||
|
if(data[((i + 1) * m_height) + (j - 1)] < 250)
|
||||||
|
data[((i + 1) * m_height) + (j - 1)] = data[(i * m_height) + j];
|
||||||
|
|
||||||
|
if(data[((i + 1) * m_height) + j] < 250)
|
||||||
|
data[((i + 1) * m_height) + j] = data[(i * m_height) + j];
|
||||||
|
|
||||||
|
if(data[((i + 1) * m_height) + (j + 1)] < 250)
|
||||||
|
data[((i + 1) * m_height) + (j + 1)] = data[(i * m_height) + j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} sem_post(&map_semaphore);
|
||||||
|
}
|
||||||
|
@@ -1,8 +1,9 @@
|
|||||||
#ifndef PHEROMONE_HPP
|
#ifndef PHEROMONE_HPP
|
||||||
#define PHEROMONE_HPP
|
#define PHEROMONE_HPP
|
||||||
|
|
||||||
#include <GL/gl.h>
|
#include <ctime>
|
||||||
#include <semaphore.h>
|
#include <semaphore.h>
|
||||||
|
#include <GL/gl.h>
|
||||||
|
|
||||||
class PheromoneMap {
|
class PheromoneMap {
|
||||||
public:
|
public:
|
||||||
@@ -10,15 +11,18 @@ public:
|
|||||||
~PheromoneMap();
|
~PheromoneMap();
|
||||||
|
|
||||||
GLuint s_build_texture();
|
GLuint s_build_texture();
|
||||||
|
void s_draw_point(float x, float y);
|
||||||
|
void s_update();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
unsigned char * data;
|
unsigned char * data;
|
||||||
unsigned m_width;
|
unsigned m_width;
|
||||||
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;
|
bool tex_created;
|
||||||
GLuint handle;
|
GLuint handle;
|
||||||
|
clock_t then;
|
||||||
|
|
||||||
void load_map(const char * file_name);
|
void load_map(const char * file_name);
|
||||||
};
|
};
|
||||||
|
@@ -2,8 +2,7 @@
|
|||||||
|
|
||||||
uniform sampler2D sTexture;
|
uniform sampler2D sTexture;
|
||||||
|
|
||||||
void main(void) {
|
void main() {
|
||||||
vec4 tex = texture2D(sTexture, gl_TexCoord[0].st);
|
vec4 tex = texture2D(sTexture, gl_TexCoord[0].st);
|
||||||
gl_FragColor = clamp(tex, 0.0, 1.0);
|
gl_FragColor = clamp(tex, 0.0, 1.0);
|
||||||
//gl_FragColor = vec4(gl_TexCoord[0].s, gl_TexCoord[0].t, 0.0, 1.0);
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user