view Framework/OpenGL/OpenGLProgram.cpp @ 1411:08fd094f9582

Merge
author Benjamin Golinvaux <bgo@osimis.io>
date Mon, 04 May 2020 13:34:46 +0200
parents 2d8ab34c8c91
children 30deba7bc8e2
line wrap: on
line source

/**
 * Stone of Orthanc
 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
 * Department, University Hospital of Liege, Belgium
 * Copyright (C) 2017-2020 Osimis S.A., Belgium
 *
 * This program is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Affero General Public License
 * as published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 **/


#include "OpenGLProgram.h"

#include "OpenGLShader.h"
#include "IOpenGLContext.h"

#include <Core/OrthancException.h>

namespace OrthancStone
{
  namespace OpenGL
  {
    OpenGLProgram::OpenGLProgram(OpenGL::IOpenGLContext& context)
      : context_(context)
    {
      program_ = glCreateProgram();
      ORTHANC_OPENGL_CHECK("glCreateProgram");
      if (program_ == 0)
      {
        throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError,
                                        "Cannot create an OpenGL program");
      }
    }


    OpenGLProgram::~OpenGLProgram()
    {
      try
      {
        if (!context_.IsContextLost())
        {
          ORTHANC_OPENGL_TRACE_CURRENT_CONTEXT("About to call glDeleteProgram");
          assert(program_ != 0);
          glDeleteProgram(program_);
          ORTHANC_OPENGL_CHECK("glDeleteProgram");
        }
      }
      catch (const Orthanc::OrthancException& e)
      {
        if (e.HasDetails())
        {
          LOG(ERROR) << "OrthancException in ~OpenGLProgram: " << e.What() << " Details: " << e.GetDetails();
        }
        else
        {
          LOG(ERROR) << "OrthancException in ~OpenGLProgram: " << e.What();
        }
      }
      catch (const std::exception& e)
      {
        LOG(ERROR) << "std::exception in ~OpenGLProgram: " << e.what();
      }
      catch (...)
      {
        LOG(ERROR) << "Unknown exception in ~OpenGLProgram";
      }
    }

    void OpenGLProgram::Use()
    {
      //ORTHANC_OPENGL_TRACE_CURRENT_CONTEXT("About to call glUseProgram");
      glUseProgram(program_);
      ORTHANC_OPENGL_CHECK("glUseProgram");
    }
        
    void OpenGLProgram::CompileShaders(const std::string& vertexCode,
                                       const std::string& fragmentCode)
    {
      assert(program_ != 0);

      OpenGLShader vertexShader(GL_VERTEX_SHADER, vertexCode);
      OpenGLShader fragmentShader(GL_FRAGMENT_SHADER, fragmentCode);

      glAttachShader(program_, vertexShader.Release());
      ORTHANC_OPENGL_CHECK("glAttachShader");
      glAttachShader(program_, fragmentShader.Release());
      ORTHANC_OPENGL_CHECK("glAttachShader");
      glLinkProgram(program_);
      ORTHANC_OPENGL_CHECK("glLinkProgram");
      glValidateProgram(program_);
      ORTHANC_OPENGL_CHECK("glValidateProgram");
    }

    GLint OpenGLProgram::GetUniformLocation(const std::string& name)
    { 
      GLint location = glGetUniformLocation(program_, name.c_str());
      ORTHANC_OPENGL_CHECK("glGetUniformLocation");

      if (location == -1)
      {
        throw Orthanc::OrthancException(Orthanc::ErrorCode_InexistentItem,
                                        "Inexistent uniform variable in shader: " + name);
      }
      else
      {
        return location;
      }
    }

    
    GLint OpenGLProgram::GetAttributeLocation(const std::string& name)
    { 
      GLint location = glGetAttribLocation(program_, name.c_str());
      ORTHANC_OPENGL_CHECK("glGetAttribLocation");

      if (location == -1)
      {
        throw Orthanc::OrthancException(Orthanc::ErrorCode_InexistentItem,
                                        "Inexistent attribute in shader: " + name);
      }
      else
      {
        return location;
      }
    }
  }
}