Mercurial > hg > orthanc-stone
comparison OrthancStone/Sources/OpenGL/OpenGLTexture.cpp @ 1512:244ad1e4e76a
reorganization of folders
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 07 Jul 2020 16:21:02 +0200 |
parents | Framework/OpenGL/OpenGLTexture.cpp@30deba7bc8e2 |
children | e731e62692a9 |
comparison
equal
deleted
inserted
replaced
1511:9dfeee74c1e6 | 1512:244ad1e4e76a |
---|---|
1 /** | |
2 * Stone of Orthanc | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium | |
6 * | |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU Affero General Public License | |
9 * as published by the Free Software Foundation, either version 3 of | |
10 * the License, or (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, but | |
13 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * Affero General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Affero General Public License | |
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 **/ | |
20 | |
21 | |
22 #include "OpenGLTexture.h" | |
23 #include "IOpenGLContext.h" | |
24 | |
25 #include <OrthancException.h> | |
26 | |
27 namespace OrthancStone | |
28 { | |
29 namespace OpenGL | |
30 { | |
31 OpenGLTexture::OpenGLTexture(OpenGL::IOpenGLContext& context) | |
32 : width_(0) | |
33 , height_(0) | |
34 , context_(context) | |
35 { | |
36 if (!context_.IsContextLost()) | |
37 { | |
38 // Generate a texture object | |
39 glGenTextures(1, &texture_); | |
40 if (texture_ == 0) | |
41 { | |
42 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError, | |
43 "Cannot create an OpenGL program"); | |
44 } | |
45 } | |
46 } | |
47 | |
48 OpenGLTexture::~OpenGLTexture() | |
49 { | |
50 try | |
51 { | |
52 if (!context_.IsContextLost()) | |
53 { | |
54 assert(texture_ != 0); | |
55 ORTHANC_OPENGL_TRACE_CURRENT_CONTEXT("About to call glDeleteTextures"); | |
56 glDeleteTextures(1, &texture_); | |
57 } | |
58 } | |
59 catch (const Orthanc::OrthancException& e) | |
60 { | |
61 if (e.HasDetails()) | |
62 { | |
63 LOG(ERROR) << "OrthancException in ~OpenGLTexture: " << e.What() << " Details: " << e.GetDetails(); | |
64 } | |
65 else | |
66 { | |
67 LOG(ERROR) << "OrthancException in ~OpenGLTexture: " << e.What(); | |
68 } | |
69 } | |
70 catch (const std::exception& e) | |
71 { | |
72 LOG(ERROR) << "std::exception in ~OpenGLTexture: " << e.what(); | |
73 } | |
74 catch (...) | |
75 { | |
76 LOG(ERROR) << "Unknown exception in ~OpenGLTexture"; | |
77 } | |
78 } | |
79 | |
80 void OpenGLTexture::Load(const Orthanc::ImageAccessor& image, | |
81 bool isLinearInterpolation) | |
82 { | |
83 if (!context_.IsContextLost()) | |
84 { | |
85 glPixelStorei(GL_UNPACK_ALIGNMENT, 1); // Disable byte-alignment restriction | |
86 | |
87 if (image.GetPitch() != image.GetBytesPerPixel() * image.GetWidth()) | |
88 { | |
89 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented, | |
90 "Unsupported non-zero padding"); | |
91 } | |
92 | |
93 // Bind it | |
94 glActiveTexture(GL_TEXTURE0); | |
95 glBindTexture(GL_TEXTURE_2D, texture_); | |
96 | |
97 GLenum sourceFormat, internalFormat; | |
98 | |
99 switch (image.GetFormat()) | |
100 { | |
101 case Orthanc::PixelFormat_Grayscale8: | |
102 sourceFormat = GL_RED; | |
103 internalFormat = GL_RED; | |
104 break; | |
105 | |
106 case Orthanc::PixelFormat_RGB24: | |
107 sourceFormat = GL_RGB; | |
108 internalFormat = GL_RGB; | |
109 break; | |
110 | |
111 case Orthanc::PixelFormat_RGBA32: | |
112 sourceFormat = GL_RGBA; | |
113 internalFormat = GL_RGBA; | |
114 break; | |
115 | |
116 default: | |
117 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented, | |
118 "No support for this format in OpenGL textures: " + | |
119 std::string(EnumerationToString(image.GetFormat()))); | |
120 } | |
121 | |
122 width_ = image.GetWidth(); | |
123 height_ = image.GetHeight(); | |
124 | |
125 GLint interpolation = (isLinearInterpolation ? GL_LINEAR : GL_NEAREST); | |
126 | |
127 // Load the texture from the image buffer | |
128 glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, image.GetWidth(), image.GetHeight(), | |
129 0, sourceFormat, GL_UNSIGNED_BYTE, image.GetBuffer()); | |
130 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, interpolation); | |
131 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, interpolation); | |
132 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | |
133 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | |
134 } | |
135 } | |
136 | |
137 | |
138 void OpenGLTexture::Bind(GLint location) | |
139 { | |
140 glActiveTexture(GL_TEXTURE0); | |
141 glBindTexture(GL_TEXTURE_2D, texture_); | |
142 glUniform1i(location, 0 /* texture unit */); | |
143 } | |
144 } | |
145 } |