-
-
Notifications
You must be signed in to change notification settings - Fork 180
/
Copy pathGLTool.c
240 lines (184 loc) · 6.33 KB
/
GLTool.c
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
* Copyright (c) scott.cgi All Rights Reserved.
*
* This source code belongs to project Mojoc, which is a pure C Game Engine hosted on GitHub.
* The Mojoc Game Engine is licensed under the MIT License, and will continue to be iterated with coding passion.
*
* License : https://github.com/scottcgi/Mojoc/blob/master/LICENSE
* GitHub : https://github.com/scottcgi/Mojoc
* CodeStyle: https://github.com/scottcgi/Mojoc/blob/master/Docs/CodeStyle.md
*
* Since : 2012-12-22
* Update : 2019-1-23
* Author : scott.cgi
*/
#include <string.h>
#include <stdlib.h>
#include "Engine/Toolkit/Platform/Log.h"
#include "Engine/Graphics/OpenGL/GLTool.h"
#include "Engine/Toolkit/Utils/FileTool.h"
#include "Engine/Graphics/Utils/Image.h"
static void SetSize(int width, int height)
{
// set the OpenGL viewport to the same size as the surface.
glViewport(0, 0, width, height);
AGLTool->screenWidth = (float) width;
AGLTool->screenHeight = (float) height;
AGLTool->screenRatio = (float) width / (float) height;
AGLTool->screenHalfWidth = (float) width / 2;
AGLTool->screenHalfHeight = (float) height / 2;
AGLTool->ratioDivideHalfWidth = AGLTool->screenRatio / AGLTool->screenHalfWidth;
AGLTool->halfWidthDivideRatio = AGLTool->screenHalfWidth / AGLTool->screenRatio;
}
static GLuint LoadShader(GLenum shaderType, const char* shaderSourceStr)
{
// create the shader object
GLuint shader = glCreateShader(shaderType);
if (shader == 0)
{
ALog_E("AGLTool LoadShader glCreateShader failed !");
return shader;
}
// load the shader source
glShaderSource(shader, 1, &shaderSourceStr, NULL);
// compile the shader
glCompileShader(shader);
GLint compiled;
// check the compile status
glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
if (compiled == GL_FALSE)
{
GLint infoLen = 0;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
if (infoLen > 0)
{
char buffer[infoLen];
glGetShaderInfoLog(shader, infoLen, NULL, buffer);
ALog_E("AGLTool LoadShader could not compile shader: %s, log: %s", shaderSourceStr, buffer);
glDeleteShader(shader);
}
else
{
ALog_E("AGLTool LoadShader could not compile shader: %s", shaderSourceStr);
}
return 0;
}
return shader;
}
static GLuint LoadProgram(const char* vertexSourceStr, const char* fragmentSourceStr)
{
GLuint program;
// load the vertex shader
GLuint vertexShader = LoadShader(GL_VERTEX_SHADER, vertexSourceStr);
if (vertexShader == 0)
{
ALog_E("AGLTool LoadProgram failed, cannot load shader: %s", vertexSourceStr);
return 0;
}
// load the fragment shader
GLuint fragmentShader = LoadShader(GL_FRAGMENT_SHADER, fragmentSourceStr);
if (fragmentShader == 0)
{
ALog_E("AGLTool LoadProgram failed, cannot load shader: %s", fragmentSourceStr);
return 0;
}
// create the program object
program = glCreateProgram();
if (program == 0)
{
ALog_E("AGLTool LoadProgram glCreateProgram failed,from shader: %s, %s", vertexSourceStr, fragmentSourceStr);
return 0;
}
glAttachShader(program, vertexShader);
glAttachShader(program, fragmentShader);
// link the program
glLinkProgram(program);
GLint linkStatus;
// check the link status
glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
if (linkStatus == GL_FALSE)
{
GLint bufLength = 0;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufLength);
if (bufLength > 0)
{
char buffer[bufLength];
glGetProgramInfoLog(program, bufLength, NULL, buffer);
ALog_E
(
"AGLTool LoadProgram cannot link program from shader: %s, %s, log: %s",
vertexSourceStr,
fragmentSourceStr,
buffer
);
}
else
{
ALog_E
(
"AGLTool LoadProgram cannot link program from shader: %s, %s",
vertexSourceStr,
fragmentSourceStr
);
}
glDeleteProgram(program);
return 0;
}
// free up no longer needed shader resources
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
return program;
}
GLuint LoadProgramFromFile(const char* vertexShaderFilePath, const char* fragmentShaderFilePath)
{
char* vertexShader = AFileTool->CreateStringFromResource(vertexShaderFilePath);
char* fragmentShader = AFileTool->CreateStringFromResource(fragmentShaderFilePath);
GLuint program = LoadProgram(vertexShader, fragmentShader);
free((void*) vertexShader);
free((void*) fragmentShader);
return program;
}
static void LoadTexture(const char* textureFilePath, Texture* outTexture)
{
GLuint textureID;
// use tightly packed data
// glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
// generate a outTexture object
glGenTextures(1, &textureID);
// bind to the outTexture
glBindTexture(GL_TEXTURE_2D, textureID);
outTexture->id = textureID;
// set filtering
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
float width;
float height;
void* pixels = AImage->CreatePixelDataFromPNG(textureFilePath, &width, &height);
ALog_A(pixels != NULL, "AGLTool LoadTexture failed, no pixels data found");
// load the data into the bound outTexture
glTexImage2D
(
GL_TEXTURE_2D,
0,
GL_RGBA,
(GLsizei) width,
(GLsizei) height,
0,
GL_RGBA,
GL_UNSIGNED_BYTE,
pixels
);
outTexture->width = AGLTool_ToGLWidth (width);
outTexture->height = AGLTool_ToGLHeight(height);
free(pixels);
}
struct AGLTool AGLTool[1] =
{{
.SetSize = SetSize,
.LoadShader = LoadShader,
.LoadProgram = LoadProgram,
.LoadProgramFromFile = LoadProgramFromFile,
.LoadTexture = LoadTexture,
}};