Ejemplo n.º 1
0
void CameraFocusSceneLighting::modifyGLState(GLState* glState, const G3MRenderContext* rc) {

  const Camera* cam = rc->getCurrentCamera();
  const Vector3D camDir = cam->getViewDirection();
  const Vector3D up = cam->getUp();
  if (_cameraDirX == camDir._x && _cameraDirY == camDir._y && _cameraDirZ == camDir._z &&
      _upX == up._x && _upY == up._y && _upZ == up._z) {
    return;
  }

  const Vector3D cameraVector = camDir.times(-1);

  //Light slightly different of camera position
  const Vector3D rotationLightDirAxis = up.cross(cameraVector);
  const Vector3D lightDir = cameraVector.rotateAroundAxis(rotationLightDirAxis, Angle::fromDegrees(45.0));

  DirectionLightGLFeature* f = (DirectionLightGLFeature*) glState->getGLFeature(GLF_DIRECTION_LIGTH);
  if (f == NULL) {
    glState->clearGLFeatureGroup(LIGHTING_GROUP);
    glState->addGLFeature(new DirectionLightGLFeature(lightDir,
                                                      _diffuseColor,
                                                      _ambientColor),
                          false);
  }
  else{
    f->setLightDirection(lightDir);
  }

  //ADD MESH
  if (_meshRenderer != NULL) {
    Vector3D lastCamDir(_cameraDirX, _cameraDirY, _cameraDirZ);

    if (lastCamDir.angleBetween(lightDir)._degrees > 10) {

      FloatBufferBuilderFromCartesian3D* vertices = FloatBufferBuilderFromCartesian3D::builderWithFirstVertexAsCenter();
      vertices->add(cam->getCartesianPosition());
      vertices->add(cam->getCartesianPosition().add(lightDir.times(1000)) );

      DirectMesh* mesh = new DirectMesh(GLPrimitive::lines(),
                                        true,
                                        vertices->getCenter(),
                                        vertices->create(),
                                        (float)3.0,
                                        (float)1.0,
                                        new Color(Color::red()));
      _meshRenderer->addMesh(mesh);
    }
  }

  //SAVING STATE
  _cameraDirX = camDir._x;
  _cameraDirY = camDir._y;
  _cameraDirZ = camDir._z;
  
  _upX = up._x;
  _upY = up._y;
  _upZ = up._z;
  
}
Ejemplo n.º 2
0
Mesh* HUDImageRenderer::createMesh(const G3MRenderContext* rc) {
  _creatingMesh = false;

  if (_mesh != NULL) {
    delete _mesh;
    _mesh = NULL;
  }

  const IStringUtils* su = IStringUtils::instance();
  const std::string textureName = "HUDImageRenderer" + su->toString(_instanceID) + "/" + su->toString(_changeCounter++);

  const TextureIDReference* texId = rc->getTexturesHandler()->getTextureIDReference(_image,
                                                                       GLFormat::rgba(),
                                                                       textureName,
                                                                       false);
  
  delete _image;
  _image = NULL;

  if (texId == NULL) {
    rc->getLogger()->logError("Can't upload texture to GPU");
    return NULL;
  }


  const Camera* camera = rc->getCurrentCamera();

  const double halfWidth  = camera->getWidth()  / 2.0;
  const double halfHeight = camera->getHeight() / 2.0;

  FloatBufferBuilderFromCartesian3D vertices = FloatBufferBuilderFromCartesian3D::builderWithoutCenter();
  vertices.add(-halfWidth,  halfHeight, 0);
  vertices.add(-halfWidth, -halfHeight, 0);
  vertices.add( halfWidth,  halfHeight, 0);
  vertices.add( halfWidth, -halfHeight, 0);

  DirectMesh* mesh = new DirectMesh(GLPrimitive::triangleStrip(),
                                    true,
                                    vertices.getCenter(),
                                    vertices.create(),
                                    1,
                                    1);

  FloatBufferBuilderFromCartesian2D texCoords;
  texCoords.add(0, 0);
  texCoords.add(0, 1);
  texCoords.add(1, 0);
  texCoords.add(1, 1);

  TextureMapping* textureMapping = new SimpleTextureMapping(texId,
                                                            texCoords.create(),
                                                            true,
                                                            true);

  return new TexturedMesh(mesh, true,
                          textureMapping, true,
                          true);
}
Ejemplo n.º 3
0
Mesh* HUDRenderer::ShownImage::createMesh(const G3MRenderContext* rc) const {
  //TEXTURED
#ifdef C_CODE
  const TextureIDReference* texId = NULL;
#endif
#ifdef JAVA_CODE
  TextureIDReference texId = null;
#endif

  _factory = rc->getFactory();

  texId = rc->getTexturesHandler()->getTextureIDReference(_image,
                                                   GLFormat::rgba(),
                                                   _name,
                                                   false);

  if (texId == NULL) {
    rc->getLogger()->logError("Can't upload texture to GPU");
    return NULL;
  }

  const int viewportWidth  = rc->getCurrentCamera()->getWidth();
  const int viewportHeight = rc->getCurrentCamera()->getHeight();

  const Vector3D halfViewportAndPosition(viewportWidth  / 2 - _position._x,
                                         viewportHeight / 2 - _position._y,
                                         0);

  const double w = _size._x;
  const double h = _size._y;

  FloatBufferBuilderFromCartesian3D vertices = FloatBufferBuilderFromCartesian3D::builderWithoutCenter();
  vertices.add(Vector3D(0, h, 0).sub(halfViewportAndPosition));
  vertices.add(Vector3D(0, 0, 0).sub(halfViewportAndPosition));
  vertices.add(Vector3D(w, h, 0).sub(halfViewportAndPosition));
  vertices.add(Vector3D(w, 0, 0).sub(halfViewportAndPosition));

  FloatBufferBuilderFromCartesian2D texCoords;
  texCoords.add(0, 0);
  texCoords.add(0, 1);
  texCoords.add(1, 0);
  texCoords.add(1, 1);

  DirectMesh *im = new DirectMesh(GLPrimitive::triangleStrip(),
                                  true,
                                  vertices.getCenter(),
                                  vertices.create(),
                                  1,
                                  1);

  TextureMapping* texMap = new SimpleTextureMapping(texId,
                                                    texCoords.create(),
                                                    true,
                                                    false);

  return new TexturedMesh(im, true, texMap, true, true);
}
Ejemplo n.º 4
0
Mesh* CircleShape::createMesh(const G3MRenderContext* rc) {
  const IMathUtils* mu = IMathUtils::instance();

  FloatBufferBuilderFromCartesian3D* vertices = FloatBufferBuilderFromCartesian3D::builderWithoutCenter();

  // first is the center
  vertices->add(0.0, 0.0, 0.0);

  const double twicePi = PI * 2;

  for (int i = 0; i <= _steps; i++) {
    const double angleInRadians = i * twicePi / _steps;
    const double x = _radius * mu->cos(angleInRadians);
    const double y = _radius * mu->sin(angleInRadians);
    vertices->add(x, y, 0);
  }

  Color* color = (_color == NULL) ? NULL : new Color(*_color);

  if (_useNormals) {

    FloatBufferBuilderFromCartesian3D* normals = FloatBufferBuilderFromCartesian3D::builderWithoutCenter();
    for (int i = 0; i <= _steps+1; i++) {
      normals->add(0.0, 0.0, 1.0);
    }

    Mesh* result = new DirectMesh(GLPrimitive::triangleFan(),
                            true,
                            Vector3D::zero,
                            vertices->create(),
                            1,
                            1,
                            color,
                            NULL,
                            1,
                            true,
                            normals->create());

    delete normals;
    delete vertices;

    return result;

  }

  Mesh* result = new DirectMesh(GLPrimitive::triangleFan(),
                          true,
                          Vector3D::zero,
                          vertices->create(),
                          1,
                          1,
                          color);

  delete vertices;

  return result;
}
Ejemplo n.º 5
0
void Sphere::createWireframeMesh(Color* color,
                                 short resolution) const {
  IMathUtils* mu = IMathUtils::instance();
  const double delta = PI / (resolution-1);

  // create vertices
//  FloatBufferBuilderFromCartesian3D vertices(CenterStrategy::firstVertex(), Vector3D::zero);
  FloatBufferBuilderFromCartesian3D vertices = FloatBufferBuilderFromCartesian3D::builderWithFirstVertexAsCenter();
  for (int i=0; i<2*resolution-2; i++) {
    const double longitude = -PI + i*delta;
    for (int j=0; j<resolution; j++) {
      const double latitude = -PI/2 + j*delta;
      const double h = mu->cos(latitude);
      const double x = h * mu->cos(longitude);
      const double y = h * mu->sin(longitude);
      const double z = mu->sin(latitude);
      vertices.add(Vector3D(x,y,z).times(_radius).add(_center));
    }
  }

  // create border indices for vertical lines
  ShortBufferBuilder indices;
  for (short i=0; i<2*resolution-2; i++) {
    for (short j=0; j<resolution-1; j++) {
      indices.add((short) (j+i*resolution));
      indices.add((short) (j+1+i*resolution));
    }
  }

  // create border indices for horizontal lines
  for (short j=1; j<resolution-1; j++) {
    for (short i=0; i<2*resolution-3; i++) {
      indices.add((short) (j+i*resolution));
      indices.add((short) (j+(i+1)*resolution));
    }
  }
  for (short j=1; j<resolution-1; j++) {
    const short i = (short) (2*resolution-3);
    indices.add((short) (j+i*resolution));
    indices.add((short) (j));
  }

  _mesh = new IndexedMesh(GLPrimitive::lines(),
                          true,
                          vertices.getCenter(),
                          vertices.create(),
                          indices.create(),
                          1,
                          1,
                          color);
}
Ejemplo n.º 6
0
Mesh* BusyMeshRenderer::createMesh(const G3MRenderContext* rc) {
  const int numStrides = 5;

  FloatBufferBuilderFromCartesian3D* vertices = FloatBufferBuilderFromCartesian3D::builderWithoutCenter();

  FloatBufferBuilderFromColor colors;
  ShortBufferBuilder indices;

  int indicesCounter = 0;

  const float innerRadius = 0;

//  const float r2=50;
  const Camera* camera = rc->getCurrentCamera();
  const int width  = camera->getWidth();
  const int height = camera->getHeight();
  const int minSize = (width < height) ? width : height;
  const float outerRadius = minSize / 15.0f;

  const IMathUtils* mu = IMathUtils::instance();

  for (int step = 0; step <= numStrides; step++) {
    const double angle = (double) step * 2 * PI / numStrides;
    const double c = mu->cos(angle);
    const double s = mu->sin(angle);

    vertices->add( (innerRadius * c), (innerRadius * s), 0);
    vertices->add( (outerRadius * c), (outerRadius * s), 0);

    indices.add((short) (indicesCounter++));
    indices.add((short) (indicesCounter++));

    //    float col = (float) (1.0 * step / numStrides);
    //    if (col>1) {
    //      colors.add(1, 1, 1, 0);
    //      colors.add(1, 1, 1, 0);
    //    }
    //    else {
    //      colors.add(1, 1, 1, 1 - col);
    //      colors.add(1, 1, 1, 1 - col);
    //    }

    //    colors.add(Color::red().wheelStep(numStrides, step));
    //    colors.add(Color::red().wheelStep(numStrides, step));

    colors.add(1, 1, 1, 1);
    colors.add(1, 1, 1, 0);
  }

  // the two last indices
  indices.add((short) 0);
  indices.add((short) 1);


  Mesh* result = new IndexedMesh(GLPrimitive::triangleStrip(),
                                true,
                                vertices->getCenter(),
                                vertices->create(),
                                indices.create(),
                                1,
                                1,
                                NULL,
                                colors.create());
  delete vertices;

  return result;
}