Esempio n. 1
0
void storeData() {
	indicies = new GLuint[getIndiciesSize()];
	objData = new GLfloat[5000000];
	readObj();
	getFinalSize();
	//std::cout << getFinalSize() << std::endl;


	//isize = 34695; //boat
	//isize = 22338; //plane
	//isize = 2901; //monkey
	//isize = 36;

	//for (int i = 0; i < 100; i++) std::cout << indicies[i];

	for (int i = 0; i < isize; i++) {

		finalData[i * 8] = objData[(indicies[i]) * 3];
		finalData[i * 8 + 1] = objData[(indicies[i]) * 3 + 1]; //multiply shit by model matrix. stupid model matrix.
		finalData[i * 8 + 2] = objData[(indicies[i]) * 3 + 2];
	}
	for (int i = 0; i < vsize; i += 8) {
		finalData[i + 6] = textureCoords[tc++];
		finalData[i + 7] = textureCoords[tc++];

		if (i % 24 == 0) { //problem here, if last 2 triangles dont divide into 3 triangles, duh stupid.
			glm::vec3 v0 = glm::vec3(finalData[i], finalData[i + 1], finalData[i + 2]);
			glm::vec3 v1 = glm::vec3(finalData[i + 8], finalData[i + 9], finalData[i + 10]);
			glm::vec3 v2 = glm::vec3(finalData[i + 16], finalData[i + 17], finalData[i + 18]);
			//glm::vec3 normal = glm::cross((v0 - v1), (v0 - v2));
			glm::vec3 normal = glm::cross((v1 - v0), (v2 - v0));

			//glm::vec3 normal = glm::cross((v0 - v2), (v0 - v1));

			finalData[i + 3] = normal.x;
			finalData[i + 4] = normal.y;
			finalData[i + 5] = normal.z;

			finalData[i + 11] = normal.x;
			finalData[i + 12] = normal.y;
			finalData[i + 13] = normal.z;

			finalData[i + 19] = normal.x;
			finalData[i + 20] = normal.y;
			finalData[i + 21] = normal.z;


			normal = glm::normalize(normal) * 1.0f;
			GLfloat avgX = (v0.x + v1.x + v2.x) / 3;
			GLfloat avgY = (v0.y + v1.y + v2.y) / 3;
			GLfloat avgZ = (v0.z + v1.z + v2.z) / 3;

			normals[nindex] = avgX;
			normals[nindex + 1] = avgY;
			normals[nindex + 2] = avgZ;
			normals[nindex + 3] = normal.x + avgX;
			normals[nindex + 4] = normal.y + avgY;
			normals[nindex + 5] = normal.z + avgZ;
			nindex += 6;

		}
	}


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

	glGenBuffers(1, &buffer);
	glBindBuffer(GL_ARRAY_BUFFER, buffer);
	glBufferData(GL_ARRAY_BUFFER, vsize * sizeof(GLfloat), finalData, GL_STATIC_DRAW);

	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)0);
	glEnableVertexAttribArray(0);


	glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
	glEnableVertexAttribArray(2);

	glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(6 * sizeof(GLfloat)));
	glEnableVertexAttribArray(3);
	
	
	glGenTextures(1, &texture);
	glBindTexture(GL_TEXTURE_2D, texture);
	int width, height;
	unsigned char* image = SOIL_load_image("C:\\Users\\Sanjiv\\Desktop\\charizard-pokemon-go-obj (1)\\lizardon_0_0.png", &width, &height, 0, SOIL_LOAD_RGB);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
	glGenerateMipmap(GL_TEXTURE_2D);
	SOIL_free_image_data(image);

	readShaders();
	initShaders();


}
Esempio n. 2
0
GLMesh::GLMesh(const std::string &fileName, int type) {
    std::vector<Vector3<stReal>> vertex;
    std::vector<Vector2<stReal>> texCoord;
    std::vector<Vector3<stReal>> normal;
    std::vector<Vector3<stReal>> tangent;
    std::vector<Vector3<stReal>> biTangent;
    std::vector<int> index;

    m_fileName = fileName;

    int numVert = 0;

    if(type == STMesh::OBJ){
        auto tMesh = new OBJMesh(fileName);
        numVert = tMesh->getVerticiesSize();
        m_drawCount = tMesh->getIndiciesSize();
        vertex.reserve(numVert);
        texCoord.reserve(numVert);
        normal.reserve(numVert);

        for(unsigned int i = 0; i < numVert; i++){
            vertex.push_back(*tMesh->verticies[i].getVertex());
            texCoord.push_back(*tMesh->verticies[i].getTexCoord());
            normal.push_back(*tMesh->verticies[i].getNormal());
        }
        index = tMesh->indicies;
        tangent = genTangent(vertex, texCoord);
        biTangent = genBiTangent(vertex, texCoord);
        delete tMesh;
    }

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

    glGenBuffers(NUM_BUFFERS, m_VBO);

    glBindBuffer(GL_ARRAY_BUFFER, m_VBO[VERTEX_BUFFER]);
    glBufferData(GL_ARRAY_BUFFER, numVert*sizeof(vertex[0]), &vertex[0], GL_STATIC_DRAW);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);

    glBindBuffer(GL_ARRAY_BUFFER, m_VBO[TEXCOORD_BUFFER]);
    glBufferData(GL_ARRAY_BUFFER, numVert* sizeof(texCoord[0]), &texCoord[0], GL_STATIC_DRAW);
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0);

    glBindBuffer(GL_ARRAY_BUFFER, m_VBO[NORMAL_BUFFER]);
    glBufferData(GL_ARRAY_BUFFER, numVert* sizeof(normal[0]), &normal[0], GL_STATIC_DRAW);
    glEnableVertexAttribArray(2);
    glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, 0);

    glBindBuffer(GL_ARRAY_BUFFER, m_VBO[TANGENT_BUFFER]);
    glBufferData(GL_ARRAY_BUFFER, numVert* sizeof(tangent[0]), &tangent[0], GL_STATIC_DRAW);
    glEnableVertexAttribArray(3);
    glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, 0, 0);

    glBindBuffer(GL_ARRAY_BUFFER, m_VBO[BITANGENT_BUFFER]);
    glBufferData(GL_ARRAY_BUFFER, numVert*sizeof(&biTangent[0]), &biTangent[0], GL_STATIC_DRAW);
    glEnableVertexAttribArray(4);
    glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, 0, 0);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_VBO[INDEX_BUFFER]);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_drawCount * sizeof(index[0]), &index[0], GL_STATIC_DRAW);

    glBindVertexArray(0);
}
Esempio n. 3
0
void storeData() {
	indicies = new GLuint[getIndiciesSize()];
	objData = new GLfloat[500000];
	readObj();
	getFinalSize();
	std::cout << getFinalSize() << std::endl;


	//isize = 34695; //boat
	//isize = 22338; //plane
	//isize = 2901; //monkey
	//isize = 36;

	//for (int i = 0; i < 100; i++) std::cout << indicies[i];

	for (int i = 0; i < isize; i++) {

		finalData[i * 6] = objData[(indicies[i]) * 3];
		finalData[i * 6 + 1] = objData[(indicies[i]) * 3 + 1]; //multiply shit by model matrix. stupid model matrix.
		finalData[i * 6 + 2] = objData[(indicies[i]) * 3 + 2];
	}
	for (int i = 0; i < vsize; i += 6) {
		if (i % 18 == 0) { //problem here, if last 2 triangles dont divide into 3 triangles, duh stupid.
			glm::vec3 v0 = glm::vec3(finalData[i], finalData[i + 1], finalData[i + 2]);
			glm::vec3 v1 = glm::vec3(finalData[i + 6], finalData[i + 7], finalData[i + 8]);
			glm::vec3 v2 = glm::vec3(finalData[i + 12], finalData[i + 13], finalData[i + 14]);
			//glm::vec3 normal = glm::cross((v0 - v1), (v0 - v2));
			glm::vec3 normal = glm::cross((v1 - v0), (v2 - v0));

			//glm::vec3 normal = glm::cross((v0 - v2), (v0 - v1));

			finalData[i + 3] = normal.x;
			finalData[i + 4] = normal.y;
			finalData[i + 5] = normal.z;

			finalData[i + 9] = normal.x;
			finalData[i + 10] = normal.y;
			finalData[i + 11] = normal.z;

			finalData[i + 15] = normal.x;
			finalData[i + 16] = normal.y;
			finalData[i + 17] = normal.z;


			normal = glm::normalize(normal) * 1.0f;
			GLfloat avgX = (v0.x + v1.x + v2.x) / 3;
			GLfloat avgY = (v0.y + v1.y + v2.y) / 3;
			GLfloat avgZ = (v0.z + v1.z + v2.z) / 3;

			normals[nindex] = avgX;
			normals[nindex + 1] = avgY;
			normals[nindex + 2] = avgZ;
			normals[nindex + 3] = normal.x + avgX;
			normals[nindex + 4] = normal.y + avgY;
			normals[nindex + 5] = normal.z + avgZ;
			nindex += 6;

		}
	}


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

	glGenBuffers(1, &buffer);
	glBindBuffer(GL_ARRAY_BUFFER, buffer);
	glBufferData(GL_ARRAY_BUFFER, vsize * sizeof(GLfloat), finalData, GL_STATIC_DRAW);

	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)0);
	glEnableVertexAttribArray(0);


	glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
	glEnableVertexAttribArray(2);

	readShaders();
	initShaders();


}