Ejemplo n.º 1
0
CWindowClient::CWindowClient(CWindow &window)
    : CAbstractWindowClient(window)
    , m_quadObj(QUAD_TOPLEFT, QUAD_SIZE)
{
    const glm::vec4 GREEN = { 0.15f, 0.4f, 0.15f, 1.f };
    GetWindow().SetBackgroundColor(GREEN);
    CheckOpenGLVersion();
    SetupOpenGLState();

    const std::string vertexShader = CFilesystemUtils::LoadFileAsString("res/copytexture.vert");
    const std::string checkersShader = CFilesystemUtils::LoadFileAsString("res/checkers.frag");
    const std::string pictureShader = CFilesystemUtils::LoadFileAsString("res/checkers-and-triangle.frag");

    m_programCheckers.CompileShader(vertexShader, ShaderType::Vertex);
    m_programCheckers.CompileShader(checkersShader, ShaderType::Fragment);
    m_programCheckers.Link();

    m_programPicture.CompileShader(vertexShader, ShaderType::Vertex);
    m_programPicture.CompileShader(pictureShader, ShaderType::Fragment);
    m_programPicture.Link();

    m_programQueue = { &m_programPicture, &m_programCheckers };
}
Ejemplo n.º 2
0
void CWindow::OnWindowInit(const glm::ivec2 &size)
{
    (void)size;
    SetupOpenGLState();
}
Ejemplo n.º 3
0
bool FontRenderer::DrawText(const char* text, float x, float y, float scalex, float scaley) {
  if (!_isInitialized) {
    return false;
  }

  if (_glyphTexturesDirty) {
    if (!CreateGlyphTextures()) {
      return false;
    }
  }

  SaveOpenGLState();
  SetupOpenGLState();

  glUseProgram(_fontProgram.Handle());
  glActiveTexture(GL_TEXTURE0);
  glUniform1i(_glyphTextureUniform, 0);
  glUniform4fv(_glyphColorUniform, 1, _fontColor);

  // Setup vertex format for rendering
  glBindVertexArray(_vaoGlyphCoords);
  glBindBuffer(GL_ARRAY_BUFFER, _vboGlyphCoords);

  // Loop through characters in string.
  float glyphPosx = x;
  float glyphPosy = y;
  for (const char* character = text; *character; character++) {
    const Glyph* glyph = GetGlyph(*character);

    glBindTexture(GL_TEXTURE_2D, glyph->texture);

    // Calculate the vertex and texture coordinates.
    const float posx = glyphPosx + glyph->left * scalex;
    const float posy = -glyphPosy - glyph->top * scaley;
    const float width = glyph->width * scalex;
    const float height = glyph->rows * scaley;

    // Upload coordinates to GPU.
    GlyphVertex* vertices = (GlyphVertex*)glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
    if (vertices != nullptr) {
      vertices[0] = GlyphVertex(posx,         -posy,          0.0f, 0.0f);
      vertices[1] = GlyphVertex(posx + width, -posy,          1.0f, 0.0f);
      vertices[2] = GlyphVertex(posx,         -posy - height, 0.0f, 1.0f);
      vertices[3] = GlyphVertex(posx + width, -posy - height, 1.0f, 1.0f);

      glUnmapBuffer(GL_ARRAY_BUFFER);
    }

    // Draw the character on the screen.
    glDrawArrays(GL_TRIANGLE_STRIP, 0, kNumVertices);

    // Advance the cursor to the start of the next character.
    glyphPosx += (glyph->advance_x >> 6) * scalex;
    glyphPosy += (glyph->advance_y >> 6) * scaley;
  }

  glUseProgram(0);
  glBindVertexArray(0);
  glBindTexture(GL_TEXTURE_2D, 0);
  
  RestorePriorOpenGLState();

  return (GetGLErrorVerbose() == GL_NO_ERROR);
}