Picking
This commit is contained in:
parent
f88bc07e4a
commit
64fa8ce59f
@ -27,6 +27,8 @@
|
|||||||
|
|
||||||
#include <QGLPixelBuffer>
|
#include <QGLPixelBuffer>
|
||||||
|
|
||||||
|
#include <QDebug>
|
||||||
|
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
@ -51,7 +53,7 @@ namespace toruschess {
|
|||||||
m_textureBuffer(0), m_textureID(0),
|
m_textureBuffer(0), m_textureID(0),
|
||||||
m_camDist(5) ,m_camRotX(45), m_camRotY(0), m_camRotZ(0),
|
m_camDist(5) ,m_camRotX(45), m_camRotY(0), m_camRotZ(0),
|
||||||
m_originX(0), m_originY(0), m_mouseLastX(0), m_mouseLastY(0),
|
m_originX(0), m_originY(0), m_mouseLastX(0), m_mouseLastY(0),
|
||||||
m_skySphere(0), m_skyTextureID(0),
|
m_skySphere(0), m_skyTextureID(0), m_skyTimerID(0), m_skyOffset(0),
|
||||||
m_pickTextureID(0) {
|
m_pickTextureID(0) {
|
||||||
qDebug("Field3d::Field3D");
|
qDebug("Field3d::Field3D");
|
||||||
for (int x = 0; x < 8; x++) for (int y = 0; y < 8; y++) m_marked[x][y] = false;
|
for (int x = 0; x < 8; x++) for (int y = 0; y < 8; y++) m_marked[x][y] = false;
|
||||||
@ -108,6 +110,8 @@ namespace toruschess {
|
|||||||
connect(m_game, SIGNAL(updated()), this, SLOT(fieldUpdated()));
|
connect(m_game, SIGNAL(updated()), this, SLOT(fieldUpdated()));
|
||||||
|
|
||||||
m_pickBuffer = new QGLPixelBuffer(QSize(1024, 1024));
|
m_pickBuffer = new QGLPixelBuffer(QSize(1024, 1024));
|
||||||
|
|
||||||
|
m_skyTimerID = startTimer(20);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Field3D::resizeGL(int w, int h) {
|
void Field3D::resizeGL(int w, int h) {
|
||||||
@ -130,7 +134,12 @@ namespace toruschess {
|
|||||||
glDisable(GL_FOG);
|
glDisable(GL_FOG);
|
||||||
glDisable(GL_LIGHTING);
|
glDisable(GL_LIGHTING);
|
||||||
glBindTexture(GL_TEXTURE_2D, m_skyTextureID);
|
glBindTexture(GL_TEXTURE_2D, m_skyTextureID);
|
||||||
|
glMatrixMode(GL_TEXTURE);
|
||||||
|
glPushMatrix();
|
||||||
|
glTranslatef(m_skyOffset, 0, 0);
|
||||||
gluSphere(m_skySphere, 256, 64, 64);
|
gluSphere(m_skySphere, 256, 64, 64);
|
||||||
|
glMatrixMode(GL_TEXTURE);
|
||||||
|
glPopMatrix();
|
||||||
glEnable(GL_LIGHTING);
|
glEnable(GL_LIGHTING);
|
||||||
|
|
||||||
double ox = 2.0*(m_originX/(double)m_textureSize), oy = 2.0*(m_originY/(double)m_textureSize);
|
double ox = 2.0*(m_originX/(double)m_textureSize), oy = 2.0*(m_originY/(double)m_textureSize);
|
||||||
@ -163,6 +172,7 @@ namespace toruschess {
|
|||||||
const int repeat = 10;
|
const int repeat = 10;
|
||||||
double ox = -2.0*(m_originX/(double)m_textureSize), oy = 2.0*(m_originY/(double)m_textureSize);
|
double ox = -2.0*(m_originX/(double)m_textureSize), oy = 2.0*(m_originY/(double)m_textureSize);
|
||||||
glEnable(GL_FOG);
|
glEnable(GL_FOG);
|
||||||
|
glEnable(GL_LIGHTING);
|
||||||
glBindTexture(GL_TEXTURE_2D, m_textureID);
|
glBindTexture(GL_TEXTURE_2D, m_textureID);
|
||||||
glBegin(GL_QUADS);
|
glBegin(GL_QUADS);
|
||||||
glTexCoord2f(-repeat+ox, repeat+oy); glVertex3f(-10*repeat, 0, -10*repeat);
|
glTexCoord2f(-repeat+ox, repeat+oy); glVertex3f(-10*repeat, 0, -10*repeat);
|
||||||
@ -175,28 +185,12 @@ namespace toruschess {
|
|||||||
glBindTexture(GL_TEXTURE_2D, 0);
|
glBindTexture(GL_TEXTURE_2D, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
Pos Field3D::findPos(int mx, int my) {
|
bool Field3D::findPos(int mx, int my, Pos &p) {
|
||||||
m_pickBuffer->makeCurrent();
|
glDisable(GL_FOG);
|
||||||
|
glDisable(GL_LIGHTING);
|
||||||
glClearColor(0.0, 0.0, 0.0, 0.0);
|
|
||||||
glEnable(GL_DEPTH_TEST);
|
|
||||||
|
|
||||||
glViewport(0, 0, 1024, 1024);
|
|
||||||
glMatrixMode(GL_PROJECTION);
|
|
||||||
glLoadIdentity();
|
|
||||||
gluPerspective(90.0, double(width())/height(), 0.01, 1000);
|
|
||||||
|
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
glEnable(GL_TEXTURE_2D);
|
glEnable(GL_TEXTURE_2D);
|
||||||
|
|
||||||
glMatrixMode(GL_MODELVIEW);
|
|
||||||
glLoadIdentity();
|
|
||||||
|
|
||||||
glTranslatef(0, 0, -m_camDist);
|
|
||||||
glRotatef(m_camRotX,1,0,0);
|
|
||||||
glRotatef(m_camRotY,0,1,0);
|
|
||||||
glRotatef(m_camRotZ,0,0,1);
|
|
||||||
|
|
||||||
if (m_boardMode == TORUS) {
|
if (m_boardMode == TORUS) {
|
||||||
double ox = 2.0*(m_originX/(double)m_textureSize), oy = 2.0*(m_originY/(double)m_textureSize);
|
double ox = 2.0*(m_originX/(double)m_textureSize), oy = 2.0*(m_originY/(double)m_textureSize);
|
||||||
glBindTexture(GL_TEXTURE_2D, m_pickTextureID);
|
glBindTexture(GL_TEXTURE_2D, m_pickTextureID);
|
||||||
@ -238,11 +232,12 @@ namespace toruschess {
|
|||||||
glDisable(GL_TEXTURE_2D);
|
glDisable(GL_TEXTURE_2D);
|
||||||
glBindTexture(GL_TEXTURE_2D, 0);
|
glBindTexture(GL_TEXTURE_2D, 0);
|
||||||
|
|
||||||
m_pickBuffer->doneCurrent();
|
unsigned char pxbuf[3];
|
||||||
QImage i = m_pickBuffer->toImage();
|
glReadPixels(mx, height() - 1 - my, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, &pxbuf);
|
||||||
QRgb col = i.pixel((1024*mx)/width(), (1024*my)/height());
|
paintGL();
|
||||||
qDebug << QColor(col);
|
qDebug() << pxbuf[0] << pxbuf[1];
|
||||||
return Pos(qRed(col), qGreen(col));
|
p = Pos(pxbuf[0] - 50, pxbuf[1] - 50);
|
||||||
|
return pxbuf[0] >= 50;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -275,9 +270,10 @@ namespace toruschess {
|
|||||||
for (int x = 0; x < 8; x++)
|
for (int x = 0; x < 8; x++)
|
||||||
for (int y = 0; y < 8; y++) {
|
for (int y = 0; y < 8; y++) {
|
||||||
QRect prect(x * pieceSize, y * pieceSize, pieceSize - 1, pieceSize - 1);
|
QRect prect(x * pieceSize, y * pieceSize, pieceSize - 1, pieceSize - 1);
|
||||||
pt.fillRect(prect, QBrush(QColor(x,y,0)));
|
pt.fillRect(prect, QBrush(QColor(x+50,y+50,50)));
|
||||||
}
|
}
|
||||||
m_pickTextureID = bindTexture(*pickimage);
|
m_pickTextureID = bindTexture(*pickimage);
|
||||||
|
// delete pickimage;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* glBindTexture(GL_TEXTURE_2D, m_textureID);
|
/* glBindTexture(GL_TEXTURE_2D, m_textureID);
|
||||||
@ -346,7 +342,8 @@ namespace toruschess {
|
|||||||
void Field3D::mousePressEvent(QMouseEvent *event) {
|
void Field3D::mousePressEvent(QMouseEvent *event) {
|
||||||
if (event->button() == Qt::LeftButton) {
|
if (event->button() == Qt::LeftButton) {
|
||||||
QList<Move> moves;
|
QList<Move> moves;
|
||||||
Pos p = findPos(event->x(), event->y());
|
Pos p;
|
||||||
|
if (findPos(event->x(), event->y(), p))
|
||||||
moves = m_game->field()->validMoves(p);
|
moves = m_game->field()->validMoves(p);
|
||||||
markMoves(moves);
|
markMoves(moves);
|
||||||
updateTexture();
|
updateTexture();
|
||||||
@ -375,13 +372,15 @@ namespace toruschess {
|
|||||||
m_mouseLastX = event->x();
|
m_mouseLastX = event->x();
|
||||||
m_mouseLastY = event->y();
|
m_mouseLastY = event->y();
|
||||||
if (event->button() == Qt::LeftButton) {
|
if (event->button() == Qt::LeftButton) {
|
||||||
/* if (m_markedMoves.size() == 0) return;
|
if (m_markedMoves.size() == 0) return;
|
||||||
Pos from = m_markedMoves[0].from();
|
Pos from = m_markedMoves[0].from();
|
||||||
QList<Move> moves;
|
QList<Move> moves;
|
||||||
markMoves(QList<Move>());
|
markMoves(QList<Move>());
|
||||||
Pos p = findPos(event->x(), event->y());
|
Pos p;
|
||||||
|
if (findPos(event->x(), event->y(), p)) {
|
||||||
Move m(m_game->field(), from, p);
|
Move m(m_game->field(), from, p);
|
||||||
m_game->move(m);*/
|
m_game->move(m);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -401,4 +400,11 @@ namespace toruschess {
|
|||||||
updateTexture();
|
updateTexture();
|
||||||
updateGL();
|
updateGL();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Field3D::timerEvent(QTimerEvent *) {
|
||||||
|
m_skyOffset = rotate<double>(m_skyOffset + 0.001, 1);
|
||||||
|
if (m_boardMode == TORUS)
|
||||||
|
updateGL();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -60,6 +60,8 @@ namespace toruschess {
|
|||||||
virtual void mouseReleaseEvent(QMouseEvent *event);
|
virtual void mouseReleaseEvent(QMouseEvent *event);
|
||||||
virtual void wheelEvent(QWheelEvent *event);
|
virtual void wheelEvent(QWheelEvent *event);
|
||||||
|
|
||||||
|
virtual void timerEvent(QTimerEvent *event);
|
||||||
|
|
||||||
protected slots:
|
protected slots:
|
||||||
void fieldUpdated();
|
void fieldUpdated();
|
||||||
|
|
||||||
@ -70,7 +72,7 @@ namespace toruschess {
|
|||||||
void freeTexture();
|
void freeTexture();
|
||||||
void updateTexture();
|
void updateTexture();
|
||||||
|
|
||||||
Pos findPos(int mx, int my);
|
bool findPos(int mx, int my, Pos &p);
|
||||||
|
|
||||||
Game *m_game;
|
Game *m_game;
|
||||||
PieceLibrary *m_lib;
|
PieceLibrary *m_lib;
|
||||||
@ -93,6 +95,8 @@ namespace toruschess {
|
|||||||
/* skybox */
|
/* skybox */
|
||||||
GLUquadric *m_skySphere;
|
GLUquadric *m_skySphere;
|
||||||
GLuint m_skyTextureID;
|
GLuint m_skyTextureID;
|
||||||
|
int m_skyTimerID;
|
||||||
|
double m_skyOffset;
|
||||||
|
|
||||||
/* Pick */
|
/* Pick */
|
||||||
GLuint m_pickTextureID;
|
GLuint m_pickTextureID;
|
||||||
|
@ -34,3 +34,5 @@ CONFIG -= release
|
|||||||
|
|
||||||
FORMS += optiondlg.ui
|
FORMS += optiondlg.ui
|
||||||
|
|
||||||
|
DISTFILES += ../README
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user