Commit 8433dead authored by Sylvain Thery's avatar Sylvain Thery
Browse files

add opacity option to String3D & Drawer

parent 00a97596
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
PRECISON; PRECISON;
VARYING_FRAG vec3 color; VARYING_FRAG vec3 color;
uniform float alpha;
FRAG_OUT_DEF; FRAG_OUT_DEF;
void main() void main()
{ {
...@@ -9,5 +10,5 @@ void main() ...@@ -9,5 +10,5 @@ void main()
if (dot(color,color) == 0.0) if (dot(color,color) == 0.0)
discard; discard;
#endif #endif
gl_FragColor=vec4(color,0.0); gl_FragColor=vec4(color,alpha);
} }
...@@ -45,14 +45,28 @@ protected: ...@@ -45,14 +45,28 @@ protected:
VBO* m_vboPos; VBO* m_vboPos;
VBO* m_vboCol; VBO* m_vboCol;
CGoGNGLuint m_unif_alpha;
float m_opacity;
void restoreUniformsAttribs(); void restoreUniformsAttribs();
public: public:
ShaderColorPerVertex(bool black_is_transparent = false); ShaderColorPerVertex(bool black_is_transparent = false);
unsigned int setAttributePosition(VBO* vbo); /**
* set the VBO of position (vec3)
*/
unsigned int setAttributePosition(VBO* vbo);
/**
* set the VBO of color (vec3)
*/
unsigned int setAttributeColor(VBO* vbo); unsigned int setAttributeColor(VBO* vbo);
/**
* set opacity (0=transparent / 1=opaque)
*/
void setOpacity(float op);
}; };
} // namespace Utils } // namespace Utils
......
...@@ -131,6 +131,11 @@ public: ...@@ -131,6 +131,11 @@ public:
*/ */
void callList(); void callList();
/**
* use as a glCallList
*/
void callList(float opacity) { m_shader->setOpacity(opacity); callList(); }
/** /**
* use as glLineWidth * use as glLineWidth
*/ */
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
VARYING_FRAG vec2 tex_coord; VARYING_FRAG vec2 tex_coord;
uniform sampler2D FontTexture; uniform sampler2D FontTexture;
uniform vec3 color; uniform vec4 color;
FRAG_OUT_DEF; FRAG_OUT_DEF;
void main (void) void main (void)
{ {
......
...@@ -69,6 +69,8 @@ protected: ...@@ -69,6 +69,8 @@ protected:
float m_scale; float m_scale;
Geom::Vec4f m_color;
unsigned int sendOneStringToVBO(const std::string& str, float **buffer); unsigned int sendOneStringToVBO(const std::string& str, float **buffer);
CGoGNGLuint m_idTexture; CGoGNGLuint m_idTexture;
...@@ -126,19 +128,33 @@ public: ...@@ -126,19 +128,33 @@ public:
* Draw all text stored with their position * Draw all text stored with their position
* @param color the color of text * @param color the color of text
*/ */
void drawAll(const Geom::Vec3f& color); void drawAll(const Geom::Vec4f& color);
void drawAll(const Geom::Vec3f& color){ drawAll( Geom::Vec4f(color[0],color[1],color[2],1.0f)); }
/** /**
* call once before several draw(id,pos) * call once before several draw(id,pos)
* @param color the color of text * @param color the color of text
*/ */
void predraw(const Geom::Vec3f& color); void predraw(const Geom::Vec4f& color);
void predraw(const Geom::Vec3f& color){ predraw( Geom::Vec4f(color[0],color[1],color[2],1.0f)); }
/** /**
* call just before a draw * call just before a draw
* @param color the color of text * @param color the color of text
*/ */
void changeColor(const Geom::Vec3f& color); void changeColor(const Geom::Vec4f& color);
void changeColor(const Geom::Vec3f& color){ changeColor( Geom::Vec4f(color[0],color[1],color[2],1.0f)); }
/**
* change opacity but keep color (0 is totally transparent 1 is totally opaque)
* @param opacity
*/
void changeOpacity(float op);
/** /**
* call once after several draw(id,pos) * call once after several draw(id,pos)
......
...@@ -73,6 +73,11 @@ ShaderColorPerVertex::ShaderColorPerVertex(bool black_is_transparent) ...@@ -73,6 +73,11 @@ ShaderColorPerVertex::ShaderColorPerVertex(bool black_is_transparent)
glxfrag.append(fragmentShaderText); glxfrag.append(fragmentShaderText);
loadShadersFromMemory(glxvert.c_str(), glxfrag.c_str()); loadShadersFromMemory(glxvert.c_str(), glxfrag.c_str());
*m_unif_alpha = glGetUniformLocation(this->program_handler(), "alpha");
glUniform1f (*m_unif_alpha, 1.0f);
m_opacity = 1.0f;
} }
unsigned int ShaderColorPerVertex::setAttributePosition(VBO* vbo) unsigned int ShaderColorPerVertex::setAttributePosition(VBO* vbo)
...@@ -92,7 +97,16 @@ void ShaderColorPerVertex::restoreUniformsAttribs() ...@@ -92,7 +97,16 @@ void ShaderColorPerVertex::restoreUniformsAttribs()
bind(); bind();
bindVA_VBO("VertexPosition", m_vboPos); bindVA_VBO("VertexPosition", m_vboPos);
bindVA_VBO("VertexColor", m_vboCol); bindVA_VBO("VertexColor", m_vboCol);
glUniform1f (*m_unif_alpha, m_opacity);
unbind();
}
void ShaderColorPerVertex::setOpacity(float op)
{
bind();
glUniform1f (*m_unif_alpha, op);
unbind(); unbind();
m_opacity = op;
} }
} // namespace Utils } // namespace Utils
......
...@@ -166,7 +166,7 @@ void Drawer::updatePositions(unsigned int first, unsigned int nb, const float* P ...@@ -166,7 +166,7 @@ void Drawer::updatePositions(unsigned int first, unsigned int nb, const float* P
} }
void Drawer::callList() void Drawer::callList(float opacity)
{ {
if (m_begins.empty()) if (m_begins.empty())
return; return;
......
...@@ -36,7 +36,7 @@ namespace Utils ...@@ -36,7 +36,7 @@ namespace Utils
std::string Strings3D::fragmentShaderText2 = std::string Strings3D::fragmentShaderText2 =
" gl_FragColor = vec4(color,0.0)*lum;\n" " gl_FragColor = color*lum;\n"
"}"; "}";
...@@ -221,11 +221,12 @@ void Strings3D::sendToVBO() ...@@ -221,11 +221,12 @@ void Strings3D::sendToVBO()
glUnmapBuffer(GL_ARRAY_BUFFER); glUnmapBuffer(GL_ARRAY_BUFFER);
} }
void Strings3D::predraw(const Geom::Vec3f& color) void Strings3D::predraw(const Geom::Vec4f& color)
{ {
m_color = color;
bind(); bind();
glUniform1i(*m_uniform_texture, 0); glUniform1i(*m_uniform_texture, 0);
glUniform3fv(*m_uniform_color, 1, color.data()); glUniform4fv(*m_uniform_color, 1, color.data());
glActiveTextureARB(GL_TEXTURE0_ARB); glActiveTextureARB(GL_TEXTURE0_ARB);
glBindTexture(GL_TEXTURE_2D, *m_idTexture); glBindTexture(GL_TEXTURE_2D, *m_idTexture);
...@@ -236,10 +237,18 @@ void Strings3D::predraw(const Geom::Vec3f& color) ...@@ -236,10 +237,18 @@ void Strings3D::predraw(const Geom::Vec3f& color)
enableVertexAttribs(); enableVertexAttribs();
} }
void Strings3D::changeColor(const Geom::Vec3f& color) void Strings3D::changeColor(const Geom::Vec4f& color)
{ {
m_color = color;
bind(); bind();
glUniform3fv(*m_uniform_color, 1, color.data()); glUniform4fv(*m_uniform_color, 1, color.data());
}
void Strings3D::changeOpacity(float op)
{
m_color[3] = op;
bind();
glUniform4fv(*m_uniform_color, 1, m_color.data());
} }
void Strings3D::postdraw() void Strings3D::postdraw()
...@@ -254,8 +263,9 @@ void Strings3D::draw(unsigned int idSt, const Geom::Vec3f& pos) ...@@ -254,8 +263,9 @@ void Strings3D::draw(unsigned int idSt, const Geom::Vec3f& pos)
glDrawArrays(GL_QUADS, m_strpos[idSt].first , m_strpos[idSt].second ); glDrawArrays(GL_QUADS, m_strpos[idSt].first , m_strpos[idSt].second );
} }
void Strings3D::drawAll(const Geom::Vec3f& color) void Strings3D::drawAll(const Geom::Vec4f& color)
{ {
m_color = color;
unsigned int nb = m_strpos.size(); unsigned int nb = m_strpos.size();
// nothing to do if no string ! // nothing to do if no string !
if (nb == 0) if (nb == 0)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment