You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

614 lines
16 KiB

#include "OGLRadarWidget.h"
#include "../../components/object/objectLGS.h"
inline bool makeDirectVertex(uint16_t Range, uint32_t sid, float &x, float &y, float &z, float &cR, float &cG, float &cB , bool Master = true){
float cAth;
float cElevation;
float cRange;
uint16_t xx, yy;
if (Range == 0) return false;
if (Range > 4000) return false;
cRange = ((float)Range) / 4096.f;
makeXY(sid, xx, yy, Master);
make3Drad(sid, cAth, cElevation, Master);
cR = sin(cElevation);
cG = 1 - cRange;
cB = cRange;
x = cRange * cos(cAth)*cos(cElevation);
y = cRange * sin(cAth)*cos(cElevation);
z = cRange * cR;// sin(cElevation);
return true;
};
//http://plonter.co.il/detail.tmpl?sku=GA-E3800N&cart=1511874043252013&lang=heb
//https://ksp.co.il/?uin=23116
float c(unsigned char byte){
return byte / 255.0f;
}
GLRadar::GLRadar(QWidget* parent)
: m_texture(0),
QOpenGLWidget(parent),
m_mDataProtectorMutex(),
targetDisplay()
{
m_background = QColor(0,0,0);
m_fAngleX = -65.f;
m_fAngleY = 0.f;
m_fAngleZ = 0.f;
m_fScale = 1.f;
m_bShowMinimums = true;
m_bLoadProcessPercent = 0;
m_bLoadProcessShown = false;
setMinimumSize(375, 375);
setFocusPolicy(Qt::StrongFocus);
m_zOutputBufferCount = 0;
m_zOutputBufferSize = 6 * TH_SIZE_NODES * sizeof(float);
m_pOutputBuffer = (float *)malloc( m_zOutputBufferSize );
if( m_pOutputBuffer == NULL ){
m_zOutputBufferSize = 0;
};
m_bMustUpdateVBOData = false;
m_pActiveRanges = NULL;
m_nRangesCount = 0;
m_texture = NULL;
m_program = NULL;
m_vshader = NULL;
m_fshader = NULL;
};
GLRadar::~GLRadar(){
makeCurrent();
if(m_texture)delete m_texture;
if(m_program)delete m_program;
if(m_vshader)delete m_vshader;
if(m_fshader)delete m_fshader;
m_vbo.destroy();
doneCurrent();
};
void GLRadar::keyPressEvent(QKeyEvent *event){
switch (event->key()) {
case Qt::Key_Home:
m_fScale *= 1.2;
break;
case Qt::Key_End:
m_fScale *= 0.8;
break;
case Qt::Key_Up:
m_fAngleX -= 5;
break;
case Qt::Key_Right:
m_fAngleZ += 10;
break;
case Qt::Key_Down:
m_fAngleX += 5;
break;
case Qt::Key_Left:
m_fAngleZ -= 10;
break;
};
update();
};
void GLRadar::updateDisplay( void ){
};
void GLRadar::updateBackStage( void ){
if( m_pOutputBuffer == NULL ) return;
if( m_pRanges!= NULL ){
m_mDataProtectorMutex.lock();
float x, y, z, c1, c2, c3;
int index, mid, sid, cRange;
int obIndex = 0;
for ( index = 0; index < m_nRangesCount; index++ ) if((m_pRanges[index].data & 0xFF000000)>0){
if ( !m_bShowMinimums ){
cRange = (m_pRanges[index].data >> 12) & 0xFFF;
if (makeDirectVertex(cRange, index, x, y, z, c1, c2, c3)){
m_pOutputBuffer[obIndex++] = x;
m_pOutputBuffer[obIndex++] = y;
m_pOutputBuffer[obIndex++] = z;
m_pOutputBuffer[obIndex++] = c1;
m_pOutputBuffer[obIndex++] = c2;
m_pOutputBuffer[obIndex++] = c3;
};
} else {
cRange = m_pRanges[index].data & 0x0FFF;
if (makeDirectVertex(cRange, index, x, y, z, c1, c2, c3)){
m_pOutputBuffer[obIndex++] = x;
m_pOutputBuffer[obIndex++] = y;
m_pOutputBuffer[obIndex++] = z;
m_pOutputBuffer[obIndex++] = c1;
m_pOutputBuffer[obIndex++] = c2;
m_pOutputBuffer[obIndex++] = c3;
};
};
};
m_zOutputBufferCount = obIndex;
m_bMustUpdateVBOData = true;
pushDataIntoVBO();
m_mDataProtectorMutex.unlock();
};
};
/*
void GLRadar::updateRanges( uint16_t *Ranges, uint32_t count ){
if( m_pOutputBuffer == NULL ) return;
if( Ranges!= NULL ){
m_mDataProtectorMutex.lock();
float x, y, z, c1, c2, c3;
int index, mid, sid;
int obIndex = 0;
for ( index = 0; index < count; index++ ){
if (makeDirectVertex(Ranges[index], index, x, y, z, c1, c2, c3)){
m_pOutputBuffer[obIndex++] = x;
m_pOutputBuffer[obIndex++] = y;
m_pOutputBuffer[obIndex++] = z;
m_pOutputBuffer[obIndex++] = c1;
m_pOutputBuffer[obIndex++] = c2;
m_pOutputBuffer[obIndex++] = c3;
};
};
m_zOutputBufferCount = obIndex;
m_bMustUpdateVBOData = true;
pushDataIntoVBO();
m_mDataProtectorMutex.unlock();
};
};
void GLRadar::updateRanges( uint32_t *Ranges, uint32_t count ){
if( m_pOutputBuffer == NULL ) return;
if( Ranges!= NULL ){
m_mDataProtectorMutex.lock();
float x, y, z, c1, c2, c3;
int index, mid, sid, cRange;
int obIndex = 0;
for ( index = 0; index < count; index++ ) if((Ranges[index] & 0xFF000000)>0){
if ( !m_bShowMinimums ){
cRange = (Ranges[index] >> 12) & 0xFFF;
if (makeDirectVertex(cRange, index, x, y, z, c1, c2, c3)){
m_pOutputBuffer[obIndex++] = x;
m_pOutputBuffer[obIndex++] = y;
m_pOutputBuffer[obIndex++] = z;
m_pOutputBuffer[obIndex++] = c1;
m_pOutputBuffer[obIndex++] = c2;
m_pOutputBuffer[obIndex++] = c3;
};
} else {
cRange = Ranges[index] & 0x0FFF;
if (makeDirectVertex(cRange, index, x, y, z, c1, c2, c3)){
m_pOutputBuffer[obIndex++] = x;
m_pOutputBuffer[obIndex++] = y;
m_pOutputBuffer[obIndex++] = z;
m_pOutputBuffer[obIndex++] = c1;
m_pOutputBuffer[obIndex++] = c2;
m_pOutputBuffer[obIndex++] = c3;
};
};
};
m_zOutputBufferCount = obIndex;
m_bMustUpdateVBOData = true;
pushDataIntoVBO();
m_mDataProtectorMutex.unlock();
};
};
*/
void GLRadar::setScaling(int scale){
if (scale > 30)
m_fScale = 1 + qreal(scale - 30) / 30 * 0.25;
else if (scale < 30)
m_fScale = 1 - (qreal(30 - scale) / 30 * 0.25);
else
m_fScale = 1;
};
void perspective( GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar){
GLdouble xmin, xmax, ymin, ymax;
ymax = zNear * tan( fovy * M_PI / 360.0 );
ymin = -ymax;
xmin = ymin * aspect;
xmax = ymax * aspect;
glFrustum( xmin, xmax, ymin, ymax, zNear, zFar );
};
/*
void GLRadar::clearTargets(void){
//while( !m_fTargets.empty() ) m_fTargets.pop();
while( !m_fTargets.empty() ) m_fTargets.erase(m_fTargets.begin());
};
*/
void GLRadar::paintRadar(){
glPushMatrix();
{
perspective(45.f, (float)width() / (float)height(), 0.1f, 100.f);
glTranslatef( 0.0f, 0.0f, -m_fScale );
glRotatef(m_fAngleY, 0.f, 1.f, 0.f);
glRotatef(m_fAngleX, 1.f, 0.f, 0.f);
glRotatef(m_fAngleZ, 0.f, 0.f, 1.f);
if( m_fTargets.size() ){
// Выставляем цвет рисования
glColor3f(1.f, .0f, .0f);
// Выставляем размер точки
glPointSize(5);
// Рисуем точки
glBegin(GL_POINTS);
{
for( int index= 0; index < m_fTargets.size(); index++ ){
sTrg3D cTarg;
cTarg = m_fTargets.at(index);//front();
glVertex3f( cTarg.x, cTarg.y, cTarg.z );
//m_fTargets.push(cTarg);
//m_fTargets.pop();
};
}
glEnd();
};
// Centre
glColor3f(1.0f, 1.0f, 1.0f);
glPointSize(5);
glBegin(GL_POINTS);
{
glVertex3f(0.f, 0.f, 0.f);
}
glEnd();
glColor3f(.0f, 1.0f, .0f);
glPointSize(2);
glBegin(GL_LINES);
{
glVertex3f(0.f, -1.f, 0.f);
glVertex3f(0.f, 1.f, 0.f);
glVertex3f(1.f, 0.f, 0.f);
glVertex3f(-1.f, 0.f, 0.f);
glVertex3f(0.f, 0.f, 1.f);
glVertex3f(0.f, 0.f, -1.f);
/* glVertex3f(0.f, 0.f, 1.f);
glVertex3f(0.f, .2f, .8f);
glVertex3f(0.f, 0.f, 1.f);
glVertex3f(.2f, 0.f, .8f);
glVertex3f(0.f, 0.f, 1.f);
glVertex3f(0.f, -.2f, .8f);
glVertex3f(0.f, 0.f, 1.f);
glVertex3f(-.2f, 0.f, .8f);*/
}
glEnd();
}
glPopMatrix();
};
void GLRadar::initializeGL(){
initializeOpenGLFunctions();
glClearColor(m_background.redF(), m_background.greenF(), m_background.blueF(), 1.0f);
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
//m_texture = new QOpenGLTexture(QImage("qt.png"));
m_vshader = new QOpenGLShader(QOpenGLShader::Vertex);
const char *vsrc1 =
"attribute highp vec4 vertexAttr;\n"
"uniform mediump mat4 matrix;\n"
"attribute lowp vec4 colorAttr;\n"
"varying lowp vec4 color;\n"
"void main(){\n"
" gl_Position = matrix * vertexAttr;\n"
" color = colorAttr;\n"
"}\n";
m_vshader->compileSourceCode(vsrc1);
m_fshader = new QOpenGLShader(QOpenGLShader::Fragment);
const char *fsrc1 =
"varying mediump vec4 color;\n"
"void main(void)\n"
"{\n"
" gl_FragColor = color;\n"
"}\n";
m_fshader->compileSourceCode(fsrc1);
m_program = new QOpenGLShaderProgram;
m_program->addShader(m_vshader);
m_program->addShader(m_fshader);
m_program->link();
m_vertexAttr = m_program->attributeLocation("vertexAttr");
m_colorAttr = m_program->attributeLocation("colorAttr");
m_matrixUniform = m_program->uniformLocation("matrix");
m_fScale = 1;
// Use a vertex buffer object. Client-side pointers are old-school and should be avoided.
m_vbo.create();
if( m_vbo.bind() ){
if( m_pOutputBuffer!= NULL ){
m_vbo.allocate( m_pOutputBuffer, m_zOutputBufferSize );
};
m_vbo.release();
};
//createGeometry();
//loadFromFile("test.raw");
//loadFromFile("data/memdump_006-00.raw");
//pushDataIntoVBO();
};
void GLRadar::pushDataIntoVBO( void ){
if( m_vbo.bind() ){
if( m_pOutputBuffer != NULL ){
//m_vbo.allocate( m_pOutputBuffer, m_zOutputBufferCount*sizeof(float)); //buf.count() * sizeof(GLfloat));
m_vbo.write( 0, m_pOutputBuffer, m_zOutputBufferCount*sizeof(float) );
};
m_vbo.release();
};
};
void GLRadar::paintGL(){
QPainter painter;
painter.begin(this);
painter.beginNativePainting();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glFrontFace(GL_CW);
glCullFace(GL_FRONT);
glPushMatrix();
{
QMatrix4x4 modelview;
//modelview.scale(m_fScale);
modelview.perspective(45.f, (float)width() / (float)height(), 0.1f, 100.f);
modelview.translate(0.0f, 0.0f, -m_fScale);
modelview.rotate(m_fAngleY, .0f, 1.f, .0f);
modelview.rotate(m_fAngleX, 1.f, .0f, .0f);
modelview.rotate(m_fAngleZ, .0f, .0f, 1.f);
//modelview.scale(m_fScale);
//modelview.translate(.0f, .0f, .0f );
//modelview.ortho(-2.0f, 2.0f, -2.0f, 2.0f, 2.0f, -2.0f);
//modelview.ortho(-2.0f, 2.0f, -2.0f, 2.0f, m_fScale*2, -m_fScale*2);
//modelview.translate(0.0f, 0.0f, -1.0f);
m_mDataProtectorMutex.lock();
m_program->bind();
m_program->setUniformValue(m_matrixUniform, modelview);
//-----
m_program->enableAttributeArray(m_vertexAttr);
m_program->enableAttributeArray(m_colorAttr);
m_vbo.bind();
// The data in the buffer is placed like this:
// vertex1.x, vertex1.y, vertex1.z, normal1.x, normal1.y, normal1.z, vertex2.x, ...
m_program->setAttributeBuffer(m_vertexAttr, GL_FLOAT, 0, 3, 6 * sizeof(GLfloat));
m_program->setAttributeBuffer(m_colorAttr, GL_FLOAT, 3 * sizeof(GLfloat), 3, 6 * sizeof(GLfloat));
m_vbo.release();
glColor3f(1.0f, .0f, 0.0f);
glPointSize(1);
//glDrawArrays(GL_TRIANGLES, 0, m_zOutputBufferCount / 6 );
//glDrawArrays(GL_LINE_LOOP, 0, m_zOutputBufferCount / 6);
glDrawArrays(GL_POINTS, 0, m_zOutputBufferCount / 6);
m_program->disableAttributeArray(m_colorAttr);
m_program->disableAttributeArray(m_vertexAttr);
//-------
m_program->release();
m_mDataProtectorMutex.unlock();
paintRadar();
};
glPopMatrix();
glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
painter.endNativePainting();
QString framesPerSecond;
painter.setPen(Qt::white);
framesPerSecond.setNum(m_fAngleX, 'f', 2);
painter.drawText(20, 20, framesPerSecond + " X Angle");
framesPerSecond.setNum(m_fAngleY, 'f', 2);
painter.drawText(20, 35, framesPerSecond + " Y Angle");
framesPerSecond.setNum(m_fAngleZ, 'f', 2);
painter.drawText(20, 50, framesPerSecond + " Z Angle");
if( m_bLoadProcessShown ){
painter.setPen(Qt::yellow);
painter.drawRect( 10, height()-20, width()-20, 10 );
painter.fillRect( 13, height()-17, (int)(((((float)width()-26.f) / 100.f ) *(float)m_bLoadProcessPercent)), 4 , QColor(255, 255, 0, 255) );
};
/*if (const int elapsed = m_time.elapsed()) {
QString framesPerSecond;
framesPerSecond.setNum(m_frames /(elapsed / 1000.0), 'f', 2);
painter.setPen(m_transparent ? Qt::black : Qt::white);
painter.drawText(20, 40, framesPerSecond + " paintGL calls / s");
}*/
painter.end();
// When requested, follow the ideal way to animate: Rely on
// blocking swap and just schedule updates continuously.
//if (!m_mainWindow->timerEnabled())
if( m_bMustUpdateVBOData ) pushDataIntoVBO();
update();
};
/*
void GLRadar::showLoadProcess( int p ){
if( p < 0 ){
m_bLoadProcessPercent = 0;
m_bLoadProcessShown = false;
} else {
if( p > 100 ) p = 100;
m_bLoadProcessPercent = p;
m_bLoadProcessShown = true;
};
};
*/
inline uint32_t L2B( uint32_t A ){
return ( (( A & 0x000000ff) << 24 ) | (( A & 0x0000ff00) << 8 ) | (( A & 0x00ff0000) >> 8 ) | (( A & 0xff000000) >> 24 ) );
};
inline uint16_t L2B( uint16_t A ){
return ( (( A & 0x00ff) << 8 ) | (( A & 0xff00) >> 8 ) );
};
void GLRadar::createGeometry(){
m_mDataProtectorMutex.lock();
float x, y, z, c1, c2, c3;
int index, mid, sid;
int obIndex = 0;
for (index = 0; index < 288*8*96; index ++ ){
if (makeDirectVertex(3500, index, x, y, z, c1, c2, c3 )){
m_pOutputBuffer[obIndex++] = x;
m_pOutputBuffer[obIndex++] = y;
m_pOutputBuffer[obIndex++] = z;
m_pOutputBuffer[obIndex++] = c1;
m_pOutputBuffer[obIndex++] = c2;
m_pOutputBuffer[obIndex++] = c3;
};
};
m_zOutputBufferCount = obIndex;
m_mDataProtectorMutex.unlock();
};
void GLRadar::resizeGL(int w, int h){
//if (w > h) setFixedSize(h, h);
//else setFixedSize(w, w);
//if (m_hasButton) {
// if (!m_btn) {
// m_btn = new QPushButton("Refresh", this);
// connect(m_btn, &QPushButton::clicked, this, &GLRadar::handleButtonPress);
// }
// m_btn->move(20, 80);
//}
};
/*
QSize GLRadar::sizeHint() const {
QSize s = size();
lastHeight = s.height();
s.setWidth((s.height() * 16) / 9);
s.setHeight(QGroupBox::sizeHint().height());
return s;
}
void GLRadar::resizeEvent(QResizeEvent * event) {
QGroupBox::resizeEvent(event);
if (lastHeight != height()) {
updateGeometry(); // it is possible that this call should be scheduled to next iteration of event loop
}
}
*/
void GLRadar::handleButtonPress(){
m_pDeviceControl->startGetScanData();
};
/*
bool GLRadar::loadFromFile(char *fName){
FILE *fd;
bool RetVal;
RetVal = false;
fd = fopen(fName, "rb");
if (fd != NULL){
fread(&m_nRangesCount, sizeof(uint32_t), 1, fd);
if (m_pRanges != NULL) free(m_pRanges);
m_pRanges = (thresholdNode *)malloc(sizeof(thresholdNode)*m_nRangesCount);
if (m_pRanges != NULL){
//int overall = 0;
//while ( ( overall < m_nRangesCount ) && !eof( fd ) )
int RetVal = fread(m_pRanges, 4, m_nRangesCount, fd);
qDebug() << "Read: " << RetVal << " Bytes.";
updateRanges( m_pRanges, m_nRangesCount );
RetVal = true;
};
free(m_pRanges);
m_pRanges = NULL;
fclose(fd);
};
return RetVal;
};
bool GLRadar::saveToFile(char *fName){
FILE *fd;
bool RetVal;
RetVal = false;
//fd = fopen(fName, "w+");
//if( fd != NULL ){
// fwrite(&m_nRangesCount, sizeof(uint32_t), 1, fd );
// fwrite(m_pRanges, sizeof(uint16_t), m_nRangesCount, fd);
// fclose(fd);
// RetVal = true;
//};
return RetVal;
};
*/
void GLRadar::wheelEvent(QWheelEvent *event){
//int numDegrees = event->delta() / 8;
//int numSteps = numDegrees / 15;
if (event->orientation() == Qt::Horizontal) {
} else {
if (event->delta() > 0){
m_fScale *= 1.2;
} else {
m_fScale *= 0.8;
};
};
update();
event->accept();
};
void GLRadar::mouseMoveEvent(QMouseEvent *event){
int dx = event->x() - m_slastPos.x();
int dy = event->y() - m_slastPos.y();
if (event->buttons() & Qt::LeftButton) {
if (dx > 2){ m_fAngleZ -= 5; };
if (dx < -2){ m_fAngleZ += 5; };
if (dy > 2){ m_fAngleX -= 5; };
if (dy < -2){ m_fAngleX += 5; };
} else if (event->buttons() & Qt::RightButton) {
}
m_slastPos = event->pos();
update();
event->accept();
}
/*
void GLRadar::addTarget( uint32_t Id, uint32_t Range, bool isMaster ){
sTrg3D tVal;
float R, A, E;
R = (float) Range / 4095.f;
make3Drad( Id, A, E, isMaster );
tVal.x = R * cos(A)*cos(E);
tVal.y = R * sin(A)*cos(E);
tVal.z = R * sin(E);
//m_fTargets.push(tVal);
//if( m_fTargets.size() > 128 ) m_fTargets.pop();
m_fTargets.push_back(tVal);
if( m_fTargets.size() > 128 ) m_fTargets.erase(m_fTargets.begin());
};
*/