AAAAWWWW YYYEEEAAAAHHHH

This commit is contained in:
Dmitri K 2016-11-11 21:40:26 -05:00
parent 608172b640
commit e57d54f861
4 changed files with 139 additions and 88 deletions

View File

@ -1,6 +1,7 @@
#include "shapes.h" #include "shapes.h"
#include <QColor> #include <QColor>
#include <QVector4D> #include <QVector4D>
#include <iostream>
void Cube::accept(Visitor &v) { void Cube::accept(Visitor &v) {
v.visit(*this); v.visit(*this);
@ -13,6 +14,7 @@ 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);
} }

View File

@ -66,29 +66,32 @@ main()
{ {
vec4 texColor; vec4 texColor;
if(drawTextures) {
texColor = texture(tex, texCoords);
} else {
texColor = ifColor;
}
if(isPickingMode){ if(isPickingMode){
fColor = ifColor; fColor = ifColor;
} else if(isLightSource) { }else{
fColor = texColor; if(drawTextures) {
} else if(isSky) { texColor = texture(tex, texCoords);
fColor = skyMult * normalize(texColor*texColor*texColor*2/1.41)*2; } else {
} else if(isPhong) { texColor = ifColor;
// Get lighting vectors }
vec3 LightDirection = normalize(lDirection);
vec3 nfNormal = normalize(fNormal);
vec3 nviewDirection = normalize(fPosition);
fColor = calcDirLight(texColor, fPosition, fNormal) if(isLightSource) {
+ calcPointLight(texColor, fPosition, fNormal, 0)/4 fColor = texColor;
+ calcPointLight(texColor, fPosition, fNormal, 1)/4 } else if(isSky) {
+ calcPointLight(texColor, fPosition, fNormal, 2)/4; fColor = skyMult * normalize(texColor*texColor*texColor*2/1.41)*2;
} else { } else if(isPhong) {
fColor = texColor * ifColor; // Get lighting vectors
} vec3 LightDirection = normalize(lDirection);
vec3 nfNormal = normalize(fNormal);
vec3 nviewDirection = normalize(fPosition);
fColor = calcDirLight(texColor, fPosition, fNormal)
+ calcPointLight(texColor, fPosition, fNormal, 0)/4
+ calcPointLight(texColor, fPosition, fNormal, 1)/4
+ calcPointLight(texColor, fPosition, fNormal, 2)/4;
} else {
fColor = texColor * ifColor;
}
}
} }

View File

@ -35,6 +35,8 @@
#include <stack> #include <stack>
#include <QMouseEvent> #include <QMouseEvent>
#include <typeinfo> #include <typeinfo>
#include <time.h>
using namespace std; using namespace std;
#define BUFFER_OFFSET(i) ((char *)NULL + (i)) #define BUFFER_OFFSET(i) ((char *)NULL + (i))
@ -65,8 +67,8 @@ namespace
class SkyboxCamera : public qglviewer::Camera class SkyboxCamera : public qglviewer::Camera
{ {
virtual qreal zNear() const { return 0.01; } virtual qreal zNear() const { return 0.001; }
virtual qreal zFar() const { return 100.0; } virtual qreal zFar() const { return 1000.0; }
}; };
Viewer::Viewer() Viewer::Viewer()
@ -128,19 +130,23 @@ void Viewer::drawSkybox()
void Viewer::draw() void Viewer::draw()
{ {
drawSkybox();
glCullFace( GL_BACK );
// Bind our vertex/fragment shaders // Bind our vertex/fragment shaders
m_program->bind(); m_program->bind();
glClear(GL_COLOR_BUFFER_BIT);
if(!isPickingActivated)
drawSkybox();
glCullFace( GL_BACK );
// Get projection and camera transformations // Get projection and camera transformations
QMatrix4x4 projectionMatrix; QMatrix4x4 projectionMatrix;
QMatrix4x4 modelViewMatrix; QMatrix4x4 modelViewMatrix;
camera()->getProjectionMatrix(projectionMatrix); camera()->getProjectionMatrix(projectionMatrix);
camera()->getModelViewMatrix(modelViewMatrix); camera()->getModelViewMatrix(modelViewMatrix);
modelViewMatrix.rotate(30,0,1,0); //modelViewMatrix.rotate(30,0,1,0);
m_program->setUniformValue(m_projMatrixLocation, projectionMatrix); m_program->setUniformValue(m_projMatrixLocation, projectionMatrix);
m_program->setUniformValue(m_mvMatrixLocation, modelViewMatrix); m_program->setUniformValue(m_mvMatrixLocation, modelViewMatrix);
@ -174,34 +180,22 @@ void Viewer::mouseMoveEvent(QMouseEvent* e) {
void Viewer::mousePressEvent(QMouseEvent* e) { 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() + 21; int y = this->parentWidget()->height() - e->y() - 23;
std::cout << "--------------------------------------------------\nPicking shape at " << x << " (" << this->x() << " + " << e->x() << "), " << y << endl; 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; std::cout << "Window geom: " << this->window()->size().width() << "w, " << this->window()->size().height() << "h" << endl;
Shape* selectedShape = pickGeom(x, y); Shape* selectedShape = pickGeom(x, y);
// OpenGL coords start at the window's bottom-left, not the frame's. ugh. // OpenGL coords start at the window's bottom-left, not the frame's. ugh.
QGLViewer::mousePressEvent(e); QGLViewer::mousePressEvent(e);
} }
void Viewer::deselect(){
std::cout << "Deselecting cell " << activeCell << endl;
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(180);
shape->setColor(newColor);
}else{
std::cout << "Cell has no children, moving on" << endl;
}
this->update();
}
void Viewer::mouseReleaseEvent(QMouseEvent* e) { void Viewer::mouseReleaseEvent(QMouseEvent* e) {
//cout << "Viewer::mouseReleaseEvent(QMouseEvent* e)" << endl; //cout << "Viewer::mouseReleaseEvent(QMouseEvent* e)" << endl;
//m_program->setUniformValue(m_isPickingModeLoc, false); //m_program->setUniformValue(m_isPickingModeLoc, false);
m_program->bind();
m_program->setUniformValue(m_isPickingModeLoc, false);
isPickingActivated = false;
QGLViewer::mouseReleaseEvent(e); QGLViewer::mouseReleaseEvent(e);
} }
@ -227,13 +221,13 @@ void Viewer::init()
// Init shaders & geometry // Init shaders & geometry
initShaders(); initShaders();
initGeometries(); initGeometries();
initBuffers();
sunRotate.rotate(-15,0,0,1); sunRotate.rotate(-15,0,0,1);
sun = sunRotate * sun; sun = sunRotate * sun;
{ {
Shape* cube = new Cube();
cube->setColor(*activeColor);
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);
@ -243,6 +237,15 @@ void Viewer::init()
Shape* s2 = new Sphere(); Shape* s2 = new Sphere();
Shape* s3 = 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);
s1->transform.rotate(360 * 1/3,0,1,0); s1->transform.rotate(360 * 1/3,0,1,0);
s1->transform.translate(1,0,0); s1->transform.translate(1,0,0);
s1->transform.scale(0.05); s1->transform.scale(0.05);
@ -265,10 +268,16 @@ void Viewer::init()
selection->addChild(s2); selection->addChild(s2);
selection->addChild(s3); selection->addChild(s3);
SceneGroup *c = new SceneGroup(); SceneGroup *sc1 = new SceneGroup();
c->addChild(cube); SceneGroup *sc2 = new SceneGroup();
c->addChild(selection); SceneGroup *sc3 = new SceneGroup();
root.addChild(c); sc1->addChild(cube1);
sc1->addChild(selection);
sc2->addChild(cube2);
sc3->addChild(cube3);
root.addChild(sc1);
root.addChild(sc2);
root.addChild(sc3);
} }
} }
@ -589,6 +598,15 @@ void Viewer::initGeometries()
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(sphereIndices), sphereIndices, GL_STATIC_DRAW); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(sphereIndices), sphereIndices, GL_STATIC_DRAW);
} }
void Viewer::initBuffers(){
glGenRenderbuffers(1, m_RenderBuffers);
glBindRenderbuffer(GL_RENDERBUFFER, m_RenderBuffers[RenderBuffer_Main]);
glRenderbufferStorage(GL_RENDERBUFFER, GL_RGB565, 1920, 1080);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, m_RenderBuffers[RenderBuffer_Main]);
}
void Viewer::visit(Cube &s) void Viewer::visit(Cube &s)
{ {
@ -597,24 +615,29 @@ void Viewer::visit(Cube &s)
int faces = floor(numVerticesCube/6); int faces = floor(numVerticesCube/6);
glBindVertexArray(m_VAOs[VAO_Cube]); glBindVertexArray(m_VAOs[VAO_Cube]);
for(int i =0; i<faces; i++){ for(int i =0; i<faces; i++)
{
QColor* faceColor = new QColor;
faceColor->setRgb(s.getColor().rgb() + i);
m_program->bind();
m_program->setUniformValue(m_isSkyLoc, false); m_program->setUniformValue(m_isSkyLoc, false);
m_program->setUniformValue(m_mvMatrixLocation, modelViewMatrix); m_program->setUniformValue(m_mvMatrixLocation, modelViewMatrix);
m_program->setUniformValue(m_normalMatrixLoc, modelViewMatrix.normalMatrix()); m_program->setUniformValue(m_normalMatrixLoc, modelViewMatrix.normalMatrix());
m_program->setUniformValue(m_colorLocation, *(new QColor(s.getColor().rgb()+i))); m_program->setUniformValue(m_colorLocation, *faceColor);
m_program->setUniformValue(m_drawTextLoc, true); m_program->setUniformValue(m_drawTextLoc, true);
m_program->setUniformValue(m_isLightLoc, false); m_program->setUniformValue(m_isLightLoc, false);
glDrawArrays(GL_TRIANGLES, i*6, 6); glDrawArrays(GL_TRIANGLES, i*6, 6);
glFinish();
delete faceColor;
} }
//glDrawArrays(GL_TRIANGLES, 0, 36); //glDrawArrays(GL_TRIANGLES, 0, 36);
} }
void Viewer::visit(Sphere &s) void Viewer::visit(Sphere &s)
{ {
// std::cout << "Sphere found"; // std::cout << "Sphere found";
QMatrix4x4 modelViewMatrix = modelStack.top() * QMatrix4x4(s.transform); QMatrix4x4 modelViewMatrix = modelStack.top() * QMatrix4x4(s.transform);
if(!isPickingActivated){ if(!isPickingActivated){
glBindVertexArray(m_VAOs[VAO_Sphere]); glBindVertexArray(m_VAOs[VAO_Sphere]);
@ -664,7 +687,9 @@ void Viewer::visit(SceneGroup &s)
modelStack.pop(); modelStack.pop();
} }
void Viewer::setPhong(bool on) { void Viewer::setPhong(bool on) {
m_program->bind();
m_program->setUniformValue(m_isPhongLoc, on); m_program->setUniformValue(m_isPhongLoc, on);
if(on) std::cout << "Phong ON\n"; if(on) std::cout << "Phong ON\n";
else std::cout << "Phong OFF\n"; else std::cout << "Phong OFF\n";
@ -682,28 +707,30 @@ void Viewer::changeColor(QColor c){
Shape* Viewer::pickGeom(int x, int y){ Shape* Viewer::pickGeom(int x, int y){
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);
isPickingActivated = true; isPickingActivated = true;
QMap<QRgb, Shape*> mapColorToShape; QMap<QRgb, Shape*> mapColorToShape;
QColor c; QColor c;
QRgb startColor = 0xFF0001; // alpha must be 100%, glReadPixels doesn't resturn alpha QRgb startColor = 0x1; // alpha must be 100%, glReadPixels doesn't resturn alpha
unsigned char pixelData[3]; 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;
std::cout << "Iterating through " << root.getChildren()->size() << "items"<<endl; // Give each cube a color, let each cube manage their face-color assignment
std::cout << "Iterating through " << root.getChildren()->size() << " items"<<endl;
for(int i = 0, l = root.getChildren()->size(); i<l; i++) int i = 0;
while(root.hasNext())
{ {
std::cout << " iterating... " << i << endl; std::cout << " iterating... " << i << endl;
SceneGroup* current = dynamic_cast<SceneGroup*>(root.childAt(i)); SceneGroup* current = dynamic_cast<SceneGroup*>(root.getChild());
Shape* currentCube; Shape* currentCube;
if(current->getChildren()->size()) if(current->getChildren()->size())
@ -711,42 +738,49 @@ Shape* Viewer::pickGeom(int x, int y){
currentCube = dynamic_cast<Shape*>(current->childAt(0)); currentCube = dynamic_cast<Shape*>(current->childAt(0));
} }
if(currentCube) currentCube->setColor(c);
if(currentCube) for (int j = 0; j<6; j++)
{ {
c.setRgb(startColor); c.setRgb(startColor);
mapColorToShape.insert(c.rgb(), currentCube); // 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; std::cout << "Setting " << currentCube << " to " << c.red() << " " << c.green() << " " << c.blue() << " " << c.alpha() << endl;
currentCube->setColor(c); startColor++;
startColor+=6;
} }
} }
root.accept(*this);
draw();
glFinish();
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
// for debugging purposes // for debugging purposes
/*QImage *im = new QImage(128,128, QImage::Format_RGB32); /*unsigned char pdata[512][512][4];
for(int i = 0; i< 128; i++){ glReadPixels(x-256, y-256, 512, 512, GL_RGBA, GL_UNSIGNED_BYTE, pdata);
for(int j = 0; j< 128; j++){ QImage *im = new QImage(512, 512, QImage::Format_RGB32);
glReadPixels(x-64+i, y+64-j, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixelData); for(int i = 0; i< 512; i++){
QColor* pickedColor = new QColor(pixelData[0], pixelData[1], pixelData[2]); for(int j = 0; j< 512; j++){
if(i==64&&j==64) pickedColor->setRgba(0xFFFFFFFF); QColor* pickedColor = new QColor();
pickedColor->setRgb(pdata[512-j][i][0], pdata[512-j][i][1], pdata[512-j][i][2]);
if(i==256 && j == 256) pickedColor->setRgba(0x88FFFFFF);
if(i==256 && j == 256)
std::cout<<"--- Color under cursor: " << (int)pdata[j][i][0] <<" "<< (int)pdata[j][i][1] <<" "<< (int)pdata[j][i][2] << endl;
pickedColor->setAlpha(255);
im->setPixelColor(i, j, pickedColor->rgb()); im->setPixelColor(i, j, pickedColor->rgb());
} }
} }
im->save("./screenshot.bmp");//*/ im->save("./screenshot.bmp");//*/
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();
Shape* pickedShape = mapColorToShape.value(pickedInt - (pickedInt % 6) + 1); Shape* pickedShape = mapColorToShape.value(pickedInt - (pickedInt % 6) + 1);
std::cout << "Picked Color: " << pickedColor->red() << " " << pickedColor->green() << " " << pickedColor->blue() << " " << pickedColor->alpha() << endl; std::cout << "Picked Color: " << pickedColor->red() << " " << pickedColor->green() << " " << pickedColor->blue() << " " << pickedColor->alpha() << endl;
std::cout << "Picked Shape: " << pickedShape << endl; std::cout << "Picked Shape: " << pickedShape << endl;
m_program->setUniformValue(m_isPickingModeLoc, false); m_program->setUniformValue(m_isPickingModeLoc, false);
isPickingActivated = false; isPickingActivated = false;
update(); doneCurrent();
return pickedShape;
return pickedShape;
} }

View File

@ -72,7 +72,8 @@ protected :
private: private:
void initShaders(); void initShaders();
void initGeometries(); void initGeometries();
void initBuffers();
void deselect(); void deselect();
Shape* pickGeom(int, int); Shape* pickGeom(int, int);
@ -121,11 +122,13 @@ private:
QColor* activeColor; QColor* activeColor;
int activeShape; int activeShape;
enum VAO_IDs { VAO_Cube, VAO_Sphere, NumVAOs }; enum VAO_IDs { VAO_Cube, VAO_Sphere, NumVAOs };
enum Buffer_IDs { VBO_Cube, VBO_Sphere, EBO_Sphere, NumBuffers }; enum Buffer_IDs { VBO_Cube, VBO_Sphere, EBO_Sphere, NumBuffers };
enum RenderBuffer_IDs { RenderBuffer_Main, NumRenderBuffers };
GLuint m_VAOs[NumVAOs]; GLuint m_VAOs[NumVAOs];
GLuint m_Buffers[NumBuffers]; GLuint m_Buffers[NumBuffers];
GLuint m_RenderBuffers[NumRenderBuffers];
Shape* generateShapeFromIndex(int); Shape* generateShapeFromIndex(int);
@ -149,6 +152,15 @@ private:
"src/data/wood_floor.jpg" "src/data/wood_floor.jpg"
}; };
int sideColors[6] = {
0xFF0000,
0x00FF00,
0x0000FF,
0x00FFFF,
0xFF00FF,
0xFFFF00,
};
QOpenGLTexture *TexturePrograms[TEX_LENGTH]; QOpenGLTexture *TexturePrograms[TEX_LENGTH];