//-------------------------------------------------------------- void ofApp::draw() { pbr.makeDepthMap(scene); firstPass.begin(); ofClear(0); cam.begin(); scene(); cam.end(); firstPass.end(); postEffect.begin(); postEffect.setUniform1f("gamma", 2.2); postEffect.setUniform1f("exposure", 1.0); firstPass.draw(0, 0); postEffect.end(); gui.begin(); { ImGui::Text(ofToString(ofGetFrameRate()).c_str()); if (ImGui::Button("load bvh file")) { ofFileDialogResult openFileResult = ofSystemLoadDialog("Select a image"); if (openFileResult.bSuccess) { bvhFile.open(openFileResult.getPath()); if (bvhFile.getExtension() == "bvh") { bvh = ofxBvh(); bvh.load(bvhFile.getAbsolutePath()); bvh.setLoop(true); bvh.play(); fileName = bvhFile.getBaseName() + "." + bvhFile.getExtension(); setupJoints(); } } } ImGui::Text(fileName.c_str()); ImGui::DragFloat("timestep", ×tep, 0.001, 0.0, 1.0); ImGui::DragFloat("scale", &scale, 0.001, 0.0, 1.0); ImGui::DragFloat("maxLifetime", &lifetime, 1.0, 0.0, 500.0); ImGui::DragFloat3("box size", &boxSize[0], 1.0, 1.0, 100.0); pbrHelper.drawGui(); } gui.end(); }
//-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(60); scene = bind(&ofApp::renderScene, this); gui.setup(); ofDisableArbTex(); pbr.setup(1024); ofxPBRFiles::getInstance()->setup("ofxPBRAssets"); pbrHelper.setup(&pbr, ofxPBRFiles::getInstance()->getPath() + "/settings", true); pbrHelper.addLight(&pbrLight1, "light1"); pbrHelper.addMaterial(&jointMaterial, "joint"); pbrHelper.addMaterial(&material1, "material1"); pbrHelper.addMaterial(&floorMaterial, "floor"); pbrHelper.addCubeMap(&cubemap, "cubeMap1"); particlesShader.load("shaders/particles"); postEffect.load("shaders/tonemap"); resizeFbos(); cam.setupPerspective(false, 60, 1, 12000); float * position = new float[numParticles * 3]; for (int i = 0; i < numParticles; i++) { position[i * 3 + 0] = ofRandom(-1, 1); position[i * 3 + 1] = ofRandom(-1, 1); position[i * 3 + 2] = ofRandom(-1, 1); } float * velocity = new float[numParticles * 3]; for (int i = 0; i < numParticles; i++) { velocity[i * 3 + 0] = ofRandom(-1, 1); velocity[i * 3 + 1] = ofRandom(-1, 1); velocity[i * 3 + 2] = ofRandom(-1, 1); } float * age = new float[numParticles]; for (int i = 0; i < numParticles; i++) { age[i] = 0; } float * lifetime = new float[numParticles]; for (int i = 0; i < numParticles; i++) { lifetime[i] = 1; } tf.setup(numParticles, "shaders/tf.vert"); tf.addBufferObject("inPosition", "outPosition", 3, GL_RGB32F, position); tf.addBufferObject("inVelocity", "outVelocity", 3, GL_RGB32F, velocity); tf.addBufferObject("inAge", "outAge", 1, GL_R32F, age); tf.addBufferObject("inLifetime", "outLifetime", 1, GL_R32F, lifetime); tf.generate(); fileName = "05_05.bvh"; bvh.load(fileName); bvh.setLoop(true); bvh.play(); ofBoxPrimitive box; box.set(1, 1, 1, 1, 1, 1); mesh = box.getMesh(); setupJoints(); }
bool ModelMS3D::loadModel(const char *filename, GLboolean is_absPath) { FILE *file; int i, j; file = fopen(Utils::getFileName(filename, is_absPath), "rb"); if (!file) { printf("Open MS3D model:%s failure!\n", filename); #if defined(WIN32) || defined(_WIN32_WCE) TCHAR errorStr[512]; wsprintf(errorStr, TEXT("Open MS3d model:[%s] error!"), Utils::getFileName(filename)); MessageBox(0, errorStr, TEXT("MS3D"), MB_OK); #endif return false; } /* initialize model and read header */ fread (&m_header, sizeof (ms3d_header_t), 1, file); #ifdef DEBUG printf("id:%s\n", m_header.id); printf("version:%d\n", m_header.version); #if defined(WIN32) || defined(_WIN32_WCE) TCHAR infoStr[512]; wsprintf(infoStr, TEXT("id:%s\nversion: %d"), m_header.id, m_header.version); MessageBox(0, infoStr, TEXT("MS3D"), MB_OK); #endif #endif if (!strcmp((char *)m_header.id, "MS3D000000") || m_header.version != 4) { printf("Invalid MS3D model or unsupport version, id:[%s], version[%d]...\n", m_header.id, m_header.version); fclose(file); return false; } //read vertices fread(&m_verticesCount, sizeof(GLushort), 1, file); #ifdef DEBUG printf("m_verticesCount:%d\n", m_verticesCount); #if defined(WIN32) || defined(_WIN32_WCE) wsprintf(infoStr, TEXT("m_verticesCount: %d"), m_verticesCount); MessageBox(0, infoStr, TEXT("MS3D"), MB_OK); #endif #endif m_vertices = new ms3d_vertex_t[m_verticesCount]; fread(m_vertices, sizeof(ms3d_vertex_t), m_verticesCount, file); //read triangles fread(&m_trianglesCount, sizeof(GLushort), 1, file); #ifdef DEBUG printf("m_trianglesCount:%d\n", m_trianglesCount); #if defined(WIN32) || defined(_WIN32_WCE) wsprintf(infoStr, TEXT("m_trianglesCount: %d"), m_trianglesCount); MessageBox(0, infoStr, TEXT("MS3D"), MB_OK); #endif #endif m_triangles = new ms3d_triangle_t[m_trianglesCount]; fread(m_triangles, sizeof(ms3d_triangle_t), m_trianglesCount, file); //read groups fread(&m_groupsCount, sizeof(GLushort), 1, file); #ifdef DEBUG printf("m_groupsCount:%d\n", m_groupsCount); #if defined(WIN32) || defined(_WIN32_WCE) wsprintf(infoStr, TEXT("m_groupsCount: %d"), m_groupsCount); MessageBox(0, infoStr, TEXT("MS3D"), MB_OK); #endif #endif //set model mesh count to group count to init model meshs. setMeshCount(m_groupsCount); m_groups = new ms3d_group_t[m_groupsCount]; for (i = 0; i < m_groupsCount; i++) { fread(&m_groups[i].flags, sizeof(GLubyte), 1, file); fread(&m_groups[i].name, MS3D_NAME_SIZE, 1, file); fread(&m_groups[i].numTriangles, sizeof(GLushort), 1, file); //after read triangles count, read triangle indices m_groups[i].triangleIndices = new GLushort[m_groups[i].numTriangles]; fread(&m_groups[i].triangleIndices[0], sizeof(GLushort), m_groups[i].numTriangles, file); //read material index fread(&m_groups[i].materialIndex, sizeof(char), 1, file); //set mesh triangle count setTriangleNums(m_groups[i].numTriangles, i); #ifdef DEBUG printf("m_groups[%d] name:%s\n", i, m_groups[i].name); printf("m_groups[%d] numTriangles:%d\n", i, m_groups[i].numTriangles); printf("m_groups[%d] materialIndex:%d\n", i, m_groups[i].materialIndex); #endif } //read materials fread(&m_materialsCount, sizeof(GLushort), 1, file); #ifdef DEBUG printf("m_materialsCount:%d\n", m_materialsCount); #endif if (m_materialsCount > 0) { m_materials = new ms3d_material_t[m_materialsCount]; fread(m_materials, sizeof(ms3d_material_t), m_materialsCount, file); #ifdef DEBUG for (i = 0; i < m_materialsCount; i++) { printf("m_materials[%d].name:%s\n", i, m_materials[i].name); printf("m_materials[%d].texture:%s\n", i, m_materials[i].texture); } #endif } //read animation fps, current time, total frames fread(&m_animationFPS, sizeof(float), 1, file); fread(&m_currentTime, sizeof(float), 1, file); fread(&m_totalFrames, sizeof(int), 1, file); //set frame count & frame index to 0 m_frameCount = m_totalFrames - 1; // totleFrames start with "1", not "0" m_frameIdx = 0; #ifdef DEBUG printf("m_animationFPS:%.4f\n", m_animationFPS); printf("m_currentTime:%.4f\n", m_currentTime); printf("m_totalFrames:%d\n", m_totalFrames); #endif //read joints fread(&m_jointsCount, sizeof(GLushort), 1, file); #ifdef DEBUG printf("m_jointsCount:%d\n", m_jointsCount); #if defined(WIN32) || defined(_WIN32_WCE) wsprintf(infoStr, TEXT("m_jointsCount: %d"), m_jointsCount); MessageBox(0, infoStr, TEXT("MS3D"), MB_OK); #endif #endif if (m_jointsCount > 0) { m_joints = new ms3d_joint_t[m_jointsCount]; for (i = 0; i < m_jointsCount; i++) { fread(&m_joints[i].header, sizeof(ms3d_joint_header_t), 1, file); //read frame rots m_joints[i].keyFramesRot = new ms3d_keyframe_rot_t[m_joints[i].header.numKeyFramesRot]; fread(&m_joints[i].keyFramesRot[0], sizeof(ms3d_keyframe_rot_t), m_joints[i].header.numKeyFramesRot, file); //read frame trans m_joints[i].keyFramesTrans = new ms3d_keyframe_pos_t[m_joints[i].header.numKeyFramesTrans]; fread(&m_joints[i].keyFramesTrans[0], sizeof(ms3d_keyframe_pos_t), m_joints[i].header.numKeyFramesTrans, file); //get parent index by parentName m_joints[i].parentJointIndex = -1; if (strlen(m_joints[i].header.parentName) > 0) { for (j = 0; j < i; j++) { if (strcmp(m_joints[i].header.parentName, m_joints[j].header.name) == 0) { m_joints[i].parentJointIndex = j; //find the parent index by name, break! break; } } } //init matrices m_joints[i].absMatrix = new Matrix(); m_joints[i].relMatrix = new Matrix(); m_joints[i].finMatrix = new Matrix(); #ifdef DEBUG printf("================================================\n"); printf("m_joints[%d] name:%s\n", i, m_joints[i].header.name); printf("m_joints[%d] parentName:%s\n", i, m_joints[i].header.parentName); printf("m_joints[%d] numKeyFramesRot:%d\n", i, m_joints[i].header.numKeyFramesRot); printf("m_joints[%d] numKeyFramesTrans:%d\n", i, m_joints[i].header.numKeyFramesTrans); printf("m_joints[%d] parentJointIndex:%d\n", i, m_joints[i].parentJointIndex); #endif } } //ignore other sections, such as sub version, comments, etc... fclose(file); setupJoints(); return true; }