TEST(OrientedBox, testOrientedBoxFromAligned) { cout << "Starting test <orientedbox>" << endl; AxisAlignedBox3d A(Vector3dd(-1, -2, -3), Vector3dd(1, 2, 3)); OrientedBox box(A); Mesh3D mesh; mesh.switchColor(); dumpToMesh(box, mesh); box.transform(Matrix44::Shift(10,10,10) * Matrix44::RotationZ(degToRad(45))); dumpToMesh(box, mesh); mesh.dumpPLY("oob.ply"); cout << "Test <orientedbox> PASSED" << endl; }
void ObjLoader::readMultiMeshFromFile(Model3D *pModel,const char *strFileName) { ifstream in(strFileName); char strLine[256]; string first; model_ = pModel; Mesh3D mesh; if(!in.is_open()) { std::cerr<<"Unable to open file: "<<strFileName<<std::endl; exit(0); } //count the number of sub-meshes int subMeshes = 0; offset_ = 0; while(!in.eof()) { in>>first; if(first == string("g")) { subMeshes++; in.getline(strLine,256); } else in.getline(strLine,256); }//end while in.clear(); in.seekg(0,ios::beg); for(int i=0;i< subMeshes;i++) { Mesh3D mesh; readSubMesh(in,&mesh); pModel->meshes_.push_back(mesh); offset_+=mesh.getNumVerts(); } }//end ReadMultiMeshFromFile
Mesh3D* WaterRenderer:: createPlane() { // initialize Mesh3D Mesh3D *plane = new Mesh3D(); std::vector< Vector3 > planeVertices; std::vector< unsigned int > planeIndices; std::vector< Vector3 > planeNormals; float d = 50; int n=300; float a=2*d/(n-1); float b=2*d/(n-1); for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ planeVertices.push_back(Vector3(-(d)+i*b, 0,-(d)+j*a)); } } for(int i=0;i<n-1;i++){ for(int j=0;j<n-1;j++){ planeIndices.push_back(n*i+j+1); planeIndices.push_back(n*i+j); planeIndices.push_back(n*(i+1)+j); planeIndices.push_back(n*i+j+1); planeIndices.push_back(n*(i+1)+j); planeIndices.push_back(n*(i+1)+j+1); } } for(int k = 0; k < n*n; k++) planeNormals.push_back(Vector3(0,1,0)); plane->setIndices(planeIndices); plane->setVertexPositions(planeVertices); plane->setVertexNormals(planeNormals); return plane; }
void dumpToMesh (OrientedBox &box, Mesh3D &mesh) { mesh.setColor(RGBColor::Cyan()); for (int i = 0; i < 200; i++) for (int j = 0; j < 200; j++) for (int k = 0; k < 200; k++) { Vector3dd T = Vector3dd(i - 100, j - 100, k - 100) / 10.0; T += box.position; if (box.isInside(T)) { mesh.addPoint(T); } } mesh.setColor(RGBColor::Yellow()); for (Vector3dd &p : box.getPoints()) { mesh.addPoint(p); } mesh.setColor(RGBColor::Green()); mesh.addOOB(box, true); }
void testIntersection3D() { Mesh3D mesh; AxisAlignedBox3d box(Vector3dd(-100, -100, -100), Vector3dd(100, 100, 100)); ConvexPolyhedron poly(box); mesh.switchColor(); mesh.currentColor = RGBColor::Blue(); mesh.addAOB(box, false); Ray3d ray(Vector3dd(-120, -90, -80), Vector3dd(2.0,1.1,1.5)); mesh.currentColor = RGBColor::White(); mesh.addLine(ray.getPoint(0), ray.getPoint(5.0)); double t1, t2; bool result = ray.clip<ConvexPolyhedron> (poly, t1, t2); mesh.currentColor = result ? RGBColor::Green() : RGBColor::Red(); mesh.addLine(ray.getPoint(t1), ray.getPoint(t2)); ofstream file("test.ply", std::ios::out); mesh.dumpPLY(file); file.close(); }
void CubeViewer:: draw_scene(DrawMode _draw_mode) { // clear screen glEnable(GL_DEPTH_TEST); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //glEnable(GL_CULL_FACE); // first bind the shader m_cubeShader.bind(); // set parameters to send to the shader m_cubeShader.setMatrix4x4Uniform("WorldCameraTransform", m_camera.getTransformation().Inverse()); m_cubeShader.setMatrix3x3Uniform("WorldCameraNormalTransform", m_camera.getTransformation().Transpose()); m_cubeShader.setMatrix4x4Uniform("ProjectionMatrix", m_camera.getProjectionMatrix()); for (std::vector<Mesh3D*>::iterator mIt = m_meshes.begin(); mIt != m_meshes.end(); ++mIt) { Mesh3D *cube = *mIt; // besides during we can apply transformations just before rendering: // save the original transformation of the cube Matrix4 originalTransformation = cube->getTransformation(); // rotate the cube before rendering cube->rotateObject(Vector3(0,1,0), M_PI/4); // send the model parameters to the shader m_cubeShader.setMatrix4x4Uniform("ModelWorldTransform", cube->getTransformation() ); m_cubeShader.setMatrix3x3Uniform("ModelWorldNormalTransform", cube->getTransformation().Inverse().Transpose()); // render the cube draw_mesh(cube); // ((( Exercise 3.6 ))) // then reset the original transformation cube->setTransformation( originalTransformation ); } // for illustration render a small sphere at the world center Matrix4 ident; ident.loadIdentity(); m_cubeShader.setMatrix4x4Uniform("ModelWorldTransform", ident ); m_cubeShader.setMatrix3x3Uniform("ModelWorldNormalTransform", ident ); glColor3f(1.0,1.0,1.0); // set sphere color to white glutSolidSphere( 0.05, 10, 10 ); // finally, unbind the shader m_cubeShader.unbind(); }
void CubeViewer:: special(int key, int x, int y) { switch (key) { case GLUT_KEY_UP: for (std::vector<Mesh3D*>::iterator mIt = m_meshes.begin(); mIt != m_meshes.end(); ++mIt) { Mesh3D *cube = *mIt; cube->rotateWorld( Vector3(0,1,0), -10.0 / 180.0 * M_PI ); } break; case GLUT_KEY_DOWN: for (std::vector<Mesh3D*>::iterator mIt = m_meshes.begin(); mIt != m_meshes.end(); ++mIt) { Mesh3D *cube = *mIt; cube->rotateWorld( Vector3(0,1,0), 10.0 / 180.0 * M_PI ); } break; case GLUT_KEY_LEFT: for (std::vector<Mesh3D*>::iterator mIt = m_meshes.begin(); mIt != m_meshes.end(); ++mIt) { Mesh3D *cube = *mIt; cube->rotateObject( Vector3(0,1,0), -10.0 / 180.0 * M_PI ); } break; case GLUT_KEY_RIGHT: for (std::vector<Mesh3D*>::iterator mIt = m_meshes.begin(); mIt != m_meshes.end(); ++mIt) { Mesh3D *cube = *mIt; cube->rotateObject( Vector3(0,1,0), 10.0 / 180.0 * M_PI ); } break; default: TrackballViewer::special(key, x, y); break; } glutPostRedisplay(); }
void SolarViewer::draw_object(Shader& sh, Mesh3D& mesh) { sh.setMatrix4x4Uniform("modelworld", mesh.getTransformation() ); glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_NORMAL_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); glVertexPointer( 3, GL_DOUBLE, 0, mesh.getVertexPointer() ); glNormalPointer( GL_DOUBLE, 0, mesh.getNormalPointer() ); glTexCoordPointer( 2, GL_DOUBLE, 0, mesh.getUvTextureCoordPointer() ); for(unsigned int i = 0; i < mesh.getNumberOfParts(); i++) { glDrawElements( GL_TRIANGLES, mesh.getNumberOfFaces(i)*3, GL_UNSIGNED_INT, mesh.getVertexIndicesPointer(i) ); } glDisableClientState(GL_NORMAL_ARRAY); glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_TEXTURE_COORD_ARRAY); }
void SolarViewer::draw_object(Shader& sh, Mesh3D& mesh, bool showTexture) { sh.setMatrix4x4Uniform("modelworld", mesh.getTransformation() ); glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_NORMAL_ARRAY); if(showTexture) glEnableClientState(GL_TEXTURE_COORD_ARRAY); glVertexPointer( 3, GL_DOUBLE, 0, mesh.getVertexPointer() ); glNormalPointer( GL_DOUBLE, 0, mesh.getNormalPointer() ); if(showTexture) glTexCoordPointer( 2, GL_DOUBLE, 0, mesh.getUvTextureCoordPointer() ); for(unsigned int i = 0; i < mesh.getNumberOfParts(); i++) { sh.setIntUniform("useTexture", showTexture && mesh.getMaterial(i).hasDiffuseTexture()); sh.setVector3Uniform("diffuseColor", mesh.getMaterial(i).m_diffuseColor.x, mesh.getMaterial(i).m_diffuseColor.y, mesh.getMaterial(i).m_diffuseColor.z ); sh.setFloatUniform("specularExp", mesh.getMaterial(i).m_specularExp); if(showTexture && mesh.getMaterial(i).hasDiffuseTexture()) { mesh.getMaterial(i).m_diffuseTexture.bind(); sh.setIntUniform("texture", mesh.getMaterial(i).m_diffuseTexture.getLayer()); } glDrawElements( GL_TRIANGLES, mesh.getNumberOfFaces(i)*3, GL_UNSIGNED_INT, mesh.getVertexIndicesPointer(i) ); if(showTexture && mesh.getMaterial(i).hasDiffuseTexture()) { mesh.getMaterial(i).m_diffuseTexture.unbind(); } } glDisableClientState(GL_NORMAL_ARRAY); glDisableClientState(GL_VERTEX_ARRAY); if(showTexture) glDisableClientState(GL_TEXTURE_COORD_ARRAY); }
Mesh3D* WaterRenderer:: createCube() { // initialize Mesh3D Mesh3D *cube = new Mesh3D(); // setup uniform cube with side length 32 and center of cube being (0,0,0) std::vector< Vector3 > cubeVertices; std::vector< unsigned int > cubeIndices; float d = 16; // front cubeVertices.push_back(Vector3(-d,-d, d)); cubeVertices.push_back(Vector3( d,-d, d)); cubeVertices.push_back(Vector3( d, d, d)); cubeVertices.push_back(Vector3(-d, d, d)); cubeIndices.push_back(0); cubeIndices.push_back(1); cubeIndices.push_back(2); cubeIndices.push_back(0); cubeIndices.push_back(2); cubeIndices.push_back(3); // right cubeVertices.push_back(Vector3( d,-d,-d)); cubeVertices.push_back(Vector3( d,-d, d)); cubeVertices.push_back(Vector3( d, d, d)); cubeVertices.push_back(Vector3( d, d,-d)); cubeIndices.push_back(4); cubeIndices.push_back(5); cubeIndices.push_back(6); cubeIndices.push_back(4); cubeIndices.push_back(6); cubeIndices.push_back(7); // back cubeVertices.push_back(Vector3( d,-d,-d)); cubeVertices.push_back(Vector3(-d,-d,-d)); cubeVertices.push_back(Vector3(-d, d,-d)); cubeVertices.push_back(Vector3( d, d,-d)); cubeIndices.push_back(8); cubeIndices.push_back(9); cubeIndices.push_back(10); cubeIndices.push_back(8); cubeIndices.push_back(10); cubeIndices.push_back(11); // left cubeVertices.push_back(Vector3(-d,-d, d)); cubeVertices.push_back(Vector3(-d,-d,-d)); cubeVertices.push_back(Vector3(-d, d,-d)); cubeVertices.push_back(Vector3(-d, d, d)); cubeIndices.push_back(12); cubeIndices.push_back(13); cubeIndices.push_back(14); cubeIndices.push_back(12); cubeIndices.push_back(14); cubeIndices.push_back(15); // top cubeVertices.push_back(Vector3(-d, d,-d)); cubeVertices.push_back(Vector3( d, d,-d)); cubeVertices.push_back(Vector3( d, d, d)); cubeVertices.push_back(Vector3(-d, d, d)); cubeIndices.push_back(16); cubeIndices.push_back(17); cubeIndices.push_back(18); cubeIndices.push_back(16); cubeIndices.push_back(18); cubeIndices.push_back(19); // bottom cubeVertices.push_back(Vector3( d,-d,-d)); cubeVertices.push_back(Vector3(-d,-d,-d)); cubeVertices.push_back(Vector3(-d,-d, d)); cubeVertices.push_back(Vector3( d,-d, d)); cubeIndices.push_back(20); cubeIndices.push_back(21); cubeIndices.push_back(22); cubeIndices.push_back(20); cubeIndices.push_back(22); cubeIndices.push_back(23); cube->setIndices(cubeIndices); cube->setVertexPositions(cubeVertices); return cube; }
Mesh3D* CubeViewer:: createCube() { // initialize Mesh3D Mesh3D *cube = new Mesh3D(); // setup uniform cube with side length 0.5 and center of cube being (0,0,0) std::vector< Vector3 > cubeVertices; std::vector< Vector3 > cubeNormals; std::vector< Vector3 > cubeColors; std::vector< unsigned int > cubeIndices; float d = 0.25; // front cubeVertices.push_back(Vector3(-d,-d, d)); cubeVertices.push_back(Vector3( d,-d, d)); cubeVertices.push_back(Vector3( d, d, d)); cubeVertices.push_back(Vector3(-d, d, d)); for(int k = 0; k < 4; k++) cubeNormals.push_back(Vector3(0,0,1)); for(int k = 0; k < 4; k++) cubeColors.push_back(Vector3(0.8,0.3,0.3)); cubeIndices.push_back(0); cubeIndices.push_back(1); cubeIndices.push_back(2); cubeIndices.push_back(0); cubeIndices.push_back(2); cubeIndices.push_back(3); // right cubeVertices.push_back(Vector3( d,-d,-d)); cubeVertices.push_back(Vector3( d,-d, d)); cubeVertices.push_back(Vector3( d, d, d)); cubeVertices.push_back(Vector3( d, d,-d)); for(int k = 0; k < 4; k++) cubeNormals.push_back(Vector3(1,0,0)); for(int k = 0; k < 4; k++) cubeColors.push_back(Vector3(0.3,0.8,0.3)); cubeIndices.push_back(4); cubeIndices.push_back(5); cubeIndices.push_back(6); cubeIndices.push_back(4); cubeIndices.push_back(6); cubeIndices.push_back(7); // back cubeVertices.push_back(Vector3( d,-d,-d)); cubeVertices.push_back(Vector3(-d,-d,-d)); cubeVertices.push_back(Vector3(-d, d,-d)); cubeVertices.push_back(Vector3( d, d,-d)); for(int k = 0; k < 4; k++) cubeNormals.push_back(Vector3(0,0,-1)); for(int k = 0; k < 4; k++) cubeColors.push_back(Vector3(0.3,0.3,0.8)); cubeIndices.push_back(8); cubeIndices.push_back(9); cubeIndices.push_back(10); cubeIndices.push_back(8); cubeIndices.push_back(10); cubeIndices.push_back(11); // left cubeVertices.push_back(Vector3(-d,-d, d)); cubeVertices.push_back(Vector3(-d,-d,-d)); cubeVertices.push_back(Vector3(-d, d,-d)); cubeVertices.push_back(Vector3(-d, d, d)); for(int k = 0; k < 4; k++) cubeNormals.push_back(Vector3(-1,0,0)); for(int k = 0; k < 4; k++) cubeColors.push_back(Vector3(0.8,0.8,0.3)); cubeIndices.push_back(12); cubeIndices.push_back(13); cubeIndices.push_back(14); cubeIndices.push_back(12); cubeIndices.push_back(14); cubeIndices.push_back(15); // top cubeVertices.push_back(Vector3(-d, d,-d)); cubeVertices.push_back(Vector3( d, d,-d)); cubeVertices.push_back(Vector3( d, d, d)); cubeVertices.push_back(Vector3(-d, d, d)); for(int k = 0; k < 4; k++) cubeNormals.push_back(Vector3(0,1,0)); for(int k = 0; k < 4; k++) cubeColors.push_back(Vector3(0.8,0.3,0.8)); cubeIndices.push_back(16); cubeIndices.push_back(17); cubeIndices.push_back(18); cubeIndices.push_back(16); cubeIndices.push_back(18); cubeIndices.push_back(19); // bottom cubeVertices.push_back(Vector3( d,-d,-d)); cubeVertices.push_back(Vector3(-d,-d,-d)); cubeVertices.push_back(Vector3(-d,-d, d)); cubeVertices.push_back(Vector3( d,-d, d)); for(int k = 0; k < 4; k++) cubeNormals.push_back(Vector3(0,-1,0)); for(int k = 0; k < 4; k++) cubeColors.push_back(Vector3(0.3,0.8,0.8)); cubeIndices.push_back(20); cubeIndices.push_back(21); cubeIndices.push_back(22); cubeIndices.push_back(20); cubeIndices.push_back(22); cubeIndices.push_back(23); cube->setIndices(cubeIndices); cube->setVertexPositions(cubeVertices); cube->setVertexNormals(cubeNormals); cube->setVertexColors(cubeColors); return cube; }
int main(int argc, char* argv[]) { using namespace jikoLib::GLLib; if(SDL_Init(SDL_INIT_EVERYTHING) < 0) { std::cerr << "Cannot Initialize SDL!: " << SDL_GetError() << std::endl; return -1; } SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5); SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5); SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5); SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); //SDL_Window* window = SDL_CreateWindow("SDL_Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 200, 200, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE); SDL_Window* window = SDL_CreateWindow("SDL_Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 400, 300, SDL_WINDOW_OPENGL); if(window == NULL) { std::cerr << "Window could not be created!: " << SDL_GetError() << std::endl; } SDL_GLContext context; context = SDL_GL_CreateContext(window); obj << Begin(); SDL_GL_SetSwapInterval(1); SDL_GL_MakeCurrent(window, context); VShader vshader; FShader fshader; ShaderProgram program; vshader << vshader_source; fshader << fshader_source; program << vshader << fshader << link_these(); Mesh3D cube; MeshSample::Cube cubeHelper(1.0); cube.copyData(cubeHelper.getVertex(), cubeHelper.getNormal(), cubeHelper.getNumVertex()); Camera camera; camera.setPos(glm::vec3(3.0f, 3.0f, 3.0f)); camera.setDrct(glm::vec3(0.0f, 0.0f, 0.0f)); camera.setFar(100.0f); int width, height; SDL_GetWindowSize(window, &width, &height); camera.setAspect(width, height); obj.connectAttrib(program, cube.getNormal(), cube.getVArray(), "norm"); obj.connectAttrib(program, cube.getVertex(), cube.getVArray(), "vertex"); program.setUniformMatrixXtv("model", glm::value_ptr(cube.getModelMatrix()), 1, 4); program.setUniformMatrixXtv("view", glm::value_ptr(camera.getViewMatrix()), 1, 4); program.setUniformMatrixXtv("projection", glm::value_ptr(camera.getProjectionMatrix()), 1, 4); program.setUniformXt("light.ambient", 0.25f, 0.25f, 0.25f, 1.0f); program.setUniformXt("light.diffuse", 1.0f, 1.0f, 1.0f, 1.0f); program.setUniformXt("light.specular", 1.0f, 1.0f, 1.0f, 1.0f); program.setUniformXt("light.position", 0.0f, 0.6f, 0.0f); program.setUniformXt("material.ambient", 0.3f, 0.25f, 0.4f, 1.0f); program.setUniformXt("material.diffuse", 0.75f, 0.0f, 1.0f, 1.0f); program.setUniformXt("material.specular", 1.0f, 1.0f, 1.0f, 1.0f); program.setUniformXt("material.shininess", 50.0f); bool quit = false; SDL_Event e; // SDL_WaitThread(threadID, NULL); while( !quit ) { //Handle events on queue while( SDL_PollEvent( &e ) != 0 ) { //User requests quit if( e.type == SDL_QUIT ) { quit = true; } } CHECK_GL_ERROR; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glEnable(GL_CULL_FACE); obj.draw(cube, program); SDL_GL_SwapWindow( window ); } // obj << End(); SDL_GL_DeleteContext(context); SDL_DestroyWindow(window); SDL_Quit(); return 0; }