/** Uses glReadPixels to read the bytes of the given context's drawing buffer into an unsigned char array @param ctx the given context2d @return an unsigned char array containing the bytes of ctx's draw buffer **/ unsigned char *context_2d_read_pixels(context_2d *ctx) { //must flush before reading as canvas may not be ready //to be read from draw_textures_flush(); unsigned char *buffer = NULL; buffer = (unsigned char *)malloc(sizeof(unsigned char) * 4 * ctx->width * ctx->height); glReadPixels(0, 0, ctx->width, ctx->height, GL_RGBA, GL_UNSIGNED_BYTE, buffer); return buffer; }
/** * @name context_2d_draw_point_sprites * @brief Draws pointsprites using the given options (in batch along a line) * @param ctx - (context_2d *) context to draw to * @param url - (const char *) name of the texture to draw from * @param point_size - (float) point sprite size * @param step_size - (float) step size to take between drawn pointsprites * @param color - (rgba *) color to draw with * @param x1 - (float) starting x-coordinate to draw along * @param y1 - (float) starting y-coordinate to draw along * @param x2 - (float) ending x-coordinate to draw along * @param y2 - (float) ending y-coordinate to draw along * @retval NONE */ void context_2d_draw_point_sprites(context_2d *ctx, const char *url, float point_size, float step_size, rgba *color, float x1, float y1, float x2, float y2) { draw_textures_flush(); context_2d_bind(ctx); texture_2d *tex = texture_manager_load_texture(texture_manager_get(), url); // If texture is not finished loading, if (!tex || !tex->loaded) { return; } static GLfloat *vertex_buffer = NULL; static unsigned int vertex_max = 64; tealeaf_shaders_bind(DRAWING_SHADER); matrix_3x3_multiply_m_f_f_f_f(GET_MODEL_VIEW_MATRIX(ctx), x1, y1, &x1, &y1); matrix_3x3_multiply_m_f_f_f_f(GET_MODEL_VIEW_MATRIX(ctx), x2, y2, &x2, &y2); // Allocate vertex array buffer if (vertex_buffer == NULL) { vertex_buffer = malloc(vertex_max * 2 * sizeof(GLfloat)); } // Add points to the buffer so there are drawing points every X pixels unsigned int count = ceilf(sqrtf((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)) / step_size); if (count < 1) { count = 1; } unsigned int vertex_count = 0; unsigned int i; for (i = 0; i < count; ++i) { if (vertex_count == vertex_max) { vertex_max = 2 * vertex_max; vertex_buffer = realloc(vertex_buffer, vertex_max * 2 * sizeof(GLfloat)); } vertex_buffer[2 * vertex_count + 0] = x1 + (x2 - x1) * ((GLfloat)i / (GLfloat)count); vertex_buffer[2 * vertex_count + 1] = y1 + (y2 - y1) * ((GLfloat)i / (GLfloat)count); vertex_count += 1; } GLTRACE(glActiveTexture(GL_TEXTURE0)); GLTRACE(glBindTexture(GL_TEXTURE_2D, tex->name)); GLTRACE(glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA)); GLTRACE(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)); GLTRACE(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)); GLTRACE(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)); GLTRACE(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)); // Render the vertex array GLTRACE(glUniform1f(global_shaders[DRAWING_SHADER].point_size, point_size)); GLTRACE(glVertexAttribPointer(global_shaders[DRAWING_SHADER].vertex_coords, 2, GL_FLOAT, GL_FALSE, 0, (float *) vertex_buffer)); float alpha = color->a * ctx->globalAlpha[ctx->mvp]; GLTRACE(glUniform4f(global_shaders[DRAWING_SHADER].draw_color, alpha * color->r, alpha * color->g, alpha * color->b, alpha)); GLTRACE(glDrawArrays(GL_POINTS, 0, vertex_count)); tealeaf_shaders_bind(PRIMARY_SHADER); }
/** * @name enable_scissor * @brief enables the use of glScissors using proprties from the given context * @param ctx - (context_2d *) * @retval NONE */ void enable_scissor(context_2d *ctx) { rect_2d *bounds = GET_CLIPPING_BOUNDS(ctx); if (rect_2d_equals(&last_scissor_rect, bounds)) { return; } draw_textures_flush(); last_scissor_rect.x = bounds->x; last_scissor_rect.y = bounds->y; last_scissor_rect.width = bounds->width; last_scissor_rect.height = bounds->height; GLTRACE(glScissor((int) bounds->x, (int) bounds->y, (int) bounds->width, (int) bounds->height)); GLTRACE(glEnable(GL_SCISSOR_TEST)); }
/** * @name context_2d_clearRect * @brief clears the given rect on the given context * @param ctx - (context_2d *) context to clear a rect from * @param rect - (const rect_2d *) rect to clear from the context * @retval NONE */ void context_2d_clearRect(context_2d *ctx, const rect_2d *rect) { draw_textures_flush(); context_2d_bind(ctx); // Draw a rectangle using triangle strip: // (0,1)-(2,3)-(4,5) and (2,3)-(4,5)-(6,7) // // With coordinates: // 4,5 - 6,7 // | \ | // 0,1 - 2,3 GLfloat v[8]; matrix_3x3_multiply(GET_MODEL_VIEW_MATRIX(ctx), rect, (float *)&v[4], (float *)&v[5], (float *)&v[6], (float *)&v[7], (float *)&v[2], (float *)&v[3], (float *)&v[0], (float *)&v[1]); glBlendFunc(GL_ONE, GL_ZERO); glUniform4f(global_shaders[PRIMARY_SHADER].draw_color, 0, 0, 0, 0); // set color to 0 glVertexAttribPointer(global_shaders[PRIMARY_SHADER].vertex_coords, 2, GL_FLOAT, GL_FALSE, 0, v); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); }
/** * @name context_2d_fillRect * @brief fills a rectangle on the given context using given options * @param ctx - (context_2d *) context to fill a rectangle on * @param rect - (const rect_2d *) rect to be filled * @param color - (const rgba *) color to fill with * @param composite_op - deprecated * @retval NONE */ void context_2d_fillRect(context_2d *ctx, const rect_2d *rect, const rgba *color) { if (use_single_shader) { return; } draw_textures_flush(); context_2d_bind(ctx); tealeaf_shaders_bind(FILL_RECT_SHADER); apply_composite_operation(ctx->globalCompositeOperation[ctx->mvp]); rect_2d_vertices in, out; rect_2d_to_rect_2d_vertices(rect, &in); matrix_3x3_multiply_m_r_r(GET_MODEL_VIEW_MATRIX(ctx), &in, &out); float alpha = color->a * ctx->globalAlpha[ctx->mvp]; // TODO: will pre-multiplied alpha cause a loss-of-precision in color for filling rectangles? GLTRACE(glUniform4f(global_shaders[FILL_RECT_SHADER].draw_color, alpha * color->r, alpha * color->g, alpha * color->b, alpha)); GLTRACE(glVertexAttribPointer(global_shaders[FILL_RECT_SHADER].vertex_coords, 2, GL_FLOAT, GL_FALSE, 0, &out)); GLTRACE(glDrawArrays(GL_TRIANGLE_STRIP, 0, 4)); tealeaf_shaders_bind(PRIMARY_SHADER); }
/** * @name tealeaf_canvas_context_2d_bind * @brief uses the given texture to bind to either the render buffer or a fbo * @param ctx - (context_2d *) pointer to the context to use for binding * @retval bool - whether the bind failed or succeeded */ bool tealeaf_canvas_context_2d_bind(context_2d *ctx) { if (canvas.active_ctx != ctx) { draw_textures_flush(); // Update active context after flushing canvas.active_ctx = ctx; if (ctx->on_screen) { tealeaf_canvas_bind_render_buffer(ctx); } else { tealeaf_canvas_bind_texture_buffer(ctx); } tealeaf_context_update_viewport(ctx, false); if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { LOG("{canvas} WARNING: Failed to make complete framebuffer %i", glCheckFramebufferStatus(GL_FRAMEBUFFER)); } return true; } else { return false; } }
/** * @name context_2d_flush * @brief flushes the texture drawing queue * @param ctx - deprecated * @retval NONE */ void context_2d_flush(context_2d *ctx) { draw_textures_flush(); }
/** * @name context_2d_clear * @brief * @param ctx - (context_2d *) * @retval NONE */ void context_2d_clear(context_2d *ctx) { draw_textures_flush(); context_2d_bind(ctx); glClearColor(0, 0, 0, 0); glClear(GL_COLOR_BUFFER_BIT); }
/** * @name disable_scissor * @brief disables the use of glScissors * @param ctx - deprecated * @retval NONE */ void disable_scissor(context_2d *ctx) { draw_textures_flush(); last_scissor_rect.x = last_scissor_rect.y = 0; last_scissor_rect.width = last_scissor_rect.height = -1; GLTRACE(glDisable(GL_SCISSOR_TEST)); }