DebugRenderer::DebugRenderer (int pointCount, const ContactPoint *points, int traceCount, const TraceData *traceData, const std::vector<b2Vec2>& waterIntersectionPoints, const DebugRendererData& data) : b2Draw(), _activeProgram(0), _pointCount(pointCount), _points(points), _traceCount(traceCount), _traceData( traceData), _waterIntersectionPoints(waterIntersectionPoints), _enableTextureArray(false) { memset(_colorArray, 0, sizeof(_colorArray)); #ifndef NO_DEBUG_RENDERER if (GLContext::get().areShadersSupported()) { glGetIntegerv(GL_ACTIVE_PROGRAM, &_activeProgram); const GLenum glError = glGetError(); if (glError != GL_INVALID_ENUM) GLContext::get().ctx_glUseProgram(0); else _activeProgram = 0; } SetFlags(b2Draw::e_shapeBit | b2Draw::e_jointBit | b2Draw::e_aabbBit | b2Draw::e_pairBit | b2Draw::e_centerOfMassBit); GL_checkError(); glPushMatrix(); const float scale = static_cast<float>(data.scale); const float x = static_cast<float>(data.x / scale); const float y = static_cast<float>(data.y / scale); glScalef(scale, scale, 1.0f); glTranslatef(x, y, 0.0f); glDisable(GL_TEXTURE_2D); _enableTextureArray = glIsEnabled(GL_TEXTURE_COORD_ARRAY); if (_enableTextureArray) glDisableClientState(GL_TEXTURE_COORD_ARRAY); GL_checkError(); #endif }
bool WaterShader::use (int x, int y, int w, int h) { if (!_initialized) return false; if (!activate()) return false; float angle = _time * M_PI_2; if (angle > M_PI_2) angle -= M_PI_2; _displacement->bindTexture(0); _water->bindTexture(1); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); GL_checkError(); glEnable(GL_BLEND); GL_checkError(); setUniformMatrix("u_worldView", _modelViewMatrix); setUniformi("u_texture", 0); setUniformi("u_texture2", 1); setUniformf("timedelta", -angle); createAndBindAttributesArray(x, y, w, h); GL_checkError(); glDrawArrays(GL_TRIANGLE_FAN, 0, 4); GL_checkError(); deactivate(); return true; }
void UIRendererGL::BeginPaint(int render_target_w, int render_target_h) { #ifdef TB_RUNTIME_DEBUG_INFO dbg_bitmap_validations = 0; #endif TBRendererBatcher::BeginPaint(render_target_w, render_target_h); _shader.activate(); const glm::mat4& ortho = glm::ortho(0.0f, (float) render_target_w, (float) render_target_h, 0.0f, -1.0f, 1.0f); _shader.setUniformMatrix("u_projection", ortho, false); g_current_texture = (GLuint) -1; g_current_batch = nullptr; glViewport(0, 0, render_target_w, render_target_h); glScissor(0, 0, render_target_w, render_target_h); glEnable(GL_BLEND); glEnable(GL_TEXTURE_2D); glDisable(GL_DEPTH_TEST); glEnable(GL_SCISSOR_TEST); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); GL_checkError(); }
bool UIRendererGL::init() { if (!_shader.loadProgram("shaders/ui")) return false; glGenBuffers(1, &_buffer); GL_checkError(); return true; }
void DebugRenderer::DrawSolidCircle (const b2Vec2& center, float32 radius, const b2Vec2& axis, const b2Color& color) { #ifndef NO_DEBUG_RENDERER const float32 k_segments = 16.0f; const int vertexCount = 16; const float32 k_increment = 2.0f * b2_pi / k_segments; float32 theta = 0.0f; GLfloat glVertices[vertexCount * 2]; for (int i = 0; i < k_segments; ++i) { const b2Vec2 v = center + radius * b2Vec2(cosf(theta), sinf(theta)); glVertices[i * 2] = v.x; glVertices[i * 2 + 1] = v.y; theta += k_increment; } setColorPointer(color, 0.5f, vertexCount); glVertexPointer(2, GL_FLOAT, 0, glVertices); glDrawArrays(GL_TRIANGLE_FAN, 0, vertexCount); setColorPointer(color, 1.0f, vertexCount); glDrawArrays(GL_LINE_LOOP, 0, vertexCount); GL_checkError(); // Draw the axis line DrawSegment(center, center + radius * axis, color); #endif }
void Client::renderBackground() { _camera.setAngles(-M_PI_2, M_PI); _camera.setPosition(glm::vec3(0.0f, 100.0f, 0.0f)); _camera.update(); const glm::mat4& view = _camera.getViewMatrix(); const glm::mat4& projection = glm::perspective(45.0f, _aspect, 0.1f, 1000.0f); _meshShader->activate(); _meshShader->setUniformMatrix("u_view", view, false); _meshShader->setUniformMatrix("u_projection", projection, false); _meshShader->setUniformVec3("u_lightpos", _lightPos); _meshShader->setUniformVec3("u_diffusecolor", _diffuseColor); _meshShader->setUniformVec3("u_specularcolor", _specularColor); _meshShader->setUniformi("u_texture", 0); const video::MeshPtr& mesh = _meshPool->getMesh("animal_rabbit"); if (mesh->initMesh(_meshShader)) { const glm::mat4& translate = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, 20.0f)); const glm::mat4& scale = glm::scale(translate, glm::vec3(0.01f)); const glm::mat4& model = glm::rotate(scale, (float) glm::sin(_now / 100L), glm::vec3(0.0, 1.0, 0.0)); _meshShader->setUniformMatrix("u_model", model, false); mesh->render(); _drawCallsEntities = 0; } _meshShader->deactivate(); glBindVertexArray(0); glBindTexture(GL_TEXTURE_2D, 0); GL_checkError(); }
DebugRenderer::~DebugRenderer () { #ifndef NO_DEBUG_RENDERER const float32 k_impulseScale = 0.1f; const float32 k_axisScale = 0.3f; const bool drawFrictionImpulse = true; const bool drawContactNormals = true; const bool drawContactImpulse = true; for (int i = 0; i < _pointCount; ++i) { const ContactPoint* point = &_points[i]; if (point->state == b2_addState) { DrawPoint(point->position, 0.2f, b2Color(0.3f, 0.95f, 0.3f)); } else if (point->state == b2_persistState) { DrawPoint(point->position, 0.1f, b2Color(0.3f, 0.3f, 0.95f)); } if (drawContactNormals) { const b2Vec2& p1 = point->position; const b2Vec2 p2 = p1 + k_axisScale * point->normal; DrawSegment(p1, p2, b2Color(0.9f, 0.9f, 0.9f)); } if (drawContactImpulse) { const b2Vec2& p1 = point->position; const b2Vec2 p2 = p1 + k_impulseScale * point->normalImpulse * point->normal; DrawSegmentWithAlpha(p1, p2, b2Color(0.9f, 0.9f, 0.3f), 0.5f); } if (drawFrictionImpulse) { const b2Vec2 tangent = b2Cross(point->normal, 1.0f); const b2Vec2& p1 = point->position; const b2Vec2 p2 = p1 + k_impulseScale * point->tangentImpulse * tangent; DrawSegment(p1, p2, b2Color(0.9f, 0.9f, 0.3f)); } } for (int i = 0; i < _traceCount; ++i) { const TraceData* data = &_traceData[i]; DrawSegment(data->start, data->end, b2Color(0.9f, 0.0f, 0.0f)); DrawPoint(data->start, 0.2f, b2Color(0.0f, 0.95f, 0.3f)); DrawPoint(data->end, 0.2f, b2Color(0.3f, 0.7f, 0.0f)); } if (!_waterIntersectionPoints.empty()) DrawPolygon(&_waterIntersectionPoints[0], _waterIntersectionPoints.size(), b2Color(0.0f, 1.0f, 1.0f)); if (_enableTextureArray) glEnableClientState(GL_TEXTURE_COORD_ARRAY); glEnable(GL_TEXTURE_2D); glPopMatrix(); GL_checkError(); if (_activeProgram != 0) GLContext::get().ctx_glUseProgram(_activeProgram); #endif }
void DebugRenderer::DrawPolygon (const b2Vec2* vertices, int vertexCount, const b2Color& color) { #ifndef NO_DEBUG_RENDERER setColorPointer(color, 1.0f, vertexCount); glVertexPointer(2, GL_FLOAT, 0, vertices); glDrawArrays(GL_LINE_LOOP, 0, vertexCount); GL_checkError(); #endif }
void DebugRenderer::DrawSegmentWithAlpha (const b2Vec2& p1, const b2Vec2& p2, const b2Color& color, float alpha) { #ifndef NO_DEBUG_RENDERER setColorPointer(color, alpha, 2); const GLfloat glVertices[] = { p1.x, p1.y, p2.x, p2.y }; glVertexPointer(2, GL_FLOAT, 0, glVertices); glDrawArrays(GL_LINES, 0, 2); GL_checkError(); #endif }
void UIRendererGL::EndPaint() { TBRendererBatcher::EndPaint(); _shader.deactivate(); glBindBuffer(GL_ARRAY_BUFFER, 0); #ifdef TB_RUNTIME_DEBUG_INFO if (TB_DEBUG_SETTING(RENDER_BATCHES)) TBDebugPrint("Frame caused %d bitmap validations.\n", dbg_bitmap_validations); #endif GL_checkError(); }
void UIRendererGL::RenderBatch(Batch *batch) { BindBitmap(batch->bitmap); if (g_current_batch != batch) { g_current_batch = batch; } glBindBuffer(GL_ARRAY_BUFFER, _buffer); glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * batch->vertex_count, batch->vertex, GL_STATIC_DRAW); glVertexAttribPointer(_shader.enableVertexAttribute("a_color"), 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(Vertex), GL_OFFSET(offsetof(Batch, vertex[0].col))); glVertexAttribPointer(_shader.enableVertexAttribute("a_texcoord"), 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), GL_OFFSET(offsetof(Batch, vertex[0].u))); glVertexAttribPointer(_shader.enableVertexAttribute("a_pos"), 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), GL_OFFSET(offsetof(Batch, vertex[0].x))); glDrawArrays(GL_TRIANGLES, 0, batch->vertex_count); GL_checkError(); }
void DebugRenderer::DrawPoint (const b2Vec2& p, float32 size, const b2Color& color) { #ifndef NO_DEBUG_RENDERER setColorPointer(color, 1.0f, 4); const float minx = p.x - size / 2.0f; const float maxx = p.x + size / 2.0f; const float miny = p.y - size / 2.0f; const float maxy = p.y + size / 2.0f; const float vertices[] = { minx, miny, maxx, miny, minx, maxy, maxx, maxy }; glVertexPointer(2, GL_FLOAT, 0, vertices); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); GL_checkError(); #endif }
bool UIBitmapGL::Init(int width, int height, uint32 *data) { assert(width == TBGetNearestPowerOfTwo(width)); assert(height == TBGetNearestPowerOfTwo(height)); m_w = width; m_h = height; glGenTextures(1, &m_texture); BindBitmap(this); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); GL_checkError(); SetData(data); return true; }
void UIRendererGL::SetClipRect(const TBRect &rect) { glScissor(m_clip_rect.x, m_screen_rect.h - (m_clip_rect.y + m_clip_rect.h), m_clip_rect.w, m_clip_rect.h); GL_checkError(); }
void Client::renderMap() { voxel::DecodedMeshData mesh; if (_world->pop(mesh)) { // Now add the mesh to the list of meshes to render. addMeshData(createMesh(mesh.mesh, mesh.translation, 1.0f)); } _drawCallsWorld = 0; _drawCallsEntities = 0; // TODO: use polyvox VolumeResampler to create a minimap of your volume // RawVolume<uint8_t> volDataLowLOD(PolyVox::Region(Vector3DInt32(0, 0, 0), Vector3DInt32(15, 31, 31))); // VolumeResampler< RawVolume<uint8_t>, RawVolume<uint8_t> > volumeResampler(&volData, PolyVox::Region(Vector3DInt32(0, 0, 0), Vector3DInt32(31, 63, 63)), &volDataLowLOD, volDataLowLOD.getEnclosingRegion()); // volumeResampler.execute(); // auto meshLowLOD = extractMarchingCubesMesh(&volDataLowLOD, volDataLowLOD.getEnclosingRegion()); // auto decodedMeshLowLOD = decodeMesh(meshLowLOD); const bool left = _moveMask & MoveDirection_MOVELEFT; const bool right = _moveMask & MoveDirection_MOVERIGHT; const bool forward = _moveMask & MoveDirection_MOVEFORWARD; const bool backward = _moveMask & MoveDirection_MOVEBACKWARD; _camera.updatePosition(_deltaFrame, left, right, forward, backward); _camera.updateViewMatrix(); glClear(GL_DEPTH_BUFFER_BIT); glEnable(GL_DEPTH_TEST); //glEnable(GL_CULL_FACE); //glCullFace(GL_BACK); GL_checkError(); const glm::mat4& view = _camera.getViewMatrix(); const glm::mat4& projection = glm::perspective(45.0f, _aspect, 0.1f, 1000.0f); _waterShader->activate(); _waterShader->setUniformMatrix("u_model", glm::mat4(1.0f), false); _waterShader->setUniformMatrix("u_view", view, false); _waterShader->setUniformMatrix("u_projection", projection, false); _waterShader->setUniformVec3("u_lightpos", _lightPos); _waterShader->setUniformVec3("u_diffusecolor", _diffuseColor); _waterShader->setUniformVec3("u_specularcolor", _specularColor); _waterShader->setUniformi("u_texture", 0); _waterShader->setUniformf("u_wavetime", 0.5f); _waterShader->setUniformf("u_wavewidth", 0.6f); _waterShader->setUniformf("u_waveheight", 1.0f); _waterTexture->bind(); glBindVertexArray(_waterData.vertexArrayObject); glDrawElements(GL_TRIANGLES, _waterData.noOfIndices, _waterData.indexType, 0); ++_drawCallsWorld; glBindVertexArray(0); _waterTexture->unbind(); _waterShader->deactivate(); GL_checkError(); _worldShader.activate(); _worldShader.setUniformMatrix("u_view", view, false); _worldShader.setUniformMatrix("u_projection", projection, false); _worldShader.setUniformf("u_fogrange", _fogRange); _worldShader.setUniformf("u_viewdistance", _viewDistance); _worldShader.setUniformi("u_texture", 0); _worldShader.setUniformVec3("u_lightpos", _lightPos); _colorTexture->bind(); for (auto i = _meshData.begin(); i != _meshData.end();) { const video::GLMeshData& meshData = *i; if (isCulled(meshData.translation)) { destroyMeshData(meshData); i = _meshData.erase(i); continue; } const glm::mat4& model = glm::translate(glm::mat4(1.0f), glm::vec3(meshData.translation.x, 0, meshData.translation.y)); _worldShader.setUniformMatrix("u_model", model, false); glBindVertexArray(meshData.vertexArrayObject); glDrawElements(GL_TRIANGLES, meshData.noOfIndices, meshData.indexType, 0); ++_drawCallsWorld; ++i; } _worldShader.deactivate(); GL_checkError(); _meshShader->activate(); _meshShader->setUniformMatrix("u_view", view, false); _meshShader->setUniformMatrix("u_projection", projection, false); _meshShader->setUniformVec3("u_lightpos", _lightPos); _meshShader->setUniformVec3("u_diffusecolor", _diffuseColor); _meshShader->setUniformVec3("u_specularcolor", _specularColor); _meshShader->setUniformi("u_texture", 0); for (const auto& e : _entities) { const frontend::ClientEntityPtr& ent = e.second; ent->update(_now); const video::MeshPtr& mesh = ent->mesh(); if (!mesh->initMesh(_meshShader)) continue; const glm::mat4& translate = glm::translate(glm::mat4(1.0f), ent->position()); const glm::mat4& scale = glm::scale(translate, glm::vec3(0.01f)); const glm::mat4& model = glm::rotate(scale, ent->orientation(), glm::vec3(0.0, 1.0, 0.0)); _meshShader->setUniformMatrix("u_model", model, false); mesh->render(); ++_drawCallsEntities; } _meshShader->deactivate(); glBindVertexArray(0); glBindTexture(GL_TEXTURE_2D, 0); GL_checkError(); const glm::ivec2& camXZ = _world->getGridPos(_camera.getPosition()); const glm::vec2 diff = _lastCameraPosition - camXZ; if (glm::length(diff.x) >= 1 || glm::length(diff.y) >= 1) { _lastCameraPosition = camXZ; extractMeshAroundCamera(40); } glBindVertexArray(0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); glDisable(GL_DEPTH_TEST); //glDisable(GL_CULL_FACE); GL_checkError(); }
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; }