Skip to content

Commit a99c094

Browse files
committed
finished implementing attrib API
1 parent 81b728d commit a99c094

4 files changed

Lines changed: 120 additions & 21 deletions

File tree

core/src/processing/core/PGraphics.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,6 +1239,21 @@ public void normal(float nx, float ny, float nz) {
12391239
}
12401240

12411241

1242+
public void attribPosition(String name, float x, float y, float z) {
1243+
showMissingWarning("attrib");
1244+
}
1245+
1246+
1247+
public void attribNormal(String name, float nx, float ny, float nz) {
1248+
showMissingWarning("attrib");
1249+
}
1250+
1251+
1252+
public void attribColor(String name, int color) {
1253+
showMissingWarning("attrib");
1254+
}
1255+
1256+
12421257
public void attrib(String name, float... values) {
12431258
showMissingWarning("attrib");
12441259
}

core/src/processing/core/PShape.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,17 @@ public void normal(float nx, float ny, float nz) {
716716
}
717717

718718

719+
public void attribPosition(String name, float x, float y, float z) {
720+
}
721+
722+
public void attribNormal(String name, float nx, float ny, float nz) {
723+
}
724+
725+
726+
public void attribColor(String name, int color) {
727+
}
728+
729+
719730
public void attrib(String name, float... values) {
720731
}
721732

core/src/processing/opengl/PGraphicsOpenGL.java

Lines changed: 54 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2625,39 +2625,73 @@ public void vertex(float x, float y, float z, float u, float v) {
26252625
}
26262626

26272627

2628+
@Override
2629+
public void attribPosition(String name, float x, float y, float z) {
2630+
VertexAttribute attrib = attribImpl(name, VertexAttribute.POSITION,
2631+
PGL.FLOAT, 3);
2632+
if (attrib != null) attrib.set(x, y, z);
2633+
}
2634+
2635+
2636+
@Override
2637+
public void attribNormal(String name, float nx, float ny, float nz) {
2638+
VertexAttribute attrib = attribImpl(name, VertexAttribute.NORMAL,
2639+
PGL.FLOAT, 3);
2640+
if (attrib != null) attrib.set(nx, ny, nz);
2641+
}
2642+
2643+
2644+
@Override
2645+
public void attribColor(String name, int color) {
2646+
VertexAttribute attrib = attribImpl(name, VertexAttribute.COLOR, PGL.INT, 1);
2647+
if (attrib != null) attrib.set(new int[] {color});
2648+
}
2649+
2650+
26282651
@Override
26292652
public void attrib(String name, float... values) {
2630-
VertexAttribute attrib = attribImpl(name, PGL.FLOAT, values.length);
2653+
VertexAttribute attrib = attribImpl(name, VertexAttribute.OTHER,
2654+
PGL.FLOAT, values.length);
26312655
if (attrib != null) attrib.set(values);
26322656
}
26332657

26342658

26352659
@Override
26362660
public void attrib(String name, int... values) {
2637-
VertexAttribute attrib = attribImpl(name, PGL.INT, values.length);
2661+
VertexAttribute attrib = attribImpl(name, VertexAttribute.OTHER,
2662+
PGL.INT, values.length);
26382663
if (attrib != null) attrib.set(values);
26392664
}
26402665

26412666

26422667
@Override
26432668
public void attrib(String name, boolean... values) {
2644-
VertexAttribute attrib = attribImpl(name, PGL.BOOL, values.length);
2669+
VertexAttribute attrib = attribImpl(name, VertexAttribute.OTHER,
2670+
PGL.BOOL, values.length);
26452671
if (attrib != null) attrib.set(values);
26462672
}
26472673

26482674

2649-
protected VertexAttribute attribImpl(String name, int type, int size) {
2675+
protected VertexAttribute attribImpl(String name, int kind, int type, int size) {
26502676
if (4 < size) {
26512677
PGraphics.showWarning("Vertex attributes cannot have more than 4 values");
26522678
return null;
26532679
}
26542680
VertexAttribute attrib = polyAttribs.get(name);
26552681
if (attrib == null) {
2656-
attrib = new VertexAttribute(this, name, type, size);
2682+
attrib = new VertexAttribute(this, name, kind, type, size);
26572683
polyAttribs.put(name, attrib);
26582684
inGeo.initAttrib(attrib);
26592685
tessGeo.initAttrib(attrib);
26602686
}
2687+
if (attrib.kind != kind) {
2688+
PGraphics.showWarning("The attribute kind cannot be changed after creation");
2689+
return null;
2690+
}
2691+
if (attrib.type != type) {
2692+
PGraphics.showWarning("The attribute type cannot be changed after creation");
2693+
return null;
2694+
}
26612695
if (attrib.size != size) {
26622696
PGraphics.showWarning("New value for vertex attribute has wrong number of values");
26632697
return null;
@@ -7398,23 +7432,17 @@ static protected class VertexAttribute {
73987432
int lastModified;
73997433
boolean active;
74007434

7401-
VertexAttribute(PGraphicsOpenGL pg, String name, int type, int size) {
7435+
VertexAttribute(PGraphicsOpenGL pg, String name, int kind, int type, int size) {
74027436
this.pg = pg;
74037437
this.name = name;
7438+
this.kind = kind;
74047439
this.type = type;
74057440
this.size = size;
74067441

7407-
tessSize = size;
7408-
7409-
if (name.indexOf("pos") == 0 && type == PGL.FLOAT && size == 3) {
7410-
kind = POSITION;
7411-
tessSize = 4;
7412-
} else if (name.indexOf("norm") == 0 && type == PGL.FLOAT && size == 3) {
7413-
kind = NORMAL;
7414-
} else if (name.indexOf("color") == 0 && type == PGL.INT && size == 1) {
7415-
kind = COLOR;
7442+
if (kind == POSITION) {
7443+
tessSize = 4; // for w
74167444
} else {
7417-
kind = OTHER;
7445+
tessSize = size;
74187446
}
74197447

74207448
if (type == PGL.FLOAT) {
@@ -7520,6 +7548,16 @@ int sizeInBytes(int length) {
75207548
return length * tessSize * elementSize;
75217549
}
75227550

7551+
void set(float x, float y, float z) {
7552+
fvalues[0] = x;
7553+
fvalues[1] = y;
7554+
fvalues[2] = z;
7555+
}
7556+
7557+
void set(int c) {
7558+
ivalues[0] = c;
7559+
}
7560+
75237561
void set(float[] values) {
75247562
PApplet.arrayCopy(values, 0, fvalues, 0, size);
75257563
}

core/src/processing/opengl/PShapeOpenGL.java

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,38 +1170,73 @@ public void normal(float nx, float ny, float nz) {
11701170
}
11711171
}
11721172

1173+
1174+
@Override
1175+
public void attribPosition(String name, float x, float y, float z) {
1176+
VertexAttribute attrib = attribImpl(name, VertexAttribute.POSITION,
1177+
PGL.FLOAT, 3);
1178+
if (attrib != null) attrib.set(x, y, z);
1179+
}
1180+
1181+
1182+
@Override
1183+
public void attribNormal(String name, float nx, float ny, float nz) {
1184+
VertexAttribute attrib = attribImpl(name, VertexAttribute.NORMAL,
1185+
PGL.FLOAT, 3);
1186+
if (attrib != null) attrib.set(nx, ny, nz);
1187+
}
1188+
1189+
1190+
@Override
1191+
public void attribColor(String name, int color) {
1192+
VertexAttribute attrib = attribImpl(name, VertexAttribute.COLOR, PGL.INT, 1);
1193+
if (attrib != null) attrib.set(new int[] {color});
1194+
}
1195+
1196+
11731197
@Override
11741198
public void attrib(String name, float... values) {
1175-
VertexAttribute attrib = attribImpl(name, PGL.FLOAT, values.length);
1199+
VertexAttribute attrib = attribImpl(name, VertexAttribute.OTHER, PGL.FLOAT,
1200+
values.length);
11761201
if (attrib != null) attrib.set(values);
11771202
}
11781203

11791204

11801205
@Override
11811206
public void attrib(String name, int... values) {
1182-
VertexAttribute attrib = attribImpl(name, PGL.INT, values.length);
1207+
VertexAttribute attrib = attribImpl(name, VertexAttribute.OTHER, PGL.INT,
1208+
values.length);
11831209
if (attrib != null) attrib.set(values);
11841210
}
11851211

11861212

11871213
@Override
11881214
public void attrib(String name, boolean... values) {
1189-
VertexAttribute attrib = attribImpl(name, PGL.BOOL, values.length);
1215+
VertexAttribute attrib = attribImpl(name, VertexAttribute.OTHER, PGL.BOOL,
1216+
values.length);
11901217
if (attrib != null) attrib.set(values);
11911218
}
11921219

11931220

1194-
protected VertexAttribute attribImpl(String name, int type, int size) {
1221+
protected VertexAttribute attribImpl(String name, int kind, int type, int size) {
11951222
if (4 < size) {
11961223
PGraphics.showWarning("Vertex attributes cannot have more than 4 values");
11971224
return null;
11981225
}
11991226
VertexAttribute attrib = polyAttribs.get(name);
12001227
if (attrib == null) {
1201-
attrib = new VertexAttribute(pg, name, type, size);
1228+
attrib = new VertexAttribute(pg, name, kind, type, size);
12021229
polyAttribs.put(name, attrib);
12031230
inGeo.initAttrib(attrib);
12041231
}
1232+
if (attrib.kind != kind) {
1233+
PGraphics.showWarning("The attribute kind cannot be changed after creation");
1234+
return null;
1235+
}
1236+
if (attrib.type != type) {
1237+
PGraphics.showWarning("The attribute type cannot be changed after creation");
1238+
return null;
1239+
}
12051240
if (attrib.size != size) {
12061241
PGraphics.showWarning("New value for vertex attribute has wrong number of values");
12071242
return null;

0 commit comments

Comments
 (0)