mirror of
https://github.com/ConjureETS/LOG750-LAB2.git
synced 2026-03-24 03:21:19 +00:00
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:
parent
f72cdda386
commit
379f310fde
@ -6,8 +6,8 @@
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>588</width>
|
||||
<height>499</height>
|
||||
<width>649</width>
|
||||
<height>560</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
@ -15,7 +15,7 @@
|
||||
</property>
|
||||
<widget class="QWidget" name="centralWidget">
|
||||
<layout class="QHBoxLayout" name="horizontalLayout">
|
||||
<item>
|
||||
<item alignment="Qt::AlignLeft|Qt::AlignTop">
|
||||
<widget class="QFrame" name="frame">
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
|
||||
@ -29,12 +29,6 @@
|
||||
<height>500</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="maximumSize">
|
||||
<size>
|
||||
<width>500</width>
|
||||
<height>500</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="frameShape">
|
||||
<enum>QFrame::StyledPanel</enum>
|
||||
</property>
|
||||
@ -45,6 +39,9 @@
|
||||
</item>
|
||||
<item>
|
||||
<layout class="QGridLayout" name="gridLayout">
|
||||
<property name="horizontalSpacing">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<item row="0" column="0">
|
||||
<widget class="QLabel" name="label">
|
||||
<property name="text">
|
||||
@ -131,7 +128,7 @@
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>588</width>
|
||||
<width>649</width>
|
||||
<height>20</height>
|
||||
</rect>
|
||||
</property>
|
||||
|
||||
@ -100,86 +100,20 @@ void Viewer::draw()
|
||||
// Traverse the Scene in order to draw its components
|
||||
|
||||
modelStack.push(modelViewMatrix);
|
||||
//root.accept(*this);
|
||||
root.accept(*this);
|
||||
update();
|
||||
}
|
||||
|
||||
void Viewer::mouseMoveEvent(QMouseEvent* e) {
|
||||
cout << "Viewer::mouseMoveEvent(QMouseEvent* e)" << endl;
|
||||
//cout << "Viewer::mouseMoveEvent(QMouseEvent* e)" << endl;
|
||||
// Normal QGLViewer behavior.
|
||||
//QGLViewer::mouseMoveEvent(e);
|
||||
}
|
||||
|
||||
void Viewer::mousePressEvent(QMouseEvent* e) {
|
||||
// Auto Return, but old code left in as reference
|
||||
return;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
//std::cout << "Picking shape at " << e->pos().x() << ", " << e->pos().y() << endl;
|
||||
Shape* selectedShape = pickGeom(e->pos().x()+20, e->pos().y()+20);
|
||||
}
|
||||
|
||||
void Viewer::deselect(){
|
||||
@ -198,7 +132,7 @@ void Viewer::deselect(){
|
||||
}
|
||||
|
||||
void Viewer::mouseReleaseEvent(QMouseEvent* e) {
|
||||
cout << "Viewer::mouseReleaseEvent(QMouseEvent* e)" << endl;
|
||||
//cout << "Viewer::mouseReleaseEvent(QMouseEvent* e)" << endl;
|
||||
//QGLViewer::mouseReleaseEvent(e);
|
||||
}
|
||||
|
||||
@ -224,12 +158,21 @@ void Viewer::init()
|
||||
initShaders();
|
||||
initGeometries();
|
||||
//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()
|
||||
{
|
||||
// Load vertex and fragment shaders
|
||||
m_program = new QOpenGLShaderProgram;
|
||||
colorPickerShaderProgram = m_program;
|
||||
if (!m_program->addShaderFromSourceFile(QOpenGLShader::Vertex, "src/shaders/basicShader.vert")) {
|
||||
cerr << "Unable to load Shader" << endl
|
||||
<< "Log file:" << endl;
|
||||
@ -256,6 +199,34 @@ void Viewer::initShaders()
|
||||
|
||||
if ((m_projMatrixLocation = m_program->uniformLocation("projMatrix")) < 0)
|
||||
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.
|
||||
@ -384,3 +355,55 @@ void Viewer::changeColor(QColor c){
|
||||
activeColor = new QColor(c);
|
||||
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;
|
||||
}
|
||||
|
||||
@ -36,7 +36,9 @@
|
||||
|
||||
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
|
||||
public:
|
||||
@ -70,13 +72,18 @@ private:
|
||||
void initShaders();
|
||||
void initGeometries();
|
||||
void deselect();
|
||||
Shape* pickGeom(int, int);
|
||||
|
||||
// shader switching variables and constants;
|
||||
QOpenGLShaderProgram *colorPickerShaderProgram;
|
||||
QOpenGLShaderProgram *textureRenderShaderprogram;
|
||||
QOpenGLShaderProgram *m_program;
|
||||
int m_vPositionLocation;
|
||||
int m_colorLocation;
|
||||
int m_projMatrixLocation;
|
||||
int m_mvMatrixLocation;
|
||||
|
||||
|
||||
SceneGroup* activeCell;
|
||||
QColor* activeColor;
|
||||
int activeShape;
|
||||
@ -88,6 +95,7 @@ private:
|
||||
GLuint m_Buffers[NumBuffers];
|
||||
|
||||
Shape* generateShapeFromIndex(int);
|
||||
|
||||
};
|
||||
|
||||
#endif // SIMPLEVIEWER_H
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user