Exemplo n.º 1
0
void MainWindow::render()
{
    glViewport(0, 0, width(), height());

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    m_program->bind();


    GLfloat colors[] = {
        1.0f, 0.0f, 0.0f,
        0.0f, 1.0f, 0.0f,
        0.0f, 0.0f, 1.0f
    };

    time_t now;
//    time(&now);
    difftime(time, now);
    double move = 1;
    if(speed != 0)
    {
        move = speed * m_frame / screen()->refreshRate();
    }



    QMatrix4x4 scale;
    QMatrix4x4 orbitRotMatrix;
    QMatrix4x4 orbitMatrix;
    QMatrix4x4 rotMatrix;
    scale.scale(4.0f);
    rotMatrix.rotate(rotateY * move, 0, 1, 0);
    cg::Planet sonne("sonne", &myMesh->v[0], scale, rotMatrix, orbitMatrix, orbitRotMatrix);

    scale.setToIdentity();
    orbitRotMatrix.setToIdentity();
    orbitMatrix.setToIdentity();
    rotMatrix.setToIdentity();
    scale.scale(0.5f);
    orbitRotMatrix.rotate(rotateY * move, 0, 1, 0);
    orbitMatrix.translate(5.0f, 0, 0);
    rotMatrix.rotate(rotateY * move, 0, 1, 0);
    cg::Planet erde("erde", &myMesh->v[0], scale, rotMatrix, orbitMatrix, orbitRotMatrix);

    scale.setToIdentity();
    orbitRotMatrix.setToIdentity();
    orbitMatrix.setToIdentity();
    rotMatrix.setToIdentity();
    scale.scale(0.3f);
    orbitRotMatrix.rotate(rotateY * move, 0, 1, 0);
    orbitMatrix.translate(1.0f, 0, 0);
    rotMatrix.rotate(rotateY * move, 0, 1, 0);
    cg::Planet mond("mond", &myMesh->v[0], scale, rotMatrix, orbitMatrix, orbitRotMatrix);

    scale.setToIdentity();
    orbitRotMatrix.setToIdentity();
    orbitMatrix.setToIdentity();
    rotMatrix.setToIdentity();
    scale.scale(0.5f);
    orbitRotMatrix.rotate(0.5 * rotateY * move + 15, 0, 1, 0);
    orbitMatrix.translate(10.0f, 0, 0);
    rotMatrix.rotate(rotateY * move, 0, 1, 0);
    cg::Planet mars("mars", &myMesh->v[0], scale, rotMatrix, orbitMatrix, orbitRotMatrix);

    scale.setToIdentity();
    orbitRotMatrix.setToIdentity();
    orbitMatrix.setToIdentity();
    rotMatrix.setToIdentity();
    scale.scale(0.3f);
    orbitRotMatrix.rotate(rotateY * move, 0, 1, 0);
    orbitMatrix.translate(1.0f, 0, 0);
    rotMatrix.rotate(rotateY * move, 0, 1, 0);
    cg::Planet deimos("deimos", &myMesh->v[0], scale, rotMatrix, orbitMatrix, orbitRotMatrix);

    scale.setToIdentity();
    orbitRotMatrix.setToIdentity();
    orbitMatrix.setToIdentity();
    rotMatrix.setToIdentity();
    scale.scale(0.15f);
    orbitRotMatrix.rotate(rotateY * move *15, 0, 1, 0);
    orbitMatrix.translate(1.0f, 0, 0);
    rotMatrix.rotate(rotateY * move, 0, 1, 0);
    cg::Planet phobos("phobos", &myMesh->v[0], scale, rotMatrix, orbitMatrix, orbitRotMatrix);

    std::vector<cg::Planet*> vecPlanet {&sonne, &erde, &mond, &mars, &deimos, &phobos};
    cg::Sunsystem sys;
    sys.addChild(&sonne)->addChild(&erde)->addChild(&mond);
    sys.addChild(&mars)->addChild(&deimos);
    sys.getChild(1)->addChild(&phobos);
    sys.run();


    QMatrix4x4 camMatrix;
    camMatrix.perspective(60.f, 4.0f/3.0f, 0.1f, 100.0f);
    camMatrix.translate(0, 0, -15);
    camMatrix.rotate(45, 1, 0, 0);



    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
//    glVertexAttribPointer(m_posAttr, 3, GL_FLOAT, GL_FALSE, 0, &myMesh->v[0]);
//    glVertexAttribPointer(m_colAttr, 3, GL_FLOAT, GL_FALSE, 0, colors);

    glEnableVertexAttribArray(0);
//    glEnableVertexAttribArray(1);


    for(cg::Planet *n : vecPlanet)
    {
        m_program->setUniformValue(m_matrixUniform, camMatrix * n->resultmatrix);
        //glDrawArrays(GL_TRIANGLES, 0, myMesh->v.size()/3);
        glDrawElements(GL_TRIANGLES, myMesh->v_f.size(), GL_UNSIGNED_INT, 0);
//        glDrawElements(GL_TRIANGLES, myMesh->v_f.size(), GL_UNSIGNED_SHORT, &myMesh->v_f[0]);
    }


    glDisableVertexAttribArray(1);
    glDisableVertexAttribArray(0);

    m_program->release();

    ++m_frame;
//    time(&time);
}
int main(int argc, char* argv[])
{
    std::shared_ptr<Material> black = std::make_shared<Material>();
    float depth = 100.0;
    glm::vec3 offset(0, 0, 0);
    Box test_box(glm::vec3{-1.0,-1.0,5.0} + offset, glm::vec3{1.0,1.0,6.0} + offset,"testbox", black);
    Sphere test_kreis(glm::vec3{0.0,0.0,5.0}, 1.0,"testkreis", black);
    Triangle test_triangle({1,0,5},{-1,0,5},{0,1,5});
    Light sonne("bla" ,{5.0,5.0,0.0}, {0.0,10.1,10.0}, {10.0,10.1,10.0});

  int win_size_x=400;
  int win_size_y=400;
  float xpos_pro=1/float(win_size_x);
  float ypos_pro=1/float(win_size_y);
  Window win(glm::ivec2(win_size_x,win_size_y));



	glm::vec3 eye{0, 0, 0};
	float focal_length = 2.0;

	float t = 0;

	win.update();



while (!win.shouldClose()) {

  for(int i=0; i<win_size_x; i++){
    for(int u=0; u<win_size_y; u++){

      float x = float(i) / win_size_x;
      float y = float(u) / win_size_y;

      auto ray_dir = glm::normalize(glm::vec3{2*x-1, 2*y-1, focal_length});
      Ray charles(eye, ray_dir);
      Intersection isec = test_kreis.intersect(charles);
      // std::cout<<isec<<std::endl;
      glm::vec3 sunPos = sonne.getPos();
      glm::vec3 Lichtstrahl = sunPos - isec.position;
      float phong = float(cos(glm::dot(Lichtstrahl, isec.normal)*3.14159/180));

      glm::vec3 kreisPos = test_kreis.getcenter();
    std::cout<<isec.position[0]<<" "<<isec.position[1]<<" "<<isec.position[2]<<" "<<std::endl;
      if (isec.hit){
        win.drawPoint(x,y, 255*phong, 
                           255*phong, 
                           255*phong);
         }
            
    }
    
  }
  // eye.y = 2.0 * std::sin(t);
  // eye.x = 2.0 * std::cos(t);
  // float t = 1.0;
  // // t += 1e-2;
  win.update();
  
}

  return 0;
}
Exemplo n.º 3
0
void MainWindow::render()
{
    glViewport(0, 0, width(), height());

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    m_program->bind();


//    GLfloat colors[] = {
//        1.0f, 0.0f, 0.0f,
//        0.0f, 1.0f, 0.0f,
//        0.0f, 0.0f, 1.0f
//    };


    now = clock();
    m_frame = m_frame + speed*difftime(now, lastTime);
    lastTime = now;


    // Planet Matrix
    scale.scale(4.0f);
    rotMatrix.rotate(m_frame * 0.2, 0, 1, 0);
    cg::Planet sonne("sonne", &myMesh->v[0], scale, rotMatrix, orbitMatrix, orbitRotMatrix);

    scale.setToIdentity();
    orbitRotMatrix.setToIdentity();
    orbitMatrix.setToIdentity();
    rotMatrix.setToIdentity();
    scale.scale(0.5f);
    orbitRotMatrix.rotate(m_frame, 0, 1, 0);
    orbitMatrix.translate(5.0f, 0, 0);
    rotMatrix.rotate(m_frame, 0, 1, 0);
    cg::Planet erde("erde", &myMesh->v[0], scale, rotMatrix, orbitMatrix, orbitRotMatrix);

    scale.setToIdentity();
    orbitRotMatrix.setToIdentity();
    orbitMatrix.setToIdentity();
    rotMatrix.setToIdentity();
    scale.scale(0.3f);
    orbitRotMatrix.rotate(m_frame, 0, 1, 0);
    orbitMatrix.translate(1.0f, 0, 0);
    rotMatrix.rotate(m_frame, 0, 1, 0);
    cg::Planet mond("mond", &myMesh->v[0], scale, rotMatrix, orbitMatrix, orbitRotMatrix);

    scale.setToIdentity();
    orbitRotMatrix.setToIdentity();
    orbitMatrix.setToIdentity();
    rotMatrix.setToIdentity();
    scale.scale(0.5f);
    orbitRotMatrix.rotate(0.5 * m_frame + 15, 0, 1, 0);
    orbitMatrix.translate(10.0f, 0, 0);
    rotMatrix.rotate(m_frame, 0, 1, 0);
    cg::Planet mars("mars", &myMesh->v[0], scale, rotMatrix, orbitMatrix, orbitRotMatrix);

    scale.setToIdentity();
    orbitRotMatrix.setToIdentity();
    orbitMatrix.setToIdentity();
    rotMatrix.setToIdentity();
    scale.scale(0.3f);
    orbitRotMatrix.rotate(m_frame, 0, 1, 0);
    orbitMatrix.translate(1.0f, 0, 0);
    rotMatrix.rotate(m_frame, 0, 1, 0);
    cg::Planet deimos("deimos", &myMesh->v[0], scale, rotMatrix, orbitMatrix, orbitRotMatrix);

    scale.setToIdentity();
    orbitRotMatrix.setToIdentity();
    orbitMatrix.setToIdentity();
    rotMatrix.setToIdentity();
    scale.scale(0.15f);
    orbitRotMatrix.rotate(m_frame *15, 0, 1, 0);
    orbitMatrix.translate(1.0f, 0, 0);
    rotMatrix.rotate(m_frame, 0, 1, 0);
    cg::Planet phobos("phobos", &myMesh->v[0], scale, rotMatrix, orbitMatrix, orbitRotMatrix);

    std::vector<cg::Planet*> vecPlanet {&sonne, &erde, &mond, &mars, &deimos, &phobos};
    cg::Sunsystem sys;
    sys.addChild(&sonne)->addChild(&erde)->addChild(&mond);
    sys.addChild(&mars)->addChild(&deimos);
    sys.getChild(1)->addChild(&phobos);
    sys.run();


    projMatrix.setToIdentity();
    //projMatrix.perspective(60.f, 4.0f/3.0f, 0.1f, 100.0f);
    projMatrix.perspective(60.f, width()/height(), 0.1f, 100.0f);
    viewMatrix.setToIdentity();
    viewMatrix.translate(0, 0, -15);
    viewMatrix.rotate(45, 1, 0, 0);


    QVector3D ambient(0.2, 0.2, 0.2);
    QVector3D diffuse_albedo(1.0, 0.88, 0.25);
    QVector3D specular_albedo(1.0, 1.0, 1.0);
    GLfloat specular_power = 128.0;
    QVector3D light_color(0.2, 0.7, 0.2);

    for(cg::Planet* n : vecPlanet)
    {
        m_program->setUniformValue(m_mv_matrix, n->resultmatrix);
        m_program->setUniformValue(m_view_matrix, viewMatrix);
        m_program->setUniformValue(m_proj_matrix, projMatrix);

        m_program->setUniformValue(m_ambient, ambient);
        m_program->setUniformValue(m_diffuse_albedo, diffuse_albedo);
        m_program->setUniformValue(m_specular_albedo, specular_albedo);
        m_program->setUniformValue(m_specular_power, specular_power);


        for(std::vector<GLfloat>& light : lightPositions)
        {
            // Because of QT
            QVector4D lightPosition (light[0], light[1], light[2], light[3]);

            m_program->setUniformValue(m_light, lightPosition);
            m_program->setUniformValue(m_light_color, light_color);
            glDrawElements(GL_TRIANGLES, myMesh->v_f.size(), GL_UNSIGNED_INT, 0);
        }
    }

    m_program->release();

    //Clear Matrices
    scale.setToIdentity();
    orbitRotMatrix.setToIdentity();
    orbitMatrix.setToIdentity();
    rotMatrix.setToIdentity();
}