void UniformObject::buildFromVector(const std::vector<glm::vec3> & verticesVector, const std::vector<size_t> & indicesVector, std::vector<glm::vec3> & normalsVector,  bool autoNormals, bool smooth){
		
		if(autoNormals) computeNormals(verticesVector, indicesVector, normalsVector);
		if(smooth)smoothNormals(verticesVector, indicesVector, normalsVector);
		
		const unsigned int nbVertices = verticesVector.size() * 4;
		const unsigned int nbIndices = indicesVector.size();
		
		m_IndicesNumber = nbIndices;
		
		GLfloat vertices[nbVertices];
		GLuint indices[nbIndices];
		GLfloat normals[nbVertices];
		
		for(size_t i = 0; i < verticesVector.size(); ++i){
			vertices[4 * i + 0] = verticesVector[i].x;
			vertices[4 * i + 1] = verticesVector[i].y;
			vertices[4 * i + 2] = verticesVector[i].z;
			vertices[4 * i + 3] = 1.0;
			
			normals[4 * i + 0] = normalsVector[i].x;
			normals[4 * i + 1] = normalsVector[i].y;
			normals[4 * i + 2] = normalsVector[i].z;
			normals[4 * i + 3] = 0.0;
		}
		
		for(size_t i = 0; i < indicesVector.size(); ++i){
			indices[i] = indicesVector[i];
		}
		
		//Binding the VAO
		glBindVertexArray(m_VAO);

		//Filling Vertices
		glEnableVertexAttribArray(m_POSITION_ATTRIB_LOCATION);
		glBindBuffer(GL_ARRAY_BUFFER, m_VBOVertices);
		glBufferData(GL_ARRAY_BUFFER, nbVertices * sizeof(GLfloat), vertices, GL_STATIC_DRAW);
		glVertexAttribPointer(m_POSITION_ATTRIB_LOCATION, 4, GL_FLOAT, GL_FALSE, 0, 0);
		
		//Filling normals
		 glEnableVertexAttribArray(m_NORMAL_ATTRIB_LOCATION);
		 glBindBuffer(GL_ARRAY_BUFFER, m_VBONormals);
		 glBufferData(GL_ARRAY_BUFFER, nbVertices * sizeof(GLfloat), normals, GL_STATIC_DRAW);
		 glVertexAttribPointer(m_NORMAL_ATTRIB_LOCATION, 4, GL_FLOAT, GL_FALSE, 0, 0);
		 
		 //Filling indices
		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_VBOIndices);
		glBufferData(GL_ELEMENT_ARRAY_BUFFER, nbIndices*sizeof(GLuint), indices, GL_STATIC_DRAW);
		
		//Unbinding the VAO
		glBindVertexArray(0);
	}
示例#2
0
Terrain::
Terrain(string heightMapName):
	COMPONENTS(3),
	width(0),
	height(0),
	heightMap(heightMapName.c_str()),
	_indexCount(0),
	 vertices(),
	 coords(),
	 normals()
{
	width = heightMap.get_width();
	height = heightMap.get_height();

   const unsigned VERTICES_PER_ROW = width;
   const unsigned VERTICES_PER_COL = height;
	
	readPNG();
	
	for(int i =0; i < 20; i++)
	{
		smoothVertices();
	}
	
   _vertexCount = (GLsizei)VERTICES_PER_ROW * (GLsizei)VERTICES_PER_COL;

   const unsigned VERTEX_COMPONENT_COUNT = 3;
   const unsigned VERTEX_ARRAY_SIZE = (unsigned)_vertexCount *
      VERTEX_COMPONENT_COUNT;
   float* vertices = new float[VERTEX_ARRAY_SIZE];
   normals = new float[VERTEX_ARRAY_SIZE];
	
   float* uv = new float [_vertexCount  * 2];

   const unsigned SQUARES = height * width;
   const unsigned TRIANGLES_PER_SQUARE = 2;
   const unsigned VERTICES_PER_TRIANGLE = 3;
   const unsigned VERTICES_PER_SQUARE = TRIANGLES_PER_SQUARE * 
      VERTICES_PER_TRIANGLE;
   _indexCount = (GLsizei)VERTICES_PER_SQUARE * (GLsizei)SQUARES;

   const unsigned INDEX_ARRAY_SIZE = (unsigned)_indexCount;
   GLuint* indices = new GLuint[INDEX_ARRAY_SIZE];
   GLuint* uvIndices = new GLuint[_indexCount];
   
   cout << "width: " << width << endl;
   cout << "height: " << height << endl; 
   int count = 0;

	
   coords = new float[2760 * 3];
   
   unsigned index = 0;
   unsigned index2 = 0;
   unsigned uvIndex = 0;
   for (unsigned z = 0; z < VERTICES_PER_COL; z++)
   {
      const float Z = (float)z * Z_DELTA;

      for (unsigned x = 0; x < VERTICES_PER_ROW; x++)
      {
         const float X = (float)x * X_DELTA;

	vertices[index++] = X;
	//cout << "Height: " << ((float)heightMap.get_pixel(x, z).red / 255.0f) * 250.0f;;
        // vertices[index++] =  ((float)heightMap.get_pixel(x, z).red / width) * 500.0f;
	vertices[index++] =  heights[x][z];
	vertices[index++] = Z;
	
		if(x < 60 && x > 20 && z > 160 && z < 230 && heights[x][z] > 10.0f)
		{
			count ++;
			cout << "(" << X << "," << heights[x][z] << "," << Z << ")" << endl;
			coords[index2++] = X;
			coords[index2++] = heights[x][z];
			coords[index2++] = Z;
		}
	uv[uvIndex++] = 1.0f/width * x;
	uv[uvIndex++] = 1.0f/height * z;
      }
   }
   cout << "Count: " << count << endl;
 //  int size = (sizeof(vertices)/sizeof(*float));
 //  cout << "Size of vertices: " << size << endl;
   
   index = 0;
   uvIndex = 0;
   for (unsigned z = 0; z < VERTICES_PER_COL - 1; z++)
   {
      for (unsigned x = 0; x < VERTICES_PER_ROW - 1; x++)
      {
         unsigned i = z * VERTICES_PER_COL + x;

         /* Top left square. */
         indices[index++] = i;
         indices[index++] = i + VERTICES_PER_ROW;
         indices[index++] = i + 1;

         /* Bottom right square. */
         indices[index++] = i + 1;
         indices[index++] = i + VERTICES_PER_ROW;
         indices[index++] = i + 1 + VERTICES_PER_ROW;
	      
	      /* Top left square. */
	uvIndices[uvIndex++] = i;
	uvIndices[uvIndex++] = i + VERTICES_PER_ROW;
	uvIndices[uvIndex++] = i + 1;

         /* Bottom right square. */
	uvIndices[uvIndex++] = i + 1;
	uvIndices[uvIndex++] = i + VERTICES_PER_ROW;
	uvIndices[uvIndex++] = i + 1 + VERTICES_PER_ROW;
      }
   }

   smoothNormals(vertices);
   
   bind();

   GLuint buffers[6];
   glGenBuffers(6, buffers);

   glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
   glBufferData(GL_ARRAY_BUFFER, VERTEX_ARRAY_SIZE * (GLsizei)sizeof(float),
         vertices, GL_STATIC_DRAW);

   glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[1]);
   glBufferData(GL_ELEMENT_ARRAY_BUFFER, INDEX_ARRAY_SIZE * (GLsizei)sizeof(GLuint),
         indices, GL_STATIC_DRAW);

   GLuint positionLocation = findAttribute("position");
   glEnableVertexAttribArray(positionLocation);
   glVertexAttribPointer(positionLocation, VERTEX_COMPONENT_COUNT, GL_FLOAT,
         GL_FALSE, 0, 0);
   
   
    glBindBuffer(GL_ARRAY_BUFFER, buffers[2]);
   glBufferData(GL_ARRAY_BUFFER, VERTEX_ARRAY_SIZE * (GLsizei)sizeof(float),
         normals, GL_STATIC_DRAW);

   glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[3]);
   glBufferData(GL_ELEMENT_ARRAY_BUFFER, INDEX_ARRAY_SIZE * (GLsizei)sizeof(GLuint),
         indices, GL_STATIC_DRAW);

   GLuint normalsLocation = findAttribute("normals");
   glEnableVertexAttribArray(normalsLocation);
   glVertexAttribPointer(normalsLocation, VERTEX_COMPONENT_COUNT, GL_FLOAT,
         GL_FALSE, 0, 0);
	 
   glBindBuffer(GL_ARRAY_BUFFER, buffers[4]);
   glBufferData(GL_ARRAY_BUFFER, _vertexCount  * 2 * (GLsizei)sizeof(float),
         uv, GL_STATIC_DRAW);

   glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[5]);
   glBufferData(GL_ELEMENT_ARRAY_BUFFER, INDEX_ARRAY_SIZE * (GLsizei)sizeof(GLuint),
	uvIndices, GL_STATIC_DRAW);

   GLuint uvLocation = findAttribute("uv");
   glEnableVertexAttribArray(uvLocation);
   glVertexAttribPointer(uvLocation, 2, GL_FLOAT,
         GL_FALSE, 0, 0);
}