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 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
|
/*
* Copyright 2018 The Emscripten Authors. All rights reserved.
* Emscripten is available under two separate licenses, the MIT license and the
* University of Illinois/NCSA Open Source License. Both these licenses can be
* found in the LICENSE file.
*/
#ifdef __EMSCRIPTEN__
#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
#else
#include <GL/glew.h>
#endif
#include <GL/glut.h>
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#include <emscripten/html5.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#ifndef __EMSCRIPTEN__
int fullscreen;
#endif
void trace(char* tag) {
static char* prev_tag = NULL;
static int prev_screen_width;
static int prev_screen_height;
static int prev_window_width;
static int prev_window_height;
static int prev_viewport_width;
static int prev_viewport_height;
static int prev_is_fullscreen;
static int prev_is_resized;
static int prev_coalesced;
int screen_width;
int screen_height;
int window_width;
int window_height;
int viewport_width;
int viewport_height;
int is_fullscreen;
int is_resized;
int coalesced;
GLint viewport[4];
screen_width = glutGet(GLUT_SCREEN_WIDTH);
screen_height = glutGet(GLUT_SCREEN_HEIGHT);
window_width = glutGet(GLUT_WINDOW_WIDTH);
window_height = glutGet(GLUT_WINDOW_HEIGHT);
glGetIntegerv(GL_VIEWPORT, viewport);
viewport_width = viewport[2];
viewport_height = viewport[3];
#ifdef __EMSCRIPTEN__
EmscriptenFullscreenChangeEvent fullscreen_status;
emscripten_get_fullscreen_status(&fullscreen_status);
is_fullscreen = fullscreen_status.isFullscreen;
is_fullscreen = is_fullscreen ? 1 : -1;
is_resized = EM_ASM_INT({
return document.getElementById('resize').checked;
});
is_resized = is_resized ? 1 : -1;
#else
is_fullscreen = 0;
is_resized = 1;
#endif
coalesced = prev_tag &&
!strcmp(tag, prev_tag) &&
screen_width == prev_screen_width &&
screen_height == prev_screen_height &&
window_width == prev_window_width &&
window_height == prev_window_height &&
viewport_width == prev_viewport_width &&
viewport_height == prev_viewport_height &&
is_fullscreen == prev_is_fullscreen &&
is_resized == prev_is_resized;
if (coalesced) {
if (!prev_coalesced) {
printf("...\n");
}
} else {
time_t t = time(NULL);
char* local_time = ctime(&t);
char* message = malloc(strlen(local_time) + strlen(tag) + 500);
sprintf(message, "[");
sprintf(message + strlen(message), "%s", local_time);
sprintf(message + strlen(message) - 1, " ");
sprintf(message + strlen(message), "%s", tag);
sprintf(message + strlen(message), "]");
sprintf(message + strlen(message), " ");
sprintf(message + strlen(message),
"screen width: %d"
", "
"screen height: %d"
", "
"window width: %d"
", "
"window height: %d"
", "
"viewport width: %d"
", "
"viewport height: %d"
", "
"fullscreen: %s"
", "
"resize: %s",
screen_width,
screen_height,
window_width,
window_height,
viewport_width,
viewport_height,
is_fullscreen > 0 ? "yes" : (is_fullscreen < 0 ? "no" : "unknown"),
is_resized > 0 ? "yes" : (is_resized < 0 ? "no" : "unknown"));
printf("%s\n", message);
free(message);
}
prev_tag = tag;
prev_screen_width = screen_width;
prev_screen_height = screen_height;
prev_window_width = window_width;
prev_window_height = window_height;
prev_viewport_width = viewport_width;
prev_viewport_height = viewport_height;
prev_is_fullscreen = is_fullscreen;
prev_is_resized = is_resized;
prev_coalesced = coalesced;
}
void onDisplay()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, 6);
glutSwapBuffers();
trace("onDisplay");
}
void onReshape(int width, int height)
{
glViewport(0, 0, width, height);
trace("onReshape");
}
void onKeyboard(unsigned char key, int x, int y)
{
if (key == 'f') {
#ifdef __EMSCRIPTEN__
EmscriptenFullscreenChangeEvent fullscreen_status;
int fullscreen;
emscripten_get_fullscreen_status(&fullscreen_status);
fullscreen = fullscreen_status.isFullscreen;
#endif
if (fullscreen) {
glutReshapeWindow(glutGet(GLUT_INIT_WINDOW_WIDTH), glutGet(GLUT_INIT_WINDOW_HEIGHT));
} else {
glutFullScreen();
}
#ifndef __EMSCRIPTEN__
fullscreen = !fullscreen;
#endif
trace("onKeyboard");
}
}
void onIdle()
{
glutPostRedisplay();
}
int main(int argc, char* argv[])
{
int win;
GLuint vertex_shader;
GLuint fragment_shader;
GLuint program;
GLuint vbo;
GLuint vao;
GLfloat vertices[] = {
/*
x, y,
*/
-1.f, -1.f,
1.f, -1.f,
-1.f, 1.f,
1.f, -1.f,
1.f, 1.f,
-1.f, 1.f,
};
glutInit(&argc, argv);
glutInitWindowSize(600, 450);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
win = glutCreateWindow(__FILE__);
#ifndef __EMSCRIPTEN__
GLenum err = glewInit();
if (err != GLEW_OK) {
printf("error: %s\n", glewGetErrorString(err));
glutDestroyWindow(win);
return EXIT_FAILURE;
}
#endif
glEnable(GL_DEPTH_TEST);
{
const GLchar* str = "precision mediump float;\n"
"attribute vec2 aPosition;\n"
"varying vec2 vPosition;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(aPosition, 0.0, 1.0);\n"
" vPosition = aPosition;\n"
"}\n";
vertex_shader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex_shader, 1, &str, NULL);
glCompileShader(vertex_shader);
{
GLint params;
GLchar* log;
glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, ¶ms);
if (params == GL_FALSE) {
glGetShaderiv(vertex_shader, GL_INFO_LOG_LENGTH, ¶ms);
log = malloc(params);
glGetShaderInfoLog(vertex_shader, params, NULL, log);
printf("%s", log);
free(log);
glutDestroyWindow(win);
return EXIT_FAILURE;
}
}
}
{
const GLchar* str = "precision mediump float;\n"
"varying vec2 vPosition;\n"
"void main()\n"
"{\n"
" if (abs(vPosition.x) > 0.9 || abs(vPosition.y) > 0.9) {\n"
" gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
" } else {\n"
" gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);\n"
" }\n"
"}\n";
fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment_shader, 1, &str, NULL);
glCompileShader(fragment_shader);
{
GLint params;
GLchar* log;
glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, ¶ms);
if (params == GL_FALSE) {
glGetShaderiv(fragment_shader, GL_INFO_LOG_LENGTH, ¶ms);
log = malloc(params);
glGetShaderInfoLog(fragment_shader, params, NULL, log);
printf("%s", log);
free(log);
glutDestroyWindow(win);
return EXIT_FAILURE;
}
}
}
program = glCreateProgram();
glAttachShader(program, vertex_shader);
glAttachShader(program, fragment_shader);
glBindAttribLocation(program, 0, "aPosition");
glLinkProgram(program);
{
GLint params;
GLchar* log;
glGetProgramiv(program, GL_LINK_STATUS, ¶ms);
if (params == GL_FALSE) {
glGetProgramiv(program, GL_INFO_LOG_LENGTH, ¶ms);
log = malloc(params);
glGetProgramInfoLog(program, params, NULL, log);
printf("%s", log);
free(log);
glutDestroyWindow(win);
return EXIT_FAILURE;
}
}
glUseProgram(program);
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof vertices, vertices, GL_STATIC_DRAW);
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, (const GLvoid*)0);
glEnableVertexAttribArray(0);
#ifndef __EMSCRIPTEN__
fullscreen = 0;
#endif
printf("You should see a green rectangle with red borders.\n");
printf("Press 'f' or click the 'Fullscreen' button on the upper right corner to enter full screen, and press 'f' or ESC to exit.\n");
printf("No matter 'Resize canvas' is checked or not, you should see the whole screen filled by the rectangle when in full screen, and after exiting, the rectangle should be restored in the window.\n");
glutDisplayFunc(onDisplay);
glutReshapeFunc(onReshape);
glutKeyboardFunc(onKeyboard);
glutIdleFunc(onIdle);
glutMainLoop();
glutDestroyWindow(win);
return EXIT_SUCCESS;
}
|