void Node::Render(GLint texLoc, GLint matLoc, GLint normalMatLoc, glm::mat4 viewMat) { transform_global = transform_local; if (m_Mesh != 0 && m_Texture != 0) { glm::mat3 normalMatrix(viewMat * transform_global); glm::inverseTranspose(normalMatrix); glUniformMatrix3fv(normalMatLoc, 1, GL_FALSE, glm::gtc::type_ptr::value_ptr(normalMatrix)); CheckOpenGLError("passing normalMat in root node"); glUniformMatrix4fv(matLoc, 1, GL_FALSE, glm::gtc::type_ptr::value_ptr(transform_global)); glUniform1i(texLoc, m_Texture->getUnit()); CheckOpenGLError("passing uniforms in root node"); glActiveTexture(GL_TEXTURE0 + m_Texture->getUnit()); glBindTexture(GL_TEXTURE_2D, m_Texture->getID()); glBindSampler(m_Texture->getUnit(), m_Texture->getSampler()); CheckOpenGLError("binding texture in root node"); m_Mesh->draw(); glBindTexture(GL_TEXTURE_2D, 0); } for (GLuint i = 0; i < m_children.size(); i++) { m_children[i]->Render(texLoc, matLoc, transform_global, normalMatLoc, viewMat); CheckOpenGLError("render children in node"); } }
void ParticleInstancingRenderer::GenerateAndBuildTBO() { debug1 << "building buffers for batched instancing" << endl; glGenBuffers(2, tbo_position_radius_batches); CheckOpenGLError(); glGenTextures(2, tex_position_radius_batches); CheckOpenGLError(); glGenBuffers(2, tbo_color_batches); CheckOpenGLError(); glGenTextures(2, tex_color_batches); CheckOpenGLError(); for(size_t i = 0; i < 2; ++i) { BuildTBO(tbo_position_radius_batches[i], tex_position_radius_batches[i], instanced_batch_size * 4 * sizeof(float), GL_DYNAMIC_DRAW_ARB, GL_RGBA32F_ARB); BuildTBO(tbo_color_batches[i], tex_color_batches[i], instanced_batch_size * 4 * sizeof(unsigned char), GL_DYNAMIC_DRAW_ARB, GL_RGBA8); } }
void Node::Render(GLint texLoc, GLint matLoc) { transform_global = transform_local; if (m_Mesh != 0 && m_Texture != 0) { //m_Shader->BindShader(); glUniformMatrix4fv(matLoc, 1, GL_FALSE, glm::gtc::type_ptr::value_ptr(transform_global)); glUniform1i(texLoc, m_Texture->getUnit()); CheckOpenGLError("passing uniform in node"); glActiveTexture(GL_TEXTURE0 + m_Texture->getUnit()); glBindTexture(GL_TEXTURE_2D, m_Texture->getID()); glBindSampler(m_Texture->getUnit(), m_Texture->getSampler()); CheckOpenGLError("binding texture in node"); m_Mesh->draw(); glBindTexture(GL_TEXTURE_2D, 0); //m_Shader->UnBindShader(); } for (GLuint i = 0; i < m_children.size(); i++) { m_children[i]->Render(texLoc, matLoc, transform_global); CheckOpenGLError("render children in node"); } }
void SphereGeometryVBO::Initialize() { std::vector<float> vertices; std::vector<unsigned short > indices; BuildSphereGrid(vertices, indices, slices, stacks); glGenBuffers(1, &vbo_vertices); CheckOpenGLError(); glBindBuffer(GL_ARRAY_BUFFER, vbo_vertices); CheckOpenGLError(); glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float), &vertices[0], GL_STATIC_DRAW); CheckOpenGLError(); glBindBuffer(GL_ARRAY_BUFFER, 0); CheckOpenGLError(); glGenBuffers(1, &vbo_indices); CheckOpenGLError(); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo_indices); CheckOpenGLError(); glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned short), &indices[0], GL_STATIC_DRAW); CheckOpenGLError(); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); CheckOpenGLError(); num_indices = indices.size(); num_vertices = vertices.size() / 2; }
bool VertexBuffer<Type>::Enable() { bool success = false; if ( bufferId != 0 ) { glBindBuffer(bufferTypeGL, bufferId); success = CheckOpenGLError("Binding Vertex buffer"); } return success; }
bool Shader::UnBindShader(void) { glUseProgram(0); //glDetachShader(m_shaderID, m_shaderVP); //if (!m_gp.empty()) //{ // glDetachShader(m_shaderID, m_shaderGP); //} //glDetachShader(m_shaderID, m_shaderFP); return CheckOpenGLError("unbind shader in shader"); }
void Node::Render(GLint texLoc, GLint matLoc, glm::mat4 const &transform, GLint normalMatLoc, glm::mat4 viewMat) { transform_global = transform * transform_local; if (m_Mesh != 0 && m_Texture != 0) { glm::mat3 normalMatrix(viewMat * transform_global); glm::inverseTranspose(normalMatrix); glUniformMatrix3fv(normalMatLoc, 1, GL_FALSE, glm::gtc::type_ptr::value_ptr(normalMatrix)); CheckOpenGLError("passing normalMat in child node"); glUniformMatrix4fv(matLoc, 1, GL_FALSE, glm::value_ptr(transform_global)); glUniform1i(texLoc, m_Texture->getUnit()); CheckOpenGLError("passing uniform in child node"); glActiveTexture(GL_TEXTURE0 + m_Texture->getUnit()); glBindTexture(GL_TEXTURE_2D, m_Texture->getID()); m_Mesh->draw(); glBindTexture(GL_TEXTURE_2D, 0); } }
void VertexBuffer<Type>::Disable() { if ( bufferId != 0 ) { glBindBuffer(bufferTypeGL, 0); bool success = CheckOpenGLError("Unbinding buffer"); if ( !success ) { Log("Problem unbinding buffer: " + std::to_string(bufferId)); Log(";("); } } }
void Node::Render(GLint texLoc, GLint matLoc, glm::mat4 const &transform) { transform_global = transform * transform_local; if (m_Mesh != 0 && m_Texture != 0) { //m_Shader->BindShader(); glUniformMatrix4fv(matLoc, 1, GL_FALSE, glm::value_ptr(transform_global)); glUniform1i(texLoc, m_Texture->getUnit()); CheckOpenGLError("passing uniform in node"); glActiveTexture(GL_TEXTURE0 + m_Texture->getUnit()); glBindTexture(GL_TEXTURE_2D, m_Texture->getID()); m_Mesh->draw(); glBindTexture(GL_TEXTURE_2D, 0); //m_Shader->UnBindShader(); } }
bool Shader::InitializeShader(void) { const GLchar *vp = m_vp.c_str(); m_shaderVP = glCreateShader(GL_VERTEX_SHADER); glShaderSource(m_shaderVP, 1, &vp, 0); glCompileShader(m_shaderVP); if (!m_gp.empty()) { const GLchar *gp = m_gp.c_str(); m_shaderGP = glCreateShader(GL_GEOMETRY_SHADER); glShaderSource(m_shaderGP, 1, &gp, 0); glCompileShader(m_shaderGP); } const GLchar *fp = m_fp.c_str(); m_shaderFP = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(m_shaderFP, 1, &fp, 0); glCompileShader(m_shaderFP); m_shaderID = glCreateProgram(); glAttachShader(m_shaderID, m_shaderVP); if (!m_gp.empty()) { glAttachShader(m_shaderID, m_shaderGP); } glAttachShader(m_shaderID, m_shaderFP); glLinkProgram(m_shaderID); printProgramInfoLog(); return CheckOpenGLError("initialize shaders in shader"); }
static GLuint LoadShader(const std::string& shader_file, GLenum type) { debug1 << "loading shader " << shader_file << endl; GLuint shader = glCreateShader(type); CheckOpenGLError(); std::vector<const char*> source_strings; std::string main = LoadFile(shader_file); source_strings.push_back(main.c_str()); glShaderSource(shader,GLsizei(source_strings.size()), &source_strings[0], 0); CheckOpenGLError(); debug1 << "compiling" << endl; glCompileShader(shader); CheckOpenGLError(); GLint compile_status; glGetShaderiv(shader, GL_COMPILE_STATUS, &compile_status); CheckOpenGLError(); GLint info_log_length = 0; glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &info_log_length); CheckOpenGLError(); if(!compile_status ) { debug1 << "error" << endl; } if(info_log_length > 0) { std::string info_log(size_t(info_log_length),' '); glGetShaderInfoLog(shader, info_log_length, 0, &info_log[0]); CheckOpenGLError(); debug1 << info_log << endl; } if(!compile_status ) { return 0; } return shader; }
void ParticleInstancingRenderer::BuildTBO(const GLuint tbo, const GLuint tex, size_t tbo_size, \ GLenum usage, GLenum internal_format) { debug1 << "\tbuilding texture buffer for instance data of size " << FormatBytes(tbo_size) << endl; glBindBufferARB(GL_TEXTURE_BUFFER_ARB, tbo); CheckOpenGLError(); glBufferDataARB(GL_TEXTURE_BUFFER_ARB, tbo_size, 0, usage); CheckOpenGLError(); glBindTexture(GL_TEXTURE_BUFFER_ARB, tex ); CheckOpenGLError(); glTexBufferARB(GL_TEXTURE_BUFFER_ARB, internal_format, tbo); CheckOpenGLError(); // clear texture / bufer bindings glBindBufferARB(GL_TEXTURE_BUFFER_ARB, 0); CheckOpenGLError(); glBindTexture(GL_TEXTURE_BUFFER_ARB, 0); CheckOpenGLError(); }
void ParticleInstancingRenderer::Render() { glPushAttrib(GL_ALL_ATTRIB_BITS); CheckOpenGLError(); const SphereGeometryVBO& level = *sphere_geometry_vbos[quality_level]; glUseProgram(program_instancing); CheckOpenGLError(); glValidateProgram(program_instancing); CheckOpenGLError(); glUniform1i(glGetUniformLocation(program_instancing,"per_instance_data_position_radius"), 0); CheckOpenGLError(); glUniform1i(glGetUniformLocation(program_instancing,"per_instance_data_attribute"), 1); CheckOpenGLError(); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, level.GetVBOIndices()); CheckOpenGLError(); glBindBuffer(GL_ARRAY_BUFFER, level.GetVBOVertices()); CheckOpenGLError(); glVertexPointer(2, GL_FLOAT, 0, 0); CheckOpenGLError(); glEnableClientState(GL_VERTEX_ARRAY); CheckOpenGLError(); const size_t total_number_of_instances = NumParticles(); const size_t number_of_batches = 1 + (total_number_of_instances - 1) / instanced_batch_size; for(size_t batch = 0, instances_remaining = total_number_of_instances; batch < number_of_batches; ++batch, instances_remaining -= instanced_batch_size) { size_t current_buffer = batch % 2; const size_t start_instance = batch * instanced_batch_size; const size_t instance_count = std::min(instances_remaining,instanced_batch_size); CopyParticleDataToGpuBuffers(start_instance, instance_count, tbo_position_radius_batches[current_buffer], tex_position_radius_batches[current_buffer], tbo_color_batches[current_buffer], tex_color_batches[current_buffer]); glActiveTexture(GL_TEXTURE0); CheckOpenGLError(); glBindTexture(GL_TEXTURE_BUFFER_ARB, tex_position_radius_batches[current_buffer]); CheckOpenGLError(); glActiveTexture(GL_TEXTURE1); CheckOpenGLError(); glBindTexture(GL_TEXTURE_BUFFER_ARB, tex_color_batches[current_buffer]); CheckOpenGLError(); glDrawElementsInstancedARB(GL_QUAD_STRIP, GLsizei(level.GetNumIndices()), GL_UNSIGNED_SHORT, 0, GLsizei(instance_count)); CheckOpenGLError(); } glDisableClientState(GL_VERTEX_ARRAY); CheckOpenGLError(); glBindBuffer(GL_ARRAY_BUFFER, 0); CheckOpenGLError(); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); CheckOpenGLError(); glActiveTexture(GL_TEXTURE1); CheckOpenGLError(); glBindTexture(GL_TEXTURE_BUFFER_ARB, 0); CheckOpenGLError(); glActiveTexture(GL_TEXTURE0); CheckOpenGLError(); glBindTexture(GL_TEXTURE_BUFFER_ARB, 0); CheckOpenGLError(); glUseProgram(0); CheckOpenGLError(); glPopAttrib(); CheckOpenGLError(); }
GLboolean ParticleInstancingRenderer::CopyParticleDataToGpuBuffers(size_t start, size_t count, \ GLuint tbo_position_radius, GLuint tex_position_radius, GLuint tbo_color, GLuint tex_color ) { const size_t number_of_particles = count; float* mapped_position_radius = 0; unsigned char* mapped_color = 0; // bind the buffers and get a mapped pointer to the elements glBindBufferARB ( GL_TEXTURE_BUFFER_ARB, tbo_position_radius ); CheckOpenGLError(); // rebind the texture to the buffer // it's not clear why this is needed, but if we don't do it // occasionally no objects get rendered. glBindTexture(GL_TEXTURE_BUFFER_ARB, tex_position_radius); CheckOpenGLError(); glTexBufferARB(GL_TEXTURE_BUFFER_ARB, GL_RGBA32F_ARB, tbo_position_radius); CheckOpenGLError(); glBindTexture(GL_TEXTURE_BUFFER_ARB, 0); CheckOpenGLError(); //#define GLMAPBUFFER #ifdef GLMAPBUFFER mapped_position_radius = reinterpret_cast<float*>(glMapBufferARB(GL_TEXTURE_BUFFER_ARB, GL_WRITE_ONLY)); CheckOpenGLError(); if(!mapped_position_radius) cerr << " mapped_position_radius null " << endl; #else glBufferData(GL_TEXTURE_BUFFER_ARB, count*4*sizeof(float), &particle_position_radius[start], GL_DYNAMIC_DRAW); CheckOpenGLError(); #endif // bind the buffers and get a mapped pointer to the elements glBindBufferARB ( GL_TEXTURE_BUFFER_ARB, tbo_color); CheckOpenGLError(); // rebind the texture to the buffer // it's not clear why this is needed, but if we don't do it // occasionally no objects get rendered. glBindTexture(GL_TEXTURE_BUFFER_ARB, tex_color); CheckOpenGLError(); glTexBufferARB(GL_TEXTURE_BUFFER_ARB, GL_RGBA8, tbo_color); CheckOpenGLError(); glBindTexture(GL_TEXTURE_BUFFER_ARB, 0); CheckOpenGLError(); #ifdef GLMAPBUFFER mapped_color = reinterpret_cast<unsigned char*>(glMapBufferARB(GL_TEXTURE_BUFFER_ARB, GL_WRITE_ONLY)); CheckOpenGLError(); if(!mapped_color) cerr << "mapping failed: mapped_color null" << endl; #else glBufferData(GL_TEXTURE_BUFFER_ARB, count*4*sizeof(unsigned char), &particle_color[start], GL_DYNAMIC_DRAW); CheckOpenGLError(); #endif #ifdef GLMAPBUFFER // now fill the buffer, this could be accelerated using OpenMP for(size_t i = 0; i < number_of_particles; ++ i) { const size_t p = start + i; mapped_position_radius[4 * i + 0] = particle_position_radius[p*4+0]; mapped_position_radius[4 * i + 1] = particle_position_radius[p*4+1]; mapped_position_radius[4 * i + 2] = particle_position_radius[p*4+2]; mapped_position_radius[4 * i + 3] = particle_position_radius[p*4+3]; mapped_color[4 * i + 0] = particle_color[p*4+0]; mapped_color[4 * i + 1] = particle_color[p*4+1]; mapped_color[4 * i + 2] = particle_color[p*4+2]; mapped_color[4 * i + 3] = particle_color[p*4+3]; } // unmap buffers const GLboolean unmapped_color = glUnmapBufferARB(GL_TEXTURE_BUFFER_ARB); CheckOpenGLError(); glBindBufferARB(GL_TEXTURE_BUFFER_ARB, tbo_position_radius); CheckOpenGLError(); const GLboolean unmapped_position_radius = glUnmapBufferARB(GL_TEXTURE_BUFFER_ARB); CheckOpenGLError(); #endif glBindBufferARB(GL_TEXTURE_BUFFER_ARB, 0); CheckOpenGLError(); /* if(!(unmapped_color==GL_TRUE && unmapped_position_radius==GL_TRUE)) cerr << "unmapping failed: unmapped_color" << static_cast<unsigned int>(unmapped_color) << " unmapped_position_radius" << static_cast<unsigned int>(unmapped_position_radius) << endl; return unmapped_color && unmapped_position_radius; */ return true; }
static GLuint LoadProgram(const std::string& vertex_shader_file, \ const std::string& fragment_shader_file, \ const std::string& geometry_shader_file = "", \ GLint vertices_out = 1, \ GLenum input_type = GL_POINTS, \ GLenum output_type = GL_POINTS) { GLuint program = glCreateProgram(); CheckOpenGLError(); GLuint vertex_shader = LoadShader(vertex_shader_file, GL_VERTEX_SHADER); CheckOpenGLError(); GLuint fragment_shader = LoadShader(fragment_shader_file, GL_FRAGMENT_SHADER); CheckOpenGLError(); glAttachShader(program, vertex_shader); CheckOpenGLError(); glAttachShader(program, fragment_shader); CheckOpenGLError(); if(!geometry_shader_file.empty()) { GLuint geometry_shader = LoadShader(geometry_shader_file, GL_GEOMETRY_SHADER_EXT); CheckOpenGLError(); glAttachShader(program, geometry_shader); CheckOpenGLError(); glProgramParameteriEXT(program, GL_GEOMETRY_VERTICES_OUT_EXT, vertices_out); CheckOpenGLError(); glProgramParameteriEXT(program, GL_GEOMETRY_INPUT_TYPE_EXT, input_type); CheckOpenGLError(); glProgramParameteriEXT(program, GL_GEOMETRY_OUTPUT_TYPE_EXT, output_type); CheckOpenGLError(); } debug1 << "linking" << endl; glLinkProgram(program); CheckOpenGLError(); GLint link_status; glGetProgramiv(program, GL_LINK_STATUS, &link_status); CheckOpenGLError(); GLint info_log_length = 0; glGetProgramiv(program, GL_INFO_LOG_LENGTH, &info_log_length); CheckOpenGLError(); if(!link_status) { debug1 << "error" << endl; } if(info_log_length > 0) { std::string info_log(size_t(info_log_length),' '); glGetProgramInfoLog(program, info_log_length, 0, &info_log[0]); CheckOpenGLError(); debug1 << info_log << endl; } if(!link_status) { return 0; } return program; }
static void RenderDrawLists(ImDrawData* draw_data) { // Avoid rendering when minimized, scale coordinates for retina displays (screen coordinates != framebuffer coordinates) ImGuiIO& io = ImGui::GetIO(); int fb_width = (int)(io.DisplaySize.x * io.DisplayFramebufferScale.x); int fb_height = (int)(io.DisplaySize.y * io.DisplayFramebufferScale.y); if (fb_width == 0 || fb_height == 0) return; draw_data->ScaleClipRects(io.DisplayFramebufferScale); // Backup GL state GLint last_program; glGetIntegerv(GL_CURRENT_PROGRAM, &last_program); GLint last_texture; glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture); GLint last_active_texture; glGetIntegerv(GL_ACTIVE_TEXTURE, &last_active_texture); GLint last_array_buffer; glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &last_array_buffer); GLint last_element_array_buffer; glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &last_element_array_buffer); GLint last_vertex_array; glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &last_vertex_array); GLint last_blend_src; glGetIntegerv(GL_BLEND_SRC, &last_blend_src); GLint last_blend_dst; glGetIntegerv(GL_BLEND_DST, &last_blend_dst); GLint last_blend_equation_rgb; glGetIntegerv(GL_BLEND_EQUATION_RGB, &last_blend_equation_rgb); GLint last_blend_equation_alpha; glGetIntegerv(GL_BLEND_EQUATION_ALPHA, &last_blend_equation_alpha); GLint last_viewport[4]; glGetIntegerv(GL_VIEWPORT, last_viewport); GLboolean last_enable_blend = glIsEnabled(GL_BLEND); GLboolean last_enable_cull_face = glIsEnabled(GL_CULL_FACE); GLboolean last_enable_depth_test = glIsEnabled(GL_DEPTH_TEST); GLboolean last_enable_scissor_test = glIsEnabled(GL_SCISSOR_TEST); CheckOpenGLError("State backup", 57); // Setup render state: alpha-blending enabled, no face culling, no depth testing, scissor enabled glEnable(GL_BLEND); glBlendEquation(GL_FUNC_ADD); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glDisable(GL_CULL_FACE); glDisable(GL_DEPTH_TEST); glEnable(GL_SCISSOR_TEST); glActiveTexture(GL_TEXTURE0); CheckOpenGLError("State setup", 75); // Setup orthographic projection matrix glViewport(0, 0, (GLsizei)fb_width, (GLsizei)fb_height); CheckOpenGLError("Viewport", 84); const float ortho_projection[4][4] = { { 2.0f/io.DisplaySize.x, 0.0f, 0.0f, 0.0f }, { 0.0f, 2.0f/-io.DisplaySize.y, 0.0f, 0.0f }, { 0.0f, 0.0f, -1.0f, 0.0f }, {-1.0f, 1.0f, 0.0f, 1.0f }, }; glUseProgram(g_shaderHandle); CheckOpenGLError("Program", 85); glUniform1i(g_attribLocationTex, 0); glUniformMatrix4fv(g_attribLocationProjMtx, 1, GL_FALSE, &ortho_projection[0][0]); CheckOpenGLError("Program + uniforms", 94); GLenum error = GL_NO_ERROR; error = glGetError(); if (GL_NO_ERROR != error) { printf("GL Error %x encountered in %s.\n", error); } glBindVertexArrayAPPLE(g_vaoHandle); for (int n = 0; n < draw_data->CmdListsCount; n++) { const ImDrawList* cmd_list = draw_data->CmdLists[n]; const ImDrawIdx* idx_buffer_offset = 0; glBindBuffer(GL_ARRAY_BUFFER, g_vboHandle); glBufferData(GL_ARRAY_BUFFER, (GLsizeiptr)cmd_list->VtxBuffer.size() * sizeof(ImDrawVert), (GLvoid*)&cmd_list->VtxBuffer.front(), GL_STREAM_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_elementsHandle); glBufferData(GL_ELEMENT_ARRAY_BUFFER, (GLsizeiptr)cmd_list->IdxBuffer.size() * sizeof(ImDrawIdx), (GLvoid*)&cmd_list->IdxBuffer.front(), GL_STREAM_DRAW); if(glGetError() != GL_NO_ERROR){std::cout << "houston we have a binding" << std::endl;} for (const ImDrawCmd* pcmd = cmd_list->CmdBuffer.begin(); pcmd != cmd_list->CmdBuffer.end(); pcmd++) { if (pcmd->UserCallback) { pcmd->UserCallback(cmd_list, pcmd); } else { glBindTexture(GL_TEXTURE_2D, (GLuint)(intptr_t)pcmd->TextureId); glScissor((int)pcmd->ClipRect.x, (int)(fb_height - pcmd->ClipRect.w), (int)(pcmd->ClipRect.z - pcmd->ClipRect.x), (int)(pcmd->ClipRect.w - pcmd->ClipRect.y)); glDrawElements(GL_TRIANGLES, (GLsizei)pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, idx_buffer_offset); } idx_buffer_offset += pcmd->ElemCount; } } if(glGetError() != GL_NO_ERROR){std::cout << "houston we have a drawing" << std::endl;} // Restore modified GL state glUseProgram(last_program); glActiveTexture(last_active_texture); glBindTexture(GL_TEXTURE_2D, last_texture); glBindVertexArrayAPPLE(last_vertex_array); glBindBuffer(GL_ARRAY_BUFFER, last_array_buffer); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, last_element_array_buffer); glBlendEquationSeparate(last_blend_equation_rgb, last_blend_equation_alpha); glBlendFunc(last_blend_src, last_blend_dst); if (last_enable_blend) glEnable(GL_BLEND); else glDisable(GL_BLEND); if (last_enable_cull_face) glEnable(GL_CULL_FACE); else glDisable(GL_CULL_FACE); if (last_enable_depth_test) glEnable(GL_DEPTH_TEST); else glDisable(GL_DEPTH_TEST); if (last_enable_scissor_test) glEnable(GL_SCISSOR_TEST); else glDisable(GL_SCISSOR_TEST); glViewport(last_viewport[0], last_viewport[1], (GLsizei)last_viewport[2], (GLsizei)last_viewport[3]); if(glGetError() != GL_NO_ERROR){std::cout << "houston we have a reinitializing" << std::endl;} }
bool HUD::createDevices() { // Build texture atlas ImGuiIO& io = ImGui::GetIO(); // Backup GL state GLint last_texture, last_array_buffer, last_vertex_array; glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture); glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &last_array_buffer); glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &last_vertex_array); CheckOpenGLError("Get state", 313); g_shaderHandle = glCreateProgram(); g_vertHandle = glCreateShader(GL_VERTEX_SHADER); g_fragHandle = glCreateShader(GL_FRAGMENT_SHADER); //glShaderSource(g_vertHandle, 1, &vertex_shader, 0); //lShaderSource(g_fragHandle, 1, &fragment_shader, 0); glCompileShader(g_vertHandle); glCompileShader(g_fragHandle); CheckOpenGLError("Shader compile", 348); glAttachShader(g_shaderHandle, g_vertHandle); glAttachShader(g_shaderHandle, g_fragHandle); glLinkProgram(g_shaderHandle); GLint status; glGetProgramiv(g_shaderHandle, GL_LINK_STATUS, &status); if( status == GL_FALSE) printf("Program did not link\n"); CheckOpenGLError("Program link", 352); g_attribLocationTex = glGetUniformLocation(g_shaderHandle, "Texture"); g_attribLocationProjMtx = glGetUniformLocation(g_shaderHandle, "ProjMtx"); g_attribLocationPosition = glGetAttribLocation(g_shaderHandle, "Position"); g_attribLocationUV = glGetAttribLocation(g_shaderHandle, "UV"); g_attribLocationColor = glGetAttribLocation(g_shaderHandle, "Color"); CheckOpenGLError("Unis and attribs", 359); glGenBuffers(1, &g_vboHandle); glGenBuffers(1, &g_elementsHandle); glGenVertexArraysAPPLE(1, &g_vaoHandle); glBindVertexArrayAPPLE(g_vaoHandle); CheckOpenGLError("Bind VAO", 343); glBindBuffer(GL_ARRAY_BUFFER, g_vboHandle); glEnableVertexAttribArray(g_attribLocationPosition); glEnableVertexAttribArray(g_attribLocationUV); glEnableVertexAttribArray(g_attribLocationColor); CheckOpenGLError("Enable Attrib Arrays", 343); #define OFFSETOF(TYPE, ELEMENT) ((size_t)&(((TYPE *)0)->ELEMENT)) glVertexAttribPointer(g_attribLocationPosition, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (GLvoid*)OFFSETOF(ImDrawVert, pos)); glVertexAttribPointer(g_attribLocationUV, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (GLvoid*)OFFSETOF(ImDrawVert, uv)); glVertexAttribPointer(g_attribLocationColor, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(ImDrawVert), (GLvoid*)OFFSETOF(ImDrawVert, col)); CheckOpenGLError("Attrib pointer", 377); #undef OFFSETOF createFontsTexture(); // Restore modified GL state glBindTexture(GL_TEXTURE_2D, last_texture); glBindBuffer(GL_ARRAY_BUFFER, last_array_buffer); glBindVertexArrayAPPLE(last_vertex_array); return true; }
bool ShaderStage::Compile() { GLboolean canCompile; glGetBooleanv(GL_SHADER_COMPILER, &canCompile); if ( canCompile != GL_FALSE) { // Build the source source = str(format("#version %d\n") % version); source += str(format("%s\n") % materialDef); source += readfile(GetFilepath().GetString()); // Compile the stage const char * stageSource = source.c_str(); stageId = glCreateShader(ShaderStageType::GetStageConstant(stageType)); CheckOpenGLError("Creating shader"); glShaderSource(stageId, 1, &stageSource, NULL); CheckOpenGLError("Setting shader source"); glCompileShader(stageId); GLint compileState; glGetShaderiv(stageId, GL_COMPILE_STATUS, &compileState); if ( compileState == GL_FALSE) { stageCompiled = false; GLint logLength; glGetShaderiv(stageId, GL_INFO_LOG_LENGTH, &logLength); if (logLength > 0 ) { GLchar * infoLog = (GLchar *)malloc(logLength); glGetShaderInfoLog(stageId, logLength, &logLength, infoLog); Log(std::string(infoLog)); } std::string filename = GetFilepath().GetString(); std::string stageName = ShaderStageType::GetStageName(stageType); CheckOpenGLError(str(format("Compiling %s Shader: %s") % stageName % filename )); if (!stageCompiled) { // Display an error message and get the shader code line in error int lineError = DisplayCompileError(stageId); // Insert line numbers and line error message into the shader source int lineNum = 1; source.insert(0, std::to_string(lineNum++) + ": "); std::size_t found = source.find("\n"); bool showError = false; while (found != std::string::npos) { if (showError) { // Insert an error message before the next newline source.insert(--found, "\t<<========== ERROR"); found+=20; showError = false; } std::string strLineNum = std::to_string(lineNum++) + ": "; source.insert(++found, strLineNum); found += strLineNum.length(); found = source.find("\n", found); if (lineNum == (lineError+1)) { showError = true; } } Log(str(format("Shader: %s\n") % filename ),"\n" + source); } } else { stageCompiled = true; } // Mark the Resource as having loaded SetReloaded(); } else { Log("ShaderStage","Shader Compiling not supported."); CheckOpenGLError("Can Compile Shaders"); } return stageCompiled; }
bool Shader::BindShader(void) { glUseProgram(m_shaderID); return CheckOpenGLError("bind shader in shader"); }