// TODO: generate bigger buffers and use glBufferSubData video::GLMeshData Client::createMesh(voxel::DecodedMesh& surfaceMesh, const glm::ivec2& translation, float scale) { // Convenient access to the vertices and indices const uint32_t* vecIndices = surfaceMesh.getRawIndexData(); const uint32_t numIndices = surfaceMesh.getNoOfIndices(); const voxel::VoxelVertexDecoded* vecVertices = surfaceMesh.getRawVertexData(); const uint32_t numVertices = surfaceMesh.getNoOfVertices(); // This struct holds the OpenGL properties (buffer handles, etc) which will be used // to render our mesh. We copy the data from the PolyVox mesh into this structure. video::GLMeshData meshData; // Create the VAO for the mesh glGenVertexArrays(1, &meshData.vertexArrayObject); core_assert(meshData.vertexArrayObject > 0); glBindVertexArray(meshData.vertexArrayObject); // The GL_ARRAY_BUFFER will contain the list of vertex positions glGenBuffers(1, &meshData.vertexBuffer); core_assert(meshData.vertexBuffer > 0); glBindBuffer(GL_ARRAY_BUFFER, meshData.vertexBuffer); glBufferData(GL_ARRAY_BUFFER, numVertices * sizeof(voxel::VoxelVertexDecoded), vecVertices, GL_STATIC_DRAW); // and GL_ELEMENT_ARRAY_BUFFER will contain the indices glGenBuffers(1, &meshData.indexBuffer); core_assert(meshData.indexBuffer > 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, meshData.indexBuffer); glBufferData(GL_ELEMENT_ARRAY_BUFFER, numIndices * sizeof(typename voxel::DecodedMesh::IndexType), vecIndices, GL_STATIC_DRAW); const int posLoc = _worldShader.enableVertexAttribute("a_pos"); glVertexAttribPointer(posLoc, 3, GL_FLOAT, GL_FALSE, sizeof(voxel::VoxelVertexDecoded), GL_OFFSET(offsetof(voxel::VoxelVertexDecoded, position))); const int matLoc = _worldShader.enableVertexAttribute("a_materialdensity"); // our material and density is encoded as 8 bits material and 8 bits density core_assert(sizeof(voxel::Voxel) == sizeof(uint16_t)); glVertexAttribIPointer(matLoc, sizeof(voxel::Voxel), GL_UNSIGNED_BYTE, sizeof(voxel::VoxelVertexDecoded), GL_OFFSET(offsetof(voxel::VoxelVertexDecoded, data))); glBindVertexArray(0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); Log::trace("mesh information:\n- mesh indices: %i, vertices: %i\n- position: %i:%i", numIndices, numVertices, translation.x, translation.y); meshData.noOfIndices = numIndices; meshData.translation = translation; meshData.scale = scale; meshData.indexType = GL_UNSIGNED_INT; return meshData; }
void ByteStream::readFormat(const char *fmt, ...) { va_list ap; va_start(ap, fmt); while (*fmt) { const char typeID = *fmt++; switch (typeID) { case 'b': *va_arg(ap, int *) = readByte(); break; case 's': *va_arg(ap, int *) = readShort(); break; case 'i': *va_arg(ap, int *) = readInt(); break; case 'l': *va_arg(ap, long *) = readLong(); break; default: core_assert(false); } } va_end(ap); }
void ByteStream::addFormat(const char *fmt, ...) { va_list ap; va_start(ap, fmt); while (*fmt) { const char typeID = *fmt++; switch (typeID) { case 'b': addByte((uint8_t) va_arg(ap, int)); break; case 's': addShort((uint16_t) va_arg(ap, int)); break; case 'i': addInt((uint32_t) va_arg(ap, int)); break; case 'l': addLong((uint64_t) va_arg(ap, long)); break; default: core_assert(false); } } va_end(ap); }
void index_buffer::init(data_t * data, size_t count) { core_assert(_size == -1); _size = count; GLCHECK(glGenBuffers(1, &_id), "Generate IBO"); buffer(data, count); }
void assert_strings_equal_n(const char * str1, const char * str2, int n) { char msg[256]; if(2*n < 240) { sprintf(msg, "\"%.*s\" to be equal to \"%.*s\"", n,str1,n, str2); } else { sprintf(msg, "strings to be equal"); } core_assert((strncmp(str1, str2, n)==0), msg); }
void assert_strings_equal(const char * str1, const char * str2) { char msg[256]; if(strlen(str1)+strlen(str2) < 240) { sprintf(msg, "\"%s\" to be equal to \"%s\"", str1, str2); } else { sprintf(msg, "strings to be equal"); } core_assert((strcmp(str1, str2)==0), msg); }
std::string ByteStream::readString() { int size = 0; std::string strbuff; strbuff.reserve(64); for (;;) { const char chr = *std::next(begin(), size); ++size; core_assert(size <= this->size()); if (chr == '\0') break; strbuff += chr; } _pos += size; return strbuff; }
bool SpawnMgr::init() { const std::string& lua = core::App::getInstance()->filesystem()->load("behaviourtrees.lua"); if (!_loader->init(lua)) { Log::error("could not load the behaviourtrees: %s", _loader->getError().c_str()); return false; } std::vector<std::string> trees; _loader->getTrees(trees); Log::info("loaded %i behaviour trees", (int)trees.size()); for (const std::string& tree : trees) { std::stringstream s; const ai::TreeNodePtr& node = _loader->load(tree); core_assert(node); s << *node; Log::debug("%s", s.str().c_str()); } return true; }
void assert_ints_equal(int i1, int i2) { char msg[128]; sprintf(msg, "%d to be equal to %d", i1, i2); core_assert((i1==i2), msg); }
void assert_lt(int v1, int v2) { char msg[128]; sprintf(msg, "%d to be less than %d", v1, v2); core_assert((v1<v2), msg); }
void assert_gt(int v1,int v2) { char msg[128]; sprintf(msg, "%d to be greater than %d", v1, v2); core_assert((v1>v2), msg); }
void assert_true(int expr) { core_assert(expr, "expression to be true"); }
void assert_custom(int expr, const char * msg) { core_assert(expr, msg); }
vertex_data & primitive_builder::at(size_t id) { core_assert(0 <= id && id < size()); return _data.at(id); }
void EventHandler::registerObserver(IEventObserver* observer) { core_assert(observer); _observers.push_back(observer); }
void assert_false(int expr) { core_assert((expr==0), "expression to be false"); }
core::AppState Client::onInit() { eventBus()->subscribe<network::NewConnectionEvent>(*this); eventBus()->subscribe<network::DisconnectEvent>(*this); eventBus()->subscribe<voxel::WorldCreatedEvent>(*this); GLDebug::enable(GLDebug::Medium); core::AppState state = UIApp::onInit(); if (state != core::Running) return state; if (!_network->start()) return core::Cleanup; core::Var::get(cfg::ClientName, "noname"); core::Var::get(cfg::ClientPassword, "nopassword"); if (!_worldShader.init()) { return core::Cleanup; } if (!_meshShader->init()) { return core::Cleanup; } if (!_waterShader->init()) { return core::Cleanup; } _waterTexture = video::TexturePtr(new video::Texture("texture/water.png")); _waterTexture->load(); const glm::vec3 vertices[4] = { glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f), glm::vec3(1.0f, 0.0f, 0.0f), glm::vec3(1.0f, 1.0f, 0.0f) }; const glm::ivec3 indices[2] = { glm::ivec3( 0, 1, 2 ), glm::ivec3( 1, 3, 2 ) }; glGenVertexArrays(1, &_waterData.vertexArrayObject); core_assert(_waterData.vertexArrayObject > 0); glBindVertexArray(_waterData.vertexArrayObject); glGenBuffers(1, &_waterData.vertexBuffer); core_assert(_waterData.vertexBuffer > 0); glBindBuffer(GL_ARRAY_BUFFER, _waterData.vertexBuffer); glBufferData(GL_ARRAY_BUFFER, 4 * sizeof(glm::vec3), vertices, GL_STATIC_DRAW); glGenBuffers(1, &_waterData.indexBuffer); core_assert(_waterData.indexBuffer > 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _waterData.indexBuffer); glBufferData(GL_ELEMENT_ARRAY_BUFFER, 2 * sizeof(glm::ivec2), indices, GL_STATIC_DRAW); const int posLoc = _waterShader->enableVertexAttribute("a_pos"); glVertexAttribPointer(posLoc, 3, GL_FLOAT, GL_FALSE, sizeof(voxel::VoxelVertexDecoded), GL_OFFSET(offsetof(voxel::VoxelVertexDecoded, position))); glBindVertexArray(0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); _waterData.noOfIndices = 2; _waterData.scale = 1.0f; _waterData.indexType = GL_UNSIGNED_INT; GL_checkError(); _camera.init(_width, _height); registerMoveCmd("+move_right", MOVERIGHT); registerMoveCmd("+move_left", MOVELEFT); registerMoveCmd("+move_forward", MOVEFORWARD); registerMoveCmd("+move_backward", MOVEBACKWARD); const int ColorTextureSize = 256; uint8_t colorTexture[ColorTextureSize * ColorTextureSize * 3]; noise::Simplex::SeamlessNoise2DRGB(colorTexture, ColorTextureSize, 3, 0.3f, 0.7f); _colorTexture = video::TexturePtr(new video::Texture(colorTexture, ColorTextureSize, ColorTextureSize, 3)); _clearColor = glm::vec3(0.0, 0.6, 0.796); _root.SetSkinBg(TBIDC("background")); new frontend::LoginWindow(this); SDL_GL_SetSwapInterval(core::Var::get(cfg::ClientVSync, "false")->boolVal()); return state; }