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,7 +14,6 @@ void Sphere::accept(Visitor &v) {
|
||||
// ***
|
||||
|
||||
void Cube::setColor(QColor& c) {
|
||||
std::cout << "Setting cubr color! " << c.rgb() << endl;
|
||||
color = QColor(c);
|
||||
}
|
||||
|
||||
@ -22,6 +21,14 @@ QColor Cube::getColor(){
|
||||
return color;
|
||||
};
|
||||
|
||||
void Cube::setType(int t) {
|
||||
this->type = t;
|
||||
}
|
||||
|
||||
int Cube::getType(){
|
||||
return this->type;
|
||||
};
|
||||
|
||||
void Sphere::setColor(QColor& c) {
|
||||
color = QColor(c);
|
||||
}
|
||||
@ -29,3 +36,11 @@ void Sphere::setColor(QColor& c) {
|
||||
QColor Sphere::getColor(){
|
||||
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:
|
||||
virtual void setColor(QColor& c) = 0;
|
||||
virtual QColor getColor() = 0;
|
||||
virtual void setType(int) = 0;
|
||||
virtual int getType() = 0;
|
||||
};
|
||||
|
||||
class Cube : public Shape
|
||||
{
|
||||
private:
|
||||
int type = 0;
|
||||
public:
|
||||
Cube(){}
|
||||
QColor color;
|
||||
void accept(Visitor& v) override;
|
||||
void setColor(QColor& c);
|
||||
QColor getColor();
|
||||
void setType(int);
|
||||
int getType();
|
||||
};
|
||||
|
||||
class Sphere : public Shape
|
||||
{
|
||||
private:
|
||||
int type = 0;
|
||||
public:
|
||||
Sphere(){}
|
||||
QColor color;
|
||||
void accept(Visitor& v) override;
|
||||
void setColor(QColor& c);
|
||||
QColor getColor();
|
||||
void setType(int);
|
||||
int getType();
|
||||
};
|
||||
#endif // SHAPES_H
|
||||
|
||||
@ -91,7 +91,7 @@ main()
|
||||
+ calcPointLight(texColor, fPosition, fNormal, 1)/4
|
||||
+ calcPointLight(texColor, fPosition, fNormal, 2)/4;
|
||||
} 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?
|
||||
int x = this->x() + e->x() - 10;
|
||||
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.
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@ -201,6 +211,7 @@ void Viewer::mouseReleaseEvent(QMouseEvent* e) {
|
||||
|
||||
void Viewer::init()
|
||||
{
|
||||
identityMatrix.setToIdentity();
|
||||
SkyboxCamera *_cam = new SkyboxCamera();
|
||||
setCamera(_cam);
|
||||
//camera()->setType(qglviewer::Camera::PERSPECTIVE);
|
||||
@ -237,14 +248,15 @@ void Viewer::init()
|
||||
Shape* s2 = new Sphere();
|
||||
Shape* s3 = new Sphere();
|
||||
|
||||
Shape* cube1 = new Cube();
|
||||
Shape* cube2 = new Cube();
|
||||
Shape* cube3 = new Cube();
|
||||
cube1->setColor(*c1);
|
||||
cube2->setColor(*c2);
|
||||
cube3->setColor(*c3);
|
||||
cube2->transform.translate(1, 0, 0);
|
||||
cube3->transform.translate(0, 0, 1);
|
||||
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);
|
||||
@ -259,25 +271,33 @@ void Viewer::init()
|
||||
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* cube2 = new Cube();
|
||||
Shape* cube3 = new Cube();
|
||||
cube1->setColor(*c1);
|
||||
cube2->setColor(*c2);
|
||||
cube3->setColor(*c3);
|
||||
cube2->transform.translate(3, 0, 0);
|
||||
cube3->transform.translate(-3, 0, 0);
|
||||
//*/
|
||||
|
||||
// uncomment this and the quaternion transformation in draw()
|
||||
// to see the cube rotate. This is how we can easily make the sun.
|
||||
// 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
|
||||
for(int i = 0; i<TEX_LENGTH; i++){
|
||||
std::cout << "Binding " << TexturePaths[i].toStdString() << endl;
|
||||
TexturePrograms[i] = new QOpenGLTexture(QImage(TexturePaths[i]));
|
||||
TexturePrograms[i]->setMinificationFilter(QOpenGLTexture::LinearMipMapLinear);
|
||||
TexturePrograms[i]->setMagnificationFilter(QOpenGLTexture::Linear);
|
||||
@ -611,26 +632,26 @@ void Viewer::initBuffers(){
|
||||
void Viewer::visit(Cube &s)
|
||||
{
|
||||
QMatrix4x4 modelViewMatrix = modelStack.top() * QMatrix4x4(s.transform);
|
||||
|
||||
int faces = floor(numVerticesCube/6);
|
||||
glBindVertexArray(m_VAOs[VAO_Cube]);
|
||||
|
||||
for(int i =0; i<faces; i++)
|
||||
{
|
||||
QColor* faceColor = new QColor;
|
||||
faceColor->setRgb(s.getColor().rgb() + i);
|
||||
m_program->bind();
|
||||
|
||||
glBindVertexArray(m_VAOs[VAO_Cube]);
|
||||
TexturePrograms[s.getType()]->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_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++)
|
||||
{
|
||||
faceColor->setRgb(s.getColor().rgb() + i);
|
||||
m_program->setUniformValue(m_colorLocation, *faceColor);
|
||||
|
||||
glDrawArrays(GL_TRIANGLES, i*6, 6);
|
||||
glFinish();
|
||||
delete faceColor;
|
||||
}
|
||||
delete faceColor;
|
||||
//glDrawArrays(GL_TRIANGLES, 0, 36);
|
||||
}
|
||||
|
||||
@ -705,32 +726,29 @@ void Viewer::changeColor(QColor c){
|
||||
this->update();
|
||||
}
|
||||
|
||||
Shape* Viewer::pickGeom(int x, int y){
|
||||
QMatrix4x4 Viewer::pickGeom(int x, int y){
|
||||
|
||||
makeCurrent();
|
||||
|
||||
m_program->bind();
|
||||
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);
|
||||
|
||||
|
||||
isPickingActivated = true;
|
||||
|
||||
QMap<QRgb, Shape*> mapColorToShape;
|
||||
QMap<QRgb, QMatrix4x4> mapColorToPosition;
|
||||
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];
|
||||
|
||||
// Give each cube a color, let each cube manage their face-color assignment
|
||||
std::cout << "Iterating through " << root.getChildren()->size() << " items"<<endl;
|
||||
int i = 0;
|
||||
while(root.hasNext())
|
||||
//std::cout << "Iterating through " << root.getChildren()->size() << " items"<<endl;
|
||||
|
||||
for(int i = 0; i<root.getChildren()->size(); i++)
|
||||
{
|
||||
std::cout << " iterating... " << i << endl;
|
||||
SceneGroup* current = dynamic_cast<SceneGroup*>(root.getChild());
|
||||
//std::cout << " iterating... " << i << endl;
|
||||
SceneGroup* current = dynamic_cast<SceneGroup*>(root.childAt(i));
|
||||
Shape* currentCube;
|
||||
|
||||
if(current->getChildren()->size())
|
||||
@ -738,16 +756,23 @@ Shape* Viewer::pickGeom(int x, int y){
|
||||
currentCube = dynamic_cast<Shape*>(current->childAt(0));
|
||||
}
|
||||
|
||||
currentCube->setColor(c);
|
||||
if(currentCube) for (int j = 0; j<6; j++)
|
||||
{
|
||||
c.setRgb(startColor);
|
||||
// QMatrix4x4 direction; direction.translate(0, 1, 0);
|
||||
mapColorToShape.insert(c.rgb(), currentCube/*->transform * direction*/);
|
||||
std::cout << "Setting " << currentCube << " to " << c.red() << " " << c.green() << " " << c.blue() << " " << c.alpha() << endl;
|
||||
currentCube->setColor(c);
|
||||
if(currentCube) for (int j = 0; j<3; j++)
|
||||
{
|
||||
for(int i = -1; i<=1; i+=2)
|
||||
{
|
||||
QMatrix4x4 currentTransform = QMatrix4x4(current->transform);
|
||||
int components[3] = {0, 0, 0};
|
||||
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);
|
||||
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);
|
||||
|
||||
QColor* pickedColor = new QColor(pixelData[0], pixelData[1], pixelData[2]);
|
||||
unsigned int pickedInt = pickedColor->rgb();
|
||||
Shape* pickedShape = mapColorToShape.value(pickedInt - (pickedInt % 6) + 1);
|
||||
std::cout << "Picked Color: " << pickedColor->red() << " " << pickedColor->green() << " " << pickedColor->blue() << " " << pickedColor->alpha() << endl;
|
||||
std::cout << "Picked Shape: " << pickedShape << endl;
|
||||
unsigned int pickedInt = pickedColor->rgb() % 0x1000000; // rgb returns ARGB. go figure.
|
||||
//std::cout << "Picked " << pickedInt << endl;
|
||||
|
||||
QMatrix4x4 pickedPosition = mapColorToPosition.value(pickedInt, identityMatrix);
|
||||
|
||||
mapColorToPosition.clear();
|
||||
m_program->setUniformValue(m_isPickingModeLoc, false);
|
||||
isPickingActivated = false;
|
||||
doneCurrent();
|
||||
|
||||
return pickedShape;
|
||||
return pickedPosition;
|
||||
}
|
||||
|
||||
@ -75,7 +75,7 @@ private:
|
||||
void initGeometries();
|
||||
void initBuffers();
|
||||
void deselect();
|
||||
Shape* pickGeom(int, int);
|
||||
QMatrix4x4 pickGeom(int, int);
|
||||
|
||||
// shader switching variables and constants;
|
||||
QOpenGLShaderProgram *colorPickerShaderProgram;
|
||||
@ -162,7 +162,7 @@ private:
|
||||
};
|
||||
|
||||
QOpenGLTexture *TexturePrograms[TEX_LENGTH];
|
||||
|
||||
QMatrix4x4 identityMatrix;
|
||||
|
||||
QQuaternion rot;
|
||||
unsigned int frame;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user