Example #1
0
// loads an object into the scene from a file
void loadObjectFromFile(string filename) {
	// declare current object
	struct ObjectInstance curObject;

	// load object
	SceneLoader sceneLoader;
	int objId = sceneLoader.loadAndAddObject(filename);
	int NumVertices = sceneLoader.objects[objId].faces.size();
	NumVertices = NumVertices * 3;
	cout << NumVertices << endl;
	curObject.points = new point4[NumVertices];
	curObject.normals = new vec4[NumVertices];
	Index = 0;
	for (unsigned int j = 0; j < sceneLoader.objects[objId].faces.size(); j++) {
		myFunc(sceneLoader.objects[objId], sceneLoader.objects[objId].faces[j],
				curObject);
	}

	// Create a vertex array object
	GLuint vao;
	glGenVertexArrays(1, &vao);
	glBindVertexArray(vao);

	// fill in rest of details for object and add to list
	curObject.vaoID = vao;
	curObject.numVertices = NumVertices;
	curObject.selectionR = idcount; ///Really only using red component to store unique id!
	printf("Set red component to %d\n", curObject.selectionR);
	curObject.selectionG = 0;
	curObject.selectionB = 0;
	curObject.selectionA = 255;
	curObject.translate = Translate(0, 0, 0);
	curObject.rotate = RotateX(0);
	curObject.scale = Scale(1, 1, 1);
	myObjects.push_back(curObject);
	counter++;
	idcount++;

	// Create and initialize a buffer object
	GLuint buffer;
	glGenBuffers(1, &buffer);
	glBindBuffer( GL_ARRAY_BUFFER, buffer);
	int pointsSize = NumVertices * sizeof(point4);
	int normalzSize = NumVertices * sizeof(vec4);
	glBufferData( GL_ARRAY_BUFFER, pointsSize + normalzSize,
	NULL, GL_STATIC_DRAW);
	glBufferSubData( GL_ARRAY_BUFFER, 0, pointsSize, curObject.points);
	glBufferSubData( GL_ARRAY_BUFFER, pointsSize, normalzSize,
			curObject.normals);

	// set up vertex arrays
	GLuint vPosition = glGetAttribLocation(mainShaderProgram, "vPosition");
	glEnableVertexAttribArray(vPosition);
	glVertexAttribPointer(vPosition, 4, GL_FLOAT, GL_FALSE, 0,
			BUFFER_OFFSET(0));

	GLuint vNormal = glGetAttribLocation(mainShaderProgram, "vNormal");
	glEnableVertexAttribArray(vNormal);
	glVertexAttribPointer(vNormal, 4, GL_FLOAT, GL_FALSE, 0,
			BUFFER_OFFSET(pointsSize));
	display();
}