void paintGL() { gl::clearAll(); program.use(); glEnable(GL_PROGRAM_POINT_SIZE); program.bindVertexArray(vao); if (baseIndex&1) { gl::pointCoordOrigin( gl::PointCoordOrigin::LOWER_LEFT ); } else { gl::pointCoordOrigin( gl::PointCoordOrigin::UPPER_LEFT ); } using namespace gl::DrawElementsInstancedBaseInstance; gl::drawElementsInstancedBaseInstance( Mode::POINTS, 1, Type::UNSIGNED_INT, 0, 4, 4*baseIndex ); glDisable(GL_PROGRAM_POINT_SIZE); }
void paintGL(){ gl::clearAll(); using namespace gl::DrawArrays; program.use(); program.bindVertexArray(vao); gl::drawArrays( Mode::TRIANGLES,0,6 ); }
void drawObject(const Object& obj) { renderProgram.uniform("obj", obj.objTransform); renderProgram.uniform("shininess", obj.material->shininess); obj.material->bind(); obj.geometry->bind(vao); obj.geometry->draw(); }
static void commit_uniforms(void) { // float for (int i = 0; i < UNIFORM_FLOAT_MAX; ++i) { if (s_uniform_float_loc[i] >= 0) s_program.Uniform(s_uniform_float_loc[i], s_uniform_float[i]); } // mat4 for (int i = 0; i < UNIFORM_MAT4_MAX; ++i) { if (s_uniform_mat4_loc[i] >= 0) s_program.Uniform(s_uniform_mat4_loc[i], 1, false, &s_uniform_mat4[i]); } }
void shutdown(void) { SGE_ASSERT(details::g_window != NULL); SGE_ASSERT(details::g_gl_context != NULL); SGE_ASSERT(s_imgui_context != NULL); shutdown_test(); s_program.Destroy(); ImGui::SetCurrentContext(s_imgui_context); ImGui_ImplOpenGL3_Shutdown(); ImGui_ImplSDL2_Shutdown(); ImGui::SetCurrentContext(NULL); ImGui::DestroyContext(s_imgui_context); s_imgui_context = NULL; if (details::g_gl_context == SDL_GL_GetCurrentContext()) SDL_GL_MakeCurrent(details::g_window, NULL); SDL_GL_DeleteContext(details::g_gl_context); details::g_gl_context = NULL; SDL_DestroyWindow(details::g_window); details::g_window = NULL; details::g_window_id = 0; }
void paintGL(){ constexpr static float clearColor[] = \ {0.5f,0.3f,0.3f,1}; gl::NamedFrameBufferObject root; root.clearColor(clearColor); root.clearDepth(1); program.use(); program.bindFrameBuffer(root); program.bindVertexArray(vao); program.setUniformMatrix4fv( 1, 1, false, &(modelScale[0][0])); program.setUniformMatrix4fv( 3, 1, false, &(normalMatrix[0][0])); using namespace gl::DrawElements; Pack pack( Mode::TRIANGLES, indexCount, Type::UNSIGNED_INT, nullptr); gl::draw(pack); }
void paintGL(){ program.use(); program.bindFrameBuffer(dFBO.frameBuffer()); program.bindVertexArray(vao); glViewport(0, 0, 512, 512); glClearColor(0.5f, 0.1f, 0.1f, 1); gl::clearAll(); gl::drawArrays(gl::DrawArraysMode::TRIANGLES, 0, 3); program.bindFrameBuffer(); glViewport(0, 0,super->width(), super->height()); glClearColor(0.1f, 0.1f, 0.5f, 1); gl::clearAll(); glViewport( (super->width()-512)/2, (super->height()-512)/2, 512, 512); renderRectangle.render( dFBO.renderBuffer() ); }
static GL::VertexArray initVertexArray(GL::Program& renderProgram) { GL::VertexArray vao; auto setupVertexAttrib = [&](const char* name, bool normalize, int buff, auto _T) { using T = decltype(_T); auto attrib = renderProgram.attrib(name); vao.vertexAttribFormat<T>(attrib, normalize); vao.vertexAttribBinding(attrib, buff); vao.enableVertexAttrib(attrib); }; setupVertexAttrib("position", 0, 0, glm::vec3()); setupVertexAttrib("vertNormal", 0, 1, glm::vec3()); setupVertexAttrib("vertTex", 0, 2, glm::vec2()); setupVertexAttrib("vertTangent", 0, 3, glm::vec3()); setupVertexAttrib("vertBitangent", 0, 4, glm::vec3()); return vao; }
static void draw_test(void) { float x, y, z; float size = 80.0f; float step = 4.0f; glm::mat4 m; glBindVertexArray(s_test_vertex_array.Id()); for (x = 0.0f; x < size; x += step) { for (y = 0.0f; y < size; y += step) { for (z = 0.0f; z < size; z += step) { m = glm::translate(glm::vec3(x, y, z)); s_program.Uniform(s_uniform_mat4_loc[UNIFORM_MODEL_MATRIX], 1, false, &m); glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0); } } } glBindVertexArray(0); }
void setup(GL::Program shaderProgram) { glClearColor( 0.f, 0.f, 0.f, 1.f ); static const float data[] = { 0.25, -0.25, 0.5, 1.0, -0.25, -0.25, 0.5, 1.0, 0.25, 0.25, 0.5, 1.0 }; GL::Point p1 = GL::Point(0.25, -0.25); GL::Point p2 = GL::Point(-0.25, -0.25); GL::Point p3 = GL::Point(0.25, 0.25); GL::Vertex v1 = GL::Vertex(p1); GL::Vertex v2 = GL::Vertex(p2); GL::Vertex v3 = GL::Vertex(p3); GL::Polygon poly = GL::Polygon(); poly.addVertex(v1); poly.addVertex(v2); poly.addVertex(v3); for (size_t i = 0; i < poly.getCoordinates().size(); i++) { printf("%f, ", poly.getCoordinates()[i]); } GLuint vbo; glGenBuffers(1, &vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo); // glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, poly.coordinatesSize(), &poly.getCoordinates().front(), GL_STATIC_DRAW); GLuint vao; glGenVertexArrays(1, &vao); glBindVertexArray(vao); GLint posAttrib = glGetAttribLocation(shaderProgram.getHandle(), "position"); glVertexAttribPointer(posAttrib, 4, GL_FLOAT, GL_FALSE, 0, NULL); glEnableVertexAttribArray(posAttrib); glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); }
static GL::Program initProgram() { GL::Program renderProgram; auto vert = GL::Shader::fromString(GL_VERTEX_SHADER, readFile("../vert.glsl")); auto frag = GL::Shader::fromString(GL_FRAGMENT_SHADER, readFile("../frag.glsl")); renderProgram.attachShader(vert); renderProgram.attachShader(frag); renderProgram.link(); renderProgram.use(); renderProgram.uniform("projection", glm::perspectiveFovRH(3.14159f/2.0f, 1024.0f, 768.0f, 0.01f, 100.0f)); renderProgram.uniform("camera", glm::translate(glm::vec3(0, 0, -2))); renderProgram.uniform("obj", glm::mat4()); renderProgram.uniform<int>("textureID", 0); renderProgram.uniform<int>("normMapID", 1); renderProgram.uniform<int>("shadowmapArrayID", 4); return renderProgram; }
void end(void) { SGE_ASSERT(details::g_window != NULL); SGE_ASSERT(details::g_gl_context != NULL); ImGui::Render(); SDL_GL_MakeCurrent(details::g_window, details::g_gl_context); glViewport(0, 0, details::g_window_rect[2], details::g_window_rect[3]); glClear(GL_COLOR_BUFFER_BIT); s_program.Use(); commit_uniforms(); draw_test(); ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); ImGui::SetCurrentContext(NULL); SDL_GL_SwapWindow(details::g_window); }
void initializeGL(){ glClearDepth(1); glClearColor(0,0,0,0); #ifdef _DEBUG_OPENGL_QT_ QGLDebugTool::setSimpleCallbackFunction(); QGLDebugTool::test(); #endif gl::createBuffers(1, &pointsBuffer); gl::createBuffers(1, &colorBuffer); gl::createVertexArrays(1,&vao); constexpr gl::Float points[] = { -0.0f,+0.5f, -0.1f,1, -0.5f,-0.5f, -0.1f,1, +0.5f,-0.5f, -0.1f,1, +0.2f,+0.5f, -0.5f,1, -0.3f,-0.5f, -0.5f,1, +0.7f,-0.5f, -0.5f,1, }; constexpr gl::Float colors[] = { 1,0,0,1, 0,1,0,1, 0,0,1,1, 1,1,0,1, 0,1,1,1, 1,0,1,1, }; gl::bufferData(pointsBuffer,sizeof(points),points); gl::bufferData(colorBuffer,sizeof(colors), colors); vao.bindBuffer(0, pointsBuffer); vao.bindBuffer(1, colorBuffer); constexpr char vs[] = R"(#version 450 layout(location=0) in vec4 ipos; layout(location=1) in vec4 icol; layout(location=2) uniform mat4 pMatrix; out vec4 color; void main(){ gl_Position = pMatrix*ipos; color = icol; } )"; constexpr char fs[] = R"(#version 450 in vec4 color; out vec4 fcolor; void main(){ fcolor = color; } )"; program = gl::VFProgramLoadSources(vs,fs); pMatrix = glm::lookAt( glm::vec3(0, 0, 1), glm::vec3(0, 0, 0), glm::vec3(0, 1, 0) ); pMatrix = glm::ortho<float>(-1, 1, -1, 1, 0, 2)*pMatrix; std::cout << pMatrix << std::endl; program.setUniformMatrix4fv(2, 1, false,&(pMatrix[0][0])); gl::enableDepthTest(); }
static bool init_program(void) { if (!s_program.Create()) return false; SGE_LOGD("Building vertex shader...\n"); if (!s_program.AddShader(GL_VERTEX_SHADER, s_vertex_shader_source)) { SGE_LOGE("Failed to build vertex shader: %s\n", s_program.InfoLog()); s_program.Destroy(); return false; } SGE_LOGD("Building fragment shader...\n"); if (!s_program.AddShader(GL_FRAGMENT_SHADER, s_fragment_shader_source)) { SGE_LOGE("Failed to build fragment shader: %s\n", s_program.InfoLog()); s_program.Destroy(); return false; } SGE_LOGD("Linking program...\n"); if (!s_program.Link()) { SGE_LOGE("Failed to link program: %s\n", s_program.InfoLog()); s_program.Destroy(); return false; } // clearing uniform locations... for (int i = 0; i < UNIFORM_FLOAT_MAX; ++i) s_uniform_float_loc[i] = -1; for (int i = 0; i < UNIFORM_MAT4_MAX; ++i) s_uniform_mat4_loc[i] = -1; // getting uniform locations... s_uniform_mat4_loc[UNIFORM_MODEL_MATRIX] = s_program.UniformLocation("sge_model_matrix"); s_uniform_mat4_loc[UNIFORM_VIEW_MATRIX] = s_program.UniformLocation("sge_view_matrix"); s_uniform_mat4_loc[UNIFORM_PROJECTION_MATRIX] = s_program.UniformLocation("sge_projection_matrix"); return true; }
void Model::Render (GLuint pass, const gl::Program &program) { GLuint result = GL_TRUE; GLuint passtype; if (!r->culling.IsVisible (bsphere.center, bsphere.radius)) return; passtype = pass & Geometry::Pass::Mask; bool shadowpass = (passtype == Geometry::Pass::ShadowMap) || (passtype == Geometry::Pass::ShadowMapTriangleTess) || (passtype == Geometry::Pass::ShadowMapQuadTess); std::map<GLuint, gl::Query>::iterator query; switch (passtype) { case Geometry::Pass::GBuffer: case Geometry::Pass::GBufferTransparency: query = queries.find (pass); if (query == queries.end ()) { auto ret = queries.insert (std::pair<GLuint, gl::Query> (pass, gl::Query ())); if (ret.second == false) throw std::runtime_error ("Cannot insert element to map."); query = ret.first; } if (query->second.IsValid ()) { do { query->second.Get (GL_QUERY_RESULT_AVAILABLE, &result); } while (result == GL_FALSE); if (result == GL_TRUE) { query->second.Get (GL_QUERY_RESULT, &result); } else { (*logstream) << "Query result not yet available" << std::endl; } } query->second.Begin (GL_ANY_SAMPLES_PASSED); break; case Geometry::Pass::GBufferSRAA: GLuint p; p = (pass & (~Geometry::Pass::Mask)) | Geometry::Pass::GBuffer; query = queries.find (pass); if (query != queries.end ()) { if (query->second.IsValid ()) { query->second.Get (GL_QUERY_RESULT_AVAILABLE, &result); if (result == GL_TRUE) { query->second.Get (GL_QUERY_RESULT, &result); } else result = GL_TRUE; } } break; } if (result == GL_TRUE) { switch (passtype) { case Geometry::Pass::GBufferTriangleTess: for (Mesh &mesh : patches) { mesh.Render (program, false, false); } break; case Geometry::Pass::GBufferQuadTess: for (Mesh &mesh : patches) { mesh.Render (program, false, true); } break; case Geometry::Pass::ShadowMapTriangleTess: for (Mesh &mesh : patches) { if (mesh.CastsShadow ()) mesh.Render (program, false, false); } break; case Geometry::Pass::ShadowMapQuadTess: for (Mesh &mesh : patches) { if (mesh.CastsShadow ()) mesh.Render (program, false, true); } break; case Geometry::Pass::GBufferTransparency: for (Mesh &mesh : transparent) { mesh.Render (program, false); } break; case Geometry::Pass::ShadowMap: for (Mesh &mesh : transparent) { if (mesh.CastsShadow ()) mesh.Render (program, true); } case Geometry::Pass::GBuffer: for (Mesh &mesh : meshes) { mesh.Render (program, false); } break; case Geometry::Pass::GBufferSRAA: for (Mesh &mesh : meshes) { mesh.Render (program, true); } break; } } else if (passtype != Geometry::Pass::GBufferSRAA) { if (passtype != Geometry::Pass::GBufferTransparency) { gl::ColorMask (GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); gl::DepthMask (GL_FALSE); } r->geometry.bboxprogram.Use (); bbox.array.Bind (); bbox.indices.Bind (GL_ELEMENT_ARRAY_BUFFER); gl::DrawElements (GL_TRIANGLES, 36, GL_UNSIGNED_BYTE, NULL); program.Use (); if (passtype != Geometry::Pass::GBufferTransparency) { gl::DepthMask (GL_TRUE); gl::ColorMask (GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); } culled++; } switch (passtype) { case Geometry::Pass::GBuffer: case Geometry::Pass::GBufferTransparency: gl::Query::End (GL_ANY_SAMPLES_PASSED); break; } }