Пытаюсь рисовать множество точек. Но проблема в том, что я не знаю как отрисовать точки так, чтобы в начальный момент времени они все попадали на экран. Ещё проблема с приближением и вращением, не пойму как это сделать относительно центра ограничивающего параллелепипеда или экрана. Прошу вашей помощи.
BoundingBox.h
#include "vector3d.h"
class BoundingBox
{
public:
BoundingBox() : valid(false) {}
void add(Vector3D aPoint)
{
if (valid)
{
if (aPoint.x < minCorner.x)
minCorner.x = aPoint.x;
else if (aPoint.x > maxCorner.x)
maxCorner.x = aPoint.x;
if (aPoint.y < minCorner.y)
minCorner.y = aPoint.y;
else if (aPoint.y > maxCorner.y)
maxCorner.y = aPoint.y;
if (aPoint.z < minCorner.z)
minCorner.z = aPoint.z;
else if (aPoint.z > maxCorner.z)
maxCorner.z = aPoint.z;
}
else
{
maxCorner = minCorner = aPoint;
valid = true;
}
}
void clear()
{
valid = false;
minCorner = maxCorner = Vector3D(0,0,0);
}
Vector3D getCenter() { return Vector3D((minCorner.x + maxCorner.x) * 0.5, (minCorner.y + maxCorner.y) * 0.5, (minCorner.z + maxCorner.z) * 0.5); }
public:
Vector3D minCorner;
Vector3D maxCorner;
bool valid;
};
Vector3D.h
class Vector3D
{
public:
Vector3D(double x1 = 0, double y1 = 0, double z1 = 0) : x(x1), y(y1), z(z1) {}
double x, y, z;
};
glwidget.h
#include <QGLWidget>
#include <vector>
#include "vector3d.h"
class GLWidget : public QGLWidget
{
public:
GLWidget(QWidget* parent = NULL);
~GLWidget() {}
protected:
void initializeGL();
void paintGL();
void resizeGL( int w, int h );
public slots:
void mousePressEvent( QMouseEvent* event );
void mouseMoveEvent( QMouseEvent* event );
void wheelEvent(QWheelEvent* pe) ;
private:
GLfloat xRot, yRot, zRot, scale, xTrans, yTrans, zTrans;
QPoint lastPos;
std::vector<Vector3D> m_points;
};
glwidget.cpp
#include "glwidget.h"
#include "boundingbox.h"
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <QMouseEvent>
double random(double min, double max)
{
return (double)(rand())/RAND_MAX*(max - min) + min;
}
GLWidget::GLWidget(QWidget *parent) : QGLWidget(parent)
{
std::srand(std::time(NULL));
for(int i = 0; i < 1024; i++)
{
Vector3D V;
V.x = random(1000.0,10000.0);
V.y = random(1000.0,10000.0);
V.z = random(1000.0,10000.0);
m_points.push_back(V);
}
xRot = yRot = zRot = xTrans = yTrans = zTrans = 0.0;
scale = 1.0;
}
void GLWidget::initializeGL()
{
glClearColor( 0.0, 0.0, 0.0, 0.0);
glShadeModel( GL_SMOOTH );
glEnable(GL_DEPTH_TEST);
}
void GLWidget::paintGL()
{
BoundingBox bbox;
for(int i = 0; i < m_points.size(); i++)
bbox.add(m_points[i]);
Vector3D boxC = bbox.getCenter();
glClearColor(0, 0, 0, 1);
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
double dL = 100;
glOrtho(bbox.minCorner.x - dL, bbox.maxCorner.x + dL,
bbox.minCorner.y - dL, bbox.maxCorner.y + dL,
bbox.minCorner.z - dL, bbox.maxCorner.z + dL);
glScalef( scale, scale, scale );
glTranslatef(-boxC.x, 0, 0);
glTranslatef(0, -boxC.y, 0);
glTranslatef(0, 0, -boxC.z);
glRotatef( xRot, 1.0, 0.0, 0.0 );
glRotatef( yRot, 0.0, 1.0, 0.0 );
glRotatef( zRot, 0.0, 0.0, 1.0 );
glTranslatef( -xTrans, 0.0, 0.0 );
glTranslatef( 0.0, yTrans, 0.0 );
glTranslatef( 0.0, 0.0, zTrans );
glBegin(GL_POINTS);
glColor3f(1,1,1);
for(unsigned i = 0; i < m_points.size(); i++)
{
Vector3D P = m_points[i];
glVertex3d(P.x, P.y, P.z);
}
glEnd();
}
void GLWidget::resizeGL(int w, int h)
{
glViewport( 0, 0, (GLint)w, (GLint)h );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 15.0 );
glMatrixMode( GL_MODELVIEW );
}
void GLWidget::mousePressEvent(QMouseEvent *event)
{
lastPos = event->pos();
updateGL();
}
void GLWidget::mouseMoveEvent( QMouseEvent* event )
{
float dx = (event->pos().x()-lastPos.x());
float dy = (event->pos().y()-lastPos.y());
if(event->buttons() == Qt::LeftButton)
{
xRot += dy;
yRot += dx;
}
else if(event->buttons() == Qt::RightButton)
{
xTrans -= dx/(1.0);
yTrans -= dy/(1.0);
}
lastPos = event->pos();
updateGL();
}
void GLWidget::wheelEvent(QWheelEvent* pe)
{
if ((pe->delta())>0)
scale*=1.1;
else if ((pe->delta())<0)
scale/=1.1;
updateGL();
}