1 /*
2 OpenVR for PyMOL Copyright Notice
3 =====================================
4
5 The OpenVR for PyMOL source code is copyrighted, but you can freely use and
6 copy it as long as you don't change or remove any of the Copyright notices.
7 OpenVR for PyMOL is made available under the following open-source license
8 terms:
9
10 ------------------------------------------------------------------------------
11 Copyright (c) 2018 EPAM Systems, Inc.
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30 ------------------------------------------------------------------------------
31
32 */
33
34 // this header
35 #include "OpenVRUtils.h"
36
37 // system headers
38 #include <stdio.h>
39 #include <math.h>
40 #include <string>
41
42 // pymol headers
43 #include "MyPNG.h"
44
45 namespace OpenVRUtils {
46
CompileShader(GLenum shaderType,char const * shader)47 static GLuint CompileShader(GLenum shaderType, char const* shader)
48 {
49 GLuint shaderID = glCreateShader(shaderType);
50 glShaderSource(shaderID, 1, &shader, NULL);
51 glCompileShader(shaderID);
52
53 GLint success = GL_FALSE;
54 glGetShaderiv(shaderID, GL_COMPILE_STATUS, &success);
55 if (!success) {
56 int infoLogLength = 0;
57 glGetShaderiv(shaderID, GL_INFO_LOG_LENGTH, &infoLogLength);
58 if (infoLogLength > 0) {
59 char *infoLog = new char[infoLogLength];
60 glGetShaderInfoLog(shaderID, infoLogLength, 0, infoLog);
61 printf("GLSL shader compilation failed, log follows:\n"
62 "=============================================================================\n"
63 "%s"
64 "=============================================================================\n", infoLog);
65 delete[] infoLog;
66 }
67 glDeleteShader(shaderID);
68 shaderID = 0;
69 }
70 return shaderID;
71 }
72
CompileProgram(char const * vertexShader,char const * fragmentShader,char const * attributes[])73 GLuint CompileProgram(char const* vertexShader, char const* fragmentShader, char const* attributes[] /* = 0 */)
74 {
75 GLuint vertexShaderID = CompileShader(GL_VERTEX_SHADER, vertexShader);
76 GLuint fragmentShaderID = CompileShader(GL_FRAGMENT_SHADER, fragmentShader);
77
78 GLuint programID = 0;
79 if (vertexShaderID && fragmentShaderID) {
80 programID = glCreateProgram();
81 glAttachShader(programID, vertexShaderID);
82 glAttachShader(programID, fragmentShaderID);
83
84 if (attributes)
85 for (int i = 0; attributes[i] != 0; ++i)
86 glBindAttribLocation(programID, i, attributes[i]);
87
88 glLinkProgram(programID);
89
90 GLint success = GL_FALSE;
91 glGetProgramiv(programID, GL_LINK_STATUS, &success);
92 if (!success) {
93 int infoLogLength = 0;
94 glGetProgramiv(programID, GL_INFO_LOG_LENGTH, &infoLogLength);
95 if (infoLogLength > 0) {
96 char *infoLog = new char[infoLogLength];
97 glGetProgramInfoLog(programID, infoLogLength, 0, infoLog);
98 printf("GLSL program linking failed, log follows:\n"
99 "=============================================================================\n"
100 "%s"
101 "=============================================================================\n", infoLog);
102 delete[] infoLog;
103 }
104
105 glDeleteProgram(programID);
106 programID = 0;
107 }
108 }
109
110 if (vertexShaderID) {
111 glDeleteShader(vertexShaderID);
112 }
113
114 if (fragmentShaderID) {
115 glDeleteShader(fragmentShaderID);
116 }
117
118 if (programID) {
119 glUseProgram(programID);
120 glUseProgram(0);
121 }
122
123 return programID;
124 }
125
LoadTexture(unsigned width,unsigned height,unsigned char const * ptr)126 GLuint LoadTexture(unsigned width, unsigned height, unsigned char const* ptr)
127 {
128 GLuint texture = 0;
129
130 glGenTextures(1, &texture);
131 glBindTexture(GL_TEXTURE_2D, texture);
132
133 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
134 glGenerateMipmap(GL_TEXTURE_2D);
135
136 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
137 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
138 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
139 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
140
141 GLfloat maxAniso;
142 glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &maxAniso);
143 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, maxAniso);
144
145 glBindTexture(GL_TEXTURE_2D, 0);
146 return texture;
147 }
148
LoadTexture(char const * filename)149 GLuint LoadTexture(char const* filename)
150 {
151 auto filePath = std::string(getenv("PYMOL_DATA"))
152 .append(PATH_SEP "openvr" PATH_SEP)
153 .append(filename);
154 auto image = MyPNGRead(filePath.c_str());
155 if (image) {
156 return LoadTexture(image->getWidth(), image->getHeight(), image->bits());
157 }
158 return 0;
159 }
160
VectorNormalize(float v[])161 void VectorNormalize(float v[])
162 {
163 double len = sqrt((v[0] * v[0]) + (v[1] * v[1]) + (v[2] * v[2]));
164 if (len > 1.0e-5f) {
165 float a = 1.0f / len;
166 v[0] *= a;
167 v[1] *= a;
168 v[2] *= a;
169 }
170 }
171
VectorCrossProduct(float const v1[],float const v2[],float cross[])172 void VectorCrossProduct(float const v1[], float const v2[], float cross[])
173 {
174 cross[0] = (v1[1] * v2[2]) - (v1[2] * v2[1]);
175 cross[1] = (v1[2] * v2[0]) - (v1[0] * v2[2]);
176 cross[2] = (v1[0] * v2[1]) - (v1[1] * v2[0]);
177 }
178
MatrixFastInverseGLGL(float const srcGL44[],float dstGL44[])179 void MatrixFastInverseGLGL(float const srcGL44[], float dstGL44[])
180 {
181 float const (*src)[4] = (float const (*)[4])srcGL44;
182 float (*dst)[4] = (float (*)[4])dstGL44;
183
184 // transpose rotation
185 dst[0][0] = src[0][0];
186 dst[0][1] = src[1][0];
187 dst[0][2] = src[2][0];
188 dst[0][3] = 0.0f;
189 dst[1][0] = src[0][1];
190 dst[1][1] = src[1][1];
191 dst[1][2] = src[2][1];
192 dst[1][3] = 0.0f;
193 dst[2][0] = src[0][2];
194 dst[2][1] = src[1][2];
195 dst[2][2] = src[2][2];
196 dst[2][3] = 0.0f;
197
198 // transpose-rotated negative translation
199 dst[3][0] = -(src[0][0] * src[3][0] + src[0][1] * src[3][1] + src[0][2] * src[3][2]);
200 dst[3][1] = -(src[1][0] * src[3][0] + src[1][1] * src[3][1] + src[1][2] * src[3][2]);
201 dst[3][2] = -(src[2][0] * src[3][0] + src[2][1] * src[3][1] + src[2][2] * src[3][2]);
202 dst[3][3] = 1.0f;
203 }
204
MatrixFastInverseVRGL(float const srcVR34[],float dstGL44[])205 void MatrixFastInverseVRGL(float const srcVR34[], float dstGL44[])
206 {
207 float const (*src)[4] = (float const (*)[4])srcVR34;
208 float (*dst)[4] = (float (*)[4])dstGL44;
209
210 // transpose rotation
211 dst[0][0] = src[0][0];
212 dst[0][1] = src[0][1];
213 dst[0][2] = src[0][2];
214 dst[0][3] = 0.0f;
215 dst[1][0] = src[1][0];
216 dst[1][1] = src[1][1];
217 dst[1][2] = src[1][2];
218 dst[1][3] = 0.0f;
219 dst[2][0] = src[2][0];
220 dst[2][1] = src[2][1];
221 dst[2][2] = src[2][2];
222 dst[2][3] = 0.0f;
223
224 // transpose-rotated negative translation
225 dst[3][0] = -(src[0][0] * src[0][3] + src[1][0] * src[1][3] + src[2][0] * src[2][3]);
226 dst[3][1] = -(src[0][1] * src[0][3] + src[1][1] * src[1][3] + src[2][1] * src[2][3]);
227 dst[3][2] = -(src[0][2] * src[0][3] + src[1][2] * src[1][3] + src[2][2] * src[2][3]);
228 dst[3][3] = 1.0f;
229 }
230
MatrixCopyVRGL(float const srcVR34[],float dstGL44[])231 void MatrixCopyVRGL(float const srcVR34[], float dstGL44[])
232 {
233 float const (*src)[4] = (float const (*)[4])srcVR34;
234 float (*dst)[4] = (float(*)[4])dstGL44;
235 dst[0][0] = src[0][0]; dst[0][1] = src[1][0]; dst[0][2] = src[2][0]; dst[0][3] = 0.0f;
236 dst[1][0] = src[0][1]; dst[1][1] = src[1][1]; dst[1][2] = src[2][1]; dst[1][3] = 0.0f;
237 dst[2][0] = src[0][2]; dst[2][1] = src[1][2]; dst[2][2] = src[2][2]; dst[2][3] = 0.0f;
238 dst[3][0] = src[0][3]; dst[3][1] = src[1][3]; dst[3][2] = src[2][3]; dst[3][3] = 1.0f;
239 }
240
241 } // namespace OpenVRUtils
242