Mercurial > hg > orthanc-stone
comparison OrthancStone/Sources/OpenGL/ImageProcessingProgram.cpp @ 2064:4e31d76c7ecd deep-learning
making ImageProcessingProgram compatible with 3D volumes
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 04 May 2023 11:06:19 +0200 |
parents | 6ea5f40ea0e9 |
children | cf3d85eb291c |
comparison
equal
deleted
inserted
replaced
2063:b6b5e1ca1cc2 | 2064:4e31d76c7ecd |
---|---|
48 * "varying" indicates variables that are shader by the vertex shader | 48 * "varying" indicates variables that are shader by the vertex shader |
49 * and the fragment shader. The reason for "v_position" is that | 49 * and the fragment shader. The reason for "v_position" is that |
50 * "a_position" (position in the target frame buffer) ranges from -1 | 50 * "a_position" (position in the target frame buffer) ranges from -1 |
51 * to 1, whereas texture samplers range from 0 to 1. | 51 * to 1, whereas texture samplers range from 0 to 1. |
52 **/ | 52 **/ |
53 static const char* VERTEX_SHADER = | 53 static const char* VERTEX_SHADER_2D = |
54 "in vec2 a_position; \n" | 54 "in vec2 a_position; \n" |
55 "out vec2 v_position; \n" | 55 "out vec2 v_position; \n" |
56 "void main() { \n" | 56 "void main() { \n" |
57 " v_position = (a_position + 1.0) / 2.0; \n" | 57 " v_position = (a_position + 1.0) / 2.0; \n" |
58 " gl_Position = vec4(a_position, 0, 1.0); \n" | 58 " gl_Position = vec4(a_position, 0, 1.0); \n" |
59 "} \n"; | 59 "} \n"; |
60 | 60 |
61 | 61 |
62 /** | |
63 * VERTEX_SHADER_3D allows to sample a 3D texture by introducing the | |
64 * "u_z" uniform whose range is in [0,1] and that allows to scan a 3D | |
65 * texture along its Z axis. | |
66 **/ | |
67 static const char* VERTEX_SHADER_3D = | |
68 "in vec2 a_position; \n" | |
69 "out vec3 v_position; \n" | |
70 "uniform float u_z; \n" | |
71 "void main() { \n" | |
72 " v_position = vec3((a_position + 1.0) / 2.0, u_z); \n" | |
73 " gl_Position = vec4(a_position, u_z, 1.0); \n" | |
74 "} \n"; | |
75 | |
76 | |
62 namespace OrthancStone | 77 namespace OrthancStone |
63 { | 78 { |
64 namespace OpenGL | 79 namespace OpenGL |
65 { | 80 { |
66 void ImageProcessingProgram::SetupPosition() | 81 void ImageProcessingProgram::SetupPosition() |
72 glEnableVertexAttribArray(positionLocation); | 87 glEnableVertexAttribArray(positionLocation); |
73 } | 88 } |
74 | 89 |
75 | 90 |
76 ImageProcessingProgram::ImageProcessingProgram(IOpenGLContext& context, | 91 ImageProcessingProgram::ImageProcessingProgram(IOpenGLContext& context, |
77 const std::string& fragmentShader) : | 92 const std::string& fragmentShader, |
93 bool addUniformZ) : | |
78 program_(context), | 94 program_(context), |
79 quad_vertexbuffer(0) | 95 quad_vertexbuffer(0) |
80 { | 96 { |
81 if (context.IsContextLost()) | 97 if (context.IsContextLost()) |
82 { | 98 { |
101 * https://webglfundamentals.org/webgl/lessons/webgl-qna-when-to-choose-highp--mediump--lowp-in-shaders.html | 117 * https://webglfundamentals.org/webgl/lessons/webgl-qna-when-to-choose-highp--mediump--lowp-in-shaders.html |
102 **/ | 118 **/ |
103 version = ("#version 300 es\n" | 119 version = ("#version 300 es\n" |
104 "precision highp float;\n" | 120 "precision highp float;\n" |
105 "precision highp sampler2D;\n" | 121 "precision highp sampler2D;\n" |
106 "precision highp sampler2DArray;\n"); | 122 "precision highp sampler2DArray;\n" |
123 "precision highp sampler3D;\n"); | |
107 #else | 124 #else |
108 /** | 125 /** |
109 * "#version 130" corresponds to: | 126 * "#version 130" corresponds to: |
110 * - OpenGL version 3.0 | 127 * - OpenGL version 3.0 |
111 * - GLSL version 1.30.10 | 128 * - GLSL version 1.30.10 |
112 **/ | 129 **/ |
113 version = "#version 130\n"; | 130 version = "#version 130\n"; |
114 #endif | 131 #endif |
115 | 132 |
116 program_.CompileShaders(version + VERTEX_SHADER, version + fragmentShader); | 133 std::string vertexShader; |
134 | |
135 if (addUniformZ) | |
136 { | |
137 vertexShader = version + VERTEX_SHADER_3D; | |
138 } | |
139 else | |
140 { | |
141 vertexShader = version + VERTEX_SHADER_2D; | |
142 } | |
143 | |
144 program_.CompileShaders(vertexShader, version + fragmentShader); | |
117 | 145 |
118 glGenBuffers(1, &quad_vertexbuffer); | 146 glGenBuffers(1, &quad_vertexbuffer); |
119 if (quad_vertexbuffer == 0) | 147 if (quad_vertexbuffer == 0) |
120 { | 148 { |
121 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError, | 149 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError, |