Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions 12_generativeMesh/01_basicMesh/src/ofApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,28 @@

//--------------------------------------------------------------
void ofApp::setup(){

// mesh.setMode(OF_PRIMITIVE_POINTS);
// mesh.setMode(OF_PRIMITIVE_LINES);
// mesh.setMode(OF_PRIMITIVE_LINE_STRIP);

mesh.setMode(OF_PRIMITIVE_LINE_LOOP);

// Alternate primitive modes to try:
//mesh.setMode(OF_PRIMITIVE_POINTS);
//mesh.setMode(OF_PRIMITIVE_LINES);
//mesh.setMode(OF_PRIMITIVE_LINE_STRIP);

mesh.enableColors();

ofVec3f top(100.0, 50.0, 0.0);
ofVec3f left(50.0, 150.0, 0.0);
ofVec3f right(150.0, 150.0, 0.0);

mesh.addVertex(top);
mesh.addColor(ofFloatColor(1.0, 0.0, 0.0));
mesh.addColor(ofFloatColor(1.0, 0.0, 0.0)); // Red

mesh.addVertex(left);
mesh.addColor(ofFloatColor(0.0, 1.0, 0.0));
mesh.addColor(ofFloatColor(0.0, 1.0, 0.0)); // Green

mesh.addVertex(right);
mesh.addColor(ofFloatColor(1.0, 1.0, 0.0));
mesh.addColor(ofFloatColor(1.0, 1.0, 0.0)); // Blue

ofBackground(0);
}
Expand Down
8 changes: 8 additions & 0 deletions 12_generativeMesh/02_meshIndices/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "ofMain.h"
#include "ofApp.h"

int main( ){
ofSetupOpenGL(150,125,OF_WINDOW);
ofRunApp(new ofApp());

}
55 changes: 55 additions & 0 deletions 12_generativeMesh/02_meshIndices/src/ofApp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include "ofApp.h"

//--------------------------------------------------------------
void ofApp::setup(){

mesh.setMode(OF_PRIMITIVE_LINES);
mesh.enableColors();
mesh.enableIndices();

ofVec3f eyeLeftTop(50.0, 25.0, 0.0);
ofVec3f eyeLeftBottom(50.0, 50.0, 0.0);
ofVec3f eyeRightTop(100.0, 25.0, 0.0);
ofVec3f eyeRightBottom(100.0, 50.0, 0.0);
ofVec3f mouthLeft(50.0, 75.0, 0.0);
ofVec3f mouthMiddle(75.0, 100.0, 0.0);
ofVec3f mouthRight(100.0, 75.0, 0.0);

mesh.addVertex(eyeLeftTop);
mesh.addVertex(eyeLeftBottom);
mesh.addIndex(0);
mesh.addIndex(1);
mesh.addColor(ofFloatColor(0.0, 1.0, 1.0));
mesh.addColor(ofFloatColor(0.0, 1.0, 1.0));

mesh.addVertex(eyeRightTop);
mesh.addVertex(eyeRightBottom);
mesh.addIndex(2);
mesh.addIndex(3);
mesh.addColor(ofFloatColor(0.0, 1.0, 1.0));
mesh.addColor(ofFloatColor(0.0, 1.0, 1.0));

mesh.addVertex(mouthLeft);
mesh.addVertex(mouthMiddle);
mesh.addVertex(mouthRight);
mesh.addIndex(4);
mesh.addIndex(5);
mesh.addIndex(5);
mesh.addIndex(6);
mesh.addColor(ofFloatColor(0.0, 1.0, 0.0));
mesh.addColor(ofFloatColor(0.0, 1.0, 0.0));
mesh.addColor(ofFloatColor(0.0, 1.0, 0.0));

ofBackground(0);
}

//--------------------------------------------------------------
void ofApp::update(){

}

//--------------------------------------------------------------
void ofApp::draw(){
mesh.draw();
}

13 changes: 13 additions & 0 deletions 12_generativeMesh/02_meshIndices/src/ofApp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

#include "ofMain.h"

class ofApp : public ofBaseApp{

public:
void setup();
void update();
void draw();

ofMesh mesh;
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

//--------------------------------------------------------------
void ofApp::setup(){
bool succ = true;
succ = image.load("stars.png");
if (!succ) {
// Do a little extra error checking to see if the image was
// loaded properly. This is not in the chapter text.
bool wasImageLoaded = image.load("stars.png");
if (!wasImageLoaded) {
cerr << "loading image failed ...\n";
}

mesh.setMode(OF_PRIMITIVE_POINTS);
mesh.enableColors();

float intensityThreshold = 150.0;
int w = image.getWidth();
Expand All @@ -20,6 +22,8 @@ void ofApp::setup(){
if (intensity >= intensityThreshold) {
ofVec3f pos(x, y, 0.0);
mesh.addVertex(pos);
// When addColor(...), the mesh will automatically convert
// the ofColor to an ofFloatColor
mesh.addColor(c);
}
}
Expand All @@ -33,6 +37,8 @@ void ofApp::update(){

//--------------------------------------------------------------
void ofApp::draw(){
ofBackground(0,0,255);
ofColor centerColor = ofColor(85, 78, 68);
ofColor edgeColor(0, 0, 0);
ofBackgroundGradient(centerColor, edgeColor, OF_GRADIENT_CIRCULAR);
mesh.draw();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

//--------------------------------------------------------------
void ofApp::setup(){
bool succ = true;
succ = image.load("stars.png");
if (!succ) {
// Do a little extra error checking to see if the image was
// loaded properly. This is not in the chapter text.
bool wasImageLoaded = image.load("stars.png");
if (!wasImageLoaded) {
cerr << "loading image failed ...\n";
}
image.resize(200, 200);

mesh.setMode(OF_PRIMITIVE_LINES);
mesh.enableColors();
mesh.enableIndices();

float intensityThreshold = 150.0;
int w = image.getWidth();
Expand All @@ -35,6 +39,8 @@ void ofApp::setup(){
ofVec3f vertb = mesh.getVertex(b);
float distance = verta.distance(vertb);
if (distance <= connectionDistance) {
// In OF_PRIMITIVE_LINES, every pair of vertices or indices will be
// connected to form a line
mesh.addIndex(a);
mesh.addIndex(b);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

//--------------------------------------------------------------
void ofApp::setup(){
bool succ = true;
succ = image.load("stars.png");
if (!succ) {
// Do a little extra error checking to see if the image was
// loaded properly. This is not in the chapter text.
bool wasImageLoaded = image.load("stars.png");
if (!wasImageLoaded) {
cerr << "loading image failed ...\n";
}
image.resize(200, 200);

mesh.setMode(OF_PRIMITIVE_LINES);
mesh.enableColors();
mesh.enableIndices();

float intensityThreshold = 150.0;
int w = image.getWidth();
Expand All @@ -17,9 +21,9 @@ void ofApp::setup(){
for (int y=0; y<h; ++y) {
ofColor c = image.getColor(x, y);
float intensity = c.getLightness();
// z-coordinate shift depends on point's saturation
if (intensity >= intensityThreshold) {
float saturation = c.getSaturation();
// Z-coordinate shift depends on the pixel's color saturation
float z = ofMap(saturation, 0, 255, -100, 100);
ofVec3f pos(x*4, y*4, z);
mesh.addVertex(pos);
Expand All @@ -36,6 +40,8 @@ void ofApp::setup(){
ofVec3f vertb = mesh.getVertex(b);
float distance = verta.distance(vertb);
if (distance <= connectionDistance) {
// In OF_PRIMITIVE_LINES, every pair of vertices or indices will be
// connected to form a line
mesh.addIndex(a);
mesh.addIndex(b);
}
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ void ofApp::setup(){

ofSetFrameRate(60);

bool succ = true;
succ = image.load("stars.png");
if (!succ) {
// Do a little extra error checking to see if the image was
// loaded properly. This is not in the chapter text.
bool wasImageLoaded = image.load("stars.png");
if (!wasImageLoaded) {
cerr << "loading image failed ...\n";
}
image.resize(200, 200);

mesh.setMode(OF_PRIMITIVE_LINES);
mesh.enableColors();
mesh.enableIndices();

float intensityThreshold = 150.0;
int w = image.getWidth();
Expand All @@ -20,13 +24,16 @@ void ofApp::setup(){
for (int y=0; y<h; ++y) {
ofColor c = image.getColor(x, y);
float intensity = c.getLightness();
// z-coordinate shift depends on point's saturation
if (intensity >= intensityThreshold) {
float saturation = c.getSaturation();
// Z-coordinate shift depends on the pixel's color saturation
float z = ofMap(saturation, 0, 255, -100, 100);
ofVec3f pos(x*4, y*4, z);
mesh.addVertex(pos);
mesh.addColor(c);
// Create "offsets" that will allow us to move the x, y and z
// coordinates of each vertex independently using noise
offsets.push_back(ofVec3f(ofRandom(0, 100000), ofRandom(0, 100000), ofRandom(0, 100000)));
}
}
}
Expand All @@ -39,33 +46,13 @@ void ofApp::setup(){
ofVec3f vertb = mesh.getVertex(b);
float distance = verta.distance(vertb);
if (distance <= connectionDistance) {
// In OF_PRIMITIVE_LINES, every pair of vertices or indices will be
// connected to form a line
mesh.addIndex(a);
mesh.addIndex(b);
}
}
}



for (int x=0; x<w; ++x) {
for (int y=0; y<h; ++y) {
ofColor c = image.getColor(x, y);
float intensity = c.getLightness();
if (intensity >= intensityThreshold) {
float saturation = c.getSaturation();
float z = ofMap(saturation, 0, 255, -100, 100);
ofVec3f pos(x*4, y*4, z);
mesh.addVertex(pos);
mesh.addColor(c);

// And add this line:
// It will create a ofVec3f with 3 random values
// These will allow us to move the x, y and z coordinates of each vertex independently
offsets.push_back(ofVec3f(ofRandom(0,100000), ofRandom(0,100000), ofRandom(0,100000)));
}
}
}

}
}

//--------------------------------------------------------------
Expand Down
Binary file not shown.
Loading