Exemplo n.º 1
0
// Loads texture for the text from .apk
void WinnerText::loadTexture()
{
	// Don't load again if already loaded
	if (texture)
		return;

	// Load texture from file
	int width, height;
	texture0 = loadTextureFromPNG("assets/blue_wins.png", width, height);
	texture1 = loadTextureFromPNG("assets/pink_wins.png", width, height);

	// Set up texture coords
	texCoords[0] = 1.0f;	texCoords[1] = 0.0f;
	texCoords[2] = 1.0f;	texCoords[3] = 1.0f;
	texCoords[4] = 0.0f;	texCoords[5] = 1.0f;
	texCoords[6] = 0.0f;	texCoords[7] = 1.0f;
	texCoords[8] = 0.0f;	texCoords[9] = 0.0f;
	texCoords[10] = 1.0f;	texCoords[11] = 0.0f;

	// Set up vertex colors
	for (int i = 0; i < vertCount; ++i)
	{
		colors[(i*4)+0] = 1.0f;
		colors[(i*4)+1] = 1.0f;
		colors[(i*4)+2] = 1.0f;
		colors[(i*4)+3] = 1.0f;
	}
}
Exemplo n.º 2
0
Texture::Texture(const char *path)
{
	if(!loadTextureFromPNG(path))
	{
		loadTextureFromBMP(path);
	}
}
Exemplo n.º 3
0
Texture::Texture(std::string path)
{
	if(!loadTextureFromPNG(path.c_str()))
	{
		loadTextureFromBMP(path.c_str());
	}
}
Exemplo n.º 4
0
JNIEXPORT void JNICALL Java_com_blackicegamesnyc_GameEngine_GameEngineRenderer_nativeInit
  (JNIEnv * env, jclass cls, jstring apkPath) {
  __android_log_print(ANDROID_LOG_DEBUG  , "KRISTOFER", "Native Init Called...\n");
  const char* str;
  jboolean isCopy;
  str = env->GetStringUTFChars(apkPath, &isCopy);
  loadAPK(str);

  int width, height;
  texture = loadTextureFromPNG("assets/sprites/texture.png", width, height);

  printGLString("Version", GL_VERSION);
  printGLString("Vendor", GL_VENDOR);
  printGLString("Renderer", GL_RENDERER);
  printGLString("Extensions", GL_EXTENSIONS);

  glEnableClientState(GL_VERTEX_ARRAY);
  glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  glEnable(GL_BLEND);
  glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  glEnable(GL_TEXTURE_2D);
  glClearColor(1,0,0,0);
  glColor4f(1,1,1,1);
  glDisable(GL_DEPTH_TEST);
  glDisable(GL_CULL_FACE);
}
Exemplo n.º 5
0
JNIEXPORT void JNICALL Java_net_catchball_CatchBallRenderer_nativeInit
  (JNIEnv * env, jclass cls, jstring apkPath) {
  const char* str;
  jboolean isCopy;
  str = env->GetStringUTFChars(apkPath, &isCopy);
  loadAPK(str);

  int width, height;
  texture = loadTextureFromPNG("assets/sprites/texture.png", width, height);
  int bblue = loadTextureFromPNG("assets/sprites/button blue.png", width, height);
  int bred = loadTextureFromPNG("assets/sprites/button red.png", width, height);
//  int houseTexture = loadTextureFromPNG("assets/sprites/house.png", width, height);
  int meteor_b = loadTextureFromPNG("assets/sprites/meteor.png", width, height);
  int meteor_y = loadTextureFromPNG("assets/sprites/meteor-y.png", width, height);
  int meteor_r = loadTextureFromPNG("assets/sprites/meteor-r.png", width, height);
  int meteor_g = loadTextureFromPNG("assets/sprites/meteor-g.png", width, height);
  int ballTexture = loadTextureFromPNG("assets/sprites/ball.png", width, height);

  printGLString("Version", GL_VERSION);
  printGLString("Vendor", GL_VENDOR);
  printGLString("Renderer", GL_RENDERER);
  printGLString("Extensions", GL_EXTENSIONS);

  glEnableClientState(GL_VERTEX_ARRAY);
  glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  glEnable(GL_BLEND);
  glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  glEnable(GL_TEXTURE_2D);
  glClearColor(0,0,0,0);
  glColor4f(1,1,1,1);
  glDisable(GL_DEPTH_TEST);
  glDisable(GL_CULL_FACE);
  
	Texture texr(RED,meteor_r);
	Texture texg(GREEN,meteor_g);
	Texture texy(YELLOW,meteor_y);
	Texture texb(BRONZE,meteor_b);

LOGI("textures created");
//  house = House(world.getWorld());
//  house.setTexture(houseTexture);
  ball = Ball(world);
  ball.setTexture(ballTexture);
LOGI("Ball created");
  meteors = meteorManager(world.getWorld());
LOGI("Meteors created");
//  meteors.setTexture(meteor);
  meteors.addTexture(texb);
  meteors.addTexture(texr);
  meteors.addTexture(texy);
  meteors.addTexture(texg);
//  buttons.setTextures(bblue, bred);
LOGI("Textures for meteors added");
}
GLuint CBAndroidTextureBuilder::reloadTexture(const string& filename)
{
	int width = 0;
	int height = 0;
	int pixWidth = 0;
	int pixHeight = 0;
	string name = "assets/" + filename;
	GLuint texID = loadTextureFromPNG(name.c_str(), width, height, pixWidth, pixHeight);
	//DebugLog("CBBuilderAndroid::ReloadTexture:%s,%d",filename.c_str(),texID);
    return texID;
}
CBTexture* CBAndroidTextureBuilder::buildTexture(const string& filename)
{
	string name = "assets/" + filename;
	int width = 0;
	int height = 0;
	int pixWidth = 0;
	int pixHeight = 0;
	CBTexture* texture;
	GLuint texID = loadTextureFromPNG(name.c_str(), width, height, pixWidth, pixHeight);
	texture = new CBTexture(texID, width, height, pixWidth, pixHeight);
	return texture;
}
//--------------------------------------------------------------
// [Assignment4]
// Create a 2D GL texture from the file given
//--------------------------------------------------------------
void G308_Geometry::ReadTexture(const char* filename, int type, GLfloat scale) {
	//Your code here
	unsigned int i;
	for (i = 0; i < strlen(filename); i++) {
		if (filename[i] == '.') {
			break;
		}
	}
	char extension[5];
	strcpy(extension, &filename[i + 1]);
	//printf(extension);

	GLuint* tex = new GLuint;
	TextureInfo t;

	if (strcmp(extension, "jpg") == 0 || strcmp(extension, "jpeg") == 0)
		loadTextureFromJPEG(const_cast<char *>(filename), &t);
	else if (strcmp(extension, "png") == 0)
		loadTextureFromPNG(const_cast<char *>(filename), &t);
	else {
		printf("Invalid format. Only supports JPEG and PNG.\n");
		exit(1);
	}

	//Init the texture storage, and set some parameters.
	//(I high recommend reading up on these commands)
	glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
	glGenTextures(1, tex);
	glBindTexture(GL_TEXTURE_2D, *tex);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

	//Only useful for PNG files, since JPEG doesn't support alpha
	if (t.hasAlpha) {
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, t.width, t.height, 0, GL_RGBA,
				GL_UNSIGNED_BYTE, t.textureData);
	} else {
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, t.width, t.height, 0, GL_RGB,
				GL_UNSIGNED_BYTE, t.textureData);
	}
	//Once the texture has been loaded by GL, we don't need this anymore.
	free(t.textureData);
	if (type==TEXTURE){
		texture = tex;
	} else if (type==NORMAL){
		normal = tex;
	}
	textureScale = scale;
}
Exemplo n.º 9
0
void init(char* filename) {
	glClearColor(0.0, 0.0, 0.0, 0.0);
	glShadeModel(GL_FLAT);
	glEnable(GL_DEPTH_TEST);

	unsigned int i;
	for (i = 0; i < strlen(filename); i++) {
		if (filename[i] == '.') {
			break;
		}
	}
	char extension[5];
	strcpy(extension, &filename[i + 1]);
	//printf(extension);
	if (strcmp(extension, "jpg") == 0 || strcmp(extension, "jpeg") == 0)
		loadTextureFromJPEG(filename, &t);
	else if (strcmp(extension, "png") == 0)
		loadTextureFromPNG(filename, &t);
	else {
		printf("Invalid format. Only supports JPEG and PNG.\n");
		exit(1);
	}

	//Init the texture storage, and set some parameters.
	//(I high recommend reading up on these commands)
	glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
	glGenTextures(1, &texName);
	glBindTexture(GL_TEXTURE_2D, texName);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

	//Only useful for PNG files, since JPEG doesn't support alpha
	if (t.hasAlpha) {
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, t.width, t.height, 0, GL_RGBA,
				GL_UNSIGNED_BYTE, t.textureData);
	} else {
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, t.width, t.height, 0, GL_RGB,
				GL_UNSIGNED_BYTE, t.textureData);
	}
	//Once the texture has been loaded by GL, we don't need this anymore.
	free(t.textureData);
}
Exemplo n.º 10
0
bool Model::loadCollada(std::string filename){
    //Load the collada xml file
    rapidxml::file<> xmlFile(filename.c_str());
    rapidxml::xml_document<> doc;
    doc.parse<0>(xmlFile.data());
    
    bool zup = std::string(doc.first_node()->first_node("asset")->first_node("up_axis")->value()) == "Z_UP";
    
    //Load textures
    rapidxml::xml_node<> *images = doc.first_node()->first_node("library_images");
    if(images->first_node("image") == 0){
        Logger::error << "Couldn't load model: file contains no texture data." << std::endl;
        return false;
    }
    std::map<std::string, unsigned int> textures;
    for(rapidxml::xml_node<> *image = images->first_node("image"); image != 0; image = image->next_sibling("image")){
        textures.insert(std::pair<std::string, unsigned int>(image->first_attribute("id")->value(), loadTextureFromPNG(filename.substr(0, filename.find_last_of("\\/")+1) + image->first_node("init_from")->value())));
    }
    
    
    //Load effects
    //TODO: only supports common profiles using phong techniques
    rapidxml::xml_node<> *effectsNode = doc.first_node()->first_node("library_effects");
    if(effectsNode->first_node("effect") == 0){
        Logger::error << "Couldn't load model: file contains no effects data." << std::endl;
        return false;
    }
    std::map<std::string, Material> effects;
    for(rapidxml::xml_node<> *effect = effectsNode->first_node("effect"); effect != 0; effect = effect->next_sibling("effect")){
        rapidxml::xml_node<> *profile = effect->first_node("profile_COMMON");
        if(profile != 0){
            rapidxml::xml_node<> *technique = profile->first_node("technique");
            Material mat;
            rapidxml::xml_node<> *phong = technique->first_node("phong");
            if(phong != 0){                
                if(phong->first_node("emission") != 0){
                    mat.emission = getVec4(phong->first_node("emission")->first_node("color"));
                }
                if(phong->first_node("ambient") != 0){
                    mat.ambient = getVec4(phong->first_node("ambient")->first_node("color"));
                }
                if(phong->first_node("diffuse") != 0){
                    if(phong->first_node("diffuse")->first_node("color") != 0){
                        mat.diffuse = getVec4(phong->first_node("diffuse")->first_node("color"));
                    }
                    if(phong->first_node("diffuse")->first_node("texture") != 0){
                        mat.diffuseTexture = textures.at(searchByAttribute(profile, "sid", 
                                searchByAttribute(profile, "sid", phong->first_node("diffuse")->first_node("texture")->first_attribute("texture")->value(), "newparam")
                                ->first_node("sampler2D")->first_node("source")->value()
                                , "newparam")->first_node("surface")->first_node("init_from")->value());
                    }
                }
                if(phong->first_node("specular") != 0){
                    if(phong->first_node("specular")->first_node("color") != 0){
                        mat.specular = getVec4(phong->first_node("specular")->first_node("color"));
                    }
                    if(phong->first_node("specular")->first_node("texture") != 0){
                        mat.specularTexture = textures.at(searchByAttribute(profile, "sid", 
                                searchByAttribute(profile, "sid", phong->first_node("specular")->first_node("texture")->first_attribute("texture")->value(), "newparam")
                                ->first_node("sampler2D")->first_node("source")->value()
                                , "newparam")->first_node("surface")->first_node("init_from")->value());
                    }
                }
                if(phong->first_node("shininess") != 0){
                    mat.shininess = std::stof(phong->first_node("shininess")->first_node("float")->value());
                }
            } else {
                Logger::error << "Couldn't load model: material doesn't use the phong technique." << std::endl;
                return false;
            }
            if(technique->first_node("extra") != 0 && technique->first_node("extra")->first_node("technique")->first_node("bump") != 0){
                mat.bumpmapTexture = textures.at(searchByAttribute(profile, "sid", 
                                searchByAttribute(profile, "sid", technique->first_node("extra")->first_node("technique")->first_node("bump")->first_node("texture")->first_attribute("texture")->value(), "newparam")
                                ->first_node("sampler2D")->first_node("source")->value()
                                , "newparam")->first_node("surface")->first_node("init_from")->value());
            }
            effects.insert(std::pair<std::string, Material>(effect->first_attribute("id")->value(), mat));
        } else {
            Logger::error << "Couldn't load model: material doesn't have a profile_COMMON tag." << std::endl;
            return false;
        }
    }
    
    
    //Load materials
    //TODO: make them overridable (collada spec))
    rapidxml::xml_node<> *materialsNode = doc.first_node()->first_node("library_materials");
    if(materialsNode->first_node("material") == 0){
        Logger::error << "Couldn't load model: file contains no material data." << std::endl;
        return false;
    }
    std::map<std::string, Material> materials;
    for(rapidxml::xml_node<> *material = materialsNode->first_node("material"); material != 0; material = material->next_sibling("material")){
        materials.insert(std::pair<std::string, Material>(material->first_attribute("id")->value(), effects.at(std::string(material->first_node("instance_effect")->first_attribute("url")->value()).substr(1))));
    }
    
    
    //Load geometry
    std::map<std::string, Mesh> meshes;
    rapidxml::xml_node<> *geometries = doc.first_node()->first_node("library_geometries");
    if(geometries->first_node("geometry") == 0){
        Logger::error << "Couldn't load model: file contains no geometry data." << std::endl;
        return false;
    }
    for(rapidxml::xml_node<> *geometry = geometries->first_node("geometry"); geometry != 0; geometry = geometry->next_sibling("geometry")){
        Mesh mesh;        
        rapidxml::xml_node<> *meshNode = geometry->first_node("mesh");
        if(meshNode == 0){
            Logger::error << "Couldn't load model: The loader can only load geometry of type \"mesh\" (geometry with name: " << geometry->first_attribute("name")->value() << ")." << std::endl;
            return false;
        }
        if(meshNode->first_node("polylist") == 0){
            Logger::error << "Couldn't load model: The loader can only load meshes that use polylists. (geometry with name: " << geometry->first_attribute("name")->value() << ")." << std::endl;
            return false;
        }
        
        //TODO: allow for multiple data types
        //<name, <stride, data>>
        std::map<std::string, std::pair<unsigned int, std::vector<float>>> sources;
        GLenum dataType;
        for(rapidxml::xml_node<> *source = meshNode->first_node("source"); source != 0; source = source->next_sibling("source")){
            std::vector<float> container;
            dataType = readData(&container, source);
            if(dataType == GL_NONE){
                return false;
            }
            sources.insert(std::pair<std::string, std::pair<unsigned int, std::vector<float>>>(source->first_attribute("id")->value(), std::pair<unsigned int, std::vector<float>>(std::stoi(source->first_node("technique_common")->first_node("accessor")->first_attribute("stride")->value()), container)));
        }
        
        //TODO: same data vertex merging (aka indexing)
        for(rapidxml::xml_node<> *polylist = meshNode->first_node("polylist"); polylist != 0; polylist = polylist->next_sibling("polylist")){
            std::vector<float> openglData;
            std::vector<unsigned int> indices;
            std::vector<std::string> tempContainer;
            std::vector<int> colladaIndices;        
            split(polylist->first_node("p")->value(), " ", &tempContainer);
            for(std::string s : tempContainer){
                colladaIndices.push_back(std::stoi(s));
            }
            std::vector<float> vertexData;
            float vertexOffset;
            float vertexStride;
            std::vector<float> normalData;
            float normalOffset;
            float normalStride;
            std::vector<float> uvData;
            float uvOffset;
            float uvStride;
            
            std::pair<unsigned int, std::vector<float>> pair;
            for(rapidxml::xml_node<> *input = polylist->first_node("input"); input != 0; input = input->next_sibling("input")){
                std::string semantic(input->first_attribute("semantic")->value());
                if(semantic == "VERTEX"){
                    pair = sources.at(std::string(meshNode->first_node("vertices")->first_node("input")->first_attribute("source")->value()).substr(1));
                    vertexData = pair.second;
                    vertexOffset = std::stoi(input->first_attribute("offset")->value());
                    vertexStride = pair.first;
                } else if(semantic == "NORMAL"){
                    pair = sources.at(std::string(input->first_attribute("source")->value()).substr(1));
                    normalData = pair.second;
                    normalOffset = std::stoi(input->first_attribute("offset")->value());
                    normalStride = pair.first;
                } else if(semantic == "TEXCOORD"){
                    pair = sources.at(std::string(input->first_attribute("source")->value()).substr(1));
                    uvData = pair.second;
                    uvOffset = std::stoi(input->first_attribute("offset")->value());
                    uvStride = pair.first;
                } else {
                    Logger::error << "Unknown input semantic: " << semantic << std::endl;
                    return 0;
                }
            }            
            PolyGroup poly;
            poly.elements = std::stoi(polylist->first_attribute("count")->value())*3;
            
            //Generate vertices
            for(unsigned int i = 0; i < poly.elements; i++){
                openglData.push_back(vertexData.at(colladaIndices.at(i*3+vertexOffset)*vertexStride));
                if(zup){
                    openglData.push_back(vertexData.at(colladaIndices.at(i*3+vertexOffset)*vertexStride+2));
                    openglData.push_back(-vertexData.at(colladaIndices.at(i*3+vertexOffset)*vertexStride+1));
                } else {
                    openglData.push_back(vertexData.at(colladaIndices.at(i*3+vertexOffset)*vertexStride+1));
                    openglData.push_back(vertexData.at(colladaIndices.at(i*3+vertexOffset)*vertexStride+2));
                }

                openglData.push_back(normalData.at(colladaIndices.at(i*3+normalOffset)*normalStride));
                if(zup){
                    openglData.push_back(normalData.at(colladaIndices.at(i*3+normalOffset)*normalStride+2));
                    openglData.push_back(-normalData.at(colladaIndices.at(i*3+normalOffset)*normalStride+1));
                } else {
                    openglData.push_back(normalData.at(colladaIndices.at(i*3+normalOffset)*normalStride+1));
                    openglData.push_back(normalData.at(colladaIndices.at(i*3+normalOffset)*normalStride+2));
                }

                openglData.push_back(uvData.at(colladaIndices.at(i*3+uvOffset)*uvStride));
                //collada stores the uv data the other way around. how silly.
                openglData.push_back(1.0f - uvData.at(colladaIndices.at(i*3+uvOffset)*uvStride+1));

                //temp index
                indices.push_back(i);
            }
            poly.vaoid = sendToOpengl(openglData, indices);
            poly.material = materials.at(polylist->first_attribute("material")->value());
            mesh.addPolyGroup(poly);
        }
        meshes.insert(std::pair<std::string, Mesh>(geometry->first_attribute("id")->value(), mesh));
        //this->meshes.insert(std::pair<std::string, std::pair<glm::mat4, Mesh>>(std::string(geometry->first_attribute("id")->value()).substr(1), std::pair<glm::mat4, Mesh>(glm::mat4(1.0f), mesh)));
    }
    
    //Load scene
    rapidxml::xml_node<> *scenes = doc.first_node()->first_node("library_visual_scenes");
    if(scenes->first_node("visual_scene") == 0){
        Logger::error << "Couldn't load model: file contains no scene data." << std::endl;
        return false;
    } else {
        rapidxml::xml_node<> *scene = scenes->first_node("visual_scene");
        for(rapidxml::xml_node<> *node = scene->first_node("node"); node != 0; node = node->next_sibling("node")){
            if(node->first_node("instance_geometry") != 0){
                std::vector<float> matrix;
                std::vector<std::string> tempContainer;
                glm::mat4 mat4 = glm::mat4(1.0f);
                if(node->first_node("matrix") == 0){
                    split(node->first_node("translate")->value(), " ", &tempContainer);
                    mat4 = glm::translate(mat4, glm::vec3(std::stof(tempContainer[0]), std::stof(tempContainer[2]), -std::stof(tempContainer[1])));
                    tempContainer.clear();
                    split(searchByAttribute(node, "sid", "rotationX", "rotate")->value(), " ", &tempContainer);
                    mat4 = glm::rotate(mat4, glm::radians(std::stof(tempContainer[3])), glm::vec3(1.0f, 0.0f, 0.0f));
                    tempContainer.clear();
                    split(searchByAttribute(node, "sid", "rotationZ", "rotate")->value(), " ", &tempContainer);
                    mat4 = glm::rotate(mat4, glm::radians(std::stof(tempContainer[3])), glm::vec3(0.0f, 1.0f, 0.0f));
                    tempContainer.clear();
                    split(searchByAttribute(node, "sid", "rotationY", "rotate")->value(), " ", &tempContainer);
                    mat4 = glm::rotate(mat4, glm::radians(std::stof(tempContainer[3])), glm::vec3(0.0f, 0.0f, -1.0f));
                    tempContainer.clear();
                    split(node->first_node("scale")->value(), " ", &tempContainer);
                    mat4 = glm::scale(mat4, glm::vec3(std::stof(tempContainer[0]), std::stof(tempContainer[2]), std::stof(tempContainer[1])));
                    //mat4 = glm::mat4(1.0f);
                } else {
                    split(node->first_node("matrix")->value(), " ", &tempContainer);
                    for(std::string s : tempContainer){
                        matrix.push_back(std::stof(s));
                    }
                    glm::mat4 mat4 = glm::make_mat4(matrix.data());
                    if(std::string(doc.first_node()->first_node("asset")->first_node("up_axis")->value()) == "Z_UP"){
                        mat4 = convertToRightHandedCoords(mat4);
                    }
                }
                this->meshes.insert(std::pair<std::string,std::pair<glm::mat4, Mesh>>(
                        node->first_attribute("id")->value(),
                        std::pair<glm::mat4, Mesh>(
                            mat4,
                            meshes.at(std::string(node->first_node("instance_geometry")->first_attribute("url")->value()).substr(1)))));
            }
        }
    }
    rapidjson::Document animDoc;
    if(readJsonFile(filename.substr(0, filename.find_last_of("/")+1) + "animation.json", animDoc)){
        rapidxml::xml_node<> *animations = doc.first_node()->first_node("library_animations");
        if(animations->first_node("animation") != 0){
            std::map<std::string, std::vector< std::pair<float, glm::vec3> > > locationKeyframes;
            std::map<std::string, std::vector< std::pair<float, glm::vec3> > > rotationKeyframes;
            for(rapidxml::xml_node<> *animNode = animations->first_node("animation"); animNode != 0; animNode = animNode->next_sibling("animation")){
                std::vector<float> inputContainer;
                readData(&inputContainer, searchByAttribute(
                        animNode,
                        "id",
                        std::string(searchByAttribute(
                            animNode->first_node("sampler"),
                            "semantic",
                            "INPUT",
                            "input"
                        )->first_attribute("source")->value()).substr(1),
                        "source"));
                std::vector<float> outputContainer;
                readData(&outputContainer, searchByAttribute(
                        animNode,
                        "id",
                        std::string(searchByAttribute(
                            animNode->first_node("sampler"),
                            "semantic",
                            "OUTPUT",
                            "input"
                        )->first_attribute("source")->value()).substr(1),
                        "source"));/*
                std::vector< std::pair<float, glm::vec3> > keyframes;
                for(int i = 0; i < inputContainer.size(); i++){
                    keyframes.push_back(std::pair<float, glm::vec3>(inputContainer.at(i), outputContainer.at(i)));
                }*/
                std::string target = animNode->first_node("channel")->first_attribute("target")->value();
                std::string meshName = target.substr(0, target.find_first_of("/"));
                if(target.substr(target.find_first_of("/")+1, target.find_first_of(".")-target.find_first_of("/")-2) == "rotation"){
                    if(!rotationKeyframes.count(meshName)){
                        rotationKeyframes.insert(std::pair<std::string, std::vector< std::pair<float, glm::vec3> > >(meshName, std::vector< std::pair<float, glm::vec3> >()));
                    }
                    for(int i = 0; i < inputContainer.size(); i++){
                        if(rotationKeyframes.at(meshName).size() <= i){
                            rotationKeyframes.at(meshName).push_back(std::pair<float, glm::vec3>(inputContainer.at(i), glm::vec3(0.0f, 0.0f, 0.0f)));
                        }
                        if(target.substr(target.find_first_of("/")+1, target.find_first_of(".")-target.find_first_of("/")-1) == "rotationX"){
                            rotationKeyframes.at(meshName).at(i).second.x = outputContainer.at(i);
                        } else if(target.substr(target.find_first_of("/")+1, target.find_first_of(".")-target.find_first_of("/")-1) == "rotationY"){
                            rotationKeyframes.at(meshName).at(i).second.y = outputContainer.at(i);
                        } else if(target.substr(target.find_first_of("/")+1, target.find_first_of(".")-target.find_first_of("/")-1) == "rotationZ"){
                            rotationKeyframes.at(meshName).at(i).second.z = outputContainer.at(i);
                        }
                    }
                } else if(target.substr(target.find_first_of("/")+1, target.find_first_of(".")) == "location"){
                    if(locationKeyframes.count(meshName)){
                        locationKeyframes.insert(std::pair<std::string, std::vector< std::pair<float, glm::vec3> > >(meshName, std::vector< std::pair<float, glm::vec3> >()));
                    }
                    for(int i = 0; i < inputContainer.size(); i++){
                        if(locationKeyframes.at(meshName).size() <= i){
                            locationKeyframes.at(meshName).push_back(std::pair<float, glm::vec3>(inputContainer.at(i), glm::vec3(0.0f, 0.0f, 0.0f)));
                        }
                        if(target.substr(target.find_first_of(".")+1) == "X"){
                            locationKeyframes.at(meshName).at(i).second.x = outputContainer.at(i);
                        } else if(target.substr(target.find_first_of(".")+1) == "Y"){
                            locationKeyframes.at(meshName).at(i).second.y = outputContainer.at(i);
                        } else if(target.substr(target.find_first_of(".")+1) == "Z"){
                            locationKeyframes.at(meshName).at(i).second.z = outputContainer.at(i);
                        }
                    }
                } else {
                    Logger::info << "unknown animation target: " << target << std::endl;
                }
            }
            for (rapidjson::SizeType i = 0; i < animDoc.Size(); i++) {
                Animation animation;
                rapidjson::Value& animjson = animDoc[i];
                animation.mesh = animjson["mesh"].GetString();
                int start = animjson["startKeyframe"].GetInt();
                int end = animjson["endKeyframe"].GetInt();
                for(int j = start; j <= end; j++){
                    glm::mat4 mat4 = this->meshes.at(animation.mesh).first;
                    glm::vec3 rotation = rotationKeyframes.count(animation.mesh) ? rotationKeyframes.at(animation.mesh).at(j).second : glm::vec3(0.0f, 0.0f, 0.0f);
                    glm::vec3 translation = locationKeyframes.count(animation.mesh) ? locationKeyframes.at(animation.mesh).at(j).second : glm::vec3(0.0f, 0.0f, 0.0f);
                    if(zup){
                        mat4 = glm::translate(mat4, glm::vec3(translation.x, translation.z, -translation.y));
                        mat4 = glm::rotate(mat4, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f));
                        mat4 = glm::rotate(mat4, glm::radians(rotation.z), glm::vec3(0.0f, 1.0f, 0.0f));
                        mat4 = glm::rotate(mat4, glm::radians(rotation.y), glm::vec3(0.0f, 0.0f, -1.0f));
                    } else {
                        mat4 = glm::translate(mat4, glm::vec3(translation.x, translation.y, translation.z));
                        mat4 = glm::rotate(mat4, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f));
                        mat4 = glm::rotate(mat4, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f));
                        mat4 = glm::rotate(mat4, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f));
                    }
                    animation.keyframes.push_back(std::make_pair(rotationKeyframes.at(animation.mesh).at(j).first, mat4));
                }
                this->animations.insert(std::make_pair(animjson["name"].GetString(), animation));
            }
        }
    }
    return true;
}
Exemplo n.º 11
0
CubeMap::
CubeMap(GLsizei w, GLsizei h):
	width(w),
	height(h),
	handle(0),
	textureHandle(0)
{
	//Create buffers
	glGenFramebuffers(1, &handle);
	glBindFramebuffer(GL_FRAMEBUFFER, handle);
	
	//Make texture for our frame buffers to store stuff in
	glActiveTexture(GL_TEXTURE10);
	
	
	glGenTextures(1, &textureHandle);
	glBindTexture(GL_TEXTURE_CUBE_MAP, textureHandle);//GL_TEXTURE_CUBE_MAP
	
	//Set some texture parameters
	glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	//glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_REPEAT);
	//glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_REPEAT);
	
	
	GLuint depthrenderbuffer;
	glGenRenderbuffers(1, &depthrenderbuffer);
	glBindRenderbuffer(GL_RENDERBUFFER, depthrenderbuffer);
	glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width,height);
	glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthrenderbuffer);

	/*
	 * 1. The texture target. We are actually saying here what part of the cube map we are loading.
	 * 2. Level of detail - used for mipmapping. SIcne we are not mipmapping anything, we set to 0.
	 * 3. Format to store image on graphics card
	 * 4. width
	 * 5. height
	 * 6. border?
	 * 7. Format in which image is stored in RAM: We pick GL_RGB because that is the format our PNG were stored in.
	 * 8. Data type our image is stored in - We used unsigneds.
	 * 9. Pointer to actual data.
	 */
	
	unsigned char* data;
	
	data = loadTextureFromPNG("textures/skybox/terrain_positive_x.png", width, height);
	glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
	
	data = loadTextureFromPNG("textures/skybox/terrain_positive_y.png", width, height);
	glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
	
	data = loadTextureFromPNG("textures/skybox/terrain_positive_z.png", width, height);
	glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
	
	data = loadTextureFromPNG("textures/skybox/terrain_negative_x.png", width, height);
	glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
	
	data = loadTextureFromPNG("textures/skybox/terrain_negative_y.png", width, height);
	glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
	
	data = loadTextureFromPNG("textures/skybox/terrain_negative_z.png", width, height);
	glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
	
	GLuint status = glCheckFramebufferStatus(GL_FRAMEBUFFER);

	if (status != GL_FRAMEBUFFER_COMPLETE)
	{
		switch (status)
		{
		 case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
		    //throw RuntimeError("Incomplete framebuffer attachment.");
			cout << "[ERROR]: Incomplete framebuffer attachment." << endl;
		 case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER:
		    //throw RuntimeError("No color attachment in draw buffer.");
			cout << "[ERROR]: No color attachment in draw buffer." << endl;
		 case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER:
		   // throw RuntimeError("No color attachment in read buffer.");
			cout << "[ERROR]: No color attachment in read buffer." << endl;
		 case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
		    //throw RuntimeError("Framebuffer is missing an attachment.");
			cout << "[ERROR]: Framebuffer is missing an attachment." << endl;
		 case GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE:
		    //throw RuntimeError("The framebuffer different size multisamples.");
			cout << "[ERROR]: The framebuffer different size multisamples." << endl;
		 case GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS:
		    //throw RuntimeError("Not all framebuffer attachments are layered.");
			cout << "[ERROR]: Not all framebuffer attachments are layered." << endl;
		 case GL_FRAMEBUFFER_UNSUPPORTED:
		    //throw RuntimeError("The framebuffer configuration is unsupported.");
			cout << "[ERROR]: The framebuffer configuration is unsupported." << endl;
		 default:
		    
		    //throw RuntimeError("Unknown framebuffer error.");
			cout << "[ERROR]: Unknown framebuffer errort." << endl;
		}
		
		exit(-1);
	}
	
	unbind();
}