summaryrefslogtreecommitdiff
path: root/frontend/_wasm/Shader.cpp
diff options
context:
space:
mode:
authorMatthew Kosarek <matthew.kosarek@vention.cc>2021-03-13 15:43:56 -0500
committerMatthew Kosarek <matthew.kosarek@vention.cc>2021-03-13 15:43:56 -0500
commit98d7d6cb702af2708f20e7cf16ee10a9f71b578a (patch)
tree92cec56a5a31a9658c44d49ed896ba8b7859450f /frontend/_wasm/Shader.cpp
parent3b0514ed3a21c39b7ea1a68d2fb381c808dcfeeb (diff)
My First WASM application is rendering a triangle on the frontend
Diffstat (limited to 'frontend/_wasm/Shader.cpp')
-rw-r--r--frontend/_wasm/Shader.cpp61
1 files changed, 61 insertions, 0 deletions
diff --git a/frontend/_wasm/Shader.cpp b/frontend/_wasm/Shader.cpp
new file mode 100644
index 0000000..5f2b00e
--- /dev/null
+++ b/frontend/_wasm/Shader.cpp
@@ -0,0 +1,61 @@
+#include "Shader.h"
+#include <string>
+
+GLuint loadIndividualShader(GLenum shaderType, const GLchar* cCode) {
+ GLuint shader = glCreateShader(shaderType);
+ glShaderSource(shader, 1, &cCode, 0);
+ glCompileShader(shader);
+ GLint success;
+ glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
+ if (!success) {
+ GLchar infoLog[512];
+ glGetShaderInfoLog(shader, 512, 0, infoLog);
+ printf("Failed to load shader: %s, Shader =%s\n", infoLog, cCode);
+ return 0;
+ }
+
+ return shader;
+}
+
+void attachShaders(Shader& retVal, const GLchar* vertexShader, const GLchar* fragmentShader) {
+ GLuint vertex = 0, fragment = 0, geometry = 0;
+ if (vertexShader) {
+ vertex = loadIndividualShader(GL_VERTEX_SHADER, vertexShader);
+ glAttachShader(retVal, vertex);
+ }
+
+ if (fragmentShader) {
+ fragment = loadIndividualShader(GL_FRAGMENT_SHADER, fragmentShader);
+ glAttachShader(retVal, fragment);
+ }
+
+ glLinkProgram(retVal);
+ GLint isLinked = 0;
+ glGetProgramiv(retVal, GL_LINK_STATUS, (int*)&isLinked);
+ if (isLinked == GL_FALSE) {
+ GLint maxLength = 0;
+ glGetProgramiv(retVal, GL_INFO_LOG_LENGTH, &maxLength);
+
+ // The maxLength includes the NULL character
+ GLchar* infoLog = new GLchar[maxLength];
+ glGetProgramInfoLog(retVal, maxLength, &maxLength, infoLog);
+ glDeleteProgram(retVal);
+ printf("Error. Could not initialize shader with vertex=%s, error=%s\n", vertexShader, infoLog);
+ delete []infoLog;
+ }
+
+ if (vertexShader)
+ glDeleteShader(vertex);
+ if (fragmentShader)
+ glDeleteShader(fragment);
+}
+
+Shader loadShader(const GLchar* vertexShader, const GLchar* fragmentShader) {
+ Shader retVal;
+ retVal = glCreateProgram();
+
+ attachShaders(retVal, vertexShader, fragmentShader);
+ useShader(retVal);
+
+ return retVal;
+}