ros node comined with opengl
Hi,
i have a problem with combining both.
my code is:
#include "ros/ros.h"
#include "sensor_msgs/LaserScan.h"
#include "laser_geometry/laser_geometry.h"
#include "tf/transform_listener.h"
#include <iostream>
#include <GL/glut.h>
using namespace std;
int camPosY=10;
namespace gui
{
class gui{
public:
gui();
void scanCallback(const sensor_msgs::LaserScan::ConstPtr& scan);
private:
ros::NodeHandle node;
laser_geometry::LaserProjection projector_;
tf::TransformListener tfListener_;
ros::Subscriber scan_sub;
};
gui::gui(){
scan_sub=node.subscribe("/scan", 100, &gui::scanCallback, this);
}
void gui::scanCallback(const sensor_msgs::LaserScan::ConstPtr& scan){
ROS_INFO("hi");
sensor_msgs::PointCloud cloud;
projector_.transformLaserScanToPointCloud("laser", *scan, cloud, tfListener_);
for(unsigned int i=0;i<cloud.points.size();i++){
cout<<cloud.points[i].x<<endl;
}
}
};
void display(void)
{
glLoadIdentity();
gluLookAt(0,camPosY,-10,0,camPosY,0,0,1,0);
glClear( GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 1.0, 0.0);
glBegin(GL_POLYGON);
glVertex3f(-2.0, -4.0, 0.0);
glVertex3f(2.0, -4.0, 0.0);
glVertex3f(2.0, 4.0, 0.0);
glVertex3f(-2.0, 4.0, 0.0);
glEnd();
glFlush();
}
void keyboard(int key,int x,int y){
if(key == GLUT_KEY_DOWN)camPosY-=1;
if(key == GLUT_KEY_UP)camPosY+=1;
glutPostRedisplay();
}
int main(int argc, char ** argv)
{
ros::init(argc, argv, "GUI");
gui::gui gui;
glutInit(&argc, argv);
glutInitDisplayMode ( GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowPosition(100,100);
glutInitWindowSize(800,600);
glutCreateWindow ("Roboter");
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60,1,0.1,1000);
glMatrixMode(GL_MODELVIEW);
gluLookAt(0,10,-10,0,0,0,0,1,0);
glutDisplayFunc(display);
glutSpecialFunc(keyboard);
glutMainLoop();
return 0;
}
here i want to show the scan data later but how can i show the display and updating it and use ros::spin()? Do i need a second thread only for ros::spin()?
thx
Please format your code properly. Mark the code and press Ctrl-K.