Mercurial > hg > orthanc-stone
comparison Framework/OpenGL/OpenGLTexture.cpp @ 579:fadacfbf5538
OpenGL programs and textures
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 19 Apr 2019 16:06:45 +0200 |
parents | |
children | 97926984d5d0 |
comparison
equal
deleted
inserted
replaced
578:21fd70df3fc9 | 579:fadacfbf5538 |
---|---|
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-2019 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 | |
24 #include <Core/OrthancException.h> | |
25 | |
26 namespace OrthancStone | |
27 { | |
28 namespace OpenGL | |
29 { | |
30 OpenGLTexture::OpenGLTexture() : | |
31 width_(0), | |
32 height_(0) | |
33 { | |
34 // Generate a texture object | |
35 glGenTextures(1, &texture_); | |
36 if (texture_ == 0) | |
37 { | |
38 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError, | |
39 "Cannot create an OpenGL program"); | |
40 } | |
41 } | |
42 | |
43 | |
44 OpenGLTexture::~OpenGLTexture() | |
45 { | |
46 assert(texture_ != 0); | |
47 glDeleteTextures(1, &texture_); | |
48 } | |
49 | |
50 | |
51 void OpenGLTexture::Load(const Orthanc::ImageAccessor& image, | |
52 bool isLinearInterpolation) | |
53 { | |
54 glPixelStorei(GL_UNPACK_ALIGNMENT, 1); // Disable byte-alignment restriction | |
55 | |
56 if (image.GetPitch() != image.GetBytesPerPixel() * image.GetWidth()) | |
57 { | |
58 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented, | |
59 "Unsupported non-zero padding"); | |
60 } | |
61 | |
62 // Bind it | |
63 glActiveTexture(GL_TEXTURE0); | |
64 glBindTexture(GL_TEXTURE_2D, texture_); | |
65 | |
66 GLenum sourceFormat, internalFormat; | |
67 | |
68 switch (image.GetFormat()) | |
69 { | |
70 case Orthanc::PixelFormat_Grayscale8: | |
71 sourceFormat = GL_RED; | |
72 internalFormat = GL_RED; | |
73 break; | |
74 | |
75 case Orthanc::PixelFormat_RGB24: | |
76 sourceFormat = GL_RGB; | |
77 internalFormat = GL_RGBA; | |
78 break; | |
79 | |
80 case Orthanc::PixelFormat_RGBA32: | |
81 sourceFormat = GL_RGBA; | |
82 internalFormat = GL_RGBA; | |
83 break; | |
84 | |
85 default: | |
86 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented, | |
87 "No support for this format in OpenGL textures: " + | |
88 std::string(EnumerationToString(image.GetFormat()))); | |
89 } | |
90 | |
91 width_ = image.GetWidth(); | |
92 height_ = image.GetHeight(); | |
93 | |
94 GLint interpolation = (isLinearInterpolation ? GL_LINEAR : GL_NEAREST); | |
95 | |
96 // Load the texture from the image buffer | |
97 glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, image.GetWidth(), image.GetHeight(), | |
98 0, sourceFormat, GL_UNSIGNED_BYTE, image.GetBuffer()); | |
99 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, interpolation); | |
100 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, interpolation); | |
101 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | |
102 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | |
103 } | |
104 | |
105 | |
106 void OpenGLTexture::Bind(GLint location) | |
107 { | |
108 glActiveTexture(GL_TEXTURE0); | |
109 glBindTexture(GL_TEXTURE_2D, texture_); | |
110 glUniform1i(location, 0 /* texture unit */); | |
111 } | |
112 } | |
113 } |