示例#1
0
文件: Cloth.cpp 项目: kikko/ofApps
void Cloth::createGeometry() {
	
	int i, j, pos;
	int numVertsW = numW+1;
	
	// create vertices
	
	float faceW = (float)width/numW;
	float faceH = (float)height/numH;
	for(i=0; i<numH+1; i++) {
		for(j=0; j<numW+1; j++) {
			pos = numVertsW*i+j;
			vertices.push_back(Vertex(pos, faceW*j, faceH*i, 0.f));
		}
    }
	
	// create faces
	
	for(i=0; i<numH; i++) {
		for(j=0; j<numW; j++) {
			pos = numVertsW*i+j;
			
			faces.push_back(Face(&vertices[pos],
								 &vertices[pos+1],
								 &vertices[pos+numVertsW+1]));
			
			faces.push_back(Face(&vertices[pos],
								 &vertices[pos+numVertsW+1],
								 &vertices[pos+numVertsW]));
			
		}
    }
	
	verticesSize = vertices.size();
	facesSize = faces.size();
	
	// compute normals
	
	computeNormals();
	
	// init buffers
	
	posData = new GLfloat[verticesSize*3]; // 3 coords per vertex
	normalsData = new GLfloat[verticesSize*3]; // 3 coords per vertex
	
	glBindBuffer(GL_ARRAY_BUFFER, verticesBuffer);
	glBufferData(GL_ARRAY_BUFFER, verticesSize*3*sizeof(GLfloat)*2, 0, GL_STREAM_DRAW);
	
	updateBufferData();
	
	facesData = new GLuint[faces.size()*3]; // 3 vertices per face
	for (int i=0; i<faces.size(); i++) {
		facesData[i*3]	 = faces[i].vertices[0]->vId;
		facesData[i*3+1] = faces[i].vertices[1]->vId;
		facesData[i*3+2] = faces[i].vertices[2]->vId;
	}
	
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, facesBuffer);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, facesSize*3*sizeof(GLuint), facesData, GL_STATIC_DRAW);
}
示例#2
0
文件: Cloth.cpp 项目: kikko/ofApps
void Cloth::update(float * distancePixels){
	
	int pos=0;
	float depthVal;
	int pW = 640.0 / (numW+1);
	int pH = 480.0 / (numH+1);
	int x, y;
	float scaleFactor = .0021;//.0021;
	float minDistance = 20;
	float prevDepth = 0;
	for(int i=0; i<numH+1; i++) {
		for(int j=0; j<numW+1; j++) {
			x = j*pW;
			y = i*pH;
			depthVal = distancePixels[j*pW + i*pH*640];
			//vertices[pos].pos.z = ofLerp(vertices[pos].pos.z, 900-(depthVal==0?200:depthVal)*6.4, 0.3);
			if (depthVal!=0) {
				//vertices[pos].pos.x = (x-640/2) * (depthVal+minDistance) * scaleFactor;
				//vertices[pos].pos.y = (y-480/2) * (depthVal+minDistance) * scaleFactor;
				//vertices[pos].pos.z = 900-(depthVal==0?200:depthVal)*6.4;
				vertices[pos].pos.x = (x-640/2) * (depthVal+minDistance) * scaleFactor;
				vertices[pos].pos.y = (y-480/2) * (depthVal+minDistance) * scaleFactor;
				vertices[pos].pos.z = -depthVal;
				prevDepth = depthVal;
			}
			else {
				vertices[pos].pos.x = (x-640/2) * (prevDepth+minDistance) * scaleFactor;
				vertices[pos].pos.y = (y-480/2) * (prevDepth+minDistance) * scaleFactor;
				vertices[pos].pos.z = -prevDepth;
				
			}
			pos++;
		}
    }
	
	// update normals
	computeNormals();
	
	// update buffer datas
	updateBufferData();
}
GLuint GLES20Utils::createBuffer(const GLfloat* data) {
    GLuint bufferName;
    glGenBuffers(1, &bufferName);
    updateBufferData(bufferName, data);
    return bufferName;
}