TexCoords Atlas::getParentTileTexCoords(tileId id) { tileId parentId = tileId(id.z()-1, id.x()/2, id.y()/2); if (parentId.z() == 0) return TexCoords(0, glm::vec4(0.0f)); TexCoords parentCoords = getTileTexCoords(parentId); if (parentCoords.texId == 0) parentCoords = getParentTileTexCoords(parentId); if (parentCoords.texId == 0) return TexCoords(0, glm::vec4(0.0f)); glm::vec4 pCoords = parentCoords.coords; glm::vec4 childCoords(0); childCoords[0] = pCoords[0]+(pCoords[2]-pCoords[0])*0.5*(id.x()-parentId.x()*2); childCoords[1] = pCoords[1]+(pCoords[3]-pCoords[1])*0.5*(id.y()-parentId.y()*2); childCoords[2] = childCoords[0] + (pCoords[2]-pCoords[0])*0.5; childCoords[3] = childCoords[1] + (pCoords[3]-pCoords[1])*0.5; #ifdef _ATLAS_DEBUG std::cout << "[Atlas] Found parent tile=" << parentId << "for tile=" << id << std::endl; std::cout << "[Atlas] Parent coords=" << pCoords << "Child coords=" << childCoords << std::endl; #endif return TexCoords(parentCoords.texId, childCoords); }
void CreateScreenQuad( MeshData& Quad) { AttribData<vec3> Vertices(4); Vertices.Set(0, vec3(-1.0f, -1.0f, 0.0f)); Vertices.Set(1, vec3( 1.0f, -1.0f, 0.0f)); Vertices.Set(2, vec3( 1.0f, 1.0f, 0.0f)); Vertices.Set(3, vec3(-1.0f, 1.0f, 0.0f)); AttribData<vec2> TexCoords(4); TexCoords.Set(0, vec2(0.0f, 0.0f)); TexCoords.Set(1, vec2(1.0f, 0.0f)); TexCoords.Set(2, vec2(1.0f, 1.0f)); TexCoords.Set(3, vec2(0.0f, 1.0f)); AttribData<GLFace16> Faces(2); Faces.Set(0, GLFace16(0, 1, 2) ); Faces.Set(1, GLFace16(2, 3, 0) ); Polygons Polylist1; Polylist1.SetMaterial(0); Polylist1.SetFaces(Faces); AttribData<poly> Polygons(1); Polygons.Set(0, Polylist1); Quad.SetVertexArray(Vertices); Quad.SetUVArray(TexCoords); Quad.SetPolygonList(Polygons); Quad.GenerateNormals(false); Quad.GenerateTangents(); }
// Called by Rocket when it wants to render geometry that it does not wish to optimise. void RocketSDL2Renderer::RenderGeometry(Rocket::Core::Vertex* vertices, int num_vertices, int* indices, int num_indices, const Rocket::Core::TextureHandle texture, const Rocket::Core::Vector2f& translation) { // SDL uses shaders that we need to disable here glUseProgramObjectARB(0); glPushMatrix(); glTranslatef(translation.x, translation.y, 0); std::vector<Rocket::Core::Vector2f> Positions(num_vertices); std::vector<Rocket::Core::Colourb> Colors(num_vertices); std::vector<Rocket::Core::Vector2f> TexCoords(num_vertices); float texw, texh; SDL_Texture* sdl_texture = NULL; if(texture) { glEnableClientState(GL_TEXTURE_COORD_ARRAY); sdl_texture = (SDL_Texture *) texture; SDL_GL_BindTexture(sdl_texture, &texw, &texh); } for(int i = 0; i < num_vertices; i++) { Positions[i] = vertices[i].position; Colors[i] = vertices[i].colour; if (sdl_texture) { TexCoords[i].x = vertices[i].tex_coord.x * texw; TexCoords[i].y = vertices[i].tex_coord.y * texh; } else TexCoords[i] = vertices[i].tex_coord; }; glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_COLOR_ARRAY); glVertexPointer(2, GL_FLOAT, 0, &Positions[0]); glColorPointer(4, GL_UNSIGNED_BYTE, 0, &Colors[0]); glTexCoordPointer(2, GL_FLOAT, 0, &TexCoords[0]); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glDrawElements(GL_TRIANGLES, num_indices, GL_UNSIGNED_INT, indices); glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_COLOR_ARRAY); if (sdl_texture) { SDL_GL_UnbindTexture(sdl_texture); glDisableClientState(GL_TEXTURE_COORD_ARRAY); } glColor4f(1.0, 1.0, 1.0, 1.0); glPopMatrix(); /* Reset blending and draw a fake point just outside the screen to let SDL know that it needs to reset its state in case it wants to render a texture */ glDisable(GL_BLEND); SDL_SetRenderDrawBlendMode(mRenderer, SDL_BLENDMODE_NONE); SDL_RenderDrawPoint(mRenderer, -1, -1); }
void Text::PrintStage(size_t level, size_t stage, Position position, Size size) { Engine::Get().Renderer()->DrawSpriteAbsolute(TexCoords(224, 77, 100, 13), position, size); PrintStageDigit(level, Position(position.x + 6 * size.width / 10.0, position.y), Size(size.width/10, size.height)); PrintStageDigit(stage, Position(position.x + 8.5 * size.width / 10.0, position.y), Size(size.width/10, size.height)); }
TexCoords Atlas::getTileTexCoords(tileId id) { std::vector<tileId>::iterator found = std::find(tilesInPositions.begin(), tilesInPositions.end(), id); if (found == tilesInPositions.end()) { return TexCoords(0, glm::vec4(0.0f)); } int pos = found - tilesInPositions.begin(); usedTiles[pos] = true; return getTexCoordsByPos(pos); }
TexCoords Lua::GetPlayerSprite(PT::PlayerType type, Direction direction, bool is_dying, double duration) const { try { return luabind::call_function<TexCoords>(m_lua_state, "GetPlayerSprite", type, direction, is_dying, duration); } catch (luabind::error& e) { std::cerr << "An error occured while calling Lua::GetPlayerSprite: " << e.what() << "\n"; } catch(luabind::cast_failed& e) { std::cerr << "An error occured while calling Lua::GetPlayerSprite: " << e.what() << "\n"; } return TexCoords(0,0,0,0); // show nothing in case of errors }
TexCoords Atlas::getTexCoordsByPos(int pos) { int atlasNum = pos/tilesInAtlas; int posInAtlas = pos%tilesInAtlas; int rowNum = posInAtlas/tilesInRow; int colNum = posInAtlas%tilesInRow; glm::vec4 coords = glm::vec4((double)colNum*tileWidth/width, (double)rowNum*tileHeight/height, (double)(colNum+1)*tileWidth/width, (double)(rowNum+1)*tileHeight/height); return TexCoords(textureIds[atlasNum], coords); }
// Called by Rocket when it wants to render geometry that it does not wish to optimise. void RocketSFMLRenderer::RenderGeometry(Rocket::Core::Vertex* vertices, int num_vertices, int* indices, int num_indices, const Rocket::Core::TextureHandle texture, const Rocket::Core::Vector2f& translation) { MyWindow->setActive(); glPushMatrix(); glTranslatef(translation.x, translation.y, 0); std::vector<Rocket::Core::Vector2f> Positions(num_vertices); std::vector<Rocket::Core::Colourb> Colors(num_vertices); std::vector<Rocket::Core::Vector2f> TexCoords(num_vertices); for(int i = 0; i < num_vertices; i++) { Positions[i] = vertices[i].position; Colors[i] = vertices[i].colour; TexCoords[i] = vertices[i].tex_coord; }; glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_COLOR_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); glVertexPointer(2, GL_FLOAT, 0, &Positions[0]); glColorPointer(4, GL_UNSIGNED_BYTE, 0, &Colors[0]); glTexCoordPointer(2, GL_FLOAT, 0, &TexCoords[0]); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); if(texture) { glEnable(GL_TEXTURE_2D); sf::Texture::bind((sf::Texture*)texture, sf::Texture::Normalized); } else { glDisableClientState(GL_TEXTURE_COORD_ARRAY); glBindTexture(GL_TEXTURE_2D, 0); }; glDrawElements(GL_TRIANGLES, num_indices, GL_UNSIGNED_INT, indices); glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_COLOR_ARRAY); glDisableClientState(GL_TEXTURE_COORD_ARRAY); glColor4f(1, 1, 1, 1); glPopMatrix(); }
// computes toTangent*/toObject* stuff void Mesh::Prepare(void) { if(_prepared) return; unsigned numVertices = 3 * NumTriangles(); _vertices = new Vertex[numVertices]; for(unsigned i = 0; i < numVertices; ++i) { _vertices[i].numTri = 0; _vertices[i].toObject[2] = M::Vector3::Zero; } for(unsigned i = 0; i < NumTriangles(); ++i) { const Triangle* triangle = &Triangles()[i]; for(int j = 0; j < 3; ++j) { int x0 = triangle->vertices[j]; int x1 = triangle->vertices[(j + 1) % 3]; int x2 = triangle->vertices[(j + 2) % 3]; M::Vector3 a0 = Vertices()[x0]; M::Vector3 a1 = Vertices()[x1]; M::Vector3 a2 = Vertices()[x2]; M::Vector3 v1 = Vertices()[x1] - Vertices()[x0]; M::Vector3 v2 = Vertices()[x2] - Vertices()[x0]; float d1_0 = TexCoords()[x1].s - TexCoords()[x0].s; float d1_1 = TexCoords()[x1].t - TexCoords()[x0].t; float d2_0 = TexCoords()[x2].s - TexCoords()[x0].s; float d2_1 = TexCoords()[x2].t - TexCoords()[x0].t; float det = d1_0 * d2_1 - d2_0 * d1_1; _vertices[x0].toObject[0] = (d2_1 * v1 - d1_1 * v2) / det; _vertices[x0].toObject[1] = (-d2_0 * v1 + d1_0 * v2) / det; _vertices[x0].toObject[2] += M::Cross(v2, v1); _vertices[x0].numTri++; } } for(unsigned i = 0; i < numVertices; ++i) { _vertices[i].toObject[2] /= (float)_vertices[i].numTri; for(int j = 0; j < 3; ++j) { _vertices[i].toObject[j].Normalize(); } M::Vector3 A0 = _vertices[i].toObject[0]; M::Vector3 A1 = _vertices[i].toObject[1]; M::Vector3 A2 = _vertices[i].toObject[2]; M::Matrix3 mat = M::Mat3::FromColumns(A0, A1, A2); M::Matrix3 inv = M::Mat3::Inverse(mat); M::Mat3::ToColumns(inv, A0, A1, A2); _vertices[i].toTangent[0] = A0; _vertices[i].toTangent[1] = A1; _vertices[i].toTangent[2] = A2; } _prepared = true; }
namespace ngl{ //--------------------------------------------------------------------------// //--------------------------------------------------------------------------// Hash_t gen_hash(const byte* data, size_t size, Hash_t initial){ if( !data || size==0 ) return initial; Hash_t hash=initial; for(int i=0; i < size; ++i) hash = data[i] + (hash << 6) + (hash << 16) - hash; return hash; } //--------------------------------------------------------------------------// //--------------------------------------------------------------------------// Hash_t gen_hash(const String &string, Hash_t initial){ if( string.empty() ) return initial; Hash_t hash=initial; const char *str=const_cast<char*>(string.c_str()); while( *str != 0 ) hash = *str++ + (hash << 6) + (hash << 16) - hash; return hash; } const Color32 Color32::black (0xff000000); const Color32 Color32::white (0xffffffff); const Color32 Color32::grey (0xff808080); const Color32 Color32::lightGrey (0xffc0c0c0); const Color32 Color32::darkGrey (0xff404040); const Color32 Color32::red (0xff0000ff); const Color32 Color32::lightRed (0xff6717ff); const Color32 Color32::darkRed (0xff4040c0); const Color32 Color32::green (0xff00ff00); const Color32 Color32::lightGreen(0xff60ffa0); const Color32 Color32::darkGreen (0xff008000); const Color32 Color32::blue (0xffff0000); const Color32 Color32::lightBlue (0xffffa060); const Color32 Color32::darkBlue (0xff800000); const Color32 Color32::yellow (0xff00ffff); const Color32 Color32::orange (0xff3a96c4); //--------------------------------------------------------------------------// //--------------------------------------------------------------------------// Error gl_error_check(const char *msg){ Error err; if( (err=glGetError()) != GL_NO_ERROR ){ std::fprintf(stderr, "%s: %s.", msg, gluErrorString(err)); return err; } return EOk; } const Glyph Glyph::null={ 0x00, TexCoords( 0.0f, 0.0f ), TexCoords( 0.0f, 0.0f ), Size2( 0, 0 ), int2( 0, 0 ), 0.0f, NULL }; //--------------------------------------------------------------------------// //--------------------------------------------------------------------------// bool Glyph::operator!=(const Glyph &obj) const{ return !( code == obj.code || owner == obj.owner || size.width == obj.size.width ); } //--------------------------------------------------------------------------// //--------------------------------------------------------------------------// bool Glyph::operator==(const Glyph &obj) const{ return code == obj.code && owner == obj.owner && size.width == obj.size.width; } struct MemHeader{ uint32_t magic; size_t prevFree; size_t nextFree; }; //--------------------------------------------------------------------------// /// \brief Copy constructor. /// \param[in] obj MemPool object to copy from. //--------------------------------------------------------------------------// MemPool::MemPool(const MemPool &obj){ } //--------------------------------------------------------------------------// /// \brief Assign operator /// \param[in] obj MemPool object to assign to this. /// \returns /// Reference to itself. //--------------------------------------------------------------------------// MemPool &MemPool::operator=(const MemPool &obj){ return *this; } //--------------------------------------------------------------------------// /// \brief Default constructor. //--------------------------------------------------------------------------// MemPool::MemPool() :m_data(NULL), m_size(0), m_available(0){ } //--------------------------------------------------------------------------// /// \brief Destructor. //--------------------------------------------------------------------------// MemPool::~MemPool(){ } //--------------------------------------------------------------------------// //--------------------------------------------------------------------------// void MemPool::init(size_t size){ m_data=new byte[m_size=m_available=size]; m_free.push_back( Chunk(0, m_size) ); } //--------------------------------------------------------------------------// //--------------------------------------------------------------------------// void MemPool::clear(){ if(m_data){ delete[] m_data; m_data =NULL; m_size =0; m_available =0; m_free.clear(); } } //--------------------------------------------------------------------------// //--------------------------------------------------------------------------// void MemPool::defragment(std::list<MemAddr2> &transfers){ transfers.clear(); } //--------------------------------------------------------------------------// //--------------------------------------------------------------------------// MemAddr_t MemPool::alloc(size_t size){ if( !m_data ){ fprintf(stderr, "alloc(): Memory pool not initialized.\n"); return NULL; } FreeList::iterator mem=m_free.begin(); for( ; (mem != m_free.end()) && (mem->size < size); ++mem){ } if( mem == m_free.end() ){ fprintf(stderr, "alloc(): No free space left.\n"); return NULL; } // found free. MemAddr_t ret=m_data+mem->off; if( mem->size > size ){ mem->size -= size; mem->off += size; } else{ m_free.erase(mem); } return ret; } //--------------------------------------------------------------------------// //--------------------------------------------------------------------------// void MemPool::free(MemAddr_t addr){ if( !m_data ){ fprintf(stderr, "alloc(): Memory pool not initialized.\n"); return; } FreeList::iterator prev=prev_free(addr); if( m_data+prev->off == addr ){ // The previous free touches the current block - merge. } } //--------------------------------------------------------------------------// //--------------------------------------------------------------------------// const byte* MemPool::data() const{ return m_data; } //--------------------------------------------------------------------------// //--------------------------------------------------------------------------// size_t MemPool::size() const{ return m_size; } //--------------------------------------------------------------------------// //--------------------------------------------------------------------------// size_t MemPool::available() const{ return 0; } //--------------------------------------------------------------------------// //--------------------------------------------------------------------------// MemPool::FreeList::iterator MemPool::prev_free(MemAddr_t addr){ for(FreeList::iterator it=++m_free.begin(); it!=m_free.end(); ++it){ if( addr < &m_data[it->off] ) return --it; } return m_free.end(); } }
void CreateSprite(Mesh *sprite, vec2 size, vec3 pos, Texture *texture, Color *colors, int colorCount) { //Square vertices Vertex vertices[] = { Vertex(Position(pos.x , pos.y , pos.z), colorCount == 1 ? *colors : colors[0], TexCoords(1, 1)), //TOP RIGHT Vertex(Position(pos.x , pos.y + (size.y), pos.z), colorCount == 1 ? *colors : colors[1], TexCoords(1, 0)), //BOTTOM RIGHT Vertex(Position(pos.x + (size.x), pos.y + (size.y), pos.z), colorCount == 1 ? *colors : colors[2], TexCoords(0, 0)), //BOTTOM LEFT Vertex(Position(pos.x + (size.x), pos.y , pos.z), colorCount == 1 ? *colors : colors[3], TexCoords(0, 1)), //TOP LEFT }; //Order of vertices that will be drawn unsigned int indices[] = { 0, 1, 3, 1, 2, 3, }; LoadMesh(&sprite->Buffers, &sprite->Data, vertices, sizeof(vertices) / sizeof(Vertex), indices, sizeof(indices) / sizeof(unsigned int)); sprite->MeshTexture = *texture; }