Mercurial > hg > orthanc-stone
annotate Framework/OpenGL/OpenGLShader.cpp @ 860:238693c3bc51 am-dev
merge default -> am-dev
author | Alain Mazy <alain@mazy.be> |
---|---|
date | Mon, 24 Jun 2019 14:35:00 +0200 |
parents | 58e1faeafb1b |
children | 1091b2adeb5a |
rev | line source |
---|---|
578
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
1 /** |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
2 * Stone of Orthanc |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
4 * Department, University Hospital of Liege, Belgium |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
5 * Copyright (C) 2017-2019 Osimis S.A., Belgium |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
6 * |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
7 * This program is free software: you can redistribute it and/or |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
8 * modify it under the terms of the GNU Affero General Public License |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
9 * as published by the Free Software Foundation, either version 3 of |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
10 * the License, or (at your option) any later version. |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
11 * |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
12 * This program is distributed in the hope that it will be useful, but |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
13 * WITHOUT ANY WARRANTY; without even the implied warranty of |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
15 * Affero General Public License for more details. |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
16 * |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
17 * You should have received a copy of the GNU Affero General Public License |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
19 **/ |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
20 |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
21 |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
22 #include "OpenGLShader.h" |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
23 |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
24 #include <Core/OrthancException.h> |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
25 |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
26 namespace OrthancStone |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
27 { |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
28 namespace OpenGL |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
29 { |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
30 static GLuint CompileShader(GLenum type, |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
31 const std::string& source) |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
32 { |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
33 // Create shader object |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
34 const GLchar* sourceString[1]; |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
35 GLint sourceStringLengths[1]; |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
36 |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
37 sourceString[0] = source.c_str(); |
701
58e1faeafb1b
Fixed truncating conversion warning
Benjamin Golinvaux <bgo@osimis.io>
parents:
599
diff
changeset
|
38 sourceStringLengths[0] = static_cast<GLint>(source.length()); |
578
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
39 GLuint shader = glCreateShader(type); |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
40 |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
41 if (shader == 0) |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
42 { |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
43 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError, |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
44 "Cannot create an OpenGL shader"); |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
45 } |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
46 else |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
47 { |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
48 // Assign and compile the source to the shader object |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
49 glShaderSource(shader, 1, sourceString, sourceStringLengths); |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
50 glCompileShader(shader); |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
51 |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
52 // Check if there were errors |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
53 int infoLen = 0; |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
54 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen); |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
55 |
599 | 56 if (infoLen > 1) // Might be equal to 1, which amounts to no error |
578
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
57 { |
584
434ceeb0bcab
layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
579
diff
changeset
|
58 std::string infoLog; |
434ceeb0bcab
layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
579
diff
changeset
|
59 infoLog.resize(infoLen + 1); |
434ceeb0bcab
layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
579
diff
changeset
|
60 glGetShaderInfoLog(shader, infoLen, NULL, &infoLog[0]); |
578
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
61 glDeleteShader(shader); |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
62 |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
63 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError, |
584
434ceeb0bcab
layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
579
diff
changeset
|
64 "Error while creating an OpenGL shader: " + infoLog); |
578
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
65 } |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
66 else |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
67 { |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
68 return shader; |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
69 } |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
70 } |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
71 } |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
72 |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
73 |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
74 OpenGLShader::OpenGLShader(GLenum type, |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
75 const std::string& source) |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
76 { |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
77 shader_ = CompileShader(type, source); |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
78 isValid_ = true; |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
79 } |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
80 |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
81 |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
82 OpenGLShader::~OpenGLShader() |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
83 { |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
84 if (isValid_) |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
85 { |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
86 glDeleteShader(shader_); |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
87 } |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
88 } |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
89 |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
90 |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
91 GLuint OpenGLShader::Release() |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
92 { |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
93 if (isValid_) |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
94 { |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
95 isValid_ = false; |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
96 return shader_; |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
97 } |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
98 else |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
99 { |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
100 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
101 } |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
102 } |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
103 } |
21fd70df3fc9
starting work on OpenGL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
104 } |