#include "Renderer2d.h" #include "WebglContext.h" #include "mathlib.h" const char* orthographicVertex = "attribute vec4 position; \n" "attribute vec4 color; \n" "uniform mat4 projection; \n" "uniform mat4 model; \n" "varying lowp vec4 VertexColor; \n" "void main() { \n" " vec4 fragmentPosition = projection * model * position; \n" " gl_Position = fragmentPosition; \n" " VertexColor = color; \n" " gl_PointSize = 4.0; \n" "}"; const char* orthographicFragment = "varying lowp vec4 VertexColor; \n" "void main() { \n" " gl_FragColor = VertexColor; \n" "}"; void Renderer2d::load(WebglContext* inContext) { context = inContext; printf("Compiling orthographic shader...\n"); shader = loadShader(orthographicVertex, orthographicFragment); useShader(shader); attributes.position = getShaderAttribute(shader, "position"); attributes.color = getShaderAttribute(shader, "color"); uniforms.projection = getShaderUniform(shader, "projection"); uniforms.model = getShaderUniform(shader, "model"); projection = Mat4x4().getOrthographicMatrix(0, context->width, 0, context->height); printf("Orthographic shader compiled.\n"); } void Renderer2d::render() { glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); glDepthMask(GL_TRUE); glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClearDepth(1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); useShader(shader); setShaderMat4(uniforms.projection, projection); } void Renderer2d::unload() { glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); glDeleteProgram(shader); } void Mesh2d::load(Vertex2d* inVertices, uint32 inNumVertices, Renderer2d* renderer, GLenum loadType) { mode = MeshMode::Vertex; numVertices = inNumVertices; isDynamic = loadType == GL_DYNAMIC_DRAW; useShader(renderer->shader); glGenVertexArrays(1, &vao); glBindVertexArray(vao); glGenBuffers(1, &vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo); glBufferData(GL_ARRAY_BUFFER, inNumVertices * sizeof(Vertex2d), &inVertices[0], loadType); glEnableVertexAttribArray(renderer->attributes.position); glVertexAttribPointer(renderer->attributes.position, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex2d), (GLvoid *)0); glEnableVertexAttribArray(renderer->attributes.color); glVertexAttribPointer(renderer->attributes.color, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex2d), (GLvoid *)offsetof(Vertex2d, color)); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); } void Mesh2d::load(Vertex2d* inVertices, uint32 inNumVertices, GLuint* inIndices, uint32 inNumIndices, Renderer2d* renderer, GLenum loadType) { mode = MeshMode::Index; numVertices = inNumVertices; numIndices = inNumIndices; isDynamic = loadType == GL_DYNAMIC_DRAW; useShader(renderer->shader); glGenVertexArrays(1, &vao); glBindVertexArray(vao); glGenBuffers(1, &vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo); glBufferData(GL_ARRAY_BUFFER, inNumVertices * sizeof(Vertex2d), &inVertices[0], loadType); glEnableVertexAttribArray(renderer->attributes.position); glVertexAttribPointer(renderer->attributes.position, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex2d), (GLvoid *)0); glEnableVertexAttribArray(renderer->attributes.color); glVertexAttribPointer(renderer->attributes.color, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex2d), (GLvoid *)offsetof(Vertex2d, color)); glGenBuffers(1, &ebo); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint) * numIndices, inIndices, loadType); glBindVertexArray(0); } void Mesh2d::render(Renderer2d* renderer, GLenum drawType) { setShaderMat4(renderer->uniforms.model, model); glBindVertexArray(vao); switch (mode) { case MeshMode::Vertex: glDrawArrays(drawType, 0, numVertices); break; case MeshMode::Index: glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); glDrawElements(drawType, numIndices, GL_UNSIGNED_INT, 0); break; } glBindVertexArray(0); } void Mesh2d::unload() { glDeleteVertexArrays(1, &vao); glDeleteBuffers(1, &vbo); if (mode == MeshMode::Index) { glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glDeleteBuffers(1, &ebo); } } void Mesh2d::updateVertices(Vertex2d* vertices) { if (!isDynamic) { printf("Cannot update vertices on a non-dynamic mesh.\n"); return; } glBindBuffer(GL_ARRAY_BUFFER, vbo); glBufferSubData(GL_ARRAY_BUFFER, 0, numVertices * sizeof(Vertex2d), &vertices[0]); }