Skip to content

Commit d68f415

Browse files
committed
Thick Lines and Varying Point Sizes for Programmable Renderer
1 parent dee2eb7 commit d68f415

14 files changed

+1248
-82
lines changed

libs/openFrameworks/app/ofAppNoWindow.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ class ofNoopRenderer: public ofBaseRenderer{
147147
void setFillMode(ofFillFlag fill){}
148148
ofFillFlag getFillMode(){return OF_FILLED;}
149149
void setLineWidth(float lineWidth){}
150+
void setPointSize(float pointSize) {}
150151
void setBlendMode(ofBlendMode blendMode){}
151152
void setLineSmoothing(bool smooth){}
152153
void setCircleResolution(int res){};

libs/openFrameworks/gl/ofGLProgrammableRenderer.cpp

+1,116-71
Large diffs are not rendered by default.

libs/openFrameworks/gl/ofGLProgrammableRenderer.h

+73-9
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,21 @@ class ofGLProgrammableRenderer: public ofBaseGLRenderer{
124124
void setRectMode(ofRectMode mode);
125125
ofRectMode getRectMode();
126126
void setLineWidth(float lineWidth);
127+
void setPointSize(float pointSize);
127128
void setDepthTest(bool depthTest);
128129
void setLineSmoothing(bool smooth);
129130
void setBlendMode(ofBlendMode blendMode);
130131
void enablePointSprites();
131132
void disablePointSprites();
132133
void enableAntiAliasing();
133134
void disableAntiAliasing();
135+
136+
// lines
137+
void enableLineSizeAttenuation();
138+
void disableLineSizeAttenuation();
139+
void enableLinesShaders();
140+
void disableLinesShaders();
141+
bool areLinesShadersEnabled() const;
134142

135143
// color options
136144
void setColor(float r, float g, float b); // 0-1
@@ -250,6 +258,43 @@ class ofGLProgrammableRenderer: public ofBaseGLRenderer{
250258
mutable ofMesh rectMesh;
251259
mutable ofMesh lineMesh;
252260
mutable ofVbo meshVbo;
261+
mutable ofMesh polylineMesh;
262+
263+
// when adding more draw modes, POINTS, LINES, etc.
264+
// store in a structure so we don't have to make a lot of variables
265+
// this structure if based on the one from ofMaterial
266+
class ShaderCollection {
267+
public:
268+
void bindAttribute( GLuint location, const std::string & name );
269+
void bindDefaults();
270+
void linkPrograms();
271+
void setupAllVertexShaders(const std::string &aShaderSrc);
272+
273+
ofShader texRectColor;
274+
ofShader texRectNoColor;
275+
ofShader tex2DColor;
276+
ofShader tex2DNoColor;
277+
ofShader noTexColor;
278+
ofShader noTexNoColor;
279+
};
280+
281+
// useful for lines //
282+
class LinesBundle {
283+
public:
284+
std::vector<glm::vec4> lineMeshNextVerts;
285+
std::vector<glm::vec4> lineMeshPrevVerts;
286+
ofVboMesh vboMesh;
287+
int vertAttribPrev = 4;
288+
int vertAttribNext = 5;
289+
};
290+
291+
struct TextureUniform {
292+
ofTextureData texData;
293+
// not going to store a texture since we don't want to retain the texture here
294+
// ofTexture texture;
295+
int textureLocation;
296+
std::string uniformName;
297+
};
253298

254299
void uploadCurrentMatrix();
255300

@@ -258,12 +303,20 @@ class ofGLProgrammableRenderer: public ofBaseGLRenderer{
258303
void endSmoothing();
259304

260305
void beginDefaultShader();
306+
std::shared_ptr<ShaderCollection>& getShaderCollectionForMode(GLuint drawMode);
261307
void uploadMatrices();
262308
void setDefaultUniforms();
263309

264-
void setAttributes(bool vertices, bool color, bool tex, bool normals);
310+
// adding a drawMode variable that will switch shaders based on GL_TRIANGLES, GL_POINTS or GL_LINES
311+
void setAttributes(bool vertices, bool color, bool tex, bool normals, GLuint drawMode);
312+
// void setAttributes(bool vertices, bool color, bool tex, bool normals);
265313
void setAlphaBitmapText(bool bitmapText);
266-
314+
315+
316+
// LINES
317+
void configureMeshToMatchWithNewVertsAndIndices(const ofMesh& aSrcMesh, ofVboMesh& aDstMesh, std::size_t aTargetNumVertices, std::size_t aTargetNumIndices);
318+
void configureLinesBundleFromMesh(LinesBundle& aLinesBundle, GLuint drawMode, const ofMesh& amesh);
319+
267320

268321
ofMatrixStack matrixStack;
269322

@@ -273,6 +326,7 @@ class ofGLProgrammableRenderer: public ofBaseGLRenderer{
273326
const ofShader * currentShader;
274327

275328
bool verticesEnabled, colorsEnabled, texCoordsEnabled, normalsEnabled, bitmapStringEnabled;
329+
bool pointSpritesEnabled;
276330
bool usingCustomShader, settingDefaultShader, usingVideoShader;
277331
int currentTextureTarget;
278332

@@ -293,13 +347,23 @@ class ofGLProgrammableRenderer: public ofBaseGLRenderer{
293347
ofBitmapFont bitmapFont;
294348
ofPath path;
295349
const ofAppBaseWindow * window;
296-
297-
ofShader defaultTexRectColor;
298-
ofShader defaultTexRectNoColor;
299-
ofShader defaultTex2DColor;
300-
ofShader defaultTex2DNoColor;
301-
ofShader defaultNoTexColor;
302-
ofShader defaultNoTexNoColor;
350+
351+
mutable GLuint mDrawMode = GL_TRIANGLES;
352+
std::unordered_map<GLuint, LinesBundle> mLinesBundleMap;
353+
mutable bool mBRenderingLines = false;
354+
mutable bool mBLineSizeAttenutation = false; // screen space
355+
mutable bool mBEnableLinesShaders = true;
356+
357+
// the index GL_TRIANGLES store everything that is not GL_POINTS or GL_LINES, GL_LINE_STRIP
358+
std::unordered_map<GLuint, std::shared_ptr<ShaderCollection> > mDefaultShadersMap;
359+
std::vector<TextureUniform> mUniformsTex;
360+
361+
// ofShader defaultTexRectColor;
362+
// ofShader defaultTexRectNoColor;
363+
// ofShader defaultTex2DColor;
364+
// ofShader defaultTex2DNoColor;
365+
// ofShader defaultNoTexColor;
366+
// ofShader defaultNoTexNoColor;
303367
ofShader defaultUniqueShader;
304368
#ifdef TARGET_ANDROID
305369
ofShader defaultOESTexColor;

libs/openFrameworks/gl/ofGLRenderer.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -1202,6 +1202,12 @@ void ofGLRenderer::setLineWidth(float lineWidth) {
12021202
glLineWidth(lineWidth);
12031203
}
12041204

1205+
//----------------------------------------------------------
1206+
void ofGLRenderer::setPointSize(float pointSize) {
1207+
currentStyle.pointSize = pointSize;
1208+
glPointSize(pointSize);
1209+
}
1210+
12051211
//----------------------------------------------------------
12061212
void ofGLRenderer::setDepthTest(bool depthTest) {
12071213
if (depthTest) {

libs/openFrameworks/gl/ofGLRenderer.h

+1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ class ofGLRenderer: public ofBaseGLRenderer{
111111
void setRectMode(ofRectMode mode);
112112
ofRectMode getRectMode();
113113
void setLineWidth(float lineWidth);
114+
void setPointSize(float pointSize);
114115
void setDepthTest(bool depthTest);
115116
void setLineSmoothing(bool smooth);
116117
void setBlendMode(ofBlendMode blendMode);

libs/openFrameworks/gl/ofShader.cpp

+10-1
Original file line numberDiff line numberDiff line change
@@ -954,10 +954,19 @@ void ofShader::setUniformTexture(const string & name, int textureTarget, GLint t
954954
}
955955
}
956956

957+
957958
//--------------------------------------------------------------
958959
void ofShader::setUniformTexture(const string & name, const ofTexture & tex, int textureLocation) const {
960+
if (bLoaded) {
961+
ofTextureData texData = tex.getTextureData();
962+
setUniformTexture( name, texData, textureLocation);
963+
}
964+
}
965+
966+
//--------------------------------------------------------------
967+
void ofShader::setUniformTexture(const string & name, const ofTextureData & texData, int textureLocation) const{
959968
if (bLoaded) {
960-
ofTextureData texData = tex.getTextureData();
969+
//ofTextureData texData = tex.getTextureData();
961970
glActiveTexture(GL_TEXTURE0 + textureLocation);
962971
if (!ofIsGLProgrammableRenderer()) {
963972
glEnable(texData.textureTarget);

libs/openFrameworks/gl/ofShader.h

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <unordered_map>
1818

1919
class ofTexture;
20+
class ofTextureData;
2021
class ofMatrix3x3;
2122
class ofParameterGroup;
2223
class ofBufferObject;
@@ -150,6 +151,7 @@ class ofShader {
150151
void setUniformTexture(const std::string & name, const ofBaseHasTexture& img, int textureLocation) const;
151152
void setUniformTexture(const std::string & name, const ofTexture& img, int textureLocation) const;
152153
void setUniformTexture(const std::string & name, int textureTarget, GLint textureID, int textureLocation) const;
154+
void setUniformTexture(const std::string & name, const ofTextureData& texData, int textureLocation) const;
153155

154156
// set a single uniform value
155157
void setUniform1i(const std::string & name, int v1) const;

libs/openFrameworks/graphics/ofCairoRenderer.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,12 @@ void ofCairoRenderer::setLineWidth(float lineWidth) {
655655
cairo_set_line_width(cr, lineWidth);
656656
}
657657

658+
//--------------------------------------------
659+
void ofCairoRenderer::setPointSize(float pointSize) {
660+
currentStyle.pointSize = pointSize;
661+
// no point size for cairo
662+
}
663+
658664
//----------------------------------------------------------
659665
void ofCairoRenderer::setDepthTest(bool depthTest) {
660666
// cairo does not do any depth testing

libs/openFrameworks/graphics/ofCairoRenderer.h

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ class ofCairoRenderer: public ofBaseRenderer{
8383
void setFillMode(ofFillFlag fill);
8484
ofFillFlag getFillMode();
8585
void setLineWidth(float lineWidth);
86+
void setPointSize(float pointSize);
8687
void setDepthTest(bool depthTest);
8788
void setBlendMode(ofBlendMode blendMode);
8889
void setLineSmoothing(bool smooth);

libs/openFrameworks/graphics/ofGraphics.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,11 @@ void ofSetLineWidth(float lineWidth) {
528528
ofGetCurrentRenderer()->setLineWidth(lineWidth);
529529
}
530530

531+
//----------------------------------------------------------
532+
void ofSetPointSize(float pointSize) {
533+
ofGetCurrentRenderer()->setPointSize(pointSize);
534+
}
535+
531536
//----------------------------------------------------------
532537
void ofSetDepthTest(bool depthTest) {
533538
ofGetCurrentRenderer()->setDepthTest(depthTest);

libs/openFrameworks/graphics/ofGraphics.h

+11
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,17 @@ void ofSetCurveResolution(int res);
774774
/// ~~~~
775775
void ofSetLineWidth(float lineWidth);
776776

777+
/// \brief Sets the size of the points after mesh.drawVertices() called after.
778+
/// ~~~~{.cpp}
779+
/// void ofApp::draw(){
780+
/// ofSetPointSize(1); // set point size to 1
781+
/// mesh.drawVertices(); // draw small points
782+
/// ofSetPointSize(10); // set point size to 10
783+
/// mesh.drawVertices(); // draw fat points
784+
/// }
785+
/// ~~~~
786+
void ofSetPointSize(float pointSize);
787+
777788
/// \brief Set depth testing on or off to either sort by z-depth (`true`)
778789
/// or draw order (`false`).
779790
void ofSetDepthTest(bool depthTest);

libs/openFrameworks/graphics/ofGraphicsBaseTypes.h

+9
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class ofStyle {
5757
sphereResolution = 20;
5858
curveResolution = 20;
5959
lineWidth = 1.0;
60+
pointSize = 1.0;
6061
polyMode = OF_POLY_WINDING_ODD;
6162
rectMode = OF_RECTMODE_CORNER;
6263
#ifdef TARGET_OPENGLES
@@ -127,6 +128,9 @@ class ofStyle {
127128
/// \warning This is not currently implemented in modern OF renderers.
128129
float lineWidth;
129130

131+
/// \brief The size of rendered points.
132+
float pointSize;
133+
130134
//bool depthTest; removed since it'll break old projects setting depth test through glEnable
131135
};
132136

@@ -834,6 +838,11 @@ class ofBaseRenderer {
834838
///
835839
/// \param lineWidth The line width to request this renderer to use.
836840
virtual void setLineWidth(float lineWidth) = 0;
841+
842+
/// \brief Set the point size this renderer should use when drawing points.
843+
///
844+
/// \param pointSize The points size to request this renderer to use.
845+
virtual void setPointSize(float pointSize) = 0;
837846

838847
/// \brief Enable/disable depth testing with this renderer.
839848
///

libs/openFrameworks/graphics/ofRendererCollection.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,12 @@ void ofRendererCollection::setLineWidth(float lineWidth){
544544
}
545545
}
546546

547+
void ofRendererCollection::setPointSize(float pointSize){
548+
for(auto renderer: renderers){
549+
renderer->setPointSize(pointSize);
550+
}
551+
}
552+
547553
void ofRendererCollection::setDepthTest(bool depthTest) {
548554
for(auto renderer: renderers){
549555
renderer->setDepthTest(depthTest);

libs/openFrameworks/graphics/ofRendererCollection.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ class ofRendererCollection: public ofBaseRenderer{
180180
ofFillFlag getFillMode();
181181

182182
void setLineWidth(float lineWidth);
183-
183+
void setPointSize(float pointSize);
184184
void setDepthTest(bool depthTest);
185185

186186
void setBlendMode(ofBlendMode blendMode);

0 commit comments

Comments
 (0)