1
0
Fork 0
This repository has been archived on 2023-03-27. You can view files and clone it, but cannot push or open issues or pull requests.
matabstrix/main.cpp

148 lines
3.7 KiB
C++
Raw Normal View History

2015-11-03 15:40:20 +00:00
#include <cstdlib>
2015-11-04 04:48:27 +00:00
#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>
2015-11-04 18:38:02 +00:00
#include <glm/gtc/matrix_transform.hpp>
2015-11-04 04:48:27 +00:00
2015-11-03 19:00:55 +00:00
#include <GL/glew.h>
2015-11-03 15:40:20 +00:00
#include <GL/glfw.h>
#include <emscripten/emscripten.h>
2015-11-04 18:38:02 +00:00
#include <emscripten/html5.h>
static EM_BOOL on_em_mousemove(int event_type, const EmscriptenMouseEvent *mouse_event, void *user_data);
2015-11-03 15:40:20 +00:00
2015-11-03 19:00:55 +00:00
static GLuint load_shader(GLenum type, const char *source);
static GLuint vertex_shader;
static GLuint fragment_shader;
static GLuint program;
2015-11-03 15:40:20 +00:00
static void iterate();
2015-11-03 19:42:02 +00:00
static GLuint view_matrix;
2015-11-03 19:00:55 +00:00
2015-11-04 18:38:02 +00:00
static float delta_z = 0, delta_x = 0;
2015-11-03 19:42:02 +00:00
static const char vertex_shader_source[] = \
"attribute vec4 position; \n"\
2015-11-04 19:01:17 +00:00
"attribute vec3 color; \n"\
"varying vec4 f_color; \n"\
2015-11-03 19:42:02 +00:00
"uniform mat4 view_matrix; \n"\
"void main(void) { \n"\
" gl_Position = view_matrix * position; \n"\
2015-11-04 19:01:17 +00:00
" f_color = vec4(color, 1.0); \n"\
2015-11-03 19:42:02 +00:00
"} \n";
2015-11-03 19:00:55 +00:00
2015-11-04 19:01:17 +00: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 19:00:55 +00:00
static const GLfloat triangle_vertices[] = {
2015-11-04 19:01:17 +00:00
0.0, 0.0, 0.8, 1.0, 0.0, 0.0,
-0.3, 0.0, 0.0, 0.0, 1.0, 0.0,
0.3, 0.0, 0.0, 0.0, 0.0, 1.0,
2015-11-03 19:00:55 +00:00
};
static GLuint triangle;
2015-11-03 11:46:05 +00:00
int main()
{
2015-11-03 15:40:20 +00: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 19:00:55 +00:00
if (glewInit() != GLEW_OK)
{
glfwCloseWindow();
glfwTerminate();
exit(EXIT_FAILURE);
}
2015-11-04 18:38:02 +00:00
emscripten_set_mousemove_callback(nullptr, nullptr, false, on_em_mousemove);
2015-11-03 19:00:55 +00: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 19:01:17 +00:00
glBindAttribLocation(program, 1, "color");
2015-11-03 19:00:55 +00:00
glLinkProgram(program);
glUseProgram(program);
2015-11-03 19:42:02 +00:00
view_matrix = glGetUniformLocation(program, "view_matrix");
2015-11-03 19:00:55 +00:00
glGenBuffers(1, &triangle);
glBindBuffer(GL_ARRAY_BUFFER, triangle);
2015-11-04 19:01:17 +00:00
glBufferData(GL_ARRAY_BUFFER, 3 * 6 * sizeof(GLfloat), triangle_vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), reinterpret_cast<const GLvoid*>(0));
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), reinterpret_cast<const GLvoid*>(3 * sizeof(GLfloat)));
2015-11-03 19:00:55 +00:00
glEnableVertexAttribArray(0);
2015-11-04 19:01:17 +00:00
glEnableVertexAttribArray(1);
2015-11-03 19:00:55 +00:00
glViewport(0, 0, 640, 480);
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 15:40:20 +00:00
}
void iterate()
{
2015-11-03 19:00:55 +00:00
glClear(GL_COLOR_BUFFER_BIT);
2015-11-04 18:38:02 +00: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-04 04:48:27 +00:00
glUniformMatrix4fv(view_matrix, 1, GL_FALSE, glm::value_ptr(view));
2015-11-03 19:42:02 +00:00
2015-11-03 19:00:55 +00:00
glDrawArrays(GL_TRIANGLES, 0, 3);
2015-11-03 15:40:20 +00:00
glfwPollEvents();
2015-11-03 11:46:05 +00:00
}
2015-11-04 18:38:02 +00: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;
}