#define STB_IMAGE_IMPLEMENTATION #include"stb_image.h" #include #include #include"texture.h" #include using namespace std; namespace vb01{ Texture::Texture(int width, int height, bool shadowMap){ this->width=width; this->height=height; glGenTextures(1,&texture); glBindTexture(GL_TEXTURE_2D,texture); if(shadowMap){ glTexImage2D(GL_TEXTURE_2D,0,GL_DEPTH_COMPONENT,width,height,0,GL_DEPTH_COMPONENT,GL_FLOAT,NULL); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_BORDER); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_BORDER); float borderColor[]={1,1,1,1}; glTexParameterfv(GL_TEXTURE_2D,GL_TEXTURE_BORDER_COLOR,borderColor); } else{ glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,width,height,0,GL_RGB,GL_UNSIGNED_BYTE,NULL); 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_MIN_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); } } Texture::Texture(FT_Face &face, char ch){ glGenTextures(1,&texture); glBindTexture(GL_TEXTURE_2D,texture); glTexImage2D( GL_TEXTURE_2D, 0, GL_RED, face->glyph->bitmap.width, face->glyph->bitmap.rows, 0, GL_RED, GL_UNSIGNED_BYTE, face->glyph->bitmap.buffer ); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); glBindTexture(GL_TEXTURE_2D,0); } Texture::Texture(string path,bool flip){ bool png=false; int length=path.length(); if(path.substr(length-4,string::npos)==".png") png=true; glGenTextures(1,&texture); glBindTexture(GL_TEXTURE_2D,texture); 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_WRAP_S,GL_REPEAT); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); stbi_set_flip_vertically_on_load(flip); data=stbi_load(path.c_str(),&width,&height,&numChannels,0); glTexImage2D(GL_TEXTURE_2D,0,png?GL_RGBA:GL_RGB,width,height,0,png?GL_RGBA:GL_RGB,GL_UNSIGNED_BYTE,data); //glGenerateMipmap(GL_TEXTURE_2D); stbi_image_free(data); } Texture::Texture(string path[],int numFrames,s64 updateRate,bool flip){ this->updateRate=updateRate; this->numFrames=numFrames; frames=new u32; for(int i=0;itype=TextureType::TEXTURE_CUBEMAP; glGenTextures(1,&texture); glBindTexture(GL_TEXTURE_CUBE_MAP,texture); stbi_set_flip_vertically_on_load(flip); for(int i=0;i<6;i++){ data=stbi_load(paths[i].c_str(),&width,&height,&numChannels,0); if(data){ glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X+i,0,GL_RGB,width,height,0,GL_RGB,GL_UNSIGNED_BYTE,data); stbi_image_free(data); } else{ cout<<"Failed to read cubemap texture "<0){ if(getTime()-lastUpdateTime>updateRate){ currentFrame=(currentFrame==numFrames-1?0:currentFrame+1); lastUpdateTime=getTime(); } } } void Texture::select(int id){ glActiveTexture(GL_TEXTURE0+id); glBindTexture(type==TEXTURE_2D?GL_TEXTURE_2D:GL_TEXTURE_CUBE_MAP,numFrames==0?texture:frames[currentFrame]); } }