MaxstARSDK
ShaderUtil.h
Go to the documentation of this file.
1 /*==============================================================================
2 Copyright 2017 Maxst, Inc. All Rights Reserved.
3 ==============================================================================*/
4 
5 
6 #pragma once
7 
8 #ifdef __ANDROID__
9 #include <GLES2/gl2.h>
10 #include <GLES2/gl2ext.h>
11 #endif
12 
13 #ifdef __IOS__
14 #include <OpenGLES/ES2/gl.h>
15 #include <OpenGLES/ES2/glext.h>
16 #endif
17 
18 #ifdef WIN32
19 #include <GL/glew.h>
20 #include <GL/glut.h>
21 #endif
22 
23 #ifdef __MacOS__
24 #include <OpenGL/gl3.h>
25 #endif
26 
27 #include <stdlib.h>
28 #include <string>
29 #include <stdlib.h>
30 
35 {
36 public:
37  static unsigned int createProgram(const char * vertexString, const char * fragmentString)
38  {
39  GLuint vertexShader = loadShader(GL_VERTEX_SHADER, vertexString);
40  if (!vertexShader)
41  {
42  return 0;
43  }
44 
45  GLuint pixelShader = loadShader(GL_FRAGMENT_SHADER, fragmentString);
46  if (!pixelShader)
47  {
48  return 0;
49  }
50 
51  GLuint program = glCreateProgram();
52  if (program)
53  {
54  glAttachShader(program, vertexShader);
55  checkGlError("glAttachShader");
56  glAttachShader(program, pixelShader);
57  checkGlError("glAttachShader");
58  glLinkProgram(program);
59  GLint linkStatus = GL_FALSE;
60  glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
61  if (linkStatus != GL_TRUE)
62  {
63  GLint bufLength = 0;
64  glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufLength);
65  if (bufLength)
66  {
67  char *buf = (char *)malloc(bufLength);
68  if (buf)
69  {
70  glGetProgramInfoLog(program, bufLength, NULL, buf);
71  printf("Could not link program:\n%s\n", buf);
72  free(buf);
73  }
74  }
75  glDeleteProgram(program);
76  program = 0;
77  }
78  }
79  return program;
80  }
81 
82  static unsigned int loadShader(unsigned int shaderType, const char *pSource)
83  {
84  GLuint shader = glCreateShader((GLenum)shaderType);
85  if (shader)
86  {
87  glShaderSource(shader, 1, &pSource, NULL);
88  glCompileShader(shader);
89  GLint compiled = 0;
90  glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
91  if (!compiled)
92  {
93  GLint infoLen = 0;
94  glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
95  if (infoLen)
96  {
97  char *buf = (char *)malloc(infoLen);
98  if (buf)
99  {
100  glGetShaderInfoLog(shader, infoLen, NULL, buf);
101  printf("Could not compile shader %d:\n%s\n", shaderType, buf);
102  free(buf);
103  }
104  glDeleteShader(shader);
105  shader = 0;
106  }
107  }
108  }
109  return shader;
110  }
111 
112  static void checkGlError(const char *op)
113  {
114  for (GLint error = glGetError(); error; error = glGetError())
115  {
116  printf("after %s() glError (0x%x)\n", op, error);
117  }
118  }
119 };
static void checkGlError(const char *op)
Definition: ShaderUtil.h:112
Shader compile utility.
Definition: ShaderUtil.h:34
static unsigned int createProgram(const char *vertexString, const char *fragmentString)
Definition: ShaderUtil.h:37
static unsigned int loadShader(unsigned int shaderType, const char *pSource)
Definition: ShaderUtil.h:82