1
0
Fork 0

Add basic GL drawing

This commit is contained in:
Meoweg 2015-11-03 19:00:55 +00:00
parent 8ff7ad3dfb
commit 07e27f0bad
1 changed files with 68 additions and 1 deletions

View File

@ -1,11 +1,39 @@
#include <cstdlib>
#include <GL/glew.h>
#include <GL/glfw.h>
#include <emscripten/emscripten.h>
static GLuint load_shader(GLenum type, const char *source);
static GLuint vertex_shader;
static GLuint fragment_shader;
static GLuint program;
static void iterate();
static const char vertex_shader_source[] = \
"attribute vec4 position; \n"\
"void main(void) { \n"\
" gl_Position = position; \n"\
"} \n";
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;
int main()
{
if (!glfwInit())
@ -17,10 +45,49 @@ int main()
exit(EXIT_FAILURE);
}
emscripten_set_main_loop(iterate, 60, 1);
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);
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;
}
void iterate()
{
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, 3);
glfwPollEvents();
}