-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenGLES2stuff.cpp
More file actions
129 lines (109 loc) · 3.59 KB
/
OpenGLES2stuff.cpp
File metadata and controls
129 lines (109 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include "OpenGLES2stuff.h"
// Functions loadShader() and createProgram(), define LOGI from Android NDK with the follwing license:
// * Copyright (C) 2009 The Android Open Source Project
// * Licensed under the Apache License, Version 2.0 (the "License");
// * http://www.apache.org/licenses/LICENSE-2.0
#ifdef ANDROID
#include <android/log.h>
#ifndef LOG_TAG
#define LOG_TAG "org.ivci.qwrt"
#endif
#ifndef LOGI
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
#endif
#endif
unsigned int OpenGLES2stuff::programID = 0;
int OpenGLES2stuff::gPositionHandle = 0;
int OpenGLES2stuff::gTexCoordHandle = 0;
int OpenGLES2stuff::uMatrixLocation = 0;
int OpenGLES2stuff::uTextureUnitLocation = 0;
vector<GLuint> OpenGLES2stuff::programIDs;
vector<GLuint> OpenGLES2stuff::shaderIDs;
OpenGLES2stuff::~OpenGLES2stuff(){
for (int i = 0; i < (int)shaderIDs.size(); i++){
glDeleteShader(shaderIDs[i]);
}
for (int i = 0; i < (int)programIDs.size(); i++){
glDeleteProgram(programIDs[i]);
}
}
void OpenGLES2stuff::init(){
#ifdef ANDROID
programID = createProgram(gVertexShader, gFragmentShader);
gPositionHandle = glGetAttribLocation(programID, "a_Position");
gTexCoordHandle = glGetAttribLocation(programID, "a_TextureCoordinates");
uMatrixLocation = glGetUniformLocation(programID, "u_Matrix");
uTextureUnitLocation = glGetUniformLocation(programID, "u_TextureUnit");
#else
GLenum err = glewInit();
if (err != GLEW_OK) {
cerr << "GLEW init failed!" << std::endl;
exit(EXIT_FAILURE);
}
#endif
}
GLuint OpenGLES2stuff::loadShader(GLenum shaderType, const char* source) {
GLuint shader = glCreateShader(shaderType);
if(shader){
glShaderSource(shader, 1, &source, NULL);
glCompileShader(shader);
GLint compiled = 0;
glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
if(!compiled) {
GLint infoLength = 0;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLength);
if(infoLength) {
char* buf = (char*)malloc(infoLength);
if (buf) {
glGetShaderInfoLog(shader, infoLength, NULL, buf);
printf("Error compiling %u:\n%s\n", shaderType, buf);
#ifdef ANDROID
LOGI("Error compiling %u:\n%s\n", shaderType, buf);
#endif
free(buf);
}
glDeleteShader(shader);
shader = 0;
SDL_Delay(1000);
exit(7);
}
}
}
shaderIDs.push_back(shader);
return shader;
}
GLuint OpenGLES2stuff::createProgram(const char* pVertexSource, const char* pFragmentSource) {
GLuint vertexShader = loadShader(GL_VERTEX_SHADER, pVertexSource);
if(!vertexShader){
return 0;
}
GLuint pixelShader = loadShader(GL_FRAGMENT_SHADER, pFragmentSource);
if(!pixelShader){
return 0;
}
GLuint program = glCreateProgram();
if(program){
glAttachShader(program, vertexShader);
glAttachShader(program, pixelShader);
glLinkProgram(program);
GLint linkStatus = GL_FALSE;
glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
if(linkStatus != GL_TRUE) {
exit(8);
GLint bufLength = 0;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufLength);
if (bufLength) {
char* buf = (char*) malloc(bufLength);
if(buf) {
glGetProgramInfoLog(program, bufLength, NULL, buf);
printf("Could not link program: %s\n", buf);
free(buf);
}
}
glDeleteProgram(program);
program = 0;
}
}
programIDs.push_back(program);
return program;
}