annotate OrthancStone/Sources/OpenGL/OpenGLTexture.cpp @ 2038:a72ca4959aa6 deep-learning

note
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sat, 28 Jan 2023 13:03:14 +0100
parents d81a7157a846
children e0906b7c67b9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
579
fadacfbf5538 OpenGL programs and textures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
fadacfbf5538 OpenGL programs and textures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Stone of Orthanc
fadacfbf5538 OpenGL programs and textures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
fadacfbf5538 OpenGL programs and textures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
1871
7053b8a0aaec upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1870
diff changeset
5 * Copyright (C) 2017-2022 Osimis S.A., Belgium
7053b8a0aaec upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1870
diff changeset
6 * Copyright (C) 2021-2022 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
579
fadacfbf5538 OpenGL programs and textures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 *
fadacfbf5538 OpenGL programs and textures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * This program is free software: you can redistribute it and/or
1598
8563ea5d8ae4 relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1596
diff changeset
9 * modify it under the terms of the GNU Lesser General Public License
579
fadacfbf5538 OpenGL programs and textures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 * as published by the Free Software Foundation, either version 3 of
fadacfbf5538 OpenGL programs and textures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 * the License, or (at your option) any later version.
fadacfbf5538 OpenGL programs and textures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 *
fadacfbf5538 OpenGL programs and textures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * This program is distributed in the hope that it will be useful, but
fadacfbf5538 OpenGL programs and textures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
1598
8563ea5d8ae4 relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1596
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
8563ea5d8ae4 relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1596
diff changeset
16 * Lesser General Public License for more details.
1596
4fb8fdf03314 removed annoying whitespace
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1564
diff changeset
17 *
1598
8563ea5d8ae4 relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1596
diff changeset
18 * You should have received a copy of the GNU Lesser General Public
8563ea5d8ae4 relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1596
diff changeset
19 * License along with this program. If not, see
8563ea5d8ae4 relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1596
diff changeset
20 * <http://www.gnu.org/licenses/>.
579
fadacfbf5538 OpenGL programs and textures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 **/
fadacfbf5538 OpenGL programs and textures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22
fadacfbf5538 OpenGL programs and textures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23
fadacfbf5538 OpenGL programs and textures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 #include "OpenGLTexture.h"
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 616
diff changeset
25 #include "IOpenGLContext.h"
579
fadacfbf5538 OpenGL programs and textures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26
2032
d10bab7cc396 added OpenGLTexture::Download()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1871
diff changeset
27 #include <Images/Image.h>
1624
59f95b9ea858 fix build
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1598
diff changeset
28 #include <Logging.h>
1455
30deba7bc8e2 simplifying include_directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1270
diff changeset
29 #include <OrthancException.h>
579
fadacfbf5538 OpenGL programs and textures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30
fadacfbf5538 OpenGL programs and textures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 namespace OrthancStone
fadacfbf5538 OpenGL programs and textures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32 {
fadacfbf5538 OpenGL programs and textures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 namespace OpenGL
fadacfbf5538 OpenGL programs and textures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 {
2032
d10bab7cc396 added OpenGLTexture::Download()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1871
diff changeset
35 OpenGLTexture::OpenGLTexture(OpenGL::IOpenGLContext& context) :
d10bab7cc396 added OpenGLTexture::Download()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1871
diff changeset
36 texture_(0),
d10bab7cc396 added OpenGLTexture::Download()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1871
diff changeset
37 width_(0),
d10bab7cc396 added OpenGLTexture::Download()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1871
diff changeset
38 height_(0),
d10bab7cc396 added OpenGLTexture::Download()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1871
diff changeset
39 context_(context)
579
fadacfbf5538 OpenGL programs and textures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 {
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 616
diff changeset
41 if (!context_.IsContextLost())
579
fadacfbf5538 OpenGL programs and textures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 {
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 616
diff changeset
43 // Generate a texture object
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 616
diff changeset
44 glGenTextures(1, &texture_);
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 616
diff changeset
45 if (texture_ == 0)
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 616
diff changeset
46 {
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 616
diff changeset
47 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError,
2032
d10bab7cc396 added OpenGLTexture::Download()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1871
diff changeset
48 "Cannot create an OpenGL texture");
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 616
diff changeset
49 }
579
fadacfbf5538 OpenGL programs and textures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 }
fadacfbf5538 OpenGL programs and textures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 }
fadacfbf5538 OpenGL programs and textures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52
fadacfbf5538 OpenGL programs and textures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 OpenGLTexture::~OpenGLTexture()
fadacfbf5538 OpenGL programs and textures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 {
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 616
diff changeset
55 try
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 616
diff changeset
56 {
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 616
diff changeset
57 if (!context_.IsContextLost())
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 616
diff changeset
58 {
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 616
diff changeset
59 assert(texture_ != 0);
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 616
diff changeset
60 ORTHANC_OPENGL_TRACE_CURRENT_CONTEXT("About to call glDeleteTextures");
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 616
diff changeset
61 glDeleteTextures(1, &texture_);
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 616
diff changeset
62 }
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 616
diff changeset
63 }
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 616
diff changeset
64 catch (const Orthanc::OrthancException& e)
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 616
diff changeset
65 {
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 616
diff changeset
66 if (e.HasDetails())
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 616
diff changeset
67 {
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 616
diff changeset
68 LOG(ERROR) << "OrthancException in ~OpenGLTexture: " << e.What() << " Details: " << e.GetDetails();
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 616
diff changeset
69 }
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 616
diff changeset
70 else
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 616
diff changeset
71 {
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 616
diff changeset
72 LOG(ERROR) << "OrthancException in ~OpenGLTexture: " << e.What();
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 616
diff changeset
73 }
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 616
diff changeset
74 }
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 616
diff changeset
75 catch (const std::exception& e)
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 616
diff changeset
76 {
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 616
diff changeset
77 LOG(ERROR) << "std::exception in ~OpenGLTexture: " << e.what();
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 616
diff changeset
78 }
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 616
diff changeset
79 catch (...)
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 616
diff changeset
80 {
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 616
diff changeset
81 LOG(ERROR) << "Unknown exception in ~OpenGLTexture";
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 616
diff changeset
82 }
579
fadacfbf5538 OpenGL programs and textures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 }
fadacfbf5538 OpenGL programs and textures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84
2034
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2033
diff changeset
85 void OpenGLTexture::Setup(Orthanc::PixelFormat format,
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2033
diff changeset
86 unsigned int width,
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2033
diff changeset
87 unsigned int height,
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2033
diff changeset
88 bool isLinearInterpolation,
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2033
diff changeset
89 const void* data)
579
fadacfbf5538 OpenGL programs and textures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 {
2034
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2033
diff changeset
91 if (context_.IsContextLost())
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2033
diff changeset
92 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2033
diff changeset
93 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError,
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2033
diff changeset
94 "OpenGL context has been lost");
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2033
diff changeset
95 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2033
diff changeset
96 else
579
fadacfbf5538 OpenGL programs and textures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 {
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 616
diff changeset
98 glPixelStorei(GL_UNPACK_ALIGNMENT, 1); // Disable byte-alignment restriction
579
fadacfbf5538 OpenGL programs and textures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 616
diff changeset
100 // Bind it
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 616
diff changeset
101 glActiveTexture(GL_TEXTURE0);
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 616
diff changeset
102 glBindTexture(GL_TEXTURE_2D, texture_);
579
fadacfbf5538 OpenGL programs and textures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103
2032
d10bab7cc396 added OpenGLTexture::Download()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1871
diff changeset
104 GLenum sourceFormat, internalFormat, pixelType;
2037
d81a7157a846 added OpenGLTexture::ConvertToOpenGLFormats()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2036
diff changeset
105 ConvertToOpenGLFormats(sourceFormat, internalFormat, pixelType, format);
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 616
diff changeset
106
2034
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2033
diff changeset
107 width_ = width;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2033
diff changeset
108 height_ = height;
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 616
diff changeset
109
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 616
diff changeset
110 GLint interpolation = (isLinearInterpolation ? GL_LINEAR : GL_NEAREST);
579
fadacfbf5538 OpenGL programs and textures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 616
diff changeset
112 // Load the texture from the image buffer
2034
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2033
diff changeset
113 glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, width, height,
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2033
diff changeset
114 0, sourceFormat, pixelType, data);
2036
444527d34647 sanity check in textures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2035
diff changeset
115
444527d34647 sanity check in textures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2035
diff changeset
116 GLint w, h;
444527d34647 sanity check in textures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2035
diff changeset
117 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &w);
444527d34647 sanity check in textures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2035
diff changeset
118 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &h);
444527d34647 sanity check in textures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2035
diff changeset
119 if (width != static_cast<unsigned int>(w) ||
444527d34647 sanity check in textures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2035
diff changeset
120 height != static_cast<unsigned int>(h))
444527d34647 sanity check in textures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2035
diff changeset
121 {
444527d34647 sanity check in textures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2035
diff changeset
122 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError,
444527d34647 sanity check in textures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2035
diff changeset
123 "Your GPU cannot create a texture of size " +
444527d34647 sanity check in textures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2035
diff changeset
124 boost::lexical_cast<std::string>(width) + " x " +
444527d34647 sanity check in textures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2035
diff changeset
125 boost::lexical_cast<std::string>(height));
444527d34647 sanity check in textures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2035
diff changeset
126 }
444527d34647 sanity check in textures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2035
diff changeset
127
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 616
diff changeset
128 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, interpolation);
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 616
diff changeset
129 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, interpolation);
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 616
diff changeset
130 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 616
diff changeset
131 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 616
diff changeset
132 }
579
fadacfbf5538 OpenGL programs and textures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133 }
fadacfbf5538 OpenGL programs and textures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134
2034
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2033
diff changeset
135 void OpenGLTexture::Load(const Orthanc::ImageAccessor& image,
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2033
diff changeset
136 bool isLinearInterpolation)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2033
diff changeset
137 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2033
diff changeset
138 if (image.GetPitch() != image.GetBytesPerPixel() * image.GetWidth())
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2033
diff changeset
139 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2033
diff changeset
140 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented,
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2033
diff changeset
141 "Pitch is not the same as the row size");
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2033
diff changeset
142 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2033
diff changeset
143 else
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2033
diff changeset
144 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2033
diff changeset
145 Setup(image.GetFormat(), image.GetWidth(), image.GetHeight(),
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2033
diff changeset
146 isLinearInterpolation, image.GetConstBuffer());
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2033
diff changeset
147 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2033
diff changeset
148 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2033
diff changeset
149
579
fadacfbf5538 OpenGL programs and textures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
150
fadacfbf5538 OpenGL programs and textures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
151 void OpenGLTexture::Bind(GLint location)
fadacfbf5538 OpenGL programs and textures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
152 {
fadacfbf5538 OpenGL programs and textures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
153 glActiveTexture(GL_TEXTURE0);
fadacfbf5538 OpenGL programs and textures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
154 glBindTexture(GL_TEXTURE_2D, texture_);
fadacfbf5538 OpenGL programs and textures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
155 glUniform1i(location, 0 /* texture unit */);
fadacfbf5538 OpenGL programs and textures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
156 }
2032
d10bab7cc396 added OpenGLTexture::Download()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1871
diff changeset
157
d10bab7cc396 added OpenGLTexture::Download()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1871
diff changeset
158
d10bab7cc396 added OpenGLTexture::Download()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1871
diff changeset
159 Orthanc::ImageAccessor* OpenGLTexture::Download(Orthanc::PixelFormat format)
d10bab7cc396 added OpenGLTexture::Download()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1871
diff changeset
160 {
d10bab7cc396 added OpenGLTexture::Download()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1871
diff changeset
161 if (context_.IsContextLost())
d10bab7cc396 added OpenGLTexture::Download()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1871
diff changeset
162 {
d10bab7cc396 added OpenGLTexture::Download()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1871
diff changeset
163 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError,
d10bab7cc396 added OpenGLTexture::Download()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1871
diff changeset
164 "OpenGL context is lost");
d10bab7cc396 added OpenGLTexture::Download()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1871
diff changeset
165 }
d10bab7cc396 added OpenGLTexture::Download()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1871
diff changeset
166
d10bab7cc396 added OpenGLTexture::Download()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1871
diff changeset
167 std::unique_ptr<Orthanc::ImageAccessor> target(new Orthanc::Image(format, width_, height_, true));
2033
23b0a42eea85 added support for floating-point images in OpenGLTexture
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2032
diff changeset
168 assert(target->GetPitch() == width_ * Orthanc::GetBytesPerPixel(format));
2032
d10bab7cc396 added OpenGLTexture::Download()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1871
diff changeset
169
d10bab7cc396 added OpenGLTexture::Download()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1871
diff changeset
170 glBindTexture(GL_TEXTURE_2D, texture_);
d10bab7cc396 added OpenGLTexture::Download()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1871
diff changeset
171
d10bab7cc396 added OpenGLTexture::Download()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1871
diff changeset
172 switch (format)
d10bab7cc396 added OpenGLTexture::Download()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1871
diff changeset
173 {
d10bab7cc396 added OpenGLTexture::Download()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1871
diff changeset
174 case Orthanc::PixelFormat_Grayscale8:
d10bab7cc396 added OpenGLTexture::Download()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1871
diff changeset
175 glGetTexImage(GL_TEXTURE_2D, 0 /* base level */, GL_RED, GL_UNSIGNED_BYTE, target->GetBuffer());
d10bab7cc396 added OpenGLTexture::Download()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1871
diff changeset
176 break;
d10bab7cc396 added OpenGLTexture::Download()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1871
diff changeset
177
d10bab7cc396 added OpenGLTexture::Download()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1871
diff changeset
178 case Orthanc::PixelFormat_RGB24:
d10bab7cc396 added OpenGLTexture::Download()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1871
diff changeset
179 glGetTexImage(GL_TEXTURE_2D, 0 /* base level */, GL_RGB, GL_UNSIGNED_BYTE, target->GetBuffer());
d10bab7cc396 added OpenGLTexture::Download()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1871
diff changeset
180 break;
d10bab7cc396 added OpenGLTexture::Download()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1871
diff changeset
181
d10bab7cc396 added OpenGLTexture::Download()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1871
diff changeset
182 case Orthanc::PixelFormat_RGBA32:
d10bab7cc396 added OpenGLTexture::Download()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1871
diff changeset
183 glGetTexImage(GL_TEXTURE_2D, 0 /* base level */, GL_RGBA, GL_UNSIGNED_BYTE, target->GetBuffer());
d10bab7cc396 added OpenGLTexture::Download()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1871
diff changeset
184 break;
d10bab7cc396 added OpenGLTexture::Download()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1871
diff changeset
185
2033
23b0a42eea85 added support for floating-point images in OpenGLTexture
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2032
diff changeset
186 case Orthanc::PixelFormat_Float32:
23b0a42eea85 added support for floating-point images in OpenGLTexture
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2032
diff changeset
187 glGetTexImage(GL_TEXTURE_2D, 0 /* base level */, GL_RED, GL_FLOAT, target->GetBuffer());
23b0a42eea85 added support for floating-point images in OpenGLTexture
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2032
diff changeset
188 break;
23b0a42eea85 added support for floating-point images in OpenGLTexture
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2032
diff changeset
189
2032
d10bab7cc396 added OpenGLTexture::Download()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1871
diff changeset
190 default:
d10bab7cc396 added OpenGLTexture::Download()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1871
diff changeset
191 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented);
d10bab7cc396 added OpenGLTexture::Download()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1871
diff changeset
192 }
d10bab7cc396 added OpenGLTexture::Download()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1871
diff changeset
193
d10bab7cc396 added OpenGLTexture::Download()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1871
diff changeset
194 return target.release();
d10bab7cc396 added OpenGLTexture::Download()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1871
diff changeset
195 }
2035
a73a8415780f added OpenGLTexture::SetClampingToZero()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2034
diff changeset
196
a73a8415780f added OpenGLTexture::SetClampingToZero()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2034
diff changeset
197
a73a8415780f added OpenGLTexture::SetClampingToZero()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2034
diff changeset
198 void OpenGLTexture::SetClampingToZero()
a73a8415780f added OpenGLTexture::SetClampingToZero()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2034
diff changeset
199 {
a73a8415780f added OpenGLTexture::SetClampingToZero()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2034
diff changeset
200 glBindTexture(GL_TEXTURE_2D, texture_);
a73a8415780f added OpenGLTexture::SetClampingToZero()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2034
diff changeset
201 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
a73a8415780f added OpenGLTexture::SetClampingToZero()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2034
diff changeset
202 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
a73a8415780f added OpenGLTexture::SetClampingToZero()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2034
diff changeset
203
a73a8415780f added OpenGLTexture::SetClampingToZero()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2034
diff changeset
204 GLfloat colorfv[4] = { 0, 0, 0, 0 };
a73a8415780f added OpenGLTexture::SetClampingToZero()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2034
diff changeset
205 glTextureParameterfv(texture_, GL_TEXTURE_BORDER_COLOR, colorfv);
a73a8415780f added OpenGLTexture::SetClampingToZero()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2034
diff changeset
206 }
2037
d81a7157a846 added OpenGLTexture::ConvertToOpenGLFormats()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2036
diff changeset
207
d81a7157a846 added OpenGLTexture::ConvertToOpenGLFormats()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2036
diff changeset
208
d81a7157a846 added OpenGLTexture::ConvertToOpenGLFormats()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2036
diff changeset
209 void OpenGLTexture::ConvertToOpenGLFormats(GLenum& sourceFormat,
d81a7157a846 added OpenGLTexture::ConvertToOpenGLFormats()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2036
diff changeset
210 GLenum& internalFormat,
d81a7157a846 added OpenGLTexture::ConvertToOpenGLFormats()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2036
diff changeset
211 GLenum& pixelType,
d81a7157a846 added OpenGLTexture::ConvertToOpenGLFormats()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2036
diff changeset
212 Orthanc::PixelFormat format)
d81a7157a846 added OpenGLTexture::ConvertToOpenGLFormats()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2036
diff changeset
213 {
d81a7157a846 added OpenGLTexture::ConvertToOpenGLFormats()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2036
diff changeset
214 switch (format)
d81a7157a846 added OpenGLTexture::ConvertToOpenGLFormats()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2036
diff changeset
215 {
d81a7157a846 added OpenGLTexture::ConvertToOpenGLFormats()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2036
diff changeset
216 case Orthanc::PixelFormat_Grayscale8:
d81a7157a846 added OpenGLTexture::ConvertToOpenGLFormats()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2036
diff changeset
217 sourceFormat = GL_RED;
d81a7157a846 added OpenGLTexture::ConvertToOpenGLFormats()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2036
diff changeset
218 internalFormat = GL_RED;
d81a7157a846 added OpenGLTexture::ConvertToOpenGLFormats()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2036
diff changeset
219 pixelType = GL_UNSIGNED_BYTE;
d81a7157a846 added OpenGLTexture::ConvertToOpenGLFormats()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2036
diff changeset
220 break;
d81a7157a846 added OpenGLTexture::ConvertToOpenGLFormats()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2036
diff changeset
221
d81a7157a846 added OpenGLTexture::ConvertToOpenGLFormats()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2036
diff changeset
222 case Orthanc::PixelFormat_RGB24:
d81a7157a846 added OpenGLTexture::ConvertToOpenGLFormats()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2036
diff changeset
223 sourceFormat = GL_RGB;
d81a7157a846 added OpenGLTexture::ConvertToOpenGLFormats()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2036
diff changeset
224 internalFormat = GL_RGB;
d81a7157a846 added OpenGLTexture::ConvertToOpenGLFormats()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2036
diff changeset
225 pixelType = GL_UNSIGNED_BYTE;
d81a7157a846 added OpenGLTexture::ConvertToOpenGLFormats()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2036
diff changeset
226 break;
d81a7157a846 added OpenGLTexture::ConvertToOpenGLFormats()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2036
diff changeset
227
d81a7157a846 added OpenGLTexture::ConvertToOpenGLFormats()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2036
diff changeset
228 case Orthanc::PixelFormat_RGBA32:
d81a7157a846 added OpenGLTexture::ConvertToOpenGLFormats()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2036
diff changeset
229 sourceFormat = GL_RGBA;
d81a7157a846 added OpenGLTexture::ConvertToOpenGLFormats()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2036
diff changeset
230 internalFormat = GL_RGBA;
d81a7157a846 added OpenGLTexture::ConvertToOpenGLFormats()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2036
diff changeset
231 pixelType = GL_UNSIGNED_BYTE;
d81a7157a846 added OpenGLTexture::ConvertToOpenGLFormats()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2036
diff changeset
232 break;
d81a7157a846 added OpenGLTexture::ConvertToOpenGLFormats()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2036
diff changeset
233
d81a7157a846 added OpenGLTexture::ConvertToOpenGLFormats()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2036
diff changeset
234 case Orthanc::PixelFormat_Float32:
d81a7157a846 added OpenGLTexture::ConvertToOpenGLFormats()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2036
diff changeset
235 sourceFormat = GL_RED;
d81a7157a846 added OpenGLTexture::ConvertToOpenGLFormats()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2036
diff changeset
236 internalFormat = GL_R32F; // Don't use "GL_RED" here, as it clamps to [0,1]
d81a7157a846 added OpenGLTexture::ConvertToOpenGLFormats()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2036
diff changeset
237 pixelType = GL_FLOAT;
d81a7157a846 added OpenGLTexture::ConvertToOpenGLFormats()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2036
diff changeset
238 break;
d81a7157a846 added OpenGLTexture::ConvertToOpenGLFormats()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2036
diff changeset
239
d81a7157a846 added OpenGLTexture::ConvertToOpenGLFormats()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2036
diff changeset
240 default:
d81a7157a846 added OpenGLTexture::ConvertToOpenGLFormats()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2036
diff changeset
241 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented,
d81a7157a846 added OpenGLTexture::ConvertToOpenGLFormats()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2036
diff changeset
242 "No support for this format in OpenGL textures: " +
d81a7157a846 added OpenGLTexture::ConvertToOpenGLFormats()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2036
diff changeset
243 std::string(EnumerationToString(format)));
d81a7157a846 added OpenGLTexture::ConvertToOpenGLFormats()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2036
diff changeset
244 }
d81a7157a846 added OpenGLTexture::ConvertToOpenGLFormats()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2036
diff changeset
245 }
579
fadacfbf5538 OpenGL programs and textures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
246 }
fadacfbf5538 OpenGL programs and textures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
247 }