void LoadOBJToSoftwareMesh(const char *objFilename, CPUTSoftwareMesh *mesh) { tObjModel objModel; objLoader(objFilename, objModel); mesh->UpdateIndexCount((int)objModel.m_indices.size()); assert(sizeof(ObjIndexInt) == sizeof(uint32_t)); memcpy(mesh->IB, &objModel.m_indices[0], sizeof(uint32_t) * objModel.m_indices.size()); int vertCount = (int)objModel.m_vertices.size(); mesh->UpdateVertexCount(vertCount); mesh->AddComponent(eSMComponent_Position); mesh->AddComponent(eSMComponent_Normal); mesh->AddComponent(eSMComponent_Tex1); for (int i = 0; i < vertCount; i++) { mesh->Pos[i] = float3(objModel.m_vertices[i].x, objModel.m_vertices[i].y, objModel.m_vertices[i].z); mesh->Normal[i] = float3(objModel.m_vertices[i].nx, objModel.m_vertices[i].ny, objModel.m_vertices[i].nz); mesh->Tex[i] = float2(objModel.m_vertices[i].u, objModel.m_vertices[i].v); } }
void CFaceModel::LoadObjFilename(const std::string &filename, bool landmarks) { AABBMin = float3(10000.0f, 10000.0f, 10000.0f); AABBMax = -AABBMin; mObjFilename = filename; { tObjModel objModel; objLoader(filename.c_str(), objModel); CopyOBJDataToSoftwareMesh(&objModel, &mMesh); } float boxSize = 8.0f; int vertCount = mMesh.GetVertCount(); if (vertCount > 0) { float3 vmin = mMesh.Pos[0]; float3 vmax = mMesh.Pos[0]; for (int i = 0; i < vertCount; i++) { vmin = Min(vmin, mMesh.Pos[i]); vmax = Max(vmax, mMesh.Pos[i]); } float3 center = (vmax + vmin) / 2.0f; float3 dim = vmax - vmin; float maxDim = floatMax(dim.x, floatMax(dim.y, dim.z)); mVertOffset = -center; mVertScale = boxSize / maxDim; // center to origin, scale, and rotate // for (int i = 0; i < vertCount; i++) { float3 *pos = &mMesh.Pos[i]; *pos -= center; *pos *= mVertScale; Swap(pos->z, pos->y); // save scale and rotate from RSSDK format. Z is up in rssdk mMesh.Tex[i].y = 1.0f - mMesh.Tex[i].y; AABBMin = Min(*pos, AABBMin); AABBMax = Max(*pos, AABBMax); } } LoadLandmarks(); // load texture SAFE_RELEASE(mTexture); std::string fnString = mObjFilename; int lastindex = (int)fnString.find_last_of("."); std::string rawname = fnString.substr(0, lastindex); std::string textureName = rawname.append("image1.png"); mTexture = CPUTTexture::Create(std::string("facetexture"), textureName, false); FlagUpdated(); }
bool initialize( void ) { // Initialize geometry with shader loader Vertex *geometry = objLoader( "sphere.obj", numberOfVertices ); Vertex circle[360]; calculateOrbitPath( circle ); text = new GLuint[spaceObjects.size()]; m_blob = new Magick::Blob[spaceObjects.size()]; // Create a Vertex Buffer object to store this vertex info on the GPU glGenBuffers(3, vbo_geometry); // Create a vertex buffer for the planet data glBindBuffer(GL_ARRAY_BUFFER, vbo_geometry[0]); glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * numberOfVertices, geometry, GL_STATIC_DRAW); // Create a vertex buffer for saturn's rings Vertex *ringGeometry = objLoader( "saturn.obj", numberOfRingVertices ); glBindBuffer( GL_ARRAY_BUFFER, vbo_geometry[1] ); glBufferData( GL_ARRAY_BUFFER, sizeof(Vertex) * numberOfRingVertices, ringGeometry, GL_STATIC_DRAW ); // Create a vertex buffer for the orbit path glBindBuffer( GL_ARRAY_BUFFER, vbo_geometry[2] ); glBufferData( GL_ARRAY_BUFFER, sizeof(circle), circle, GL_STATIC_DRAW ); //--Geometry done // Create a texture buffer object glGenTextures(spaceObjects.size(), text); for( unsigned int i = 0; i < spaceObjects.size(); i++ ) { // Load the image Magick::Image *m_pImage; try { m_pImage = new Magick::Image(spaceObjects[i].objectImageFile); m_pImage->write(&m_blob[i], "RGBA"); } catch( Magick::Error& Error ) { std::cout << "Error loading texture " << spaceObjects[i].objectImageFile; std::cout << ": " << Error.what() << std::endl; } // Create a binding for each texture glActiveTexture( GL_TEXTURE0 ); glBindTexture(GL_TEXTURE_2D, text[i]); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_pImage->columns(), m_pImage->rows(), 0, GL_RGBA, GL_UNSIGNED_BYTE, m_blob[i].data() ); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // Deallocate the image delete m_pImage; m_pImage = NULL; } // Initialize shader array shader vAndFShaders[2]; // Load data into our array of shaders vAndFShaders[0].setShaderFile( "vShader.vert" ); vAndFShaders[0].readShaderFile(); vAndFShaders[0].setShaderType( GL_VERTEX_SHADER ); vAndFShaders[1].setShaderFile( "fShader.frag" ); vAndFShaders[1].readShaderFile(); vAndFShaders[1].setShaderType( GL_FRAGMENT_SHADER ); // Compile and link shaders using "shader.h" program = vAndFShaders[0].compileAndLinkShaders( vAndFShaders ); //Now we set the locations of the attributes and uniforms //this allows us to access them easily while rendering loc_position = glGetAttribLocation(program, const_cast<const char*>("v_position")); if(loc_position == -1) { std::cerr << "[F] POSITION NOT FOUND" << std::endl; return false; } loc_uv = glGetAttribLocation(program, const_cast<const char*>("v_texture")); if(loc_uv == -1) { std::cerr << "[F] UV_POSITION NOT FOUND" << std::endl; return false; } loc_mvpmat = glGetUniformLocation(program, const_cast<const char*>("mvpMatrix")); if(loc_mvpmat == -1) { std::cerr << "[F] MVPMATRIX NOT FOUND" << std::endl; return false; } //--Init the view and projection matrices // if you will be having a moving camera the view matrix will need to more dynamic // ...Like you should update it before you render more dynamic // for this project having them static will be fine view = glm::lookAt( glm::vec3(cos(shiftVal)*zoomVal, height, sin (shiftVal)*zoomVal), //Eye Position glm::vec3(0.0,0.0,0.0), //Focus point glm::vec3(0.0, 1.0, 0.0)); //Positive Y is up projection = glm::perspective( 45.0f, //the FoV typically 90 degrees is good which is what this is set to float(w)/float(h), //Aspect Ratio, so Circles stay Circular 0.01f, //Distance to the near plane, normally a small value like this 100.0f); //Distance to the far plane, //enable depth testing glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LESS); //and its done return true; }