void GLUtils::displayBasicText(const float x, const float y, std::string text) { // First we will allocate a buffer that will be used as our VBO for vertex data. // Sean Barret says this will be ~270 bytes per character, so we will use 300 for good measure GLuint rawBufferSize = text.length() * 300; void *rawTextVertexBuffer = (void*)malloc(rawBufferSize); // Call into the stb_easy_font code to get the vertex data for the given text GLuint numQuadPrimitives = stb_easy_font_print(x, y, (char*)(text.c_str()), NULL, rawTextVertexBuffer, (int)rawBufferSize); // Set everything up to draw (these bindings will have to be restored by the caller) if (!s_textPipelineInitialized) s_textPipelineInitialized = s_intitializeTextPipeline(); if (!s_textPipelineInitialized) return; // Copy the data into the VBO glBufferData(GL_ARRAY_BUFFER, rawBufferSize, rawTextVertexBuffer, GL_STREAM_DRAW); // Set the color of the text via the uniform GLuint uniColor = glGetUniformLocation(s_basicShaderProgram, "basicColor"); glUniform3f(uniColor, 1.0f, 0.0f, 0.0f); // Draw the text glDrawArrays(GL_QUADS, 0, 4 * numQuadPrimitives); // Unset the pipeline flag because we assume other bindings will happen before this is called again s_textPipelineInitialized = false; }
void display_gl_text_render(display_gl_text_t* texts, mat4_t* projection_view) { int32 i; int32 vb_index = 0; v4_t color_diffuse; v4_t color_ambient; mat4_t local; v4_set(&color_diffuse, 1.f, 1.f, 1.0f, 1.0f); v4_set(&color_ambient, 0.0f, 0.0f, 0.0f, 0.0f); mat4_ident(&local); for (i = 0; i < texts->text_count; ++i) { t_text* text = &(texts->texts[i]); vb_index += stb_easy_font_print(text->position.x, text->position.y, text->text, (uint8*)&text->rgba, texts->text_mesh_vb + vb_index, texts->text_mesh_vb_size) * 64; } glDisable(GL_DEPTH_TEST); glBindBuffer(GL_ARRAY_BUFFER, display_gl_mesh_get_vb(texts->text_mesh)); glBufferSubData(GL_ARRAY_BUFFER, 0, vb_index, texts->text_mesh_vb); display_gl_mesh_render_start(texts->text_renderer, MESH_TYPE_VC); display_gl_mesh_set_ambient(texts->text_renderer, &color_ambient); display_gl_mesh_set_diffuse(texts->text_renderer, &color_diffuse); display_gl_mesh_set_projection(texts->text_renderer, projection_view); display_gl_mesh_set_local(texts->text_renderer, &local); display_gl_mesh_render_count(texts->text_mesh, (vb_index / 64) * 6); }
JNIEXPORT jint JNICALL Java_org_lwjgl_stb_STBEasyFont_nstb_1easy_1font_1print(JNIEnv *__env, jclass clazz, jfloat x, jfloat y, jlong textAddress, jlong colorAddress, jlong vertex_bufferAddress, jint vbuf_size) { char *text = (char *)(intptr_t)textAddress; unsigned char *color = (unsigned char *)(intptr_t)colorAddress; void *vertex_buffer = (void *)(intptr_t)vertex_bufferAddress; UNUSED_PARAMS(__env, clazz) return (jint)stb_easy_font_print(x, y, text, color, vertex_buffer, vbuf_size); }
inline void draw_text(int x, int y, const char * text) { char buffer[20000]; // ~100 chars glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer(2, GL_FLOAT, 16, buffer); glDrawArrays(GL_QUADS, 0, 4*stb_easy_font_print((float)x, (float)(y-7), (char *)text, nullptr, buffer, sizeof(buffer))); glDisableClientState(GL_VERTEX_ARRAY); }
static void print_string(float x, float y, char *text, float r, float g, float b) { static char buffer[99999]; int num_quads; num_quads = stb_easy_font_print(x, y, text, NULL, buffer, sizeof(buffer)); glColor3f(r,g,b); glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer(2, GL_FLOAT, 16, buffer); glDrawArrays(GL_QUADS, 0, num_quads*4); glDisableClientState(GL_VERTEX_ARRAY); }
bool drawText(float x, float y, std::string text, Vector3 c, float scale ) { static char buffer[99999]; // ~500 chars int num_quads; if (scale == 0) return true; x /= scale; y /= scale; if (Shader::current) Shader::current->disable(); num_quads = stb_easy_font_print(x, y, (char*)(text.c_str()), NULL, buffer, sizeof(buffer)); Matrix44 projection_matrix; projection_matrix.ortho(0, Game::instance->window_width / scale, Game::instance->window_height / scale, 0, -1, 1); glDisable(GL_DEPTH_TEST); glDisable(GL_CULL_FACE); glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadMatrixf(Matrix44().m); glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadMatrixf(projection_matrix.m); glColor3f(c.x, c.y, c.z); glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer(2, GL_FLOAT, 16, buffer); glDrawArrays(GL_QUADS, 0, num_quads * 4); glDisableClientState(GL_VERTEX_ARRAY); glPopMatrix(); glMatrixMode(GL_MODELVIEW); glPopMatrix(); glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); return true; }