Cursor in Text box

Help with integrating libRocket into your engine and game

Cursor in Text box

Postby miccerisier on Thu Mar 14, 2013 7:25 am


I made my own OpenGL renderer for the librocket, all works correctly except the cursor in the text box (or textarea ...).
Someone know what function that draws the cursor and perhaps what is my problem exactly?

Posts: 4
Joined: Thu Mar 14, 2013 6:59 am

Re: Cursor in Text box

Postby Supertim on Sat Jun 14, 2014 8:56 pm

I saw similar issues when integrating libRocket with a native OpenGL interface using GLSL shaders. To mimic the immediate mode rendering, you need to modulate the texture sampler by the vertex color variant. The issue you're seeing is most likely related to the texture sampler being 0 (color only rendering), but it messes up your modulation and results in odd artifacts.

This can be further exacerbated by highlighting text in an input control - you will see the artifact become more obvious.

Make sure your shader modulates the texture (and modulator float is sent as uniform in rendering code too), so that if texture is 0 - you use vertex color component only. It may not be the most elegant way to do it, but here is my working GLSL shader to do that:

Fragment Shader
Code: Select all
#version 150

// Uniform texture handle
uniform sampler2D tex;
uniform float texModulator;

// Incoming attributes
in vec2 v_TexCoord;
in vec4 v_Color;

// Final fragment color
out vec4 outColor;

void main()
   outColor = texture(tex, v_TexCoord)*v_Color;               // Modulate the texture by multiplying by color value
   outColor = outColor*texModulator + (1.0-texModulator)*v_Color;   // Modulate the final value * texModulator, in case texture sampler was 0

Vertex Shader
Please note that the vertex shader is a little bit "ugly" :) I send the translation as a uniform vector instead of a matrix, and the vertex position gets adjusted that way. There is no reason to avoid matrices, but I just couldn't bring myself to send 16 floats down the pipe where I could get away with 2. Your milage may vary.

Code: Select all
#version 150

// Transformation matrices/vectors
uniform mat4 projection;
uniform vec2 translation;

// Incoming attributes
in vec4 in_Position;
in vec4 in_Color;
in vec2 in_TexCoord;

// Varying vertex attributes
out vec2 v_TexCoord;
out vec4 v_Color;

void main()
   // Set texture coordinates
   v_TexCoord = in_TexCoord;
   v_Color = in_Color;

   vec4 position = in_Position;
   position.x += translation.x;
   position.y += translation.y;

   // Set final vertex position
   gl_Position = projection * position;

Finally, in the render wrapper inside the RenderCompiledGeometry method be sure to do something like this:
Note: obviously I have my shader wrapper there, but the underlying methods should be very obvious.

Code: Select all
   // Send data to the shader
   m_shader->uniform2f("translation", translation.x, translation.y);
   if(geom->texture != NULL)
      // If texture exists, set it and modulate it to overtake color completely
      m_shader->uniformTexture("tex", 0, geom->texture->getID());
      m_shader->uniform1f("texModulator", 1.0f);
   // If texture is NULL, modulate it to 0%, and color takes over completely
      m_shader->uniform1f("texModulator", 0.0f);

I know this information may be too late for you, but if anyone else is interested in a solution - I thought I'd make it available here. Let me know if you find it useful!

- Supertim
Rocket Engineer
Rocket Engineer
Posts: 16
Joined: Sat Jun 14, 2014 5:31 am

Return to Engine Integration