-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathglsample.cpp
192 lines (171 loc) · 6.74 KB
/
glsample.cpp
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
/**
uc::apng::loader
Copyright (c) 2017, Kentaro Ushiyama
This software is released under the MIT License.
http://opensource.org/licenses/mit-license.php
*/
#define STB_IMAGE_IMPLEMENTATION
#include"uc_apng_loader.h"
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <array>
#include <vector>
#include <string>
#include <chrono>
#include <thread>
#if defined(_MSC_VER)
#include <GL/glew.h>
#include <GL/wglew.h>
#endif
#include <GLFW/glfw3.h>
namespace
{
const std::string VERTEX_SHADER = R"(
attribute vec4 a_position;
attribute vec2 a_texCoord;
varying vec2 v_texCoord;
void main()
{
gl_Position = a_position;
v_texCoord = a_texCoord;
})";
const std::string FRAGMENT_SHADER = R"(
#ifdef GL_ES
precision mediump float;
#endif
varying vec2 v_texCoord;
uniform sampler2D s_texture;
void main()
{
gl_FragColor = texture2D( s_texture, v_texCoord );
})";
void loadShader(GLuint prg, GLenum shaderType, const std::string& shader)
{
GLuint sh = glCreateShader(shaderType);
const GLchar* vsrcs[1] = { shader.c_str() };
const GLint vlen[1] = { static_cast<GLint>(shader.size()) };
glShaderSource(sh, 1, vsrcs, vlen);
glCompileShader(sh);
GLint compiled;
GLint loglen;
glGetShaderiv(sh, GL_COMPILE_STATUS, &compiled);
glGetShaderiv(sh, GL_INFO_LOG_LENGTH, &loglen);
std::vector<char> msg(loglen);
glGetShaderInfoLog(sh, loglen, nullptr, msg.data());
glAttachShader(prg, sh);
glDeleteShader(sh);
if (compiled == GL_FALSE) throw std::runtime_error(msg.data());
}
}
int main(int argc, char** argv)
{
if (argc < 2) {
std::cerr << "Usage : " << argv[0] << " [APNG filename]" << std::endl;
return 1;
}
if (!glfwInit()) {
std::cerr << "Error : glfwInit() failed." << std::endl;
return -1;
}
try {
uint32_t remainPlay{};
std::vector<uc::apng::frame> frames;
{
const std::string filename = argv[1];
auto loader = uc::apng::create_file_loader(filename);
remainPlay = loader.num_plays();
std::cout << "\n\"" << filename << "\" ("
<< loader.width() << "x" << loader.height() << "), "
<< loader.num_frames() << "frames, "
<< loader.num_plays() << " times to loop (0 indicates infinite looping).\n";
while (loader.has_frame()) {
frames.push_back(loader.next_frame());
}
if (frames.empty()) throw std::runtime_error("apng has no frame.");
}
auto frame = frames.begin();
auto window = glfwCreateWindow(frame->image.width(), frame->image.height(), "apng player", nullptr, nullptr);
if (!window) throw std::runtime_error("glfwCreateWindow() failed.");
glfwMakeContextCurrent(window);
#ifdef __glew_h__
glewExperimental = TRUE;
GLenum err = glewInit();
if (err != GLEW_OK) throw std::runtime_error("glewInit() failed.");
#endif
auto prg = glCreateProgram();
loadShader(prg, GL_VERTEX_SHADER, VERTEX_SHADER);
loadShader(prg, GL_FRAGMENT_SHADER, FRAGMENT_SHADER);
glLinkProgram(prg);
GLint linked;
GLint loglen = 0;
glGetProgramiv(prg, GL_LINK_STATUS, &linked);
glGetProgramiv(prg, GL_INFO_LOG_LENGTH, &loglen);
std::vector<char> msg(loglen);
glGetProgramInfoLog(prg, loglen, nullptr, msg.data());
if (linked == GL_FALSE) throw std::runtime_error(msg.data());
glUseProgram(prg);
std::array<GLuint, 2> vbo;
glGenBuffers(static_cast<GLsizei>(vbo.size()), vbo.data());
GLushort indices[] = { 0, 1, 2, 2, 1, 3 };
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo[0]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
GLfloat vertices[] = {
-1.0f, 1.0f, 0.0f, 0.0f, 0.0f,
1.0f, 1.0f, 0.0f, 1.0f, 0.0f,
-1.0f, -1.0f, 0.0f, 0.0f, 1.0f,
1.0f, -1.0f, 0.0f, 1.0f, 1.0f
};
glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
auto positionLoc = glGetAttribLocation(prg, "a_position");
auto texCoordLoc = glGetAttribLocation(prg, "a_texCoord");
glVertexAttribPointer(positionLoc, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), 0);
glVertexAttribPointer(texCoordLoc, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), reinterpret_cast<GLvoid *>(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(positionLoc);
glEnableVertexAttribArray(texCoordLoc);
glEnable(GL_TEXTURE_2D);
glActiveTexture(GL_TEXTURE0);
GLuint tex;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
using namespace std::chrono;
auto nextTime = high_resolution_clock::now();
nextTime += microseconds(1000000) * frame->delay_num / frame->delay_den;
while (!glfwWindowShouldClose(window)) {
glClear(GL_COLOR_BUFFER_BIT);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, frame->image.width(), frame->image.height(),
0, GL_RGBA, GL_UNSIGNED_BYTE, frame->image.data());
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0);
glfwSwapBuffers(window);
glfwPollEvents();
++frame;
if (frame == frames.end()) {
if (remainPlay > 0 && --remainPlay == 0) {
break;
}
frame = frames.begin();
}
std::this_thread::sleep_until(nextTime);
nextTime += microseconds(1000000) * frame->delay_num / frame->delay_den;
}
glDisableVertexAttribArray(texCoordLoc);
glDisableVertexAttribArray(positionLoc);
glDeleteTextures(1, &tex);
glDeleteBuffers(static_cast<GLsizei>(vbo.size()), vbo.data());
glDeleteProgram(prg);
glfwDestroyWindow(window);
std::cout << "quit.\n";
} catch (std::exception& ex) {
std::cerr << "Error : " << ex.what() << std::endl;
}
glfwTerminate();
return 0;
}