mirror of
https://github.com/ConjureETS/LOG750-LAB2.git
synced 2026-03-24 03:21:19 +00:00
fixing cube putting
This commit is contained in:
parent
e57d54f861
commit
b985770871
@ -14,12 +14,19 @@ void Sphere::accept(Visitor &v) {
|
|||||||
// ***
|
// ***
|
||||||
|
|
||||||
void Cube::setColor(QColor& c) {
|
void Cube::setColor(QColor& c) {
|
||||||
std::cout << "Setting cubr color! " << c.rgb() << endl;
|
color = QColor(c);
|
||||||
color = QColor(c);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QColor Cube::getColor(){
|
QColor Cube::getColor(){
|
||||||
return color;
|
return color;
|
||||||
|
};
|
||||||
|
|
||||||
|
void Cube::setType(int t) {
|
||||||
|
this->type = t;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Cube::getType(){
|
||||||
|
return this->type;
|
||||||
};
|
};
|
||||||
|
|
||||||
void Sphere::setColor(QColor& c) {
|
void Sphere::setColor(QColor& c) {
|
||||||
@ -29,3 +36,11 @@ void Sphere::setColor(QColor& c) {
|
|||||||
QColor Sphere::getColor(){
|
QColor Sphere::getColor(){
|
||||||
return color;
|
return color;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
void Sphere::setType(int t) {
|
||||||
|
this->type = t;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Sphere::getType(){
|
||||||
|
return this->type;
|
||||||
|
};
|
||||||
|
|||||||
@ -9,25 +9,35 @@ class Shape : public GlNode
|
|||||||
public:
|
public:
|
||||||
virtual void setColor(QColor& c) = 0;
|
virtual void setColor(QColor& c) = 0;
|
||||||
virtual QColor getColor() = 0;
|
virtual QColor getColor() = 0;
|
||||||
|
virtual void setType(int) = 0;
|
||||||
|
virtual int getType() = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Cube : public Shape
|
class Cube : public Shape
|
||||||
{
|
{
|
||||||
|
private:
|
||||||
|
int type = 0;
|
||||||
public:
|
public:
|
||||||
Cube(){}
|
Cube(){}
|
||||||
QColor color;
|
QColor color;
|
||||||
void accept(Visitor& v) override;
|
void accept(Visitor& v) override;
|
||||||
void setColor(QColor& c);
|
void setColor(QColor& c);
|
||||||
QColor getColor();
|
QColor getColor();
|
||||||
|
void setType(int);
|
||||||
|
int getType();
|
||||||
};
|
};
|
||||||
|
|
||||||
class Sphere : public Shape
|
class Sphere : public Shape
|
||||||
{
|
{
|
||||||
|
private:
|
||||||
|
int type = 0;
|
||||||
public:
|
public:
|
||||||
Sphere(){}
|
Sphere(){}
|
||||||
QColor color;
|
QColor color;
|
||||||
void accept(Visitor& v) override;
|
void accept(Visitor& v) override;
|
||||||
void setColor(QColor& c);
|
void setColor(QColor& c);
|
||||||
QColor getColor();
|
QColor getColor();
|
||||||
|
void setType(int);
|
||||||
|
int getType();
|
||||||
};
|
};
|
||||||
#endif // SHAPES_H
|
#endif // SHAPES_H
|
||||||
|
|||||||
@ -91,7 +91,7 @@ main()
|
|||||||
+ calcPointLight(texColor, fPosition, fNormal, 1)/4
|
+ calcPointLight(texColor, fPosition, fNormal, 1)/4
|
||||||
+ calcPointLight(texColor, fPosition, fNormal, 2)/4;
|
+ calcPointLight(texColor, fPosition, fNormal, 2)/4;
|
||||||
} else {
|
} else {
|
||||||
fColor = texColor * ifColor;
|
fColor = texColor + ifColor;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -182,11 +182,21 @@ void Viewer::mousePressEvent(QMouseEvent* e) {
|
|||||||
// TODO: figure out how to get this weird offset ↓↓ frame position maybe?
|
// TODO: figure out how to get this weird offset ↓↓ frame position maybe?
|
||||||
int x = this->x() + e->x() - 10;
|
int x = this->x() + e->x() - 10;
|
||||||
int y = this->parentWidget()->height() - e->y() - 23;
|
int y = this->parentWidget()->height() - e->y() - 23;
|
||||||
std::cout << "--------------------------------------------------\nPicking shape at " << x << " (" << this->x() << " + " << e->x() << "), " << y << endl;
|
|
||||||
std::cout << "Window geom: " << this->window()->size().width() << "w, " << this->window()->size().height() << "h" << endl;
|
|
||||||
Shape* selectedShape = pickGeom(x, y);
|
|
||||||
// OpenGL coords start at the window's bottom-left, not the frame's. ugh.
|
|
||||||
|
|
||||||
|
std::cout << "--------------------------------------------------\nPicking shape at " << x << " (" << this->x() << " + " << e->x() << "), " << y << endl;
|
||||||
|
std::cout << "Window geom: " << this->window()->size().width() << "w, " << this->window()->size().height() << "h" << endl;
|
||||||
|
|
||||||
|
QMatrix4x4 selectedPosition = pickGeom(x, y);
|
||||||
|
if(!selectedPosition.isIdentity())
|
||||||
|
{
|
||||||
|
Cube* c = new Cube;
|
||||||
|
c->setType(TEX_WOODFLOOR);
|
||||||
|
SceneGroup* container = new SceneGroup;
|
||||||
|
//selectedPosition.copyDataTo(container->transform.data());
|
||||||
|
container->transform = selectedPosition;
|
||||||
|
container->addChild(c);
|
||||||
|
root.addChild(container);
|
||||||
|
}
|
||||||
QGLViewer::mousePressEvent(e);
|
QGLViewer::mousePressEvent(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -201,6 +211,7 @@ void Viewer::mouseReleaseEvent(QMouseEvent* e) {
|
|||||||
|
|
||||||
void Viewer::init()
|
void Viewer::init()
|
||||||
{
|
{
|
||||||
|
identityMatrix.setToIdentity();
|
||||||
SkyboxCamera *_cam = new SkyboxCamera();
|
SkyboxCamera *_cam = new SkyboxCamera();
|
||||||
setCamera(_cam);
|
setCamera(_cam);
|
||||||
//camera()->setType(qglviewer::Camera::PERSPECTIVE);
|
//camera()->setType(qglviewer::Camera::PERSPECTIVE);
|
||||||
@ -229,55 +240,64 @@ void Viewer::init()
|
|||||||
|
|
||||||
{
|
{
|
||||||
|
|
||||||
QColor* c1 = new QColor(255, 0, 255, 255);
|
QColor* c1 = new QColor(255, 0, 255, 255);
|
||||||
QColor* c2 = new QColor(0, 255, 255, 255);
|
QColor* c2 = new QColor(0, 255, 255, 255);
|
||||||
QColor* c3 = new QColor(255, 255, 0, 255);
|
QColor* c3 = new QColor(255, 255, 0, 255);
|
||||||
|
|
||||||
Shape* s1 = new Sphere();
|
Shape* s1 = new Sphere();
|
||||||
Shape* s2 = new Sphere();
|
Shape* s2 = new Sphere();
|
||||||
Shape* s3 = new Sphere();
|
Shape* s3 = new Sphere();
|
||||||
|
|
||||||
|
selection = new SceneGroup();
|
||||||
|
selection->addChild(s1);
|
||||||
|
selection->addChild(s2);
|
||||||
|
selection->addChild(s3);
|
||||||
|
|
||||||
|
//SceneGroup *sc1 = new SceneGroup();
|
||||||
|
//sc1->addChild(selection);
|
||||||
|
//root.addChild(sc1);
|
||||||
|
root.addChild(selection);
|
||||||
|
|
||||||
|
s1->transform.rotate(360 * 1/3,0,1,0);
|
||||||
|
s1->transform.translate(1,0,0);
|
||||||
|
s1->transform.scale(0.05);
|
||||||
|
s1->setColor(*c1);
|
||||||
|
s2->transform.rotate(360 * 2/3,0,1,0);
|
||||||
|
s2->transform.translate(1,0,0);
|
||||||
|
s2->transform.scale(0.05);
|
||||||
|
s2->setColor(*c2);
|
||||||
|
s3->transform.rotate(360 * 3/3,0,1,0);
|
||||||
|
s3->transform.translate(1,0,0);
|
||||||
|
s3->transform.scale(0.05);
|
||||||
|
s3->setColor(*c3);
|
||||||
|
|
||||||
|
|
||||||
|
for(int i = 0; i < 10; i++){
|
||||||
|
for(int j = 0; j < 10; j++){
|
||||||
|
Shape* cube = new Cube();
|
||||||
|
SceneGroup *sc = new SceneGroup();
|
||||||
|
cube->setColor(*c1);
|
||||||
|
cube->setType(1);
|
||||||
|
sc->addChild(cube);
|
||||||
|
sc->transform.translate(-5+i, -5, -5+j);
|
||||||
|
root.addChild(sc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*
|
||||||
Shape* cube1 = new Cube();
|
Shape* cube1 = new Cube();
|
||||||
Shape* cube2 = new Cube();
|
Shape* cube2 = new Cube();
|
||||||
Shape* cube3 = new Cube();
|
Shape* cube3 = new Cube();
|
||||||
cube1->setColor(*c1);
|
cube1->setColor(*c1);
|
||||||
cube2->setColor(*c2);
|
cube2->setColor(*c2);
|
||||||
cube3->setColor(*c3);
|
cube3->setColor(*c3);
|
||||||
cube2->transform.translate(1, 0, 0);
|
cube2->transform.translate(3, 0, 0);
|
||||||
cube3->transform.translate(0, 0, 1);
|
cube3->transform.translate(-3, 0, 0);
|
||||||
|
//*/
|
||||||
s1->transform.rotate(360 * 1/3,0,1,0);
|
|
||||||
s1->transform.translate(1,0,0);
|
|
||||||
s1->transform.scale(0.05);
|
|
||||||
s1->setColor(*c1);
|
|
||||||
s2->transform.rotate(360 * 2/3,0,1,0);
|
|
||||||
s2->transform.translate(1,0,0);
|
|
||||||
s2->transform.scale(0.05);
|
|
||||||
s2->setColor(*c2);
|
|
||||||
s3->transform.rotate(360 * 3/3,0,1,0);
|
|
||||||
s3->transform.translate(1,0,0);
|
|
||||||
s3->transform.scale(0.05);
|
|
||||||
s3->setColor(*c3);
|
|
||||||
|
|
||||||
// uncomment this and the quaternion transformation in draw()
|
// uncomment this and the quaternion transformation in draw()
|
||||||
// to see the cube rotate. This is how we can easily make the sun.
|
// to see the cube rotate. This is how we can easily make the sun.
|
||||||
// cube->transform.translate(3, 0, 0);
|
// cube->transform.translate(3, 0, 0);
|
||||||
|
|
||||||
selection = new SceneGroup();
|
|
||||||
selection->addChild(s1);
|
|
||||||
selection->addChild(s2);
|
|
||||||
selection->addChild(s3);
|
|
||||||
|
|
||||||
SceneGroup *sc1 = new SceneGroup();
|
|
||||||
SceneGroup *sc2 = new SceneGroup();
|
|
||||||
SceneGroup *sc3 = new SceneGroup();
|
|
||||||
sc1->addChild(cube1);
|
|
||||||
sc1->addChild(selection);
|
|
||||||
sc2->addChild(cube2);
|
|
||||||
sc3->addChild(cube3);
|
|
||||||
root.addChild(sc1);
|
|
||||||
root.addChild(sc2);
|
|
||||||
root.addChild(sc3);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -378,6 +398,7 @@ void Viewer::initShaders()
|
|||||||
|
|
||||||
// load remaining textures
|
// load remaining textures
|
||||||
for(int i = 0; i<TEX_LENGTH; i++){
|
for(int i = 0; i<TEX_LENGTH; i++){
|
||||||
|
std::cout << "Binding " << TexturePaths[i].toStdString() << endl;
|
||||||
TexturePrograms[i] = new QOpenGLTexture(QImage(TexturePaths[i]));
|
TexturePrograms[i] = new QOpenGLTexture(QImage(TexturePaths[i]));
|
||||||
TexturePrograms[i]->setMinificationFilter(QOpenGLTexture::LinearMipMapLinear);
|
TexturePrograms[i]->setMinificationFilter(QOpenGLTexture::LinearMipMapLinear);
|
||||||
TexturePrograms[i]->setMagnificationFilter(QOpenGLTexture::Linear);
|
TexturePrograms[i]->setMagnificationFilter(QOpenGLTexture::Linear);
|
||||||
@ -611,26 +632,26 @@ void Viewer::initBuffers(){
|
|||||||
void Viewer::visit(Cube &s)
|
void Viewer::visit(Cube &s)
|
||||||
{
|
{
|
||||||
QMatrix4x4 modelViewMatrix = modelStack.top() * QMatrix4x4(s.transform);
|
QMatrix4x4 modelViewMatrix = modelStack.top() * QMatrix4x4(s.transform);
|
||||||
|
QColor* faceColor = new QColor;
|
||||||
|
|
||||||
int faces = floor(numVerticesCube/6);
|
|
||||||
glBindVertexArray(m_VAOs[VAO_Cube]);
|
glBindVertexArray(m_VAOs[VAO_Cube]);
|
||||||
|
TexturePrograms[s.getType()]->bind();
|
||||||
|
|
||||||
|
m_program->setUniformValue(m_isSkyLoc, false);
|
||||||
|
m_program->setUniformValue(m_drawTextLoc, true);
|
||||||
|
m_program->setUniformValue(m_isLightLoc, false);
|
||||||
|
m_program->setUniformValue(m_mvMatrixLocation, modelViewMatrix);
|
||||||
|
m_program->setUniformValue(m_normalMatrixLoc, modelViewMatrix.normalMatrix());
|
||||||
|
|
||||||
|
int faces = floor(numVerticesCube/6);
|
||||||
for(int i =0; i<faces; i++)
|
for(int i =0; i<faces; i++)
|
||||||
{
|
{
|
||||||
QColor* faceColor = new QColor;
|
|
||||||
faceColor->setRgb(s.getColor().rgb() + i);
|
faceColor->setRgb(s.getColor().rgb() + i);
|
||||||
m_program->bind();
|
|
||||||
m_program->setUniformValue(m_isSkyLoc, false);
|
|
||||||
m_program->setUniformValue(m_mvMatrixLocation, modelViewMatrix);
|
|
||||||
m_program->setUniformValue(m_normalMatrixLoc, modelViewMatrix.normalMatrix());
|
|
||||||
m_program->setUniformValue(m_colorLocation, *faceColor);
|
m_program->setUniformValue(m_colorLocation, *faceColor);
|
||||||
m_program->setUniformValue(m_drawTextLoc, true);
|
|
||||||
m_program->setUniformValue(m_isLightLoc, false);
|
|
||||||
|
|
||||||
glDrawArrays(GL_TRIANGLES, i*6, 6);
|
glDrawArrays(GL_TRIANGLES, i*6, 6);
|
||||||
glFinish();
|
|
||||||
delete faceColor;
|
|
||||||
}
|
}
|
||||||
|
delete faceColor;
|
||||||
//glDrawArrays(GL_TRIANGLES, 0, 36);
|
//glDrawArrays(GL_TRIANGLES, 0, 36);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -705,32 +726,29 @@ void Viewer::changeColor(QColor c){
|
|||||||
this->update();
|
this->update();
|
||||||
}
|
}
|
||||||
|
|
||||||
Shape* Viewer::pickGeom(int x, int y){
|
QMatrix4x4 Viewer::pickGeom(int x, int y){
|
||||||
|
|
||||||
makeCurrent();
|
makeCurrent();
|
||||||
|
|
||||||
m_program->bind();
|
m_program->bind();
|
||||||
m_program->setUniformValue(m_isPickingModeLoc, true);
|
m_program->setUniformValue(m_isPickingModeLoc, true);
|
||||||
|
|
||||||
//glReadBuffer(GL_COLOR_ATTACHMENT0);
|
|
||||||
//glBindFramebuffer(GL_FRAMEBUFFER, m_RenderBuffers[RenderBuffer_Main]);
|
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
|
||||||
|
|
||||||
|
|
||||||
isPickingActivated = true;
|
isPickingActivated = true;
|
||||||
|
|
||||||
QMap<QRgb, Shape*> mapColorToShape;
|
QMap<QRgb, QMatrix4x4> mapColorToPosition;
|
||||||
QColor c;
|
QColor c;
|
||||||
QRgb startColor = 0x1; // alpha must be 100%, glReadPixels doesn't resturn alpha
|
QRgb startColor = 1; // alpha must be 100%, glReadPixels doesn't resturn alpha
|
||||||
unsigned char pixelData[3];
|
unsigned char pixelData[3];
|
||||||
|
|
||||||
// Give each cube a color, let each cube manage their face-color assignment
|
// Give each cube a color, let each cube manage their face-color assignment
|
||||||
std::cout << "Iterating through " << root.getChildren()->size() << " items"<<endl;
|
//std::cout << "Iterating through " << root.getChildren()->size() << " items"<<endl;
|
||||||
int i = 0;
|
|
||||||
while(root.hasNext())
|
for(int i = 0; i<root.getChildren()->size(); i++)
|
||||||
{
|
{
|
||||||
std::cout << " iterating... " << i << endl;
|
//std::cout << " iterating... " << i << endl;
|
||||||
SceneGroup* current = dynamic_cast<SceneGroup*>(root.getChild());
|
SceneGroup* current = dynamic_cast<SceneGroup*>(root.childAt(i));
|
||||||
Shape* currentCube;
|
Shape* currentCube;
|
||||||
|
|
||||||
if(current->getChildren()->size())
|
if(current->getChildren()->size())
|
||||||
@ -738,16 +756,23 @@ Shape* Viewer::pickGeom(int x, int y){
|
|||||||
currentCube = dynamic_cast<Shape*>(current->childAt(0));
|
currentCube = dynamic_cast<Shape*>(current->childAt(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
c.setRgb(startColor);
|
||||||
currentCube->setColor(c);
|
currentCube->setColor(c);
|
||||||
if(currentCube) for (int j = 0; j<6; j++)
|
if(currentCube) for (int j = 0; j<3; j++)
|
||||||
{
|
{
|
||||||
c.setRgb(startColor);
|
for(int i = -1; i<=1; i+=2)
|
||||||
// QMatrix4x4 direction; direction.translate(0, 1, 0);
|
{
|
||||||
mapColorToShape.insert(c.rgb(), currentCube/*->transform * direction*/);
|
QMatrix4x4 currentTransform = QMatrix4x4(current->transform);
|
||||||
std::cout << "Setting " << currentCube << " to " << c.red() << " " << c.green() << " " << c.blue() << " " << c.alpha() << endl;
|
int components[3] = {0, 0, 0};
|
||||||
startColor++;
|
components[j] = i;
|
||||||
|
//std::cout << "Setting " << startColor << " to {" << components[0] << ", " << components[0] << ", " << components[1] << ", " << components[2] << "}\n";
|
||||||
|
currentTransform.translate(components[1], -components[2], -components[0]);
|
||||||
|
mapColorToPosition.insert(startColor, currentTransform);
|
||||||
|
startColor++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
root.accept(*this);
|
root.accept(*this);
|
||||||
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
||||||
|
|
||||||
@ -772,15 +797,15 @@ Shape* Viewer::pickGeom(int x, int y){
|
|||||||
glReadPixels(x, y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixelData);
|
glReadPixels(x, y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixelData);
|
||||||
|
|
||||||
QColor* pickedColor = new QColor(pixelData[0], pixelData[1], pixelData[2]);
|
QColor* pickedColor = new QColor(pixelData[0], pixelData[1], pixelData[2]);
|
||||||
unsigned int pickedInt = pickedColor->rgb();
|
unsigned int pickedInt = pickedColor->rgb() % 0x1000000; // rgb returns ARGB. go figure.
|
||||||
Shape* pickedShape = mapColorToShape.value(pickedInt - (pickedInt % 6) + 1);
|
//std::cout << "Picked " << pickedInt << endl;
|
||||||
std::cout << "Picked Color: " << pickedColor->red() << " " << pickedColor->green() << " " << pickedColor->blue() << " " << pickedColor->alpha() << endl;
|
|
||||||
std::cout << "Picked Shape: " << pickedShape << endl;
|
|
||||||
|
|
||||||
|
QMatrix4x4 pickedPosition = mapColorToPosition.value(pickedInt, identityMatrix);
|
||||||
|
|
||||||
|
mapColorToPosition.clear();
|
||||||
m_program->setUniformValue(m_isPickingModeLoc, false);
|
m_program->setUniformValue(m_isPickingModeLoc, false);
|
||||||
isPickingActivated = false;
|
isPickingActivated = false;
|
||||||
doneCurrent();
|
doneCurrent();
|
||||||
|
|
||||||
return pickedShape;
|
return pickedPosition;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -75,7 +75,7 @@ private:
|
|||||||
void initGeometries();
|
void initGeometries();
|
||||||
void initBuffers();
|
void initBuffers();
|
||||||
void deselect();
|
void deselect();
|
||||||
Shape* pickGeom(int, int);
|
QMatrix4x4 pickGeom(int, int);
|
||||||
|
|
||||||
// shader switching variables and constants;
|
// shader switching variables and constants;
|
||||||
QOpenGLShaderProgram *colorPickerShaderProgram;
|
QOpenGLShaderProgram *colorPickerShaderProgram;
|
||||||
@ -162,7 +162,7 @@ private:
|
|||||||
};
|
};
|
||||||
|
|
||||||
QOpenGLTexture *TexturePrograms[TEX_LENGTH];
|
QOpenGLTexture *TexturePrograms[TEX_LENGTH];
|
||||||
|
QMatrix4x4 identityMatrix;
|
||||||
|
|
||||||
QQuaternion rot;
|
QQuaternion rot;
|
||||||
unsigned int frame;
|
unsigned int frame;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user