Adding picking code.

Notes: once more shaders are created, switch between them for picking
instead of applying colors and trying to juggle that shit.
This commit is contained in:
Dmitri K 2016-11-02 02:47:57 -04:00
parent f72cdda386
commit 379f310fde
3 changed files with 111 additions and 83 deletions

View File

@ -6,8 +6,8 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>588</width> <width>649</width>
<height>499</height> <height>560</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
@ -15,7 +15,7 @@
</property> </property>
<widget class="QWidget" name="centralWidget"> <widget class="QWidget" name="centralWidget">
<layout class="QHBoxLayout" name="horizontalLayout"> <layout class="QHBoxLayout" name="horizontalLayout">
<item> <item alignment="Qt::AlignLeft|Qt::AlignTop">
<widget class="QFrame" name="frame"> <widget class="QFrame" name="frame">
<property name="sizePolicy"> <property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred"> <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
@ -29,12 +29,6 @@
<height>500</height> <height>500</height>
</size> </size>
</property> </property>
<property name="maximumSize">
<size>
<width>500</width>
<height>500</height>
</size>
</property>
<property name="frameShape"> <property name="frameShape">
<enum>QFrame::StyledPanel</enum> <enum>QFrame::StyledPanel</enum>
</property> </property>
@ -45,6 +39,9 @@
</item> </item>
<item> <item>
<layout class="QGridLayout" name="gridLayout"> <layout class="QGridLayout" name="gridLayout">
<property name="horizontalSpacing">
<number>6</number>
</property>
<item row="0" column="0"> <item row="0" column="0">
<widget class="QLabel" name="label"> <widget class="QLabel" name="label">
<property name="text"> <property name="text">
@ -131,7 +128,7 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>588</width> <width>649</width>
<height>20</height> <height>20</height>
</rect> </rect>
</property> </property>

View File

@ -100,86 +100,20 @@ void Viewer::draw()
// Traverse the Scene in order to draw its components // Traverse the Scene in order to draw its components
modelStack.push(modelViewMatrix); modelStack.push(modelViewMatrix);
//root.accept(*this); root.accept(*this);
update();
} }
void Viewer::mouseMoveEvent(QMouseEvent* e) { void Viewer::mouseMoveEvent(QMouseEvent* e) {
cout << "Viewer::mouseMoveEvent(QMouseEvent* e)" << endl; //cout << "Viewer::mouseMoveEvent(QMouseEvent* e)" << endl;
// Normal QGLViewer behavior. // Normal QGLViewer behavior.
//QGLViewer::mouseMoveEvent(e); //QGLViewer::mouseMoveEvent(e);
} }
void Viewer::mousePressEvent(QMouseEvent* e) { void Viewer::mousePressEvent(QMouseEvent* e) {
// Auto Return, but old code left in as reference // Auto Return, but old code left in as reference
return; //std::cout << "Picking shape at " << e->pos().x() << ", " << e->pos().y() << endl;
Shape* selectedShape = pickGeom(e->pos().x()+20, e->pos().y()+20);
cout << "Viewer::mouseMoveEvent(QMouseEvent* e) : " << e->button() << endl;
if(e->button() == 1){ // LMB
int truY = 10 - (e->y()) / 500.0 * GRID_SIZE;
int truX = (e->x() / 500.0) * GRID_SIZE;
cout << " -->Getting cell at " << truX << " : " << truY << endl;
SceneGroup* row = dynamic_cast<SceneGroup*> (root.childAt(truY));
cout << " -->" << row << endl;
SceneGroup* cell = dynamic_cast<SceneGroup*> (row->childAt(truX));
cout << " -->" << cell << endl;
if(e->modifiers() & Qt::ShiftModifier){
// add a shape
if(!cell->getChildren()->size()){
// WARNING: CODE DEGEULASSE
Shape* s = nullptr;
if(activeShape == 1){
s = new Triangle();
}else if(activeShape == 2){
s = new Square();
}else if(activeShape == 3){
s = new Circle();
}
// WARNING: END OF CODE DEGEULASSE
//activeCell->getChildren()->at(0) = s;
if(s != nullptr){
s->setColor(*activeColor);
cell->addChild(s);
this->update();
deselect();
activeCell = cell;
}
}
}
if(e->modifiers() & Qt::ControlModifier){
// select a shape
deselect();
activeCell = cell;
if(activeCell != nullptr && activeCell->getChildren()->size()){
std::cout << "Cell has children..." << endl;
Shape* shape = dynamic_cast<Shape*> (activeCell->childAt(0));
QColor newColor = shape->getColor();
std::cout << newColor.Rgb << endl;
newColor.setAlpha(255);
shape->setColor(newColor);
//emit shapeSelected(getTypeIndex(typeof activeCell->childAt(0)));
int shapeId = 0;
if(typeid(*(activeCell->getChildren()->at(0))) == typeid(Triangle))
shapeId = 1;
if(typeid(*(activeCell->getChildren()->at(0))) == typeid(Square))
shapeId = 2;
if(typeid(*(activeCell->getChildren()->at(0))) == typeid(Circle))
shapeId = 3;
emit shapeSelected(shapeId);
}else{
emit shapeSelected(0);
}
}
}
} }
void Viewer::deselect(){ void Viewer::deselect(){
@ -198,7 +132,7 @@ void Viewer::deselect(){
} }
void Viewer::mouseReleaseEvent(QMouseEvent* e) { void Viewer::mouseReleaseEvent(QMouseEvent* e) {
cout << "Viewer::mouseReleaseEvent(QMouseEvent* e)" << endl; //cout << "Viewer::mouseReleaseEvent(QMouseEvent* e)" << endl;
//QGLViewer::mouseReleaseEvent(e); //QGLViewer::mouseReleaseEvent(e);
} }
@ -224,12 +158,21 @@ void Viewer::init()
initShaders(); initShaders();
initGeometries(); initGeometries();
//initGrid(); //initGrid();
/*QColor* color = new QColor(255, 128, 0, 255);
Circle* c = new Circle;
c->setColor(*color);
SceneGroup* cell = new SceneGroup;
cell->addChild(c);
root.addChild(cell);*/
//update();
} }
void Viewer::initShaders() void Viewer::initShaders()
{ {
// Load vertex and fragment shaders // Load vertex and fragment shaders
m_program = new QOpenGLShaderProgram; m_program = new QOpenGLShaderProgram;
colorPickerShaderProgram = m_program;
if (!m_program->addShaderFromSourceFile(QOpenGLShader::Vertex, "src/shaders/basicShader.vert")) { if (!m_program->addShaderFromSourceFile(QOpenGLShader::Vertex, "src/shaders/basicShader.vert")) {
cerr << "Unable to load Shader" << endl cerr << "Unable to load Shader" << endl
<< "Log file:" << endl; << "Log file:" << endl;
@ -256,6 +199,34 @@ void Viewer::initShaders()
if ((m_projMatrixLocation = m_program->uniformLocation("projMatrix")) < 0) if ((m_projMatrixLocation = m_program->uniformLocation("projMatrix")) < 0)
qDebug() << "Unable to find shader location for " << "projMatrix"; qDebug() << "Unable to find shader location for " << "projMatrix";
/*
* Adding Texture Shader
*
*/
textureRenderShaderprogram = new QOpenGLShaderProgram;
if (!textureRenderShaderprogram->addShaderFromSourceFile(QOpenGLShader::Vertex, "src/shaders/textureShader.vert")) {
cerr << "Unable to load Shader" << endl
<< "Log file:" << endl;
qDebug() << textureRenderShaderprogram->log();
}
if (!textureRenderShaderprogram->addShaderFromSourceFile(QOpenGLShader::Fragment, "src/shaders/textureShader.frag")) {
cerr << "Unable to load Shader" << endl
<< "Log file:" << endl;
qDebug() << textureRenderShaderprogram->log();
}
textureRenderShaderprogram->link();
if ((m_vPositionLocation = m_program->attributeLocation("vPosition")) < 0)
qDebug() << "Unable to find shader location for " << "vPosition";
if ((m_mvMatrixLocation = m_program->uniformLocation("mvMatrix")) < 0)
qDebug() << "Unable to find shader location for " << "mvMatrix";
if ((m_projMatrixLocation = m_program->uniformLocation("projMatrix")) < 0)
qDebug() << "Unable to find shader location for " << "projMatrix";
} }
// Creates the basic shapes in memory. We only have 3, so we just prep them all in advance. // Creates the basic shapes in memory. We only have 3, so we just prep them all in advance.
@ -384,3 +355,55 @@ void Viewer::changeColor(QColor c){
activeColor = new QColor(c); activeColor = new QColor(c);
this->update(); this->update();
} }
Shape* Viewer::pickGeom(int x, int y){
QMap<QRgb, Shape*> mapColorToShape;
QColor c;
QRgb startColor = 0xFF000033; // alpha must be 100%, glReadPixels doesn't resturn alpha
unsigned char pixelData[3];
// Traverse tree
/* TODO: Make this recurse through SceneGroups, with like "populateMap(map, root, color)"
* Right now it's fine because we have simple Minecraft rules
* but could be important in the future
*/
QOpenGLShaderProgram *lastshader = m_program;
//colorPickerShaderProgram->bind();
std::cout << "Iterating through " << root.getChildren()->size() << "items"<<endl;
for(int i = 0, l = root.getChildren()->size(); i<l; i++)
{
std::cout << " iterating... " << i << endl;
SceneGroup* current = dynamic_cast<SceneGroup*>(root.childAt(i));
Shape* currentCube;
if(current->getChildren()->size())
{
currentCube = dynamic_cast<Shape*>(current->childAt(0));
}
if(currentCube)
{
c.setRgba(startColor);
mapColorToShape.insert(c.rgba(), currentCube);
std::cout << "Setting " << currentCube << " to " << c.red() << " " << c.green() << " " << c.blue() << " " << c.alpha() << endl;
currentCube->setColor(c);
startColor++;
}
}
root.accept(*this);
update();
draw();
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
// TODO: figure out how to get this weird offset ↓↓ frae position maybe?
glReadPixels(x, camera()->screenHeight() - 1 - y + 62, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixelData);
QColor* pickedColor = new QColor(pixelData[0], pixelData[1], pixelData[2]);
Shape* pickedShape = mapColorToShape.value(pickedColor->rgba());
std::cout << "Picked Color: " << pickedColor->red() << " " << pickedColor->green() << " " << pickedColor->blue() << " " << pickedColor->alpha() << endl;
std::cout << "Picked Shape: " << pickedShape << endl;
//lastshader->bind();
return pickedShape;
}

View File

@ -36,7 +36,9 @@
QT_FORWARD_DECLARE_CLASS(QOpenGLShaderProgram) QT_FORWARD_DECLARE_CLASS(QOpenGLShaderProgram)
class Viewer : public QGLViewer, protected QOpenGLFunctions_4_0_Core, public Visitor class Viewer : public QGLViewer,
protected QOpenGLFunctions_4_0_Core,
public Visitor
{ {
Q_OBJECT Q_OBJECT
public: public:
@ -70,13 +72,18 @@ private:
void initShaders(); void initShaders();
void initGeometries(); void initGeometries();
void deselect(); void deselect();
Shape* pickGeom(int, int);
// shader switching variables and constants;
QOpenGLShaderProgram *colorPickerShaderProgram;
QOpenGLShaderProgram *textureRenderShaderprogram;
QOpenGLShaderProgram *m_program; QOpenGLShaderProgram *m_program;
int m_vPositionLocation; int m_vPositionLocation;
int m_colorLocation; int m_colorLocation;
int m_projMatrixLocation; int m_projMatrixLocation;
int m_mvMatrixLocation; int m_mvMatrixLocation;
SceneGroup* activeCell; SceneGroup* activeCell;
QColor* activeColor; QColor* activeColor;
int activeShape; int activeShape;
@ -88,6 +95,7 @@ private:
GLuint m_Buffers[NumBuffers]; GLuint m_Buffers[NumBuffers];
Shape* generateShapeFromIndex(int); Shape* generateShapeFromIndex(int);
}; };
#endif // SIMPLEVIEWER_H #endif // SIMPLEVIEWER_H