void ObjObject::initObj(){

	GLuint VBO;
	//GLuint CBO;
	GLuint IBO;

	glGenVertexArrays (1, &vertexArrayObject);
	glBindVertexArray(vertexArrayObject);

	// TODO: read .obj
	ifstream readfile;
	readfile.open(modelName);
	vector<vec3> vertices;
	vector<vec3> normals;
	vector<vec2> tex;
	vector<vec3> faces;

	vector<pntVertexData> v;
	vector<GLuint> indices;

	// the idea to use fscanf came instead of trying to use aoif to convert char* to ints and floats came from opengl-tutorial.org
	FILE* file = fopen(modelName.c_str(), "r");

	if(file == NULL){
		return;
	}

	while(1){
		char header[128];
		int num = fscanf(file,  "%s", header);
		if(num == EOF){
			break;
		}
		
		// if this is a vertex line...
		if ( strcmp( header, "v" ) == 0 ){
			vec3 vertex;
			// read values and store
			fscanf(file, "%f %f %f\n", &vertex.x, &vertex.y, &vertex.z );
			vertices.push_back(vertex);
		}

		// if this is a texture coordinate (UV) line...
		if ( strcmp( header, "vt" ) == 0 ){
			vec2 texCoords;
			fscanf(file, "%f %f\n", &texCoords.x, &texCoords.y);
			tex.push_back(texCoords);
		}

		// if this is a normals line...
		if ( strcmp( header, "vn" ) == 0 ){
			vec3 normal;
			// read values and store
			fscanf(file, "%f %f %f\n", &normal.x, &normal.y, &normal.z );
			normals.push_back(normal);
		}

		// if this is a faces line
		if ( strcmp( header, "f" ) == 0 ){
			vec3 vertex;
			vec3 normal;
			vec3 texture;

			int vert1, vert2, vert3;
			int normal1, normal2, normal3;
			int tex1, tex2, tex3;

			// read values and store
			fscanf(file, "%u/%u/%u %u/%u/%u %u/%u/%u\n", &vert1, &tex1, &normal1, &vert2, &tex2, &normal2, &vert3, &tex3, &normal3);
			//vertices.push_back(vertex);

			v.push_back(pntVertexData(vertices[vert1-1],normals[normal1-1],tex[tex1-1]));
			indices.push_back((GLuint)v.size()-1);
			v.push_back(pntVertexData(vertices[vert2-1],normals[normal2-1],tex[tex2-1]));
			indices.push_back((GLuint)v.size()-1);
			v.push_back(pntVertexData(vertices[vert3-1],normals[normal3-1],tex[tex3-1]));
			indices.push_back((GLuint)v.size()-1);
		}

	}

	
	vector<vec4> c;

	for(int i = 0; i < v.size(); i++){
		c.push_back(vec4(1,1,1,1));
	}
	

	numberOfIndices = indices.size();

	glGenVertexArrays (1, &vertexArrayObject);
	glBindVertexArray( vertexArrayObject );

	// finally, create the buffer to hold interleaved  and bind the data to them
	glGenBuffers(1, &VBO);
	glBindBuffer(GL_ARRAY_BUFFER, VBO); // Buffer for vertex data
	glBufferData(GL_ARRAY_BUFFER, v.size() * sizeof(pntVertexData), &v[0], GL_STATIC_DRAW); //Buffering vertex data

	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(pntVertexData), 0);
	glEnableVertexAttribArray(0);

	glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(pntVertexData), (const GLvoid*)sizeof(vec3) );
	glEnableVertexAttribArray(1);

	glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE,  sizeof(pntVertexData),  (const GLvoid*)(2 * sizeof(vec3)) );
	glEnableVertexAttribArray(3);

	// Generate a buffer for the indices
	glGenBuffers(1, &IBO);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), &indices[0] , GL_STATIC_DRAW);
} // end initialize
示例#2
0
// Construct visual object display list.
void Wall::initialize()
{
	//textureObject = setupTexture( this->textureFileName);
	textureObject = setupTexture( "stone_texture_012_stacked_slate.bmp");//this->textureFileName);

	buildShaderProgram( );

	// vector containers to hold  data
	vector<pntVertexData> v; // vertex positions
	vector<unsigned int> indices; // indices
	GLuint VBO, IBO; // Identifiers for buffer objects

	vec3 n = vec3( 0.0f, 0.0f, 1.0f);

	vec3 v0 = vec3( -width/2, 0.0f, 0.0f); 
	vec3 v1 = vec3( width/2, 0.0f, 0.0f); 
	vec3 v2 = vec3( width/2, height, 0.0f); 
	vec3 v3 = vec3( -width/2, height, 0.0f); 

	vec2 t0 = vec2(0.0f, 0.0f);
	vec2 t1 = vec2(2.0f, 0.0f);
	vec2 t2 = vec2(2.0f, 1.5f);
	vec2 t3 = vec2(0.0f, 1.5f);

	v.push_back( pntVertexData( v0, n, t0) ); // 0
	v.push_back( pntVertexData( v1, n, t1) ); // 1
	v.push_back( pntVertexData( v2, n, t2) ); // 2
	v.push_back( pntVertexData( v3, n, t3) ); // 3

	indices.push_back( 0 );
	indices.push_back( 1 );
	indices.push_back( 2 );
	indices.push_back( 0 );
	indices.push_back( 2 );
	indices.push_back( 3 );

	glGenVertexArrays (1, &vertexArrayObject);
	glBindVertexArray( vertexArrayObject );

	// Create the buffer to hold interleaved data and bind the data to them
	glGenBuffers(1, &VBO);
	glBindBuffer(GL_ARRAY_BUFFER, VBO); // Buffer for vertex data
	glBufferData(GL_ARRAY_BUFFER, v.size() * sizeof(pntVertexData), &v[0], GL_STATIC_DRAW); //Buffering vertex data

	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(pntVertexData), 0);
	glEnableVertexAttribArray(0);

	glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(pntVertexData), (const GLvoid*)sizeof(vec3) );
	glEnableVertexAttribArray(1);

	glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, sizeof(pntVertexData), (const GLvoid*)(2 * sizeof(vec3)) );
	glEnableVertexAttribArray(3);

	// Generate a buffer for the indices
	glGenBuffers(1, &IBO);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), &indices[0] , GL_STATIC_DRAW);

	// store the number of indices for later use
	indiceCount = indices.size();
 
	v.clear();
	indices.clear();

	VisualObject::initialize();

} // end initialize
示例#3
0
void Cylinder::initializeCylinderTop()
{
	float angleStep = 2.0f * M_PI / slices; 

	// vector containers to hold  data
	vector<pntVertexData> v; // vertex positions
	vector<unsigned int> indices; // indices
	GLuint VBO, IBO; // Identifiers for buffer objects

	GLuint currentIndex = 0;

    vec3 normal = vec3(0.0, 0.0, 1.0 );
	vec3 topCtr = vec3(0.0, 0.0,  height );

	float theta = 0.0f;

    for (int j = 0; j <= slices; j++) {

		theta = j * angleStep;
		vec3 v0 = vec3( cos(theta) * radius, sin(theta)*radius, height );
		v.push_back( pntVertexData( v0, normal, getCylindericalTextCoords( theta, v0, height)) ); 
		indices.push_back(currentIndex++);

		theta = (j+1) * angleStep;
		v0 = vec3( cos(theta) * radius, sin(theta)*radius, height );
		v.push_back( pntVertexData( v0, normal, getCylindericalTextCoords( theta, v0, height)) ); 
		indices.push_back(currentIndex++);

		theta = (j) * angleStep + angleStep/2;
		v.push_back( pntVertexData( topCtr, normal, getCylindericalTextCoords( theta, topCtr, height) )); 
		indices.push_back(currentIndex++);
	}

	glGenVertexArrays (1, &vertexArrayObjectForTop);
	glBindVertexArray( vertexArrayObjectForTop);

	// finally, create the buffer to hold interleaved  and bind the data to them
	glGenBuffers(1, &VBO);
	glBindBuffer(GL_ARRAY_BUFFER, VBO); // Buffer for vertex data
	glBufferData(GL_ARRAY_BUFFER, v.size() * sizeof(pntVertexData), &v[0], GL_STATIC_DRAW); //Buffering vertex data

	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(pntVertexData), 0);
	glEnableVertexAttribArray(0);

	glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(pntVertexData), (const GLvoid*)sizeof(vec3) );
	glEnableVertexAttribArray(1);

	glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, sizeof(pntVertexData), (const GLvoid*)(2 * sizeof(vec3)) );
	glEnableVertexAttribArray(3);

	// Generate a buffer for the indices
	glGenBuffers(1, &IBO);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), &indices[0] , GL_STATIC_DRAW);

	// store the number of indices for later use
	topIndicesCount = indices.size();

	v.clear();
	indices.clear();

	VisualObject::initialize();

} // end initializeCylinderTop
示例#4
0
void Cylinder::initializeCylinderBody()
{
    // Step in z and radius as stacks are drawn.
    double z0,z1;
    const double zStep = height / ( ( stacks > 0 ) ? stacks : 1 );
	float angleStep = 2.0f * M_PI / slices;

	// vector containers to hold  data
	vector<pntVertexData> v; // vertex positions
	vector<unsigned int> indices; // indices
	GLuint VBO, IBO; // Identifiers for buffer objects

	// Do the stacks */
	z0 = 0.0;
    z1 = zStep;
	GLuint currentIndex = 0;

	float theta = 0.0f;

    for ( int i=1; i<=stacks; i++)
    {
        if (i==stacks) {
            z1 = height;
		}

        for (int j=0; j < slices; j++ ) {

			theta = j * angleStep;

            vec3 n01 = vec3(cos(theta), sin(theta), 0.0 );     
			vec3 v0 = vec3(cos(theta)*radius, sin(theta)*radius, z0  );
			v.push_back( pntVertexData( v0, n01, getCylindericalTextCoords( theta, v0, height)) ); // 0
			
			vec3 v1 = vec3(cos(theta)*radius, sin(theta)*radius, z1  );
			v.push_back( pntVertexData( v1, n01, getCylindericalTextCoords( theta, v1, height )) ); // 1

			theta = (j+1)* angleStep;
			vec3 n23 = vec3(cos(theta), sin(theta), 0.0 );

			vec3 v2 = vec3(cos(theta)*radius, sin(theta)*radius, z0  );
			v.push_back( pntVertexData( v2, n23, getCylindericalTextCoords( theta, v2, height ) ) ); // 2

            vec3 v3 = vec3(cos(theta)*radius, sin(theta)*radius, z1  );
			v.push_back( pntVertexData( v3, n23, getCylindericalTextCoords( theta, v3, height ) ) ); // 3

			indices.push_back(currentIndex);
			indices.push_back(currentIndex + 2);
			indices.push_back(currentIndex + 3);

			indices.push_back(currentIndex);
			indices.push_back(currentIndex + 3);
			indices.push_back(currentIndex + 1);

			currentIndex += 4;
        }

        z0 = z1; 
		z1 += zStep;
    }

	glGenVertexArrays (1, &vertexArrayObject);
	glBindVertexArray( vertexArrayObject );

	// finally, create the buffer to hold interleaved  and bind the data to them
	glGenBuffers(1, &VBO);
	glBindBuffer(GL_ARRAY_BUFFER, VBO); // Buffer for vertex data
	glBufferData(GL_ARRAY_BUFFER, v.size() * sizeof(pntVertexData), &v[0], GL_STATIC_DRAW); //Buffering vertex data

	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(pntVertexData), 0);
	glEnableVertexAttribArray(0);

	glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(pntVertexData), (const GLvoid*)sizeof(vec3) );
	glEnableVertexAttribArray(1);

	glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, sizeof(pntVertexData), (const GLvoid*)(2 * sizeof(vec3)) );
	glEnableVertexAttribArray(3);

	// Generate a buffer for the indices
	glGenBuffers(1, &IBO);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), &indices[0] , GL_STATIC_DRAW);

	// store the number of indices for later use
	bodyIndicesCount = indices.size();
 
	v.clear();
	indices.clear();

} // end initializeCylinderBody
示例#5
0
文件: Cube.cpp 项目: loomisdf/cse-387
void Cube::initialize()
{
	// Buffer Vertex Data for a Color cube
	GLfloat hW = 1 / 2.0f;
	GLfloat hH = 1 / 2.0f;
	GLfloat hD = 1 / 2.0f;

	vector<pntVertexData> v;

	// Vertex Positions
	glm::vec4 v0(-hW, -hH, hD, 1.0f);
	glm::vec4 v1(-hW, -hH, -hD, 1.0f);
	glm::vec4 v2(hW, -hH, -hD, 1.0f);
	glm::vec4 v3(hW, -hH, hD, 1.0f);
	glm::vec4 v4(-hW, hH, hD, 1.0f);
	glm::vec4 v5(-hW, hH, -hD, 1.0f);
	glm::vec4 v6(hW, hH, -hD, 1.0f);
	glm::vec4 v7(hW, hH, hD, 1.0f);

	// Create interleaved position and normal data
	glm::vec3 n = glm::vec3(-1, 0, 0);
	v.push_back(pntVertexData(v0, n, glm::vec2(0.0f, 0.0f)));
	v.push_back(pntVertexData(v4, n, glm::vec2(0.0f, 1.0f)));
	v.push_back(pntVertexData(v1, n, glm::vec2(1.0f, 0.0f)));

	v.push_back(pntVertexData(v1, n, glm::vec2(1.0f, 0.0f)));
	v.push_back(pntVertexData(v4, n, glm::vec2(0.0f, 1.0f)));
	v.push_back(pntVertexData(v5, n, glm::vec2(1.0f, 1.0f)));

	n = glm::vec3(1, 0, 0);
	v.push_back(pntVertexData(v3, n, glm::vec2(0.0f, 0.0f)));
	v.push_back(pntVertexData(v2, n, glm::vec2(1.0f, 0.0f)));
	v.push_back(pntVertexData(v6, n, glm::vec2(1.0f, 1.0f)));

	v.push_back(pntVertexData(v3, n, glm::vec2(0.0f, 0.0f)));
	v.push_back(pntVertexData(v6, n, glm::vec2(1.0f, 1.0f)));
	v.push_back(pntVertexData(v7, n, glm::vec2(0.0f, 1.0f)));

	n = glm::vec3(0, 0, 1);
	v.push_back(pntVertexData(v0, n, glm::vec2(0.0f, 1.0f)));
	v.push_back(pntVertexData(v3, n, glm::vec2(1.0f, 1.0f)));
	v.push_back(pntVertexData(v7, n, glm::vec2(1.0f, 0.0f)));

	v.push_back(pntVertexData(v0, n, glm::vec2(0.0f, 1.0f)));
	v.push_back(pntVertexData(v7, n, glm::vec2(1.0f, 0.0f)));
	v.push_back(pntVertexData(v4, n, glm::vec2(0.0f, 0.0f)));

	n = glm::vec3(0, 0, -1);
	v.push_back(pntVertexData(v1, n, glm::vec2(1.0f, 0.0f)));
	v.push_back(pntVertexData(v5, n, glm::vec2(1.0f, 1.0f)));
	v.push_back(pntVertexData(v2, n, glm::vec2(0.0f, 0.0f)));

	v.push_back(pntVertexData(v2, n, glm::vec2(0.0f, 0.0f)));
	v.push_back(pntVertexData(v5, n, glm::vec2(1.0f, 1.0f)));
	v.push_back(pntVertexData(v6, n, glm::vec2(0.0f, 1.0f)));

	n = glm::vec3(0, 1, 0);
	v.push_back(pntVertexData(v4, n, glm::vec2(0.0f, 0.0f)));
	v.push_back(pntVertexData(v7, n, glm::vec2(2.0f, 0.0f)));
	v.push_back(pntVertexData(v6, n, glm::vec2(2.0f, 2.0f)));

	v.push_back(pntVertexData(v4, n, glm::vec2(0.0f, 0.0f)));
	v.push_back(pntVertexData(v6, n, glm::vec2(2.0f, 2.0f)));
	v.push_back(pntVertexData(v5, n, glm::vec2(0.0f, 2.0f)));

	n = glm::vec3(0, -1, 0);
	v.push_back(pntVertexData(v0, n, glm::vec2(0.0f, 1.0f)));
	v.push_back(pntVertexData(v2, n, glm::vec2(2.0f, 0.0f)));
	v.push_back(pntVertexData(v3, n, glm::vec2(2.0f, 1.0f)));

	v.push_back(pntVertexData(v0, n, glm::vec2(0.0f, 1.0f)));
	v.push_back(pntVertexData(v1, n, glm::vec2(0.0f, 0.0f)));
	v.push_back(pntVertexData(v2, n, glm::vec2(2.0f, 0.0f)));

	glUseProgram(shaderProgram);


	material = Material(glGetUniformLocation(shaderProgram, "object.ambientMat"),
						glGetUniformLocation(shaderProgram, "object.diffuseMat"),
						glGetUniformLocation(shaderProgram, "object.specularMat"),
						glGetUniformLocation(shaderProgram, "object.specularExp"),
						glGetUniformLocation(shaderProgram, "object.emmissiveMat"),
						glGetUniformLocation(shaderProgram, "object.textureMode"));

	// Generate vertex array object and bind it for the first time
	glGenVertexArrays(1, &vertexArrayObject);
	glBindVertexArray(vertexArrayObject);

	GLuint VBO;

	glGenBuffers(1, &VBO);
	glBindBuffer(GL_ARRAY_BUFFER, VBO);

	glBufferData(GL_ARRAY_BUFFER, v.size() * sizeof(pntVertexData), &v[0], GL_STATIC_DRAW);

	glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(pntVertexData), 0);
	glEnableVertexAttribArray(0);

	glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(pntVertexData), (const GLvoid*)sizeof(glm::vec4));
	glEnableVertexAttribArray(1);

	glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(pntVertexData), (const GLvoid*)(sizeof(glm::vec3)+sizeof(glm::vec4)));
	glEnableVertexAttribArray(2);

	numberOfIndices = v.size();
	v.clear();

	// Set the modeling transformation
	glm::mat4 modelingTransformation = glm::translate(glm::vec3(0.0, 0.0f, 0.0f)) * glm::rotate(M_PI / 4.0f, glm::vec3(0.0f, 1.0f, 0.0f));
	GLuint modelLocation = glGetUniformLocation(shaderProgram, "modelMatrix");
	assert(modelLocation != 0xFFFFFFFF);
	glUniformMatrix4fv(modelLocation, 1, GL_FALSE, glm::value_ptr(modelingTransformation));
	
}
void Cube::initialize()
{
	GLfloat hW = width/2.0f;
	GLfloat hH = height/2.0f;
	GLfloat hD = depth/2.0f;

	setShaderValues();

	vector<pntVertexData> v;

	vec3 v0 = vec3( -hW, -hH, hD);
	vec3 v1 =  vec3( -hW, -hH, -hD);
	vec3 v2 = vec3( hW, -hH, -hD);
	vec3 v3 = vec3( hW, -hH, hD);
	vec3 v4 = vec3( -hW, hH, hD);
	vec3 v5 = vec3( -hW, hH, -hD);
	vec3 v6 = vec3( hW, hH, -hD);
	vec3 v7 = vec3(hW, hH, hD);

	vec3 normal;

	vec2 t0 = vec2(0.0f, 0.0f);
	vec2 t1 = vec2(1.0f, 0.0f);
	vec2 t2 = vec2(1.0f, 1.0f);
	vec2 t3 = vec2(0.0f, 1.0f);

	// 0 4 1
	// 1 4 5
	// 3 2 6
	// 3 6 7
	// 0 3 7
	// 0 7 4
	// 1 5 2
	// 2 5 6
	// 4 7 5
	// 4 6 5
	// 0 2 3
	// 0 1 2

	normal = findUnitNormal(v0, v4, v1);
	v.push_back(pntVertexData(v0, normal, t1));
	v.push_back(pntVertexData(v4, normal, t2));
	v.push_back(pntVertexData(v1, normal, t0));

	normal = findUnitNormal(v1, v4, v5);
	v.push_back(pntVertexData(v1, normal, t0));
	v.push_back(pntVertexData(v4, normal, t2));
	v.push_back(pntVertexData(v5, normal, t3));

	normal = findUnitNormal(v3, v2, v6);
	v.push_back(pntVertexData(v3, normal, t0));
	v.push_back(pntVertexData(v2, normal, t1));
	v.push_back(pntVertexData(v6, normal, t2));

	normal = findUnitNormal(v3, v6, v7);
	v.push_back(pntVertexData(v3, normal, t0));
	v.push_back(pntVertexData(v6, normal, t2));
	v.push_back(pntVertexData(v7, normal, t3));

	normal = findUnitNormal(v0, v3, v7);
	v.push_back(pntVertexData(v0, normal, t0));
	v.push_back(pntVertexData(v3, normal, t1));
	v.push_back(pntVertexData(v7, normal, t2));

	normal = findUnitNormal(v0, v7, v4);
	v.push_back(pntVertexData(v0, normal, t0));
	v.push_back(pntVertexData(v7, normal, t2));
	v.push_back(pntVertexData(v4, normal, t3));

	normal = findUnitNormal(v1, v5, v2);
	v.push_back(pntVertexData(v1, normal, t0));
	v.push_back(pntVertexData(v5, normal, t3));
	v.push_back(pntVertexData(v2, normal, t1));

	normal = findUnitNormal(v2, v5, v6);
	v.push_back(pntVertexData(v2, normal, t1));
	v.push_back(pntVertexData(v5, normal, t3));
	v.push_back(pntVertexData(v6, normal, t2));

	normal = findUnitNormal(v4, v7, v6);
	v.push_back(pntVertexData(v4, normal, t0));
	v.push_back(pntVertexData(v7, normal, t1));
	v.push_back(pntVertexData(v6, normal, t2));

	normal = findUnitNormal(v4, v6, v5);
	v.push_back(pntVertexData(v4, normal, t0));
	v.push_back(pntVertexData(v6, normal, t2));
	v.push_back(pntVertexData(v5, normal, t3));

	normal = findUnitNormal(v0, v2, v3);
	v.push_back(pntVertexData(v0, normal, t3));
	v.push_back(pntVertexData(v2, normal, t1));
	v.push_back(pntVertexData(v3, normal, t2));

	normal = findUnitNormal(v0, v1, v2);
	v.push_back(pntVertexData(v0, normal, t3));
	v.push_back(pntVertexData(v1, normal, t0));
	v.push_back(pntVertexData(v2, normal, t1));

	GLuint VBO;

	glGenVertexArrays (1, &vertexArrayObject); 
	glBindVertexArray( vertexArrayObject );

	glGenBuffers(1, &VBO);
	glBindBuffer(GL_ARRAY_BUFFER, VBO);
	glBufferData(GL_ARRAY_BUFFER, v.size() * sizeof(pntVertexData), &v[0], GL_STATIC_DRAW);

	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(pntVertexData), 0);
	glEnableVertexAttribArray(0);

	glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(pntVertexData), (const GLvoid*) sizeof(vec3));
	glEnableVertexAttribArray(1);

	glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, sizeof(pntVertexData), (const GLvoid*)( sizeof(vec3) * 2));
	glEnableVertexAttribArray(2);

	glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, sizeof(pntVertexData), (const GLvoid*)(2 * sizeof(vec3)) );
	glEnableVertexAttribArray(3);

	numberOfIndices = v.size();
	v.clear();

	VisualObject::initialize();

} // end initialize
示例#7
0
void Cone::initializeConeBody()
{
    // Step in z and radius as stacks are drawn.
    double z0,z1;
	double r0, r1;

	const float angleStep = 2.0f * M_PI / slices;
	const float zStep = height / ( ( stacks > 0 ) ? stacks : 1 );
    const float rStep = base / ( ( stacks > 0 ) ? stacks : 1 );

	// Scaling factors for vertex normals 
    const float cosn = ( height / sqrt ( height * height + base * base ));
    const float sinn = ( base   / sqrt ( height * height + base * base ));

	// vector containers to hold  data
	vector<pntVertexData> v; // vertex positions
	vector<unsigned int> indices; // indices
	GLuint VBO, IBO; // Identifiers for buffer objects

	// Do the stacks
	z0 = 0.0;
    z1 = zStep;

    r0 = base;
    r1 = r0 - rStep;

	GLuint currentIndex = 0;

	float theta = 0.0f;

	for( int i = 0; i < stacks-1; i++ )
    {
        for( int j=0; j < slices; j++)
        {
			theta = j* angleStep;

            vec3 n01 = vec3(cos(theta) * cosn, sin(theta)*cosn, sinn);

            vec3 v0 = vec3(cos(theta) * r0,   sin(theta)*r0,   z0  );
			v.push_back( pntVertexData( v0, n01, getPlanarTextCoords( v0, base, height  )) ); // 0
            
			vec3 v1 = vec3(cos(theta) * r1,   sin(theta)*r1,   z1  );
			v.push_back( pntVertexData( v1, n01, getPlanarTextCoords( v1, base, height )) ); // 1

			theta = (j+1)* angleStep;
			vec3 n23 = vec3(cos(theta) * cosn, sin(theta) * cosn, sinn);

            vec3 v2 = vec3(cos(theta) * r0,   sin(theta) * r0,   z0  );
			v.push_back( pntVertexData( v2, n23, getPlanarTextCoords( v2, base, height )) ); // 2
           
			vec3 v3 = vec3(cos(theta) * r1,   sin(theta) * r1,  z1  );
			v.push_back( pntVertexData( v3, n23, getPlanarTextCoords( v3, base, height )) ); // 3

			indices.push_back(currentIndex);
			indices.push_back(currentIndex + 2);
			indices.push_back(currentIndex + 3);

			indices.push_back(currentIndex);
			indices.push_back(currentIndex + 3);
			indices.push_back(currentIndex + 1);

			currentIndex += 4;
        }

        z0 = z1; 
		z1 += zStep;
        r0 = r1; 
		r1 -= rStep;
    }

	vec3 n = vec3(cos(0.0)*cosn, sin(0.0)*cosn, sinn);


	for (int j=0; j < slices; j++)
	{
		theta = j* angleStep;

		vec3 v0 = vec3( cos(theta) * r0, sin(theta) * r0, z0 );
		v.push_back( pntVertexData( v0, n, getPlanarTextCoords( v0, base, height )) ); // 0
		
		vec3 v1 = vec3( 0, 0, height);
		v.push_back( pntVertexData( v1, n, getPlanarTextCoords( v1, base, height )) ); // 1

		theta = (j+1)* angleStep;

		n = vec3 ( cos(theta) * cosn, sin(theta) * cosn, sinn );
		vec3 v2 = vec3( cos(theta) * r0, sin(theta)*r0, z0 );
		v.push_back( pntVertexData( v2, n, getPlanarTextCoords( v2, base, height )) ); // 2

		indices.push_back(currentIndex);
		indices.push_back(currentIndex + 2);
		indices.push_back(currentIndex + 1);
		currentIndex += 3;
	}

	glGenVertexArrays (1, &vertexArrayObject);
	glBindVertexArray( vertexArrayObject );

	// Create the buffer to hold interleaved data and bind the data to them
	glGenBuffers(1, &VBO);
	glBindBuffer(GL_ARRAY_BUFFER, VBO); // Buffer for vertex data
	glBufferData(GL_ARRAY_BUFFER, v.size() * sizeof(pntVertexData), &v[0], GL_STATIC_DRAW); //Buffering vertex data

	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(pntVertexData), 0);
	glEnableVertexAttribArray(0);

	glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(pntVertexData), (const GLvoid*)sizeof(vec3) );
	glEnableVertexAttribArray(1);

	glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, sizeof(pntVertexData), (const GLvoid*)(2 * sizeof(vec3)) );
	glEnableVertexAttribArray(3);

	// Generate a buffer for the indices
	glGenBuffers(1, &IBO);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), &indices[0] , GL_STATIC_DRAW);

	// store the number of indices for later use
	bodyIndicesCount = indices.size();
 
	v.clear();
	indices.clear();

} // end
示例#8
0
void Cone::initializeConeBottom()
{
	float angleStep = 2.0f * M_PI / slices;

	// vector containers to hold  data
	vector<pntVertexData> v; // vertex positions
	vector<unsigned int> indices; // indices
	GLuint VBO, IBO; // Identifiers for buffer objects
	
	GLuint currentIndex = 0;

    vec3 n = vec3(0.0f,0.0f,-1.0f);
	vec3 ctr = vec3( 0.0f, 0.0f, 0.0f );

	float theta = 0.0f;

    for (int j = slices; j >= 0; j--) {			

		theta = j * angleStep;
		vec3 v0 = vec3( cos( theta ) * base, sin( theta ) * base, 0.0f);
		v.push_back( pntVertexData( v0, n, getPlanarTextCoords( v0, base, height )));
		indices.push_back(currentIndex++);

		theta = (j-1)* angleStep;

		vec3 v1 = vec3( cos( theta ) * base, sin( theta ) * base, 0.0f);
		v.push_back( pntVertexData( v1, n, getPlanarTextCoords( v1, base, height )));
		indices.push_back(currentIndex++);

		v.push_back( pntVertexData( ctr, n, getPlanarTextCoords( ctr, base, height )));
		indices.push_back(currentIndex++);
	}

	glGenVertexArrays (1, &vertexArrayObjectForBottom);
	glBindVertexArray( vertexArrayObjectForBottom );

	// Create the buffer to hold interleaved data and bind the data to them
	glGenBuffers(1, &VBO);
	glBindBuffer(GL_ARRAY_BUFFER, VBO); // Buffer for vertex data
	glBufferData(GL_ARRAY_BUFFER, v.size() * sizeof(pntVertexData), &v[0], GL_STATIC_DRAW); //Buffering vertex data

	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(pntVertexData), 0);
	glEnableVertexAttribArray(0);

	glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(pntVertexData), (const GLvoid*)sizeof(vec3) );
	glEnableVertexAttribArray(1);

	glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, sizeof(pntVertexData), (const GLvoid*)(2 * sizeof(vec3)) );
	glEnableVertexAttribArray(3);

	// Generate a buffer for the indices
	glGenBuffers(1, &IBO);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), &indices[0] , GL_STATIC_DRAW);

	// store the number of indices for later use
	bottomIndicesCount = indices.size();
 
	v.clear();
	indices.clear();

} // end initializeConeBottom
示例#9
0
void Box::initTopBottomBack(){

	GLuint VBO;
	GLuint CBO;
	GLuint IBO;

	glGenVertexArrays (1, &vertexArrayObjectFBTB);
	glBindVertexArray(vertexArrayObjectFBTB);

	vec3 v0 = vec3(-width/2, width/2, width/2);
	vec3 v1 = vec3(width/2, width/2, width/2);
	vec3 v2 = vec3(-width/2, -width/2, width/2);
	vec3 v3 = vec3(width/2, -width/2, width/2);
	
	vec3 v4 = vec3(-width/2, width/2, -width/2);
	vec3 v5 = vec3(width/2, width/2, -width/2);
	vec3 v6 = vec3(-width/2, -width/2, -width/2);
	vec3 v7 = vec3(width/2, -width/2, -width/2);

	vector<pntVertexData> v;

	vec2 t0 = vec2(0,1);
	vec2 t1 = vec2(1,1);
	vec2 t2 = vec2(0,0);
	vec2 t3 = vec2(1,0);

	vec2 t6 = vec2(0,1);
	vec2 t7 = vec2(1,1);
	vec2 t4 = vec2(0,0);
	vec2 t5 = vec2(1,0);

	v.push_back(pntVertexData(v0, normalize(vec3(-1,1,1)), t0));
	v.push_back(pntVertexData(v1, normalize(vec3(1,1,1)), t1));
	v.push_back(pntVertexData(v2, normalize(vec3(-1,-1,1)), t2));
	v.push_back(pntVertexData(v3, normalize(vec3(1,-1,1)), t3));
	
	v.push_back(pntVertexData(v4, normalize(vec3(-1,1,-1)), t4));
	v.push_back(pntVertexData(v5, normalize(vec3(1,1,-1)), t5));
	v.push_back(pntVertexData(v6, normalize(vec3(-1,-1,-1)), t6));
	v.push_back(pntVertexData(v7, normalize(vec3(1,-1,-1)), t7));

	/*
	glGenBuffers(1, &VBO);
	glBindBuffer(GL_ARRAY_BUFFER, VBO);
	glBufferData(GL_ARRAY_BUFFER, v.size() * sizeof(vec3), &v[0], GL_STATIC_DRAW);
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
	glEnableVertexAttribArray(0);
	*/
	vector<vec4> c;

	vec4 c1 = vec4(1.0f, 0.0f, 0.0f, 1.0f);
	vec4 c2 = vec4(0.0f, 1.0f, 0.0f, 1.0f);
	vec4 c3 = vec4(0.0f, 0.0f, 1.0f, 1.0f);
	vec4 c4 = vec4(1.0f, 1.0f, 0.0f, 1.0f);
	vec4 c5 = vec4(1.0f, 0.0f, 1.0f, 1.0f);

	c.push_back(c1);
	c.push_back(c2);
	c.push_back(c3);
	c.push_back(c4);
	c.push_back(c5);
	c.push_back(c1);
	c.push_back(c2);
	c.push_back(c3);
	/*
	glGenBuffers(1, &CBO);
	glBindBuffer(GL_ARRAY_BUFFER, CBO);
	glBufferData(GL_ARRAY_BUFFER, c.size() * sizeof(vec4), &c[0], GL_STATIC_DRAW);
	glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, 0, 0);
	glEnableVertexAttribArray(2);
	*/
	vector<GLuint> indices;

	// front
	indices.push_back(0);
	indices.push_back(2);
	indices.push_back(1);

	indices.push_back(1);
	indices.push_back(2);
	indices.push_back(3);

	// back
	indices.push_back(6);
	indices.push_back(4);
	indices.push_back(5);

	indices.push_back(5);
	indices.push_back(7);
	indices.push_back(6);

	// top
	indices.push_back(1);
	indices.push_back(5);
	indices.push_back(0);

	indices.push_back(0);
	indices.push_back(5);
	indices.push_back(4);

	// bottom
	indices.push_back(7);
	indices.push_back(3);
	indices.push_back(6);

	indices.push_back(6);
	indices.push_back(3);
	indices.push_back(2);

	numberOfIndices = indices.size();

	/*
	glGenBuffers(1, &IBO);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
	glBufferData( GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLuint), &indices[0], GL_STATIC_DRAW);
	*/

	glGenVertexArrays (1, &vertexArrayObjectFBTB);
	glBindVertexArray( vertexArrayObjectFBTB );

	// finally, create the buffer to hold interleaved  and bind the data to them
	glGenBuffers(1, &VBO);
	glBindBuffer(GL_ARRAY_BUFFER, VBO); // Buffer for vertex data
	glBufferData(GL_ARRAY_BUFFER, v.size() * sizeof(pntVertexData), &v[0], GL_STATIC_DRAW); //Buffering vertex data

	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(pntVertexData), 0);
	glEnableVertexAttribArray(0);

	glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(pntVertexData), (const GLvoid*)sizeof(vec3) );
	glEnableVertexAttribArray(1);

	glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE,  sizeof(pntVertexData),  (const GLvoid*)(2 * sizeof(vec3)) );
	glEnableVertexAttribArray(3);

	// Generate a buffer for the indices
	glGenBuffers(1, &IBO);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), &indices[0] , GL_STATIC_DRAW);
} // end initialize