Skip to content

Commit

Permalink
finished implementing attrib API
Browse files Browse the repository at this point in the history
  • Loading branch information
codeanticode committed Jul 13, 2015
1 parent 81b728d commit a99c094
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 21 deletions.
15 changes: 15 additions & 0 deletions core/src/processing/core/PGraphics.java
Expand Up @@ -1239,6 +1239,21 @@ public void normal(float nx, float ny, float nz) {
}


public void attribPosition(String name, float x, float y, float z) {
showMissingWarning("attrib");
}


public void attribNormal(String name, float nx, float ny, float nz) {
showMissingWarning("attrib");
}


public void attribColor(String name, int color) {
showMissingWarning("attrib");
}


public void attrib(String name, float... values) {
showMissingWarning("attrib");
}
Expand Down
11 changes: 11 additions & 0 deletions core/src/processing/core/PShape.java
Expand Up @@ -716,6 +716,17 @@ public void normal(float nx, float ny, float nz) {
}


public void attribPosition(String name, float x, float y, float z) {
}

public void attribNormal(String name, float nx, float ny, float nz) {
}


public void attribColor(String name, int color) {
}


public void attrib(String name, float... values) {
}

Expand Down
70 changes: 54 additions & 16 deletions core/src/processing/opengl/PGraphicsOpenGL.java
Expand Up @@ -2625,39 +2625,73 @@ public void vertex(float x, float y, float z, float u, float v) {
}


@Override
public void attribPosition(String name, float x, float y, float z) {
VertexAttribute attrib = attribImpl(name, VertexAttribute.POSITION,
PGL.FLOAT, 3);
if (attrib != null) attrib.set(x, y, z);
}


@Override
public void attribNormal(String name, float nx, float ny, float nz) {
VertexAttribute attrib = attribImpl(name, VertexAttribute.NORMAL,
PGL.FLOAT, 3);
if (attrib != null) attrib.set(nx, ny, nz);
}


@Override
public void attribColor(String name, int color) {
VertexAttribute attrib = attribImpl(name, VertexAttribute.COLOR, PGL.INT, 1);
if (attrib != null) attrib.set(new int[] {color});
}


@Override
public void attrib(String name, float... values) {
VertexAttribute attrib = attribImpl(name, PGL.FLOAT, values.length);
VertexAttribute attrib = attribImpl(name, VertexAttribute.OTHER,
PGL.FLOAT, values.length);
if (attrib != null) attrib.set(values);
}


@Override
public void attrib(String name, int... values) {
VertexAttribute attrib = attribImpl(name, PGL.INT, values.length);
VertexAttribute attrib = attribImpl(name, VertexAttribute.OTHER,
PGL.INT, values.length);
if (attrib != null) attrib.set(values);
}


@Override
public void attrib(String name, boolean... values) {
VertexAttribute attrib = attribImpl(name, PGL.BOOL, values.length);
VertexAttribute attrib = attribImpl(name, VertexAttribute.OTHER,
PGL.BOOL, values.length);
if (attrib != null) attrib.set(values);
}


protected VertexAttribute attribImpl(String name, int type, int size) {
protected VertexAttribute attribImpl(String name, int kind, int type, int size) {
if (4 < size) {
PGraphics.showWarning("Vertex attributes cannot have more than 4 values");
return null;
}
VertexAttribute attrib = polyAttribs.get(name);
if (attrib == null) {
attrib = new VertexAttribute(this, name, type, size);
attrib = new VertexAttribute(this, name, kind, type, size);
polyAttribs.put(name, attrib);
inGeo.initAttrib(attrib);
tessGeo.initAttrib(attrib);
}
if (attrib.kind != kind) {
PGraphics.showWarning("The attribute kind cannot be changed after creation");
return null;
}
if (attrib.type != type) {
PGraphics.showWarning("The attribute type cannot be changed after creation");
return null;
}
if (attrib.size != size) {
PGraphics.showWarning("New value for vertex attribute has wrong number of values");
return null;
Expand Down Expand Up @@ -7398,23 +7432,17 @@ static protected class VertexAttribute {
int lastModified;
boolean active;

VertexAttribute(PGraphicsOpenGL pg, String name, int type, int size) {
VertexAttribute(PGraphicsOpenGL pg, String name, int kind, int type, int size) {
this.pg = pg;
this.name = name;
this.kind = kind;
this.type = type;
this.size = size;

tessSize = size;

if (name.indexOf("pos") == 0 && type == PGL.FLOAT && size == 3) {
kind = POSITION;
tessSize = 4;
} else if (name.indexOf("norm") == 0 && type == PGL.FLOAT && size == 3) {
kind = NORMAL;
} else if (name.indexOf("color") == 0 && type == PGL.INT && size == 1) {
kind = COLOR;
if (kind == POSITION) {
tessSize = 4; // for w
} else {
kind = OTHER;
tessSize = size;
}

if (type == PGL.FLOAT) {
Expand Down Expand Up @@ -7520,6 +7548,16 @@ int sizeInBytes(int length) {
return length * tessSize * elementSize;
}

void set(float x, float y, float z) {
fvalues[0] = x;
fvalues[1] = y;
fvalues[2] = z;
}

void set(int c) {
ivalues[0] = c;
}

void set(float[] values) {
PApplet.arrayCopy(values, 0, fvalues, 0, size);
}
Expand Down
45 changes: 40 additions & 5 deletions core/src/processing/opengl/PShapeOpenGL.java
Expand Up @@ -1170,38 +1170,73 @@ public void normal(float nx, float ny, float nz) {
}
}


@Override
public void attribPosition(String name, float x, float y, float z) {
VertexAttribute attrib = attribImpl(name, VertexAttribute.POSITION,
PGL.FLOAT, 3);
if (attrib != null) attrib.set(x, y, z);
}


@Override
public void attribNormal(String name, float nx, float ny, float nz) {
VertexAttribute attrib = attribImpl(name, VertexAttribute.NORMAL,
PGL.FLOAT, 3);
if (attrib != null) attrib.set(nx, ny, nz);
}


@Override
public void attribColor(String name, int color) {
VertexAttribute attrib = attribImpl(name, VertexAttribute.COLOR, PGL.INT, 1);
if (attrib != null) attrib.set(new int[] {color});
}


@Override
public void attrib(String name, float... values) {
VertexAttribute attrib = attribImpl(name, PGL.FLOAT, values.length);
VertexAttribute attrib = attribImpl(name, VertexAttribute.OTHER, PGL.FLOAT,
values.length);
if (attrib != null) attrib.set(values);
}


@Override
public void attrib(String name, int... values) {
VertexAttribute attrib = attribImpl(name, PGL.INT, values.length);
VertexAttribute attrib = attribImpl(name, VertexAttribute.OTHER, PGL.INT,
values.length);
if (attrib != null) attrib.set(values);
}


@Override
public void attrib(String name, boolean... values) {
VertexAttribute attrib = attribImpl(name, PGL.BOOL, values.length);
VertexAttribute attrib = attribImpl(name, VertexAttribute.OTHER, PGL.BOOL,
values.length);
if (attrib != null) attrib.set(values);
}


protected VertexAttribute attribImpl(String name, int type, int size) {
protected VertexAttribute attribImpl(String name, int kind, int type, int size) {
if (4 < size) {
PGraphics.showWarning("Vertex attributes cannot have more than 4 values");
return null;
}
VertexAttribute attrib = polyAttribs.get(name);
if (attrib == null) {
attrib = new VertexAttribute(pg, name, type, size);
attrib = new VertexAttribute(pg, name, kind, type, size);
polyAttribs.put(name, attrib);
inGeo.initAttrib(attrib);
}
if (attrib.kind != kind) {
PGraphics.showWarning("The attribute kind cannot be changed after creation");
return null;
}
if (attrib.type != type) {
PGraphics.showWarning("The attribute type cannot be changed after creation");
return null;
}
if (attrib.size != size) {
PGraphics.showWarning("New value for vertex attribute has wrong number of values");
return null;
Expand Down

0 comments on commit a99c094

Please sign in to comment.