void glLoadTextures() { glGenTextures(1, &textures[WTRTX]); glBindTexture(GL_TEXTURE_2D, textures[WTRTX]); glfwLoadTexture2D("..\\Resources\\Water.tga",GLFW_BUILD_MIPMAPS_BIT); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T, GL_REPEAT); glGenTextures(1, &textures[ISLTX]); glBindTexture(GL_TEXTURE_2D, textures[ISLTX]); glfwLoadTexture2D("..\\Resources\\volcano\\Map__1_Cellular.tga",GLFW_BUILD_MIPMAPS_BIT); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T, GL_REPEAT); glGenTextures(1, &textures[BSODTX]); glBindTexture(GL_TEXTURE_2D, textures[BSODTX]); glfwLoadTexture2D("..\\Resources\\BSOD.tga",GLFW_BUILD_MIPMAPS_BIT); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T, GL_REPEAT); glGenTextures(1, &textures[LOADTX]); glBindTexture(GL_TEXTURE_2D, textures[LOADTX]); glfwLoadTexture2D("..\\Resources\\Loading.tga",GLFW_BUILD_MIPMAPS_BIT); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T, GL_REPEAT); }
unsigned int tga_load(const char *path) { int err; GLuint texture; char *full_path; full_path = malloc(strlen(path) + strlen(IMAGE_DATA_PATH) + strlen(DATA_PATH) + 3); sprintf(full_path, "%s/%s/%s", DATA_PATH, IMAGE_DATA_PATH, path); glGenTextures(1, &texture); glBindTexture(GL_TEXTURE_2D, texture); err = glfwLoadTexture2D(full_path, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); ok_printf("Loaded '%s'", full_path); return texture; }
GLboolean LoadTextures( void ) { int i; // Generate texture objects glGenTextures( NUM_TEXTURES, tex_id ); // Load textures for( i = 0; i < NUM_TEXTURES; i ++ ) { // Select texture object glBindTexture( GL_TEXTURE_2D, tex_id[ i ] ); // Set texture parameters 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_LINEAR ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); // Upload texture from file to texture memory if( !glfwLoadTexture2D( tex_name[ i ], 0 ) ) { fprintf( stderr, "Failed to load texture %s\n", tex_name[ i ] ); return GL_FALSE; } } return GL_TRUE; }
void Particle::LoadTextures(void) { glGenTextures(1, &particleTexture); glBindTexture(GL_TEXTURE_2D, particleTexture); glfwLoadTexture2D("textures/particle.tga", GLFW_BUILD_MIPMAPS_BIT | GL_RGBA); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); }
void Demo01::InitializeGL() { shaderManager.CreateShaderProgram("Simple"); shaderManager.AttachShader("SimpleVertex" , VERTEX); shaderManager.AttachShader("SimpleFragment", FRAGMENT); shaderManager.LoadShaderSource("SimpleVertex" , "data/shaders/demo01/texture.vert.glsl"); shaderManager.LoadShaderSource("SimpleFragment", "data/shaders/demo01/grey.frag.glsl"); shaderManager.CompileShader("SimpleVertex"); shaderManager.CompileShader("SimpleFragment"); shaderManager.AttachShaderToProgram("Simple", "SimpleVertex"); shaderManager.AttachShaderToProgram("Simple", "SimpleFragment"); shaderManager.BindAttribute("Simple", 0, "inPosition"); shaderManager.BindAttribute("Simple", 1, "inTexCoord"); shaderManager.LinkProgramObject("Simple"); shaderManager["Simple"]->Activate(); modelMat = glm::mat4(1); float* vert = new float[12]; vert[0] = 1.0f; vert[ 1] = 0.0f; vert[ 2] =-1.0f; vert[3] = 1.0f; vert[ 4] = 0.0f; vert[ 5] = 1.0f; vert[6] = -1.0f; vert[ 7] = 0.0f; vert[ 8]= 1.0f; vert[9] = -1.0f; vert[10] = 0.0f; vert[11]= -1.0f; float* texCoords = new float[8]; texCoords[0] = 0.0f; texCoords[1] = 0.0f; texCoords[2] = 1.0f; texCoords[3] = 0.0f; texCoords[4] = 1.0f; texCoords[5] = 1.0f; texCoords[6] = 0.0f; texCoords[7] = 1.0f; glGenVertexArrays(1, &vaoID[0]); glBindVertexArray(vaoID[0]); glGenBuffers(2, vboID); glBindBuffer(GL_ARRAY_BUFFER, vboID[0]); glBufferData(GL_ARRAY_BUFFER, 12*sizeof(GLfloat), vert, GL_STATIC_DRAW); shaderManager["Simple"]->VertexAttribPointer("inPosition",3,GL_FLOAT,0,0); shaderManager["Simple"]->EnableAttribArray("inPosition"); glBindBuffer(GL_ARRAY_BUFFER, vboID[1]); glBufferData(GL_ARRAY_BUFFER, 8*sizeof(GLfloat), texCoords, GL_STATIC_DRAW); shaderManager["Simple"]->VertexAttribPointer("inTexCoord",2,GL_FLOAT,0,0); shaderManager["Simple"]->EnableAttribArray("inTexCoord"); delete [] vert; delete [] texCoords; glGenTextures(1, &textureID); glBindTexture(GL_TEXTURE_2D, textureID); glfwLoadTexture2D("data/images/textures/sharavaa.tga", 0); 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_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glGenerateMipmap(GL_TEXTURE_2D); }
bool RTRS::TextureManager::ItlLoadDefaultTexture(const char* pcTexturePath, TEXTURETYPE eTextureType) { bool bOk = true; switch(eTextureType) { case TEXTURETYPE::COLOR: { glGenTextures(1, &m_glnDefaultColorTexture); glBindTexture(GL_TEXTURE_2D, m_glnDefaultColorTexture); break; } case TEXTURETYPE::NORMAL: { glGenTextures(1, &m_glnDefaultNormalTexture); glBindTexture(GL_TEXTURE_2D, m_glnDefaultNormalTexture); break; } case TEXTURETYPE::BUMP: { glGenTextures(1, &m_glnDefaultBumpTexture); glBindTexture(GL_TEXTURE_2D, m_glnDefaultBumpTexture); break; } case TEXTURETYPE::SPECULAR: { glGenTextures(1, &m_glnDefaultSpecularTexture); glBindTexture(GL_TEXTURE_2D, m_glnDefaultSpecularTexture); break; } default: { glGenTextures(1, &m_glnDefaultColorTexture); glBindTexture(GL_TEXTURE_2D, m_glnDefaultColorTexture); break; } } if(glfwLoadTexture2D(pcTexturePath, 0)) { glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); bOk = true; } else bOk = false; glBindTexture(GL_TEXTURE_2D, 0); //check for an Opengl error CheckForOpenGLError(); return bOk; }
/** * @brief get_2dtga_pixel: obtain '.tga' 2D texture data * @param name: filename * @param width: image width * @param height: image height * @param format: image color format * @param type: * @return image data */ GLvoid* get_2dtga_pixel(const char* name, int width, int height, GLenum format, GLenum type) { GLuint tmp; GLvoid* img = new float[width * height * 3]; glGenTextures(1, &tmp); glBindTexture(GL_TEXTURE_2D, tmp); glfwLoadTexture2D(name, 0); glGetTexImage(GL_TEXTURE_2D, 0, format, type, img); return img; }
// init the font bool OglUtilsInitFont() { float fCharX; float fCharY; // Load texture glBindTexture(GL_TEXTURE_2D, 1); if (GL_TRUE == glfwLoadTexture2D("data/Font2048.tga", GLFW_BUILD_MIPMAPS_BIT)) { printf("Loaded texture successfully.\n"); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_LOD_BIAS, -0.5); } else { printf("Error: Failed to load texture.\n"); return false; } glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // Font 1 oFontBase[0] = glGenLists(256); for (int iLoop1 = 0; iLoop1 < 256; iLoop1++) { fCharX = float(iLoop1 % 16) / 16.0f; fCharY = float(iLoop1 / 16) / 16.0f; glNewList(oFontBase[0] + iLoop1, GL_COMPILE); //const float offset = 0.002f; const float offset = 0.004f; #if DECISION_RENDER_TEXT_VCENTERED_MID float VerticalOffset = 0.00125f; if (('a' <= iLoop1 && iLoop1 <= 'z') || '_' == iLoop1) VerticalOffset = -0.00225f; #else float VerticalOffset = 0; #endif glBegin(GL_QUADS); glTexCoord2f(fCharX+offset, 1 - fCharY - 0.0625f+offset + VerticalOffset); glVertex2i(0, 16); glTexCoord2f(fCharX + 0.0625f-offset, 1 - fCharY - 0.0625f+offset + VerticalOffset); glVertex2i(16, 16); glTexCoord2f(fCharX + 0.0625f-offset, 1 - fCharY-offset + VerticalOffset); glVertex2i(16, 0); glTexCoord2f(fCharX+offset, 1 - fCharY-offset + VerticalOffset); glVertex2i(0, 0); glEnd(); glTranslated(fontWidth[0], 0.0, 0.0); glEndList(); } return true; }
Setting::DisplayMap() { if ( isDisplayMap == false ) { glGenTextures( 1, &maps ); glBindTexture( GL_TEXTURE_2D, maps ); // Set texture parameters glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR ); glfwLoadTexture2D( "../Model/grass.tga", GLFW_BUILD_MIPMAPS_BIT ); } }
void EnemyShip::loadTexture() { glGenTextures(1, &texture); //gera 3 texturas no vetor textures (GLuint textures[3]) glBindTexture(GL_TEXTURE_2D, texture); //binda a textura da nave parada (0) GLuint flags = GLFW_BUILD_MIPMAPS_BIT | GLFW_ALPHA_MAP_BIT; if(glfwLoadTexture2D("mehsw-enemy.tga", flags) == GL_FALSE) //carrega a textura std::cout << "falha ao carregar textura do inimigo" << std::endl; glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); //seta o MIN filter glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); //set o MAG filter glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT ); //seta o wrap glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ); }
bool RTRS::TextureManager::ItlLoadTexture(const char* pcTextureName, const char* pcTexturePath, TEXTURETYPE eTextureType) { bool bOk = true; m_pLogger->debug() << "Loading texture: " << pcTexturePath << m_pLogger->endl(); RTRS::TextureMap* pTextureMap = NULL; switch(eTextureType) { case TEXTURETYPE::COLOR: pTextureMap = m_pColorTextureMap; break; case TEXTURETYPE::NORMAL: pTextureMap = m_pNormalTextureMap; break; case TEXTURETYPE::BUMP: pTextureMap = m_pBumpTextureMap; break; case TEXTURETYPE::SPECULAR: pTextureMap = m_pSpecularTextureMap; break; } assert(pTextureMap != NULL); RTRS::TextureMap::iterator iter = pTextureMap->find(pcTextureName); if(iter == pTextureMap->end()) { GLuint nTextureID; glGenTextures(1, &nTextureID); glBindTexture(GL_TEXTURE_2D, nTextureID); if(glfwLoadTexture2D(pcTexturePath, 0)) { glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); pTextureMap->insert(RTRS::TextureMapPair(pcTextureName, nTextureID)); bOk = true; } else bOk = false; glBindTexture(GL_TEXTURE_2D, 0); //check for an Opengl error CheckForOpenGLError(); } else { bOk = false; } return bOk; }
void TScreenItem::loadframe(TFrame* tar,const char* path) { printf("%d load frame %s\n",this,path); glEnable(GL_TEXTURE_2D); glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_BLEND); glGenTextures(1,&(tar->content)); glBindTexture(GL_TEXTURE_2D,tar->content); 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); glfwLoadTexture2D(path,0); if(!Anim) Anim=tar;else Now->next=tar; Now=tar; }
bool build_texutre_from_file(const char *filepath, texture_t &tex) { GLuint texture_handle; glGenTextures(1, &texture_handle); glBindTexture(GL_TEXTURE_2D, texture_handle); if (!glfwLoadTexture2D(filepath, GLFW_BUILD_MIPMAPS_BIT)) return false; glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glBindTexture(GL_TEXTURE_2D, 0); tex.handle = texture_handle; return true; }
void window_setup_drawing(char * tilemap_image) { glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho (0, num_tiles_x, num_tiles_y, 0, 0, 1); glDisable(GL_DEPTH_TEST); glEnable(GL_TEXTURE_2D); glBindTexture (GL_TEXTURE_2D, GFX_TILESET_ID); glfwLoadTexture2D(tilemap_image, GLFW_ORIGIN_UL_BIT); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); //glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); }
bool Texture::load(string name) { _name = name; if ( _isLoaded ) return true; glGenTextures(1, &_textureID); glBindTexture(GL_TEXTURE_2D, _textureID); glfwLoadTexture2D(name.c_str(), 0); 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_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glGenerateMipmap(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, 0); _isLoaded = true; return true; }
void Mesh::createNormalMap(std::string filename) { glGenTextures(1, &this->normalMapID); glBindTexture(GL_TEXTURE_2D, this->normalMapID); checkGLError("Could not generate and bind the texture"); //Note, the below should be fully customizable, but today is not that day 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_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); checkGLError("Could not set Texture parameters"); //Load texture in and create mipmaps if (!glfwLoadTexture2D(filename.c_str(),0)) { std::cout << "Could not load texture: " << filename << std::endl; throw "Could not load texture"; } glGenerateMipmap(GL_TEXTURE_2D); checkGLError("Could not generate mipmaps"); glBindTexture(GL_TEXTURE_2D, 0); }
GLuint loadTGA_glfw(const char * imagepath){ // Create one OpenGL texture GLuint textureID; glGenTextures(1, &textureID); // "Bind" the newly created texture : all future texture functions will modify this texture glBindTexture(GL_TEXTURE_2D, textureID); // Read the file, call glTexImage2D with the right parameters glfwLoadTexture2D(imagepath, 0); // Nice trilinear filtering. 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_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glGenerateMipmap(GL_TEXTURE_2D); // Return the ID of the texture we just created return textureID; }
void InitializeOpenGL() { glEnable(GL_TEXTURE_2D); { glBindTexture(GL_TEXTURE_2D, 2); // http://clkoerner.com/2011/10/13/apple-linen/ if (GL_TRUE == glfwLoadTexture2D("data/Background.tga", GLFW_ORIGIN_UL_BIT | GLFW_BUILD_MIPMAPS_BIT)) { glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); } else throw 1; } if (!OglUtilsInitFont()) glfwCloseWindow(); glEnable(GL_CULL_FACE); //glClearColor(0.600f, 0.741f, 0.565f, 1); glClearColor(1, 1, 1, 1); }
void LoadTextures( void ) { int i; // Generate texture objects glGenTextures( NUM_TEXTURES, tex_id ); // Load textures for( i = 0; i < NUM_TEXTURES; i ++ ) { // Select texture object glBindTexture( GL_TEXTURE_2D, tex_id[ i ] ); // Set texture parameters 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_LINEAR ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); // Upload texture from file to texture memory glfwLoadTexture2D( tex_name[ i ], 0 ); } }
void Graphics_init() { glfwInit(); glfwOpenWindow(WIDTH, HEIGHT, 0, 0, 0, 0, 0, 0, GLFW_WINDOW); glfwSetWindowCloseCallback(&closeWindowCallBack, NULL); glDisable(GL_CULL_FACE); glPixelStorei(GL_UNPACK_ALIGNMENT, 4); // 4-byte pixel alignment glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0,WIDTH,HEIGHT,0,-1, 1); glMatrixMode(GL_MODELVIEW); //glLoadIdentity(); glEnable(GL_BLEND); glDisable(GL_DEPTH_TEST); //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_SRC_ALPHA, GL_ONE); glClearColor(0,0,0,1); glClear(GL_COLOR_BUFFER_BIT); printGLError(); initVBO(); printGLError(); glGenTextures(1, &texture); glBindTexture(GL_TEXTURE_2D,texture); glfwLoadTexture2D("ball.tga", GLFW_BUILD_MIPMAPS_BIT); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); glEnable( GL_TEXTURE_2D ); printGLError(); }
void GuiRender::loadSkin(string skinImage, string skinFile){ glGenTextures(1,&skinID); glBindTexture(GL_TEXTURE_2D,skinID); if(glfwLoadTexture2D(skinImage.c_str(), GLFW_ORIGIN_UL_BIT)){ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); } ifstream file; file.open(skinFile.c_str(),ios::in); if(file.good()){ string vrstica,name; for(int count=0;!file.eof();count++){ getline(file,vrstica); stringstream stream(vrstica); stream >> name >> textures[count][0] >> textures[count][1] >> textures[count][4] >> textures[count][5]; textures[count][6] = textures[count][0] /= 128; textures[count][3] = textures[count][1] /= 128; textures[count][2] = textures[count][4] /= 128; textures[count][7] = textures[count][5] /= 128; } }
void Demo03::Initialize() { // particle өгөгдлүүдийг агуулах бөгөөд texel-д хандахад зориулагдсан цэгүүд GLfloat points[PARTICLE_TEXTURE_WIDTH * PARTICLE_TEXTURE_WIDTH * 2]; // эхний particle-үүдийг агуулах массив GLfloat particle[PARTICLE_TEXTURE_WIDTH * PARTICLE_TEXTURE_WIDTH * 4]; GLuint x, y; for (y = 0; y < PARTICLE_TEXTURE_WIDTH; y++) { for (x = 0; x < PARTICLE_TEXTURE_WIDTH; x++) { points[x * 2 + 0 + y * PARTICLE_TEXTURE_WIDTH * 2] = (GLfloat) x / (GLfloat) PARTICLE_TEXTURE_WIDTH; points[x * 2 + 1 + y * PARTICLE_TEXTURE_WIDTH * 2] = (GLfloat) y / (GLfloat) PARTICLE_TEXTURE_WIDTH; } } for (y = 0; y < PARTICLE_TEXTURE_WIDTH; y++) { for (x = 0; x < PARTICLE_TEXTURE_WIDTH; x++) { particle[x * 4 + 0 + y * PARTICLE_TEXTURE_WIDTH * 4] = 0.0f; particle[x * 4 + 1 + y * PARTICLE_TEXTURE_WIDTH * 4] = 0.0f; particle[x * 4 + 2 + y * PARTICLE_TEXTURE_WIDTH * 4] = 0.0f; particle[x * 4 + 3 + y * PARTICLE_TEXTURE_WIDTH * 4] = -1.0f; // w < 0.0 гэдэг нь particle үхсэн гэсэн үг. тэгэхээр дараагийн фрэймд дахин шинэчлэгдэнэ. } } // particle shader shaderManager.CreateShaderProgram("Particle"); shaderManager.AttachShader("ParticleVertex" , VERTEX); shaderManager.AttachShader("ParticleFragment", FRAGMENT); shaderManager.LoadShaderSource("ParticleVertex" , "data/shaders/demo03/particle.vert.glsl"); shaderManager.LoadShaderSource("ParticleFragment", "data/shaders/demo03/particle.frag.glsl"); shaderManager.CompileShader("ParticleVertex"); shaderManager.CompileShader("ParticleFragment"); shaderManager.AttachShaderToProgram("Particle", "ParticleVertex"); shaderManager.AttachShaderToProgram("Particle", "ParticleFragment"); shaderManager.BindAttribute("Particle", 0, "a_vertex"); shaderManager.LinkProgramObject("Particle"); // point updater shader shaderManager.CreateShaderProgram("PointUpdater"); shaderManager.AttachShader("PointUpdaterVertex" , VERTEX); shaderManager.AttachShader("PointUpdaterFragment", FRAGMENT); shaderManager.LoadShaderSource("PointUpdaterVertex" , "data/shaders/demo03/update_points.vert.glsl"); shaderManager.LoadShaderSource("PointUpdaterFragment", "data/shaders/demo03/update_points.frag.glsl"); shaderManager.CompileShader("PointUpdaterVertex"); shaderManager.CompileShader("PointUpdaterFragment"); shaderManager.AttachShaderToProgram("PointUpdater", "PointUpdaterVertex"); shaderManager.AttachShaderToProgram("PointUpdater", "PointUpdaterFragment"); shaderManager.BindAttribute("PointUpdater", 0, "a_vertex"); shaderManager.LinkProgramObject("PointUpdater"); glGenTextures(1, &textureID); glBindTexture(GL_TEXTURE_2D, textureID); glfwLoadTexture2D("data/images/textures/particle.tga", 0); 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_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glBindTexture(GL_TEXTURE_2D, 0); glGenTextures(2, positionTextures); glBindTexture(GL_TEXTURE_2D, positionTextures[0]); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, PARTICLE_TEXTURE_WIDTH, PARTICLE_TEXTURE_WIDTH, 0, GL_RGBA, GL_FLOAT, particle); // float төрөлтэй тоонууд агуулсан текстур үүсгэх 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_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glBindTexture(GL_TEXTURE_2D, 0); glBindTexture(GL_TEXTURE_2D, positionTextures[1]); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, PARTICLE_TEXTURE_WIDTH, PARTICLE_TEXTURE_WIDTH, 0, GL_RGBA, GL_FLOAT, particle); 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_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glBindTexture(GL_TEXTURE_2D, 0); glGenFramebuffers(2, positionFramebuffers); // хоёр ширхэг framebuffer үүсгэх glBindFramebuffer(GL_FRAMEBUFFER, positionFramebuffers[0]); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, positionTextures[0], 0); // текстур зураг зааж өгөх glBindFramebuffer(GL_FRAMEBUFFER, positionFramebuffers[1]); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, positionTextures[1], 0); // текстур зураг зааж өгөх glBindFramebuffer(GL_FRAMEBUFFER, 0); glGenBuffers(1, &verticesSphereVBO); glBindBuffer(GL_ARRAY_BUFFER, verticesSphereVBO); glBufferData(GL_ARRAY_BUFFER, PARTICLE_TEXTURE_WIDTH * PARTICLE_TEXTURE_WIDTH * 2 * sizeof(GLfloat), (GLfloat*) points, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); shaderManager["Particle"]->Activate(); glActiveTexture(GL_TEXTURE0); shaderManager["Particle"]->SetUniform1i("u_texture", 0); glBindTexture(GL_TEXTURE_2D, textureID); glActiveTexture(GL_TEXTURE1); shaderManager["Particle"]->SetUniform1i("u_positionTexture", 0); glGenVertexArrays(1, &sphereVAO); glBindVertexArray(sphereVAO); glBindBuffer(GL_ARRAY_BUFFER, verticesSphereVBO); shaderManager["Particle"]->VertexAttribPointer("a_vertex", 2, GL_FLOAT, GL_FALSE, 0, 0); shaderManager["Particle"]->EnableAttribArray("a_vertex"); shaderManager["Particle"]->Deactivate(); shaderManager["PointUpdater"]->Activate(); shaderManager["PointUpdater"]->SetUniform1i("u_positionTexture", 1); // texture unit 1 идэвхитэй байгаа. shaderManager["PointUpdater"]->SetUniform1f("u_positionTextureWidth", (GLfloat) PARTICLE_TEXTURE_WIDTH); glGenVertexArrays(1, &vaoUpdatePoints); // програм бүрт VAO ашиглах болно glBindVertexArray(vaoUpdatePoints); glBindBuffer(GL_ARRAY_BUFFER, verticesSphereVBO); shaderManager["PointUpdater"]->VertexAttribPointer("a_vertex", 2, GL_FLOAT, GL_FALSE, 0, 0); shaderManager["PointUpdater"]->EnableAttribArray("a_vertex"); glEnable(GL_PROGRAM_POINT_SIZE); // gl_PointSize-ийг shader програмд хэрэглэнэ glBlendFunc(GL_SRC_ALPHA, GL_ONE); // particle-үүдийг нэвтрэлттэй харуулах үүрэгтэй }
int main( void ) { int width, height, x; double time; GLboolean running; GLuint textureID; char* texturePath = "mipmaps.tga"; // Initialise GLFW if( !glfwInit() ) { fprintf( stderr, "Failed to initialize GLFW\n" ); exit( EXIT_FAILURE ); } // Open OpenGL window if( !glfwOpenWindow( 640, 480, 0,0,0,0, 0,0, GLFW_WINDOW ) ) { fprintf( stderr, "Failed to open GLFW window\n" ); glfwTerminate(); exit( EXIT_FAILURE ); } glfwSetWindowTitle( "Trilinear interpolation" ); // Enable sticky keys glfwEnable( GLFW_STICKY_KEYS ); // Enable vertical sync (on cards that support it) glfwSwapInterval( 1 ); // Generate and bind our texture ID glGenTextures( 1, &textureID ); glBindTexture( GL_TEXTURE_2D, textureID ); // Load texture from file into video memory, including mipmap levels if( !glfwLoadTexture2D( texturePath, GLFW_BUILD_MIPMAPS_BIT ) ) { fprintf( stderr, "Failed to load texture %s\n", texturePath ); glfwTerminate(); exit( EXIT_FAILURE ); } // Use trilinear interpolation (GL_LINEAR_MIPMAP_LINEAR) glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); // Enable plain 2D texturing glEnable( GL_TEXTURE_2D ); running = GL_TRUE; while( running ) { // Get time and mouse position time = glfwGetTime(); glfwGetMousePos( &x, NULL ); // Get window size (may be different than the requested size) glfwGetWindowSize( &width, &height ); height = height > 0 ? height : 1; // Set viewport glViewport( 0, 0, width, height ); // Clear color buffer glClearColor( 0.0f, 0.0f, 0.0f, 0.0f); glClear( GL_COLOR_BUFFER_BIT ); // Select and setup the projection matrix glMatrixMode( GL_PROJECTION ); glLoadIdentity(); gluPerspective( 65.0f, (GLfloat)width / (GLfloat)height, 1.0f, 50.0f ); // Select and setup the modelview matrix glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); gluLookAt( 0.0f, 3.0f, -20.0f, // Eye-position 0.0f, -4.0f, -11.0f, // View-point 0.0f, 1.0f, 0.0f ); // Up-vector // Draw a textured quad glRotatef( 0.05f * (GLfloat)x + (GLfloat)time * 5.0f, 0.0f, 1.0f, 0.0f ); glBegin( GL_QUADS ); glTexCoord2f( -20.0f, 20.0f ); glVertex3f( -50.0f, 0.0f, -50.0f ); glTexCoord2f( 20.0f, 20.0f ); glVertex3f( 50.0f, 0.0f, -50.0f ); glTexCoord2f( 20.0f, -20.0f ); glVertex3f( 50.0f, 0.0f, 50.0f ); glTexCoord2f( -20.0f, -20.0f ); glVertex3f( -50.0f, 0.0f, 50.0f ); glEnd(); // Swap buffers glfwSwapBuffers(); // Check if the ESC key was pressed or the window was closed running = !glfwGetKey( GLFW_KEY_ESC ) && glfwGetWindowParam( GLFW_OPENED ); } // Close OpenGL window and terminate GLFW glfwTerminate(); exit( EXIT_SUCCESS ); }
// main function used to load a BMF font // note: must be called within a valid OpenGL context because it allocates // a texture and creates display lists for the font bool bmf_load(bm_font *font, char *bmf_path) { FILE* bmf = NULL; char linebuf[4096]; char *readresult; int i; long p; char tga_path[BMF_MAX_PATH]; BMchar temp_char; BMkerninfo temp_kerning; bmf = fopen(bmf_path, "r"); if (!bmf) return false; fgets(linebuf, 4096, bmf); // skip the first info line // read header readresult = fgets(linebuf, 4096, bmf); if (sscanf(linebuf, "common lineHeight=%d base=%d scaleW=%d scaleH=%d pages=%d\n", &font->line_h, &font->base, &font->w, &font->h, &font->pages) != 5) { fclose(bmf); return false; } if (fscanf(bmf, "page id=0 file=%s\n", tga_path) != 1) { fclose(bmf); return false; } if (fscanf(bmf, "chars count=%d\n", &font->chars_count) != 1) { fclose(bmf); return false; } // allocate (and init to 0) the chars font->chars = (BMchar *) zcalloc(font->chars_count, sizeof(BMchar)); i = 0; NextChar: // read a character from the input file // note that the ordinal 'i' is also read and in a second pass temp_char is // swapped into the main array of chars readresult = fgets(linebuf, 4096, bmf); if (readresult && sscanf(linebuf, "char id=%d x=%d y=%d width=%d height=%d xoffset=%d yoffset=%d xadvance=%d page=%d\n", &temp_char.id, &temp_char.x, &temp_char.y, &temp_char.w, &temp_char.h, &temp_char.x_ofs, &temp_char.y_ofs, &temp_char.x_advance, &temp_char.page) == 9) { //assert( i==0 || font->chars[i-1].id < temp_char.id ); // assume sorted order for binary search font->chars[i] = temp_char; i++; goto NextChar; } if (sscanf(linebuf, "kernings count=%d\n", &font->kerninfo_count) != 1) { fclose(bmf); goto Fail; } // allocate (and init to 0) the kernings font->kerninfo = (BMkerninfo *) zcalloc(font->kerninfo_count, sizeof(BMkerninfo)); i = 0; NextKerning: // read a character from the input file // note that the ordinal 'i' is also read and in a second pass temp_char is // swapped into the main array of chars readresult = fgets(linebuf, 4096, bmf); if (readresult && sscanf(linebuf, "kerning first=%d second=%d amount=%d\n", &temp_kerning.first, &temp_kerning.second, &temp_kerning.kerning ) == 3) { font->kerninfo[i] = temp_kerning; i++; goto NextKerning; } fclose(bmf); // close the source file /* OLD p = (long)strrchr(bmf_path, '.'); if (!p) goto Fail; p-=(long)bmf_path; memcpy(tga_path, bmf_path, p+1); strcpy(tga_path+p+1, "tga"); */ // TODO compose the tga path relative to the given bmf_path // remove quotes memmove(tga_path, tga_path+1, strlen(tga_path)-1+1); tga_path[strlen(tga_path)-1] = 0; glGenTextures(1, &font->fontTex); // create the main font texture glBindTexture(GL_TEXTURE_2D, font->fontTex); // and set as current glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR ); if (!glfwLoadTexture2D(tga_path, GLFW_BUILD_MIPMAPS_BIT)) { // try to load the font TGA file glDeleteTextures(1, &font->fontTex); zfree(font->kerninfo); Fail: zfree(font->chars); return false; } font->scale = 1.f; return true; // into an OpenGL display list }
bool SetupScene(int texBase, GLuint *textures) { if (!glfwInit()) { printf("glfwInit() failed\n"); return false; } if( !glfwOpenWindow(640, 480, 0, 0, 0, 0, 0, 0, GLFW_WINDOW)) { printf("glfwOpenWindow() failed\n"); return false; } glfwSetWindowTitle("Heavy Metal"); glGenTextures(NUM_TEXTURES, textures); glBindTexture(GL_TEXTURE_2D, textures[BLOCK_TEXTURE]); if (!glfwLoadTexture2D("block.tga", GLFW_BUILD_MIPMAPS_BIT)) { printf("Texture load failed.\n"); //return false; } else { glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); } glBindTexture(GL_TEXTURE_2D, textures[BG_TEXTURE]); if (!glfwLoadTexture2D("bg.tga", GLFW_BUILD_MIPMAPS_BIT)) { printf("Texture load failed.\n"); //return false; } else { 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 ); } glBindTexture(GL_TEXTURE_2D, textures[FONT_TEXTURE]); if (!glfwLoadTexture2D("font.tga", GLFW_BUILD_MIPMAPS_BIT)) { printf("Texture load failed.\n"); } else { 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 ); } glBindTexture(GL_TEXTURE_2D, textures[MAIN_BG_TEXTURE]); if (!glfwLoadTexture2D("main_bg.tga", GLFW_BUILD_MIPMAPS_BIT)) { printf("Texture load failed.\n"); } else { 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 ); } glEnable(GL_TEXTURE_2D); glViewport(0, 0, 640, 480); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0f, 640, 480, 0.0f, -1.0f, 1.0f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); // The Type Of Depth Test To Do glShadeModel(GL_SMOOTH); /*glCullFace( GL_FRONT ); glEnable( GL_CULL_FACE );*/ //load the font into the calllists for later use GenerateFont(texBase, textures); game->CaptureKeys(true); return true; }
Terrain::Terrain(void) { _width = 1024; _height = 1024; ifstream textFile("height.dat"); if(!textFile.is_open()) { cerr << "Error whilst reading heightmap" << endl; EndGame(1); } else { for(int z = 0; z < 1024; z++) { getline(textFile, _line); if(_line.empty()) { break; } for(int x = 0; x < 1024; x++) { pos = _line.find(' '); if(pos == string::npos) { break; } string _line2(_line, 0, pos); stringstream strh(_line2); strh >> _heightData[z][x]; _line.erase(0,pos+1); } } textFile.close(); } glGenTextures(1, &_tex); glBindTexture(GL_TEXTURE_2D, _tex); // GL_MODULATE for correct lighting and shit, not really needed because of shaders. // When the texture area is small, bilinear filter the closest mipmap. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); // When texture area is large, bilinear filter the original. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); if(glfwLoadTexture2D("terrain1.tga", GLFW_BUILD_MIPMAPS_BIT) != GL_TRUE) { cerr << "Error while reading the terrain texture!" << endl; EndGame(1); } // Calculate the normals. _normals = new Vec3f*[_height]; Vec3f** normals2 = new Vec3f*[_height]; for(int i = 0; i < _height; i++) { _normals[i] = new Vec3f[_width]; normals2[i] = new Vec3f[_width]; } // Rough normals. for(int z = 0; z < _height; z++) { for(int x = 0; x < _width; x++) { Vec3f sum(0.0, 0.0, 0.0); Vec3f down; if(z > 0) { down = Vec3f(0.0, GetHeight(x, z-1) - GetHeight(x, z), -1.0); } Vec3f up; if(z < _height-1) { up = Vec3f(0.0, GetHeight(x, z+1) - GetHeight(x,z), 1.0); } Vec3f left; if(x > 0) { left = Vec3f(-1.0, GetHeight(x-1, z) - GetHeight(x,z), 0.0); } Vec3f right; if(x < _width-1) { right = Vec3f(1.0, GetHeight(x+1, z) - GetHeight(x,z), 0.0); } if(x > 0 && z > 0) { sum += down.Cross(left).Normalize(); } if(x > 0 && z < _height-1) { sum += left.Cross(up).Normalize(); } if(x < _width-1 && z < _height-1) { sum += up.Cross(right).Normalize(); } if(x < _width-1 && z > 0) { sum += right.Cross(down).Normalize(); } normals2[x][z] = sum; } } // Smooth notmals. const float FALLOUT_RATIO = 0.5f; for(int z = 0; z < _height; z++) { for(int x = 0; x < _width; x++) { Vec3f sum = normals2[x][z]; if(x > 0) { sum += normals2[x-1][z] * FALLOUT_RATIO; } if(x < _width-1) { sum += normals2[x+1][z] * FALLOUT_RATIO; } if(z > 0) { sum += normals2[x][z-1] * FALLOUT_RATIO; } if(z < _height-1) { sum += normals2[x][z+1] * FALLOUT_RATIO; } if(sum.Magnitude() == 0) { sum = Vec3f(0.0, 1.0, 0.0); } _normals[x][z] = sum; } } // Delete the first array. for(int i = 0; i < _height; i++) { delete [] normals2[i]; } delete [] normals2; }
int main(int argc, char **argv) { srand(time(NULL)); rand(); if (!glfwInit()) { return -1; } #ifdef __APPLE__ glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3); glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 2); glfwOpenWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); #endif if (!glfwOpenWindow(800, 600, 8, 8, 8, 0, 24, 0, GLFW_WINDOW)) { return -1; } glfwSwapInterval(VSYNC); glfwDisable(GLFW_MOUSE_CURSOR); glfwSetWindowTitle("Modern GL"); glfwSetKeyCallback(on_key); glfwSetMouseButtonCallback(on_mouse_button); #ifndef __APPLE__ if (glewInit() != GLEW_OK) { return -1; } #endif if (db_init()) { return -1; } glEnable(GL_CULL_FACE); glEnable(GL_DEPTH_TEST); glEnable(GL_LINE_SMOOTH); glLogicOp(GL_INVERT); glClearColor(0.53, 0.81, 0.92, 1.00); GLuint vertex_array; glGenVertexArrays(1, &vertex_array); glBindVertexArray(vertex_array); GLuint texture; glGenTextures(1, &texture); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, texture); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glfwLoadTexture2D("texture.tga", 0); GLuint block_program = load_program( "shaders/block_vertex.glsl", "shaders/block_fragment.glsl"); GLuint matrix_loc = glGetUniformLocation(block_program, "matrix"); GLuint camera_loc = glGetUniformLocation(block_program, "camera"); GLuint sampler_loc = glGetUniformLocation(block_program, "sampler"); GLuint timer_loc = glGetUniformLocation(block_program, "timer"); GLuint position_loc = glGetAttribLocation(block_program, "position"); GLuint normal_loc = glGetAttribLocation(block_program, "normal"); GLuint uv_loc = glGetAttribLocation(block_program, "uv"); GLuint line_program = load_program( "shaders/line_vertex.glsl", "shaders/line_fragment.glsl"); GLuint line_matrix_loc = glGetUniformLocation(line_program, "matrix"); GLuint line_position_loc = glGetAttribLocation(line_program, "position"); Chunk chunks[MAX_CHUNKS]; int chunk_count = 0; FPS fps = {0, 0}; float matrix[16]; float x = (rand_double() - 0.5) * 10000; float z = (rand_double() - 0.5) * 10000; float y = 0; float dy = 0; float rx = 0; float ry = 0; int px = 0; int py = 0; int loaded = db_load_state(&x, &y, &z, &rx, &ry); ensure_chunks(chunks, &chunk_count, floorf(roundf(x) / CHUNK_SIZE), floorf(roundf(z) / CHUNK_SIZE), 1); if (!loaded) { y = highest_block(chunks, chunk_count, x, z) + 2; } glfwGetMousePos(&px, &py); double previous = glfwGetTime(); while (glfwGetWindowParam(GLFW_OPENED)) { update_fps(&fps, SHOW_FPS); double now = glfwGetTime(); double dt = MIN(now - previous, 0.2); previous = now; if (exclusive) { int mx, my; glfwGetMousePos(&mx, &my); float m = 0.0025; rx += (mx - px) * m; ry -= (my - py) * m; if (rx < 0) { rx += RADIANS(360); } if (rx >= RADIANS(360)){ rx -= RADIANS(360); } ry = MAX(ry, -RADIANS(90)); ry = MIN(ry, RADIANS(90)); px = mx; py = my; } else { glfwGetMousePos(&px, &py); } if (left_click) { left_click = 0; int hx, hy, hz; if (hit_test(chunks, chunk_count, 0, x, y, z, rx, ry, &hx, &hy, &hz)) { if (hy > 0) { set_block(chunks, chunk_count, hx, hy, hz, 0); } } } if (right_click) { right_click = 0; int hx, hy, hz; if (hit_test(chunks, chunk_count, 1, x, y, z, rx, ry, &hx, &hy, &hz)) { if (!player_intersects_block(2, x, y, z, hx, hy, hz)) { set_block(chunks, chunk_count, hx, hy, hz, block_type); } } } int sz = 0; int sx = 0; ortho = glfwGetKey(GLFW_KEY_LSHIFT); if (glfwGetKey('Q')) break; if (glfwGetKey('W')) sz--; if (glfwGetKey('S')) sz++; if (glfwGetKey('A')) sx--; if (glfwGetKey('D')) sx++; if (dy == 0 && glfwGetKey(GLFW_KEY_SPACE)) { dy = 8; } float vx, vy, vz; get_motion_vector(flying, sz, sx, rx, ry, &vx, &vy, &vz); float speed = flying ? 20 : 5; int step = 8; float ut = dt / step; vx = vx * ut * speed; vy = vy * ut * speed; vz = vz * ut * speed; for (int i = 0; i < step; i++) { if (flying) { dy = 0; } else { dy -= ut * 25; dy = MAX(dy, -250); } x += vx; y += vy + dy * ut; z += vz; if (collide(chunks, chunk_count, 2, &x, &y, &z)) { dy = 0; } } int p = floorf(roundf(x) / CHUNK_SIZE); int q = floorf(roundf(z) / CHUNK_SIZE); ensure_chunks(chunks, &chunk_count, p, q, 0); update_matrix_3d(matrix, x, y, z, rx, ry); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // render chunks glUseProgram(block_program); glUniformMatrix4fv(matrix_loc, 1, GL_FALSE, matrix); glUniform3f(camera_loc, x, y, z); glUniform1i(sampler_loc, 0); glUniform1f(timer_loc, glfwGetTime()); for (int i = 0; i < chunk_count; i++) { Chunk *chunk = chunks + i; if (chunk_distance(chunk, p, q) > RENDER_CHUNK_RADIUS) { continue; } if (!chunk_visible(chunk, matrix)) { continue; } draw_chunk(chunk, position_loc, normal_loc, uv_loc); } // render focused block wireframe int hx, hy, hz; if (hit_test(chunks, chunk_count, 0, x, y, z, rx, ry, &hx, &hy, &hz)) { glUseProgram(line_program); glLineWidth(1); glEnable(GL_COLOR_LOGIC_OP); glUniformMatrix4fv(line_matrix_loc, 1, GL_FALSE, matrix); GLuint buffer = make_cube_buffer(hx, hy, hz, 0.51); draw_lines(buffer, line_position_loc, 3, 48); glDeleteBuffers(1, &buffer); glDisable(GL_COLOR_LOGIC_OP); } update_matrix_2d(matrix); // render crosshairs glUseProgram(line_program); glLineWidth(4); glEnable(GL_COLOR_LOGIC_OP); glUniformMatrix4fv(line_matrix_loc, 1, GL_FALSE, matrix); GLuint buffer = make_line_buffer(); draw_lines(buffer, line_position_loc, 2, 4); glDeleteBuffers(1, &buffer); glDisable(GL_COLOR_LOGIC_OP); glfwSwapBuffers(); } db_save_state(x, y, z, rx, ry); db_close(); glfwTerminate(); return 0; }