int DrawGLScene(GLvoid) { const float hxy = 0.01; const float hz = 0.15; const float dt = 1e-2; const int hairLenght = 30; const int gridX = 128; const int gridY = 128; static ObjLoader loader("cudada.obj"); static HairSimulation simu(0., 0., 0., 3.0, gridX, gridY, hairLenght, hxy, hz); static bool init = false; if(!init) simu.initHair(); init = true; simu.integrate(dt); float maxZ = hairLenght * hz; std::vector<float> X = simu.getMassPositionX(); std::vector<float> Y = simu.getMassPositionY(); std::vector<float> Z = simu.getMassPositionZ(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glTranslatef(0.f,0.f,-32.0f); glRotatef(rtri,0.0f,1.0f,0.0f); //glRotatef(45,0.0f,1.0f,0.0f); //{ // GLfloat mat_ambient[] = { 1.0, 1.0, 0.0, 1.0 }; // GLfloat mat_specular[] = { 0.2, 0.15, 0.15, 1.0 }; // glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); // glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient); //} for(int i = 0 ; i < gridX ; ++i) for(int j = 0 ; j < gridY ; ++j) { if(i%8 == 0) if(j % 8 == 0) { glBegin(GL_LINE_STRIP); for(int z = 0 ; z < hairLenght ; ++z) { int idx = z * gridX * gridY + j * gridX + i; glColor3f(0.9, 0.9, 0.7); glVertex3f( X[idx], Y[idx], Z[idx]); //glNormal3f( 0., 0., 1.0); } glEnd(); } } std::vector<float>& vertices = loader.getVertices(); std::vector<float>& normals = loader. getVerticesNormal(); std::vector<float>& triangleNormals = loader. getTrianglesNormal(); std::vector<int>& triangles = loader. getTriangles(); glRotatef(90,1.0f,0.0f,0.0f); //glTranslatef(0.f,0.f,-32.0f); //glRotatef(90,0.0f,1.0f,0.0f); //glRotatef(90,1.0f,0.0f,0.0f); //glRotatef(180,0.0f,1.0f,0.0f); //glRotatef(-90,1.0f,0.0f,0.0f); //glRotatef(45,0.0f,0.0f,1.0f); { GLfloat mat_ambient[] = { 1.0, 0.0, 0.0, 1.0 }; GLfloat mat_specular[] = { 0.2, 0.15, 0.15, 1.0 }; GLfloat mat_emissive[] = { 1.0, 0.0, 0.0, 1.0 }; glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient); glMaterialfv(GL_FRONT, GL_EMISSION, mat_emissive); } glBegin(GL_TRIANGLES); for(int i = 0 ; i < triangles.size()/3 ; i++) { float Ax = vertices[3*triangles[3*i] + 0]; float Ay = vertices[3*triangles[3*i] + 1]; float Az = vertices[3*triangles[3*i] + 2]; float Bx = vertices[3*triangles[3*i+1] + 0]; float By = vertices[3*triangles[3*i+1] + 1]; float Bz = vertices[3*triangles[3*i+1] + 2]; float Cx = vertices[3*triangles[3*i+2] + 0]; float Cy = vertices[3*triangles[3*i+2] + 1]; float Cz = vertices[3*triangles[3*i+2] + 2]; float Anx = normals[3*triangles[3*i] + 0]; float Any = normals[3*triangles[3*i] + 1]; float Anz = normals[3*triangles[3*i] + 2]; float Bnx = normals[3*triangles[3*i+1] + 0]; float Bny = normals[3*triangles[3*i+1] + 1]; float Bnz = normals[3*triangles[3*i+1] + 2]; float Cnx = normals[3*triangles[3*i+2] + 0]; float Cny = normals[3*triangles[3*i+2] + 1]; float Cnz = normals[3*triangles[3*i+2] + 2]; glNormal3f( Anx, Any, Anz); glVertex3f( Ax, Ay, Az); glColor3f(1.0, 0.0, 0.0); glNormal3f( Bnx, Bny, Bnz); glVertex3f( Bx, By, Bz); glColor3f(1.0, 0.0, 0.0); glNormal3f( Cnx, Cny, Cnz); glVertex3f( Cx, Cy, Cz); glColor3f(1.0, 0.0, 0.0); } glEnd(); rtri+=0.2f; return TRUE; }
Track::Track(GLint givenVertexBufferLoc, GLint givenNormalBufferLoc) { setLoader(new ObjLoader()); ObjLoader * myLoaderRef = getLoader(); myLoaderRef -> loadObj(TRACK_PATH, MTL_BASEPATH); // In this OpenGL program, x-z is the horizontal plane. // The following code snippet grabs the x and z coordinates of all the // vertices that are at the bottom of the inner walls of the track. //std::cout << "ListPlot[ { " << std::endl; std::vector<GLfloat> vertices = myLoaderRef->getVertices(); for (int i = 0; i < vertices.size(); i += 3) { if (vertices[i + 1] > 1) continue; //if (i) std::cout << ", "; //std::cout << "{" << vertices[i] << ", " << vertices[i + 2] << "}"; walls.push_back(glm::vec2(vertices[i], vertices[i+2])); } //std::cout << " } ]" << std::endl; glGenBuffers(1, &vertexBuffer); glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer); glBufferData(GL_ARRAY_BUFFER, myLoaderRef -> getVertices().size() * sizeof(GLfloat), myLoaderRef -> getVertices().data(), GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); glGenBuffers(1, &normalBuffer); glBindBuffer(GL_ARRAY_BUFFER, normalBuffer); glBufferData(GL_ARRAY_BUFFER, myLoaderRef -> getNormals().size() * sizeof(GLfloat), myLoaderRef -> getNormals().data(), GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); glGenBuffers(1, &indexBuffer); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer); glBufferData(GL_ELEMENT_ARRAY_BUFFER, myLoaderRef -> getIndices().size() * sizeof(GLuint), myLoaderRef -> getIndices().data(), GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); vertexCount = (unsigned int) myLoaderRef -> getIndices().size(); // setup initial model matrix scaleMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, -1.0f, 0.0f)) * glm::mat4(1.0f); setModelMatrix(scaleMatrix); // wrap states using vao glGenVertexArraysAPPLE(1, &vertexArrayObjectHandle); glBindVertexArrayAPPLE(vertexArrayObjectHandle); // bind vertex array glEnableVertexAttribArray(givenVertexBufferLoc); glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer); glVertexAttribPointer(givenVertexBufferLoc, 3, // to simplify program, we always use triangles GL_FLOAT, // type of elements in vertex buffer is GLfloat GL_FALSE, // not normalized 0, // to simplify program, we keep each object in a homogeneous buffer 0); glEnableVertexAttribArray(givenNormalBufferLoc); glBindBuffer(GL_ARRAY_BUFFER, normalBuffer); glVertexAttribPointer(givenNormalBufferLoc, 3, GL_FLOAT, GL_FALSE, 0, 0); // bind index array glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer); // finished: unbind vao to clear state glBindVertexArrayAPPLE(0); }