summaryrefslogtreecommitdiff
path: root/shared.c
diff options
context:
space:
mode:
Diffstat (limited to 'shared.c')
-rw-r--r--shared.c151
1 files changed, 151 insertions, 0 deletions
diff --git a/shared.c b/shared.c
new file mode 100644
index 0000000..f938281
--- /dev/null
+++ b/shared.c
@@ -0,0 +1,151 @@
+/*
+ * Copyright (c) 2013, NVIDIA CORPORATION. All rights reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#include "shared.h"
+
+#include <math.h>
+
+// EGL_KHR_stream functions
+PFNEGLCREATESTREAMKHRPROC eglCreateStreamKHR;
+PFNEGLDESTROYSTREAMKHRPROC eglDestroyStreamKHR;
+PFNEGLSTREAMATTRIBKHRPROC eglStreamAttribKHR;
+PFNEGLQUERYSTREAMKHRPROC eglQueryStreamKHR;
+
+// EGL_KHR_stream_consumer_gltexture functions
+PFNEGLSTREAMCONSUMERGLTEXTUREEXTERNALKHRPROC eglStreamConsumerGLTextureExternalKHR;
+PFNEGLSTREAMCONSUMERACQUIREKHRPROC eglStreamConsumerAcquireKHR;
+PFNEGLSTREAMCONSUMERRELEASEKHRPROC eglStreamConsumerReleaseKHR;
+
+// EGL_KHR_stream_producer_eglsurface functions
+PFNEGLCREATESTREAMPRODUCERSURFACEKHRPROC eglCreateStreamProducerSurfaceKHR;
+
+void InitEGLExtFuncs(void)
+{
+ #define GET_FUNC(name, type) \
+ do { \
+ name = (type)eglGetProcAddress(#name); \
+ if (!name) { \
+ FATAL_ERROR("Failed to find extension function " #name); \
+ } \
+ } while(0)
+ GET_FUNC(eglCreateStreamKHR, PFNEGLCREATESTREAMKHRPROC);
+ GET_FUNC(eglDestroyStreamKHR, PFNEGLDESTROYSTREAMKHRPROC);
+ GET_FUNC(eglStreamAttribKHR, PFNEGLSTREAMATTRIBKHRPROC);
+ GET_FUNC(eglQueryStreamKHR, PFNEGLQUERYSTREAMKHRPROC);
+ GET_FUNC(eglCreateStreamProducerSurfaceKHR, PFNEGLCREATESTREAMPRODUCERSURFACEKHRPROC);
+ GET_FUNC(eglStreamConsumerGLTextureExternalKHR, PFNEGLSTREAMCONSUMERGLTEXTUREEXTERNALKHRPROC);
+ GET_FUNC(eglStreamConsumerAcquireKHR, PFNEGLSTREAMCONSUMERACQUIREKHRPROC);
+ GET_FUNC(eglStreamConsumerReleaseKHR, PFNEGLSTREAMCONSUMERRELEASEKHRPROC);
+ #undef GET_FUNC
+}
+
+void LoadShader(unsigned int sObj, const char *src, int srcLen)
+{
+ const char * source[1];
+ int length[1];
+ int compileStatus;
+
+ source[0] = src;
+ length[0] = srcLen;
+ glShaderSource(sObj, (sizeof(length) / sizeof(length[0])), source, length);
+ glCompileShader(sObj);
+ glGetShaderiv(sObj, GL_COMPILE_STATUS, &compileStatus);
+
+ if (compileStatus != 1) {
+ int infoLogLength, out;
+ char *infoLog;
+
+ glGetShaderiv(sObj, GL_INFO_LOG_LENGTH, &infoLogLength);
+ infoLog = malloc(infoLogLength+1);
+ memset(infoLog, 0, infoLogLength+1);
+ glGetShaderInfoLog(sObj, infoLogLength, &out, infoLog);
+ DEBUG_PRINT(("Shader compilation failed:\n"));
+ DEBUG_PRINT(("%s\n", infoLog));
+ free(infoLog);
+
+ FATAL_ERROR("Failed to load shader");
+ }
+}
+
+void CreateRotationMatrix(float mat[16], float angle, float x, float y, float z)
+{
+ /* Calculate the rotation matrix */
+ float norm, cosA, sinA, sqX, sqY, sqZ, xY, xZ, yZ;
+ cosA = cos(angle * M_PI / 180.0f);
+ sinA = sin(angle * M_PI / 180.0f);
+ sqX = x*x;
+ sqY = y*y;
+ sqZ = z*z;
+ xY = x * y;
+ xZ = x * z;
+ yZ = y * z;
+
+ mat[0] = sqX + (cosA * (1.0f - sqX));
+ mat[1] = xY - (cosA * xY) + (sinA * z);
+ mat[2] = xZ - (cosA * xZ) - (sinA * y);
+ mat[3] = 0.0f;
+ mat[4] = xY - (cosA * xY) - (sinA *z);
+ mat[5] = sqY + (cosA * (1.0f - sqY));
+ mat[6] = yZ - (cosA * yZ) + (sinA * x);
+ mat[7] = 0.0f;
+ mat[8] = xZ - (cosA * xZ) + (sinA * y);
+ mat[9] = yZ - (cosA * yZ) - (sinA * x);
+ mat[10] = sqZ + (cosA * (1.0f - sqZ));
+ mat[11] = 0.0f;
+ mat[12] = 0.0f;
+ mat[13] = 0.0f;
+ mat[14] = 0.0f;
+ mat[15] = 1.0f;
+}
+
+void CreatePerspectiveMatrix(float mat[16],
+ double fovy,
+ double aspect,
+ double zNear,
+ double zFar)
+{
+ const double f = 1.0 / tan(fovy / 2.0);
+
+ /* Column 0 */
+ mat[0] = (float)(f / aspect);
+ mat[1] = 0.0f;
+ mat[2] = 0.0f;
+ mat[3] = 0.0f;
+
+ /* Column 1 */
+ mat[4] = 0.0f;
+ mat[5] = (float)f;
+ mat[6] = 0.0f;
+ mat[7] = 0.0f;
+
+ /* Column 2 */
+ mat[8] = 0.0f;
+ mat[9] = 0.0f;
+ mat[10] = (-zFar - zNear) / (zFar - zNear);
+ mat[11] = -1.0f;
+
+ /* Column 3 */
+ mat[12] = 0.0f;
+ mat[13] = 0.0f;
+ mat[14] = (2.0f * zFar * zNear) / (zNear - zFar);
+ mat[15] = 0.0f;
+}