2015-11-03 10:40:20 -05:00
|
|
|
#include <cstdlib>
|
|
|
|
|
2015-11-03 23:48:27 -05:00
|
|
|
#include <glm/glm.hpp>
|
|
|
|
#include <glm/gtc/type_ptr.hpp>
|
2015-11-04 13:38:02 -05:00
|
|
|
#include <glm/gtc/matrix_transform.hpp>
|
2015-11-03 23:48:27 -05:00
|
|
|
|
2015-11-03 14:00:55 -05:00
|
|
|
#include <GL/glew.h>
|
2015-11-03 10:40:20 -05:00
|
|
|
#include <GL/glfw.h>
|
|
|
|
|
|
|
|
#include <emscripten/emscripten.h>
|
2015-11-04 13:38:02 -05:00
|
|
|
#include <emscripten/html5.h>
|
|
|
|
|
|
|
|
static EM_BOOL on_em_mousemove(int event_type, const EmscriptenMouseEvent *mouse_event, void *user_data);
|
2015-11-03 10:40:20 -05:00
|
|
|
|
2015-11-03 14:00:55 -05:00
|
|
|
static GLuint load_shader(GLenum type, const char *source);
|
|
|
|
|
|
|
|
static GLuint vertex_shader;
|
|
|
|
static GLuint fragment_shader;
|
|
|
|
|
|
|
|
static GLuint program;
|
|
|
|
|
2015-11-03 10:40:20 -05:00
|
|
|
static void iterate();
|
|
|
|
|
2015-11-03 14:42:02 -05:00
|
|
|
static GLuint view_matrix;
|
2015-11-03 14:00:55 -05:00
|
|
|
|
2015-11-04 13:38:02 -05:00
|
|
|
static float delta_z = 0, delta_x = 0;
|
2015-11-03 14:42:02 -05:00
|
|
|
|
|
|
|
static const char vertex_shader_source[] = \
|
|
|
|
"attribute vec4 position; \n"\
|
2015-11-04 14:01:17 -05:00
|
|
|
"attribute vec3 color; \n"\
|
|
|
|
|
|
|
|
"varying vec4 f_color; \n"\
|
2015-11-03 14:42:02 -05:00
|
|
|
|
|
|
|
"uniform mat4 view_matrix; \n"\
|
|
|
|
|
|
|
|
"void main(void) { \n"\
|
|
|
|
" gl_Position = view_matrix * position; \n"\
|
2015-11-04 14:01:17 -05:00
|
|
|
" f_color = vec4(color, 1.0); \n"\
|
2015-11-03 14:42:02 -05:00
|
|
|
"} \n";
|
2015-11-03 14:00:55 -05:00
|
|
|
|
2015-11-04 14:01:17 -05:00
|
|
|
static const char fragment_shader_source[] = \
|
|
|
|
"precision lowp float; \n"\
|
|
|
|
|
|
|
|
"varying vec4 f_color; \n"\
|
|
|
|
|
|
|
|
"void main(void) { \n"\
|
|
|
|
" gl_FragColor = f_color; \n"\
|
|
|
|
"} \n";
|
2015-11-03 14:00:55 -05:00
|
|
|
|
2015-11-04 14:39:44 -05:00
|
|
|
static GLfloat cube_vertices[] = {
|
|
|
|
// front
|
|
|
|
-1.0, -1.0, 1.0,
|
|
|
|
1.0, -1.0, 1.0,
|
|
|
|
1.0, 1.0, 1.0,
|
|
|
|
-1.0, 1.0, 1.0,
|
|
|
|
// back
|
|
|
|
-1.0, -1.0, -1.0,
|
|
|
|
1.0, -1.0, -1.0,
|
|
|
|
1.0, 1.0, -1.0,
|
|
|
|
-1.0, 1.0, -1.0,
|
2015-11-03 14:00:55 -05:00
|
|
|
};
|
|
|
|
|
2015-11-04 14:39:44 -05:00
|
|
|
const GLfloat cube_colors[] = {
|
|
|
|
// front colors
|
|
|
|
1.0, 0.0, 0.0,
|
|
|
|
0.0, 1.0, 0.0,
|
|
|
|
0.0, 0.0, 1.0,
|
|
|
|
1.0, 1.0, 1.0,
|
|
|
|
// back colors
|
|
|
|
1.0, 0.0, 0.0,
|
|
|
|
0.0, 1.0, 0.0,
|
|
|
|
0.0, 0.0, 1.0,
|
|
|
|
1.0, 1.0, 1.0,
|
|
|
|
};
|
|
|
|
|
|
|
|
const GLushort cube_elements[] = {
|
|
|
|
// front
|
|
|
|
0, 1, 2,
|
|
|
|
2, 3, 0,
|
|
|
|
// top
|
|
|
|
3, 2, 6,
|
|
|
|
6, 7, 3,
|
|
|
|
// back
|
|
|
|
7, 6, 5,
|
|
|
|
5, 4, 7,
|
|
|
|
// bottom
|
|
|
|
4, 5, 1,
|
|
|
|
1, 0, 4,
|
|
|
|
// left
|
|
|
|
4, 0, 3,
|
|
|
|
3, 7, 4,
|
|
|
|
// right
|
|
|
|
1, 5, 6,
|
|
|
|
6, 2, 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
static GLuint cube_vertices_id, cube_colors_id;
|
|
|
|
static GLuint cube_elements_id;
|
2015-11-03 14:00:55 -05:00
|
|
|
|
2015-11-03 06:46:05 -05:00
|
|
|
int main()
|
|
|
|
{
|
2015-11-03 10:40:20 -05:00
|
|
|
if (!glfwInit())
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
|
|
|
if (!glfwOpenWindow(640, 480, 8, 8, 8, 8, 16, 0, GLFW_WINDOW))
|
|
|
|
{
|
|
|
|
glfwTerminate();
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2015-11-03 14:00:55 -05:00
|
|
|
if (glewInit() != GLEW_OK)
|
|
|
|
{
|
|
|
|
glfwCloseWindow();
|
|
|
|
glfwTerminate();
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2015-11-04 13:38:02 -05:00
|
|
|
emscripten_set_mousemove_callback(nullptr, nullptr, false, on_em_mousemove);
|
|
|
|
|
2015-11-03 14:00:55 -05:00
|
|
|
vertex_shader = load_shader(GL_VERTEX_SHADER, vertex_shader_source);
|
|
|
|
fragment_shader = load_shader(GL_FRAGMENT_SHADER, fragment_shader_source);
|
|
|
|
|
|
|
|
program = glCreateProgram();
|
|
|
|
glAttachShader(program, vertex_shader);
|
|
|
|
glAttachShader(program, fragment_shader);
|
|
|
|
glBindAttribLocation(program, 0, "position");
|
2015-11-04 14:01:17 -05:00
|
|
|
glBindAttribLocation(program, 1, "color");
|
2015-11-03 14:00:55 -05:00
|
|
|
glLinkProgram(program);
|
|
|
|
glUseProgram(program);
|
|
|
|
|
2015-11-03 14:42:02 -05:00
|
|
|
view_matrix = glGetUniformLocation(program, "view_matrix");
|
|
|
|
|
2015-11-04 14:39:44 -05:00
|
|
|
glGenBuffers(1, &cube_vertices_id);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, cube_vertices_id);
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(cube_vertices), cube_vertices, GL_STATIC_DRAW);
|
|
|
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, reinterpret_cast<const GLvoid*>(0));
|
2015-11-03 14:00:55 -05:00
|
|
|
glEnableVertexAttribArray(0);
|
2015-11-04 14:39:44 -05:00
|
|
|
|
|
|
|
glGenBuffers(1, &cube_colors_id);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, cube_colors_id);
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(cube_colors), cube_colors, GL_STATIC_DRAW);
|
|
|
|
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, reinterpret_cast<const GLvoid*>(0));
|
2015-11-04 14:01:17 -05:00
|
|
|
glEnableVertexAttribArray(1);
|
2015-11-03 14:00:55 -05:00
|
|
|
|
2015-11-04 14:39:44 -05:00
|
|
|
glGenBuffers(1, &cube_elements_id);
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cube_elements_id);
|
|
|
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(cube_elements), cube_elements, GL_STATIC_DRAW);
|
|
|
|
|
2015-11-03 14:00:55 -05:00
|
|
|
glViewport(0, 0, 640, 480);
|
|
|
|
|
2015-11-04 14:39:44 -05:00
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
|
2015-11-03 14:00:55 -05:00
|
|
|
glClearColor(0, 0, 0, 0);
|
|
|
|
|
|
|
|
emscripten_set_main_loop(iterate, 0, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
GLuint load_shader(const GLenum type, const char *source)
|
|
|
|
{
|
|
|
|
const GLuint shader = glCreateShader(type);
|
|
|
|
glShaderSource(shader, 1, &source, nullptr);
|
|
|
|
glCompileShader(shader);
|
|
|
|
return shader;
|
2015-11-03 10:40:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void iterate()
|
|
|
|
{
|
2015-11-04 14:39:44 -05:00
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
2015-11-03 14:00:55 -05:00
|
|
|
|
2015-11-04 13:38:02 -05:00
|
|
|
glm::mat4 view = glm::mat4(1.0f)
|
|
|
|
* glm::rotate(glm::mat4(1.0f), glm::radians(delta_x - 90), glm::vec3(1.0f, 0.0f, 0.0f))
|
|
|
|
* glm::rotate(glm::mat4(1.0f), glm::radians(delta_z), glm::vec3(0.0f, 0.0f, 1.0f));
|
|
|
|
|
2015-11-03 23:48:27 -05:00
|
|
|
glUniformMatrix4fv(view_matrix, 1, GL_FALSE, glm::value_ptr(view));
|
2015-11-03 14:42:02 -05:00
|
|
|
|
2015-11-04 14:39:44 -05:00
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cube_elements_id);
|
|
|
|
glDrawElements(GL_TRIANGLES, 6 * 2 * 3, GL_UNSIGNED_SHORT, 0);
|
2015-11-03 14:00:55 -05:00
|
|
|
|
2015-11-03 10:40:20 -05:00
|
|
|
glfwPollEvents();
|
2015-11-03 06:46:05 -05:00
|
|
|
}
|
2015-11-04 13:38:02 -05:00
|
|
|
|
|
|
|
EM_BOOL on_em_mousemove(int event_type, const EmscriptenMouseEvent *mouse_event, void *user_data)
|
|
|
|
{
|
|
|
|
delta_z += mouse_event->movementX;
|
|
|
|
delta_x += mouse_event->movementY;
|
|
|
|
|
|
|
|
if (delta_z < 0)
|
|
|
|
delta_z = 359;
|
|
|
|
else
|
|
|
|
if (delta_z >= 360)
|
|
|
|
delta_z = 0;
|
|
|
|
|
|
|
|
if (delta_x < -90)
|
|
|
|
delta_x = -90;
|
|
|
|
else
|
|
|
|
if (delta_x > 90)
|
|
|
|
delta_x = 90;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|