void AABBRenderer::initialize() { m_cubeMesh.reset(new CubeMesh()); initializeProgram(); initializeVAO(); m_initialized = true; }
void Painter::initializeGL(const Shape& shape, const std::string &vert, const std::string &frag) { initializeProgram(vert, frag); initializeVAO(shape); }
bool OsmAnd::AtlasMapRendererDebugStage_OpenGL::initializeRects2D() { const auto gpuAPI = getGPUAPI(); GL_CHECK_PRESENT(glGenBuffers); GL_CHECK_PRESENT(glBindBuffer); GL_CHECK_PRESENT(glBufferData); GL_CHECK_PRESENT(glEnableVertexAttribArray); GL_CHECK_PRESENT(glVertexAttribPointer); GL_CHECK_PRESENT(glDeleteShader); GL_CHECK_PRESENT(glDeleteProgram); // Compile vertex shader const QString vertexShader = QLatin1String( // Input data "INPUT vec2 in_vs_vertexPosition; ""\n" " ""\n" // Parameters: common data "uniform mat4 param_vs_mProjectionViewModel; ""\n" "uniform vec4 param_vs_rect; ""\n" "uniform float param_vs_angle; ""\n" " ""\n" "void main() ""\n" "{ ""\n" " vec2 rectCenter = param_vs_rect.yx; ""\n" " vec2 p = in_vs_vertexPosition*param_vs_rect.wz + rectCenter; ""\n" " vec4 v; ""\n" " float cos_a = cos(param_vs_angle); ""\n" " float sin_a = sin(param_vs_angle); ""\n" " p -= rectCenter; ""\n" " v.x = rectCenter.x + p.x*cos_a - p.y*sin_a; ""\n" " v.y = rectCenter.y + p.x*sin_a + p.y*cos_a; ""\n" " v.z = -1.0; ""\n" " v.w = 1.0; ""\n" " ""\n" " gl_Position = param_vs_mProjectionViewModel * v; ""\n" "} ""\n"); auto preprocessedVertexShader = vertexShader; gpuAPI->preprocessVertexShader(preprocessedVertexShader); gpuAPI->optimizeVertexShader(preprocessedVertexShader); const auto vsId = gpuAPI->compileShader(GL_VERTEX_SHADER, qPrintable(preprocessedVertexShader)); if (vsId == 0) { LogPrintf(LogSeverityLevel::Error, "Failed to compile AtlasMapRendererDebugStage_OpenGL vertex shader"); return false; } // Compile fragment shader const QString fragmentShader = QLatin1String( // Parameters: common data "uniform lowp vec4 param_fs_color; ""\n" " ""\n" "void main() ""\n" "{ ""\n" " FRAGMENT_COLOR_OUTPUT = param_fs_color; ""\n" "} ""\n"); auto preprocessedFragmentShader = fragmentShader; QString preprocessedFragmentShader_UnrolledPerLayerProcessingCode; gpuAPI->preprocessFragmentShader(preprocessedFragmentShader); gpuAPI->optimizeFragmentShader(preprocessedFragmentShader); const auto fsId = gpuAPI->compileShader(GL_FRAGMENT_SHADER, qPrintable(preprocessedFragmentShader)); if (fsId == 0) { glDeleteShader(vsId); GL_CHECK_RESULT; LogPrintf(LogSeverityLevel::Error, "Failed to compile AtlasMapRendererDebugStage_OpenGL fragment shader"); return false; } // Link everything into program object GLuint shaders[] = { vsId, fsId }; QHash< QString, GPUAPI_OpenGL::GlslProgramVariable > variablesMap; _programRect2D.id = gpuAPI->linkProgram(2, shaders, true, &variablesMap); if (!_programRect2D.id.isValid()) { LogPrintf(LogSeverityLevel::Error, "Failed to link AtlasMapRendererDebugStage_OpenGL program"); return false; } bool ok = true; const auto& lookup = gpuAPI->obtainVariablesLookupContext(_programRect2D.id, variablesMap); ok = ok && lookup->lookupLocation(_programRect2D.vs.in.vertexPosition, "in_vs_vertexPosition", GlslVariableType::In); ok = ok && lookup->lookupLocation(_programRect2D.vs.param.mProjectionViewModel, "param_vs_mProjectionViewModel", GlslVariableType::Uniform); ok = ok && lookup->lookupLocation(_programRect2D.vs.param.rect, "param_vs_rect", GlslVariableType::Uniform); ok = ok && lookup->lookupLocation(_programRect2D.vs.param.angle, "param_vs_angle", GlslVariableType::Uniform); ok = ok && lookup->lookupLocation(_programRect2D.fs.param.color, "param_fs_color", GlslVariableType::Uniform); if (!ok) { glDeleteProgram(_programRect2D.id); GL_CHECK_RESULT; _programRect2D.id.reset(); return false; } // Vertex data (x,y) float vertices[4][2] = { { -0.5f, -0.5f }, { -0.5f, 0.5f }, { 0.5f, 0.5f }, { 0.5f, -0.5f } }; const auto verticesCount = 4; // Index data GLushort indices[6] = { 0, 1, 2, 0, 2, 3 }; const auto indicesCount = 6; _vaoRect2D = gpuAPI->allocateUninitializedVAO(); // Create vertex buffer and associate it with VAO glGenBuffers(1, &_vboRect2D); GL_CHECK_RESULT; glBindBuffer(GL_ARRAY_BUFFER, _vboRect2D); GL_CHECK_RESULT; glBufferData(GL_ARRAY_BUFFER, verticesCount * sizeof(float)* 2, vertices, GL_STATIC_DRAW); GL_CHECK_RESULT; glEnableVertexAttribArray(*_programRect2D.vs.in.vertexPosition); GL_CHECK_RESULT; glVertexAttribPointer(*_programRect2D.vs.in.vertexPosition, 2, GL_FLOAT, GL_FALSE, sizeof(float)* 2, nullptr); GL_CHECK_RESULT; // Create index buffer and associate it with VAO glGenBuffers(1, &_iboRect2D); GL_CHECK_RESULT; glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _iboRect2D); GL_CHECK_RESULT; glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesCount * sizeof(GLushort), indices, GL_STATIC_DRAW); GL_CHECK_RESULT; gpuAPI->initializeVAO(_vaoRect2D); glBindBuffer(GL_ARRAY_BUFFER, 0); GL_CHECK_RESULT; glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); GL_CHECK_RESULT; return true; }
bool OsmAnd::AtlasMapRendererDebugStage_OpenGL::initializeQuads3D() { const auto gpuAPI = getGPUAPI(); GL_CHECK_PRESENT(glGenBuffers); GL_CHECK_PRESENT(glBindBuffer); GL_CHECK_PRESENT(glBufferData); GL_CHECK_PRESENT(glEnableVertexAttribArray); GL_CHECK_PRESENT(glVertexAttribPointer); GL_CHECK_PRESENT(glDeleteShader); GL_CHECK_PRESENT(glDeleteProgram); // Compile vertex shader const QString vertexShader = QLatin1String( // Input data // (1.0, 0.0, 0.0, 0.0) for first point // (0.0, 1.0, 0.0, 0.0) for second point // (0.0, 0.0, 1.0, 0.0) for third point // (0.0, 0.0, 0.0, 1.0) for fourth point "INPUT vec4 in_vs_vertexPosition; ""\n" " ""\n" // Parameters: common data "uniform mat4 param_vs_mProjectionViewModel; ""\n" "uniform vec4 param_vs_v0; ""\n" "uniform vec4 param_vs_v1; ""\n" "uniform vec4 param_vs_v2; ""\n" "uniform vec4 param_vs_v3; ""\n" " ""\n" "void main() ""\n" "{ ""\n" " vec4 v = ""\n" " in_vs_vertexPosition.x*param_vs_v0 + ""\n" " in_vs_vertexPosition.y*param_vs_v1 + ""\n" " in_vs_vertexPosition.z*param_vs_v2 + ""\n" " in_vs_vertexPosition.w*param_vs_v3; ""\n" " ""\n" " gl_Position = param_vs_mProjectionViewModel * v; ""\n" "} ""\n"); auto preprocessedVertexShader = vertexShader; gpuAPI->preprocessVertexShader(preprocessedVertexShader); gpuAPI->optimizeVertexShader(preprocessedVertexShader); const auto vsId = gpuAPI->compileShader(GL_VERTEX_SHADER, qPrintable(preprocessedVertexShader)); if (vsId == 0) { LogPrintf(LogSeverityLevel::Error, "Failed to compile AtlasMapRendererDebugStage_OpenGL vertex shader"); return false; } // Compile fragment shader const QString fragmentShader = QLatin1String( // Parameters: common data "uniform lowp vec4 param_fs_color; ""\n" " ""\n" "void main() ""\n" "{ ""\n" " FRAGMENT_COLOR_OUTPUT = param_fs_color; ""\n" "} ""\n"); auto preprocessedFragmentShader = fragmentShader; QString preprocessedFragmentShader_UnrolledPerLayerProcessingCode; gpuAPI->preprocessFragmentShader(preprocessedFragmentShader); gpuAPI->optimizeFragmentShader(preprocessedFragmentShader); const auto fsId = gpuAPI->compileShader(GL_FRAGMENT_SHADER, qPrintable(preprocessedFragmentShader)); if (fsId == 0) { glDeleteShader(vsId); GL_CHECK_RESULT; LogPrintf(LogSeverityLevel::Error, "Failed to compile AtlasMapRendererDebugStage_OpenGL fragment shader"); return false; } // Link everything into program object GLuint shaders[] = { vsId, fsId }; QHash< QString, GPUAPI_OpenGL::GlslProgramVariable > variablesMap; _programQuad3D.id = gpuAPI->linkProgram(2, shaders, true, &variablesMap); if (!_programQuad3D.id.isValid()) { LogPrintf(LogSeverityLevel::Error, "Failed to link AtlasMapRendererDebugStage_OpenGL program"); return false; } bool ok = true; const auto& lookup = gpuAPI->obtainVariablesLookupContext(_programQuad3D.id, variablesMap); ok = ok && lookup->lookupLocation(_programQuad3D.vs.in.vertexPosition, "in_vs_vertexPosition", GlslVariableType::In); ok = ok && lookup->lookupLocation(_programQuad3D.vs.param.mProjectionViewModel, "param_vs_mProjectionViewModel", GlslVariableType::Uniform); ok = ok && lookup->lookupLocation(_programQuad3D.vs.param.v0, "param_vs_v0", GlslVariableType::Uniform); ok = ok && lookup->lookupLocation(_programQuad3D.vs.param.v1, "param_vs_v1", GlslVariableType::Uniform); ok = ok && lookup->lookupLocation(_programQuad3D.vs.param.v2, "param_vs_v2", GlslVariableType::Uniform); ok = ok && lookup->lookupLocation(_programQuad3D.vs.param.v3, "param_vs_v3", GlslVariableType::Uniform); ok = ok && lookup->lookupLocation(_programQuad3D.fs.param.color, "param_fs_color", GlslVariableType::Uniform); if (!ok) { glDeleteProgram(_programQuad3D.id); GL_CHECK_RESULT; _programQuad3D.id.reset(); return false; } // Vertex data (x,y,z,w) float vertices[4][4] = { { 1.0f, 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f, 0.0f }, { 0.0f, 0.0f, 1.0f, 0.0f }, { 0.0f, 0.0f, 0.0f, 1.0f } }; const auto verticesCount = 4; // Index data GLushort indices[6] = { 0, 1, 2, 0, 2, 3 }; const auto indicesCount = 6; _vaoQuad3D = gpuAPI->allocateUninitializedVAO(); // Create vertex buffer and associate it with VAO glGenBuffers(1, &_vboQuad3D); GL_CHECK_RESULT; glBindBuffer(GL_ARRAY_BUFFER, _vboQuad3D); GL_CHECK_RESULT; glBufferData(GL_ARRAY_BUFFER, verticesCount * sizeof(float)*4, vertices, GL_STATIC_DRAW); GL_CHECK_RESULT; glEnableVertexAttribArray(*_programQuad3D.vs.in.vertexPosition); GL_CHECK_RESULT; glVertexAttribPointer(*_programQuad3D.vs.in.vertexPosition, 4, GL_FLOAT, GL_FALSE, sizeof(float)*4, nullptr); GL_CHECK_RESULT; // Create index buffer and associate it with VAO glGenBuffers(1, &_iboQuad3D); GL_CHECK_RESULT; glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _iboQuad3D); GL_CHECK_RESULT; glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesCount * sizeof(GLushort), indices, GL_STATIC_DRAW); GL_CHECK_RESULT; gpuAPI->initializeVAO(_vaoQuad3D); glBindBuffer(GL_ARRAY_BUFFER, 0); GL_CHECK_RESULT; glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); GL_CHECK_RESULT; return true; }