Commit 9f9550db authored by Thery Sylvain's avatar Thery Sylvain

add possibility to have color per vertex in pointSprites

parent 9d6fd632
// PointSprite::fragmentShaderText
uniform sampler2D SpriteTexture;
uniform float size;
uniform vec3 color;
#ifdef WITH_COLOR_PER_VERTEX
VARYING_FRAG vec3 colorsprite;
#else
uniform vec3 colorsprite;
#endif
VARYING_FRAG vec2 texCoord;
VARYING_FRAG vec2 positionFragIn;
VARYING_FRAG vec4 mvpFragIn;
......@@ -14,5 +22,5 @@ void main(void)
float z = size * sqrt(1.0-dot(v,v));
vec2 zfrag = positionFragIn + vec2(z,0.0);
gl_FragDepth = 0.5 + 0.5 * dot(zfrag, mvpFragIn.xy) / dot(zfrag, mvpFragIn.zw);
gl_FragColor = vec4(color,0.0)*lum;
}
\ No newline at end of file
gl_FragColor = vec4(colorsprite,0.0)*lum;
}
......@@ -6,6 +6,12 @@ uniform mat4 ProjectionMatrix;
VARYING_OUT vec2 texCoord;
VARYING_OUT vec2 positionFragIn;
VARYING_OUT vec4 mvpFragIn;
#ifdef WITH_COLOR_PER_VERTEX
VARYING_IN vec3 color[1];
VARYING_OUT vec3 colorsprite;
#endif
void main()
{
vec4 posCenter = ModelViewMatrix * POSITION_IN(0);
......@@ -16,6 +22,10 @@ void main()
mvpFragIn.z = ProjectionMatrix[2][3];
mvpFragIn.w = ProjectionMatrix[3][3];
texCoord = vec2(0.0,1.0);
#ifdef WITH_COLOR_PER_VERTEX
colorsprite = color[0];
#endif
gl_Position = ProjectionMatrix * pos;
EmitVertex();
pos = posCenter + vec4(-size, -size, 0.0, 0.0);
......
......@@ -62,8 +62,9 @@ protected:
public:
/**
* init shaders, texture and variables
* @param withColorPerVertex if true use colorVBO else use predraw(color)
*/
PointSprite(float radius=1.0f);
PointSprite(bool withColorPerVertex=false, float radius=1.0f);
/**
* clean shaders, texture and variables
......@@ -72,9 +73,15 @@ public:
/**
* call once before sending points to gpu
* @param color set global color of sprite
*/
void predraw(const Geom::Vec3f& color);
/**
* call once before sending points to gpu
*/
void predraw();
/**
* call once after sending points to gpu
*/
......@@ -91,6 +98,11 @@ public:
*/
unsigned int setAttributePosition(VBO* vbo);
/**
* set position attribute
*/
unsigned int setAttributeColor(VBO* vbo);
};
}
......
// PointSprite::vertexShaderText
ATTRIBUTE vec3 VertexPosition;
#ifdef WITH_COLOR_PER_VERTEX
ATTRIBUTE vec3 VertexColor;
VARYING_VERT vec3 color;
#endif
void main ()
{
gl_Position = vec4(VertexPosition,1.0);
#ifdef WITH_COLOR_PER_VERTEX
color = VertexColor; //VertexColor;
#endif
}
......@@ -40,78 +40,23 @@ GLuint PointSprite::m_uniform_texture = 0;
unsigned char* PointSprite::m_ptrSphere = NULL;
//std::string PointSprite::vertexShaderText =
//"ATTRIBUTE vec3 VertexPosition;\n"
//"void main ()\n"
//"{\n"
//" gl_Position = vec4(VertexPosition,1.0);\n"
//"}";
//std::string PointSprite::geometryShaderText =
//"uniform float size;\n"
//"uniform mat4 ModelViewMatrix;\n"
//"uniform mat4 ProjectionMatrix;\n"
//"VARYING_OUT vec2 texCoord;\n"
//"VARYING_OUT vec2 positionFragIn;\n"
//"VARYING_OUT vec4 mvpFragIn;\n"
//"void main()\n"
//"{\n"
//" vec4 posCenter = ModelViewMatrix * POSITION_IN(0);\n"
//" vec4 pos = posCenter + vec4(-size, size, 0.0, 0.0);\n"
//" positionFragIn = posCenter.zw;\n"
//" mvpFragIn.x = ProjectionMatrix[2][2];\n"
//" mvpFragIn.y = ProjectionMatrix[3][2];\n"
//" mvpFragIn.z = ProjectionMatrix[2][3];\n"
//" mvpFragIn.w = ProjectionMatrix[3][3];\n"
//" texCoord = vec2(0.0,1.0);\n"
//" gl_Position = ProjectionMatrix * pos;\n"
//" EmitVertex();\n"
//" pos = posCenter + vec4(-size, -size, 0.0, 0.0);\n"
//" texCoord = vec2(0.0,0.0);\n"
//" gl_Position = ProjectionMatrix * pos;\n"
//" EmitVertex();\n"
//" pos = posCenter + vec4( size, size, 0.0, 0.0);\n"
//" texCoord = vec2(1.0,1.0);\n"
//" gl_Position = ProjectionMatrix * pos;\n"
//" EmitVertex();\n"
//" pos = posCenter + vec4( size,-size, 0.0, 0.0);\n"
//" texCoord = vec2(1.0,0.0);\n"
//" gl_Position = ProjectionMatrix * pos;\n"
//" EmitVertex();\n"
//" EndPrimitive();\n"
//"}";
//std::string PointSprite::fragmentShaderText =
//"uniform sampler2D SpriteTexture;\n"
//"uniform float size;\n"
//"uniform vec3 color;\n"
//"VARYING_FRAG vec2 texCoord;\n"
//"VARYING_FRAG vec2 positionFragIn;\n"
//"VARYING_FRAG vec4 mvpFragIn;\n"
//"void main(void)\n"
//"{\n"
//" float lum = texture2D(SpriteTexture, texCoord).s;\n"
//" if (lum==0.0)\n"
//" discard;\n"
//" vec2 v = texCoord-vec2(0.5,0.5);\n"
//" float z = size * sqrt(1.0-dot(v,v));\n"
//" vec2 zfrag = positionFragIn + vec2(z,0.0);\n"
//" gl_FragDepth = 0.5 + 0.5 * dot(zfrag, mvpFragIn.xy) / dot(zfrag, mvpFragIn.zw);\n"
//" gl_FragColor = vec4(color,0.0)*lum;\n"
//"}";
PointSprite::PointSprite(float radius)
PointSprite::PointSprite(bool withColorPervertex, float radius)
{
std::string defineColor("#define WITH_COLOR_PER_VERTEX 1\n");
std::string glxvert(*GLSLShader::DEFINES_GL);
if (withColorPervertex)
glxvert.append(defineColor);
glxvert.append(vertexShaderText);
std::string glxgeom = GLSLShader::defines_Geom("points","triangle_strip",4);
if (withColorPervertex)
glxgeom.append(defineColor);
glxgeom.append(geometryShaderText);
std::string glxfrag(*GLSLShader::DEFINES_GL);
if (withColorPervertex)
glxfrag.append(defineColor);
glxfrag.append(fragmentShaderText);
loadShadersFromMemory(glxvert.c_str(), glxfrag.c_str(), glxgeom.c_str(), GL_POINTS, GL_TRIANGLE_STRIP,4);
......@@ -149,6 +94,11 @@ unsigned int PointSprite::setAttributePosition(VBO* vbo)
return bindVA_VBO("VertexPosition", vbo);
}
unsigned int PointSprite::setAttributeColor(VBO* vbo)
{
return bindVA_VBO("VertexColor", vbo);
}
void PointSprite::predraw(const Geom::Vec3f& color)
{
bind();
......@@ -159,6 +109,15 @@ void PointSprite::predraw(const Geom::Vec3f& color)
glEnable(GL_TEXTURE_2D);
}
void PointSprite::predraw()
{
bind();
glUniform1i(m_uniform_texture, 0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_idTexture);
glEnable(GL_TEXTURE_2D);
}
void PointSprite::postdraw()
{
glDisable(GL_TEXTURE_2D);
......
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