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

109 lines
2.5 KiB
C++
Raw Normal View History

2015-11-03 15:40:20 +00:00
#include <cstdlib>
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-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-03 19:42:02 +00:00
static GLfloat view[] = {
1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0 ,0.0, 0.0, 1.0,
};
static const char vertex_shader_source[] = \
"attribute vec4 position; \n"\
"uniform mat4 view_matrix; \n"\
"void main(void) { \n"\
" gl_Position = view_matrix * position; \n"\
"} \n";
2015-11-03 19:00:55 +00:00
static const char fragment_shader_source[] = \
"void main(void) { \n"\
" gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0); \n"\
"} \n";
static const GLfloat triangle_vertices[] = {
0.0, 0.8, 0.0,
-0.8, -0.8, 0.0,
0.8, -0.8, 0.0,
};
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);
}
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");
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);
glBufferData(GL_ARRAY_BUFFER, 3 * 7 * sizeof(GLfloat), triangle_vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), reinterpret_cast<const GLvoid*>(0));
glEnableVertexAttribArray(0);
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-03 19:42:02 +00:00
glUniformMatrix4fv(view_matrix, 1, GL_FALSE, view);
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
}