diff --git a/src/field3d.cpp b/src/field3d.cpp index b77b2d8..5a8263a 100644 --- a/src/field3d.cpp +++ b/src/field3d.cpp @@ -27,6 +27,8 @@ #include +#include + #include template @@ -51,7 +53,7 @@ namespace toruschess { m_textureBuffer(0), m_textureID(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_skySphere(0), m_skyTextureID(0), + m_skySphere(0), m_skyTextureID(0), m_skyTimerID(0), m_skyOffset(0), m_pickTextureID(0) { qDebug("Field3d::Field3D"); 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())); m_pickBuffer = new QGLPixelBuffer(QSize(1024, 1024)); + + m_skyTimerID = startTimer(20); } void Field3D::resizeGL(int w, int h) { @@ -130,7 +134,12 @@ namespace toruschess { glDisable(GL_FOG); glDisable(GL_LIGHTING); glBindTexture(GL_TEXTURE_2D, m_skyTextureID); + glMatrixMode(GL_TEXTURE); + glPushMatrix(); + glTranslatef(m_skyOffset, 0, 0); gluSphere(m_skySphere, 256, 64, 64); + glMatrixMode(GL_TEXTURE); + glPopMatrix(); glEnable(GL_LIGHTING); 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; double ox = -2.0*(m_originX/(double)m_textureSize), oy = 2.0*(m_originY/(double)m_textureSize); glEnable(GL_FOG); + glEnable(GL_LIGHTING); glBindTexture(GL_TEXTURE_2D, m_textureID); glBegin(GL_QUADS); glTexCoord2f(-repeat+ox, repeat+oy); glVertex3f(-10*repeat, 0, -10*repeat); @@ -175,28 +185,12 @@ namespace toruschess { glBindTexture(GL_TEXTURE_2D, 0); } - Pos Field3D::findPos(int mx, int my) { - m_pickBuffer->makeCurrent(); - - 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); - + bool Field3D::findPos(int mx, int my, Pos &p) { + glDisable(GL_FOG); + glDisable(GL_LIGHTING); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 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) { double ox = 2.0*(m_originX/(double)m_textureSize), oy = 2.0*(m_originY/(double)m_textureSize); glBindTexture(GL_TEXTURE_2D, m_pickTextureID); @@ -238,11 +232,12 @@ namespace toruschess { glDisable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, 0); - m_pickBuffer->doneCurrent(); - QImage i = m_pickBuffer->toImage(); - QRgb col = i.pixel((1024*mx)/width(), (1024*my)/height()); - qDebug << QColor(col); - return Pos(qRed(col), qGreen(col)); + unsigned char pxbuf[3]; + glReadPixels(mx, height() - 1 - my, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, &pxbuf); + paintGL(); + qDebug() << pxbuf[0] << pxbuf[1]; + 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 y = 0; y < 8; y++) { 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); +// delete pickimage; } /* glBindTexture(GL_TEXTURE_2D, m_textureID); @@ -346,8 +342,9 @@ namespace toruschess { void Field3D::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { QList moves; - Pos p = findPos(event->x(), event->y()); - moves = m_game->field()->validMoves(p); + Pos p; + if (findPos(event->x(), event->y(), p)) + moves = m_game->field()->validMoves(p); markMoves(moves); updateTexture(); updateGL(); @@ -375,13 +372,15 @@ namespace toruschess { m_mouseLastX = event->x(); m_mouseLastY = event->y(); if (event->button() == Qt::LeftButton) { -/* if (m_markedMoves.size() == 0) return; + if (m_markedMoves.size() == 0) return; Pos from = m_markedMoves[0].from(); QList moves; markMoves(QList()); - Pos p = findPos(event->x(), event->y()); - Move m(m_game->field(), from, p); - m_game->move(m);*/ + Pos p; + if (findPos(event->x(), event->y(), p)) { + Move m(m_game->field(), from, p); + m_game->move(m); + } } } @@ -401,4 +400,11 @@ namespace toruschess { updateTexture(); updateGL(); } + + void Field3D::timerEvent(QTimerEvent *) { + m_skyOffset = rotate(m_skyOffset + 0.001, 1); + if (m_boardMode == TORUS) + updateGL(); + } + } diff --git a/src/field3d.h b/src/field3d.h index ef180c7..e30b374 100644 --- a/src/field3d.h +++ b/src/field3d.h @@ -60,6 +60,8 @@ namespace toruschess { virtual void mouseReleaseEvent(QMouseEvent *event); virtual void wheelEvent(QWheelEvent *event); + virtual void timerEvent(QTimerEvent *event); + protected slots: void fieldUpdated(); @@ -70,7 +72,7 @@ namespace toruschess { void freeTexture(); void updateTexture(); - Pos findPos(int mx, int my); + bool findPos(int mx, int my, Pos &p); Game *m_game; PieceLibrary *m_lib; @@ -93,6 +95,8 @@ namespace toruschess { /* skybox */ GLUquadric *m_skySphere; GLuint m_skyTextureID; + int m_skyTimerID; + double m_skyOffset; /* Pick */ GLuint m_pickTextureID; diff --git a/src/src.pro b/src/src.pro index d75dcd5..d27c832 100644 --- a/src/src.pro +++ b/src/src.pro @@ -34,3 +34,5 @@ CONFIG -= release FORMS += optiondlg.ui +DISTFILES += ../README +