Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
#include "Cross.h"
#include <osg/Geometry>
#include <osg/Array>
#include <osgText/Text>
Cross::Cross(float cross_len) {
geom = new osg::Geometry;
this->addDrawable(geom);
this->vertices = new osg::Vec3Array;
this->vertices->push_back(osg::Vec3(-cross_len, 0.0, 0.0));
this->vertices->push_back(osg::Vec3(cross_len, 0.0, 0.0));
this->vertices->push_back(osg::Vec3(0.0, -cross_len, 0.0));
this->vertices->push_back(osg::Vec3(0.0, cross_len, 0.0));
this->vertices->push_back(osg::Vec3(0.0, 0.0, -cross_len));
this->vertices->push_back(osg::Vec3(0.0, 0.0, cross_len));
this->geom->setVertexArray(this->vertices);
lines = new osg::DrawElementsUInt(osg::PrimitiveSet::LINES, 0);
lines->push_back(5);
lines->push_back(4);
lines->push_back(3);
lines->push_back(2);
lines->push_back(1);
lines->push_back(0);
geom->addPrimitiveSet(lines);
}
void Cross::setVertexLabel(int id, std::string label) {
osg::Vec3 vec = this->vertices[0][id];
osgText::TextBase::AxisAlignment plane;
osgText::TextBase::AlignmentType alignment;
for(int i=0; i < 3; i++) {
if(vec[i] == 0) {
continue;
}
if(vec[i] < 0) {
alignment = osgText::Text::RIGHT_CENTER;
}
else {
alignment = osgText::Text::LEFT_CENTER;
}
}
plane = osgText::Text::XY_PLANE;
osg::Geode *v_geode = new osg::Geode;
osgText::Text *v_text = new osgText::Text;
v_geode->addDrawable(v_text);
this->addDrawable(v_text);
v_text->setPosition(vec);
v_text->setText(label);
v_text->setCharacterSize(10);
v_text->setDrawMode(osgText::Text::TEXT |
osgText::Text::ALIGNMENT |
osgText::Text::BOUNDINGBOX);
v_text->setAlignment(alignment);
v_text->setAxisAlignment(plane);
}
#ifndef _CROSS_H_
#define _CROSS_H_
#include <osg/Geode>
#include <osg/Geometry>
#include <string>
class Cross : public osg::Geode {
private:
osg::Geometry *geom;
osg::Vec3Array *vertices;
osg::DrawElementsUInt *lines;
public:
Cross(float cross_len);
void setVertexLabel(int id, std::string label);
};
#endif
#include "HUD.h"
#include <string>
HUD::HUD() {
this->hud_geode = new osg::Geode;
this->hud_text = new osgText::Text;
this->hud_view_m = new osg::MatrixTransform();
this->setMatrix(osg::Matrix::ortho2D(0,1024,0,768));
this->hud_view_m->setMatrix(osg::Matrix::identity());
this->hud_view_m->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
this->hud_view_m->addChild(hud_geode);
this->addChild(hud_view_m);
this->hud_text->setCharacterSize(10);
//this->hud_text->setFont("C:/WINDOWS/Fonts/impact.ttf");
this->hud_text->setAxisAlignment(osgText::Text::SCREEN);
this->hud_text->setPosition( osg::Vec3(0,750, -1) );
this->hud_geode->addDrawable(hud_text);
}
void HUD::setStatus(std::string text) {
this->hud_text->setText(text);
}
#ifndef _HUD_H
#define _HUD_H_
#include <osg/Projection>
#include <osg/MatrixTransform>
#include <osgText/Text>
#include <osg/Geode>
#include <string>
class HUD : public osg::Projection {
private:
osg::Geode *hud_geode;
osg::MatrixTransform *hud_view_m;
osgText::Text *hud_text;
public:
HUD();
void setStatus(std::string text);
};
#endif
SRC=Simple.cpp Cross.cpp HUD.cpp
CXXFLAGS=-DLINUX
INCLUDE = -I. -I/usr/include/ -I/usr/include/X11/ -I/usr/local/include/GL
LDLIBS = -lm -ldl -lGL -lGLU -lpthread -lXext -lX11 -losg -losgViewer -losgSim -losgGA -losgText
LDFLAGS = -L. -L/usr/lib -L/usr/X11R6/lib -L/usr/local/lib
COBJS=$(patsubst %.cpp,%.o,$(SRC))
CC=g++
EXE=simple
all: $(EXE)
$(EXE): $(COBJS)
$(CC) -o $(EXE) $(COBJS) $(INCLUDE) $(LDFLAGS) $(LDLIBS)
$(COBJS): %.o : %.cpp
$(CC) -c $(CXXFLAGS) -o $@ $<
.PHONY: clean
clean:
rm -rf $(EXE) $(COBJS)
#include <osgViewer/Viewer>
#include <osgGA/TrackballManipulator>
#include <osg/Math>
#include <iostream>
#include <iomanip>
#include <sstream>
#include "Cross.h"
#include "HUD.h"
//#define S std::setw(4)
#define S '\t'
#define DEBUG_MATD(matd, s) s.fill(' '); s \
<< S << matd(0,0) << S << ' ' << S << matd(0, 1) << S << ' ' << S << matd(0, 2) << S << ' ' << S << matd(0, 3) << '\n' \
<< S << matd(1,0) << S << ' ' << S << matd(1, 1) << S << ' ' << S << matd(1, 2) << S << ' ' << S << matd(1, 3) << '\n' \
<< S << matd(2,0) << S << ' ' << S << matd(2, 1) << S << ' ' << S << matd(2, 2) << S << ' ' << S << matd(2, 3) << '\n' \
<< S << matd(3,0) << S << ' ' << S << matd(3, 1) << S << ' ' << S << matd(3, 2) << S << ' ' << S << matd(3, 3) << '\n'
std::string getMatrixRepresentation(osg::Matrixd &m) {
std::stringstream s;
s.fill(' ');
s.setf(std::ios::right, std::ios::adjustfield);
for(int i=0; i < 4; i++) {
for(int j=0; j < 4; j++) {
s << std::setw(10) << m(i, j);
}
s << '\n';
}
return s.str();
}
int main(void) {
osgViewer::Viewer viewer;
osg::Group *root = new osg::Group();
viewer.setSceneData(root);
osgGA::TrackballManipulator *cameraManipulator = new osgGA::TrackballManipulator();
viewer.setCameraManipulator(cameraManipulator);
viewer.realize();
osg::Matrixd camera_pos(
1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 500.0, 1.0 //position along x,y,z-axes (panning)
);
#define RAD 1
#define DEG 45.0
#if RAD
float q_sin = sinf(osg::DegreesToRadians(DEG));
float q_cos = cosf(osg::DegreesToRadians(DEG));
#else
float q_sin = sinf(DEG);
float q_cos = cosf(DEG);
#endif
osg::Matrixd rotate_x(
1.0, 0.0, 0.0, 0.0,
0.0, q_cos, -q_sin, 0.0,
0.0, q_sin, q_cos, 0.0,
0.0, 0.0, 0.0, 1.0
);
osg::Matrixd rotate_y(
q_cos, 0.0, q_sin, 0.0,
0.0, 1.0, 0.0, 0.0,
-q_sin, 0.0, q_cos, 0.0,
0.0, 0.0, 0.0, 1.0
);
osg::Matrixd rotate_z(
q_cos, -q_sin, 0.0, 0.0, // 90 deg rotation to the left
q_sin, q_cos, 0.0, 0.0, // along the z axis (roll). reason: sin(90°) = 1
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0
);
camera_pos = camera_pos * rotate_x;
camera_pos = camera_pos * rotate_y;
//camera_pos = camera_pos * rotate_z; // we do not want to rotate along the
//z-axis
osg::Matrixd correction(
cosf(90), 0.0, sinf(90), 0.0,
0.0, 1.0, 0.0, 0.0,
-sinf(90), 0.0, cosf(90), 0.0,
0.0, 0.0, 0.0, 1.0
);
//camera_pos = camera_pos * correction;
cameraManipulator->setByMatrix(camera_pos);
Cross *crossGeode = new Cross(100.0);
crossGeode->setVertexLabel(0, "-X");
crossGeode->setVertexLabel(1, "X");
crossGeode->setVertexLabel(2, "-Y");
crossGeode->setVertexLabel(3, "Y");
crossGeode->setVertexLabel(4, "-Z");
crossGeode->setVertexLabel(5, "Z");
/*
osg::MatrixTransform *cross_pos = new osg::MatrixTransform;
cross_pos->addChild(crossGeode);
osg::Matrixd *cross_matrix = new osg::Matrixd(
);
cross_pos->setMatrix(*cross_matrix);
*/
//root->addChild(cross_pos);
root->addChild(crossGeode);
HUD *hudGeode = new HUD();
root->addChild(hudGeode);
#define ANIMATE 0
#define DEG_DELTA 0.1
#if ANIMATE
float dir=DEG_DELTA;
float deg=0;
q_cos = cosf(osg::DegreesToRadians(DEG_DELTA));
q_sin = sinf(osg::DegreesToRadians(DEG_DELTA));
osg::Matrixd rotate(
q_cos, 0.0, q_sin, 0.0,
0.0, 1.0, 0.0, 0.0,
-q_sin, 0.0, q_cos, 0.0,
0.0, 0.0, 0.0, 1.0
);
#endif
osg::Matrixd camM;
while(!viewer.done()) {
#if ANIMATE
camM = cameraManipulator->getMatrix();
//viewer.getCamera()->setViewMatrix(rotate);
cameraManipulator->setByMatrix(camM * rotate);
std::stringstream oss;
deg = (camM.getRotate().y());
deg = osg::RadiansToDegrees(camM.getRotate().y());
oss << getMatrixRepresentation(camM) << " DEG: " << deg;
hudGeode->setStatus(oss.str());
#else
camM = cameraManipulator->getMatrix();
hudGeode->setStatus(getMatrixRepresentation(camM));
#endif
viewer.frame();
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.