/** * Vraci program (zkompilovane shadery) pro renderovani uzivatelskeho pohledu */ GLuint Shaders::getUserViewProgram() { const char *p_s_vertex_shader = "#version 330\n" "in vec3 v_pos;\n" // atributy - vstup z dat vrcholu "in vec3 v_col;\n" // barva vrcholu "\n" "uniform mat4 t_modelview_projection_matrix;\n" // parametr shaderu - transformacni matice "\n" "out vec3 v_color;\n" "\n" "void main()\n" "{\n" " gl_Position = t_modelview_projection_matrix * vec4(v_pos, 1.0);\n" // musime zapsat pozici " v_color = v_col;\n" "}\n"; const char *p_s_fragment_shader = "#version 330\n" "in vec3 v_color;\n" // vstupy z vertex shaderu "\n" "out vec4 frag_color;\n" // vystup do framebufferu "\n" "void main()\n" "{\n" " frag_color = vec4(v_color, 1.0f);\n" "}\n"; // zkompiluje vertex / fragment shader, pripoji je k programu n_user_vertex_shader = glCreateShader(GL_VERTEX_SHADER); glShaderSource(n_user_vertex_shader, 1, &p_s_vertex_shader, NULL); glCompileShader(n_user_vertex_shader); if(!CheckShader(n_user_vertex_shader, "vertex shader")) return false; n_user_fragment_shader = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(n_user_fragment_shader, 1, &p_s_fragment_shader, NULL); glCompileShader(n_user_fragment_shader); if(!CheckShader(n_user_fragment_shader, "fragment shader")) return false; n_user_program_object = glCreateProgram(); glAttachShader(n_user_program_object, n_user_vertex_shader); glAttachShader(n_user_program_object, n_user_fragment_shader); // nabinduje atributy (propojeni mezi obsahem VBO a vstupem do vertex shaderu) glBindAttribLocation(n_user_program_object, 0, "v_pos"); glBindAttribLocation(n_user_program_object, 1, "v_col"); // nabinduje vystupni promenou (propojeni mezi framebufferem a vystupem fragment shaderu) glBindFragDataLocation(n_user_program_object, 0, "frag_color"); // slinkuje program glLinkProgram(n_user_program_object); if(!CheckProgram(n_user_program_object, "program")) return false; else return n_user_program_object; }
void Shader::Init() { std::string tmp_vertexShaderSourceCode = ""; std::string tmp_geometryShaderSourceCode = ""; std::string tmp_fragmentShaderSourceCode = ""; try { tmp_vertexShaderSourceCode = LoadFile( m_vertexShaderFile ); } catch(std::string &e){ ERROR_MESSAGE(e);} try { tmp_geometryShaderSourceCode = LoadFile( m_geometryShaderFile ); } catch(std::string &e){ ERROR_MESSAGE(e);} try { tmp_fragmentShaderSourceCode = LoadFile( m_fragmentShaderFile ); } catch(std::string &e){ ERROR_MESSAGE(e);} // TODO: geometry shader GLuint tmp_vertexShaderObject = glCreateShader(GL_VERTEX_SHADER); GLuint tmp_fragmentShaderObject = glCreateShader(GL_FRAGMENT_SHADER); // attach shader source code to shader // https://stackoverflow.com/questions/6047527/how-to-convert-stdstring-to-const-char const char *c_str = tmp_vertexShaderSourceCode.c_str(); glShaderSource(tmp_vertexShaderObject,1,&c_str,NULL); c_str = tmp_fragmentShaderSourceCode.c_str(); glShaderSource(tmp_fragmentShaderObject,1,&c_str,NULL); glCompileShader(tmp_vertexShaderObject); glCompileShader(tmp_fragmentShaderObject); // now check for errors: CheckShader(tmp_vertexShaderObject); CheckShader(tmp_fragmentShaderObject); m_program = glCreateProgram(); glAttachShader(m_program,tmp_vertexShaderObject); glAttachShader(m_program,tmp_fragmentShaderObject); glLinkProgram(m_program); }
void ShaderProgram::AttachShader(std::string filename) //todo: yep, return bool and check every time. i'll do it later { if (_handler == 0) { OutputDebugStringA("Trying to attach shader. Shader program is not created!"); return; } std::string fileExt = GetFileExtension(filename); auto it = ShaderTypesMap.find(fileExt); if (it == ShaderTypesMap.end()) { OutputDebugStringA("Shader extension is not found"); return; } GLint shaderType = it->second; GLuint shader = _graphics.CreateShader(shaderType); AddSourceToShader(filename, shader); _graphics.CompileShader(shader); bool compileSucessfull = CheckShader(shader, shaderType, filename); if (compileSucessfull) { _graphics.AttachShader(_handler, shader); _attachedShaders.push_back(shader); } }
/* Creates a shader program */ GLuint CreateShaderProgram(string vertex_source, string fragment_source) { GLuint program; // Vertex shader source code. This draws the vertices in our window. We have 3 vertices since we're drawing an triangle. // Each vertex is represented by a vector of size 4 (x, y, z, w) coordinates. // static const string vertex_code = vs_string_CoordSystem; const char * vs_source = vertex_source.c_str(); // Fragment shader source code. This determines the colors in the fragment generated in the shader pipeline. In this case, it colors the inside of our triangle specified by our vertex shader. // static const string fragment_code = fs_string_CoordSystem; const char * fs_source = fragment_source.c_str(); // This next section we'll generate the OpenGL program and attach the shaders to it so that we can render our triangle. program = glCreateProgram(); // We create a shader with our fragment shader source code and compile it. GLuint fs = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fs, 1, &fs_source, NULL); glCompileShader(fs); bool ret = CheckShader(fs, GL_FRAGMENT_SHADER); if(!ret){cout << "Problems compiling GL_FRAGMENT_SHADER" << endl;} // We create a shader with our vertex shader source code and compile it. GLuint vs = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vs, 1, &vs_source, NULL); glCompileShader(vs); ret = CheckShader(vs, GL_VERTEX_SHADER); if(!ret){cout << "Problems compiling GL_VERTEX_SHADER" << endl;} // We'll attach our two compiled shaders to the OpenGL program. glAttachShader(program, vs); glAttachShader(program, fs); glLinkProgram(program); glUseProgram(program); return program; }
GLuint CreateShader(GLenum shaderType, const std::string& source) { GLuint shader = glCreateShader(shaderType); if (shader == 0) return 0; // set shader source const char* raw_str = source.c_str(); glShaderSource(shader, 1, &raw_str, NULL); // compile shader object glCompileShader(shader); // check compilation error if (!CheckShader(shader)){ glDeleteShader(shader); return 0; } return shader; }
bool ImGui_ImplOpenGL3_CreateDeviceObjects() { // Backup GL state GLint last_texture, last_array_buffer; glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture); glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &last_array_buffer); #ifndef IMGUI_IMPL_OPENGL_ES2 GLint last_vertex_array; glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &last_vertex_array); #endif // Parse GLSL version string int glsl_version = 130; sscanf(g_GlslVersionString, "#version %d", &glsl_version); const GLchar* vertex_shader_glsl_120 = "uniform mat4 ProjMtx;\n" "attribute vec2 Position;\n" "attribute vec2 UV;\n" "attribute vec4 Color;\n" "varying vec2 Frag_UV;\n" "varying vec4 Frag_Color;\n" "void main()\n" "{\n" " Frag_UV = UV;\n" " Frag_Color = Color;\n" " gl_Position = ProjMtx * vec4(Position.xy,0,1);\n" "}\n"; const GLchar* vertex_shader_glsl_130 = "uniform mat4 ProjMtx;\n" "in vec2 Position;\n" "in vec2 UV;\n" "in vec4 Color;\n" "out vec2 Frag_UV;\n" "out vec4 Frag_Color;\n" "void main()\n" "{\n" " Frag_UV = UV;\n" " Frag_Color = Color;\n" " gl_Position = ProjMtx * vec4(Position.xy,0,1);\n" "}\n"; const GLchar* vertex_shader_glsl_300_es = "precision mediump float;\n" "layout (location = 0) in vec2 Position;\n" "layout (location = 1) in vec2 UV;\n" "layout (location = 2) in vec4 Color;\n" "uniform mat4 ProjMtx;\n" "out vec2 Frag_UV;\n" "out vec4 Frag_Color;\n" "void main()\n" "{\n" " Frag_UV = UV;\n" " Frag_Color = Color;\n" " gl_Position = ProjMtx * vec4(Position.xy,0,1);\n" "}\n"; const GLchar* vertex_shader_glsl_410_core = "layout (location = 0) in vec2 Position;\n" "layout (location = 1) in vec2 UV;\n" "layout (location = 2) in vec4 Color;\n" "uniform mat4 ProjMtx;\n" "out vec2 Frag_UV;\n" "out vec4 Frag_Color;\n" "void main()\n" "{\n" " Frag_UV = UV;\n" " Frag_Color = Color;\n" " gl_Position = ProjMtx * vec4(Position.xy,0,1);\n" "}\n"; const GLchar* fragment_shader_glsl_120 = "#ifdef GL_ES\n" " precision mediump float;\n" "#endif\n" "uniform sampler2D Texture;\n" "varying vec2 Frag_UV;\n" "varying vec4 Frag_Color;\n" "void main()\n" "{\n" " gl_FragColor = Frag_Color * texture2D(Texture, Frag_UV.st);\n" "}\n"; const GLchar* fragment_shader_glsl_130 = "uniform sampler2D Texture;\n" "in vec2 Frag_UV;\n" "in vec4 Frag_Color;\n" "out vec4 Out_Color;\n" "void main()\n" "{\n" " Out_Color = Frag_Color * texture(Texture, Frag_UV.st);\n" "}\n"; const GLchar* fragment_shader_glsl_300_es = "precision mediump float;\n" "uniform sampler2D Texture;\n" "in vec2 Frag_UV;\n" "in vec4 Frag_Color;\n" "layout (location = 0) out vec4 Out_Color;\n" "void main()\n" "{\n" " Out_Color = Frag_Color * texture(Texture, Frag_UV.st);\n" "}\n"; const GLchar* fragment_shader_glsl_410_core = "in vec2 Frag_UV;\n" "in vec4 Frag_Color;\n" "uniform sampler2D Texture;\n" "layout (location = 0) out vec4 Out_Color;\n" "void main()\n" "{\n" " Out_Color = Frag_Color * texture(Texture, Frag_UV.st);\n" "}\n"; // Select shaders matching our GLSL versions const GLchar* vertex_shader = NULL; const GLchar* fragment_shader = NULL; if (glsl_version < 130) { vertex_shader = vertex_shader_glsl_120; fragment_shader = fragment_shader_glsl_120; } else if (glsl_version >= 410) { vertex_shader = vertex_shader_glsl_410_core; fragment_shader = fragment_shader_glsl_410_core; } else if (glsl_version == 300) { vertex_shader = vertex_shader_glsl_300_es; fragment_shader = fragment_shader_glsl_300_es; } else { vertex_shader = vertex_shader_glsl_130; fragment_shader = fragment_shader_glsl_130; } // Create shaders const GLchar* vertex_shader_with_version[2] = { g_GlslVersionString, vertex_shader }; g_VertHandle = glCreateShader(GL_VERTEX_SHADER); glShaderSource(g_VertHandle, 2, vertex_shader_with_version, NULL); glCompileShader(g_VertHandle); CheckShader(g_VertHandle, "vertex shader"); const GLchar* fragment_shader_with_version[2] = { g_GlslVersionString, fragment_shader }; g_FragHandle = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(g_FragHandle, 2, fragment_shader_with_version, NULL); glCompileShader(g_FragHandle); CheckShader(g_FragHandle, "fragment shader"); g_ShaderHandle = glCreateProgram(); glAttachShader(g_ShaderHandle, g_VertHandle); glAttachShader(g_ShaderHandle, g_FragHandle); glLinkProgram(g_ShaderHandle); CheckProgram(g_ShaderHandle, "shader program"); 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"); // Create buffers glGenBuffers(1, &g_VboHandle); glGenBuffers(1, &g_ElementsHandle); ImGui_ImplOpenGL3_CreateFontsTexture(); // Restore modified GL state glBindTexture(GL_TEXTURE_2D, last_texture); glBindBuffer(GL_ARRAY_BUFFER, last_array_buffer); #ifndef IMGUI_IMPL_OPENGL_ES2 glBindVertexArray(last_vertex_array); #endif return true; }
/* Inits the shader program for this object */ void GLSphere::initShader(void) { // Vertex shader source code. This draws the vertices in our window. We have 3 vertices since we're drawing an triangle. // Each vertex is represented by a vector of size 4 (x, y, z, w) coordinates. // static const string vertex_code = vs_string_CoordSystem; static const char * vs_source = vs_string_GLSphere_410.c_str(); // Fragment shader source code. This determines the colors in the fragment generated in the shader pipeline. In this case, it colors the inside of our triangle specified by our vertex shader. // static const string fragment_code = fs_string_CoordSystem; static const char * fs_source = fs_string_GLSphere_410.c_str(); // This next section we'll generate the OpenGL program and attach the shaders to it so that we can render our triangle. _program = glCreateProgram(); // We create a shader with our fragment shader source code and compile it. GLuint fs = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fs, 1, &fs_source, NULL); glCompileShader(fs); CheckShader(fs, GL_FRAGMENT_SHADER); // We create a shader with our vertex shader source code and compile it. GLuint vs = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vs, 1, &vs_source, NULL); glCompileShader(vs); CheckShader(vs, GL_VERTEX_SHADER); // We'll attach our two compiled shaders to the OpenGL program. glAttachShader(_program, vs); glAttachShader(_program, fs); glLinkProgram(_program); glUseProgram(_program); _modelMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, 0.0f)); // Create our model matrix which will halve the size of our model int projectionMatrixLocation = glGetUniformLocation(_program, "projectionMatrixBox"); // Get the location of our projection matrix in the shader _viewMatrixLocation = glGetUniformLocation(_program, "viewMatrixBox"); // Get the location of our view matrix in the shader _modelMatrixLocation = glGetUniformLocation(_program, "modelMatrixBox"); // Get the location of our model matrix in the shader _inverseViewMatrixLocation = glGetUniformLocation(_program, "inverseViewMatrix"); _light_source0._lightPosIdx = glGetUniformLocation(_program, "light_position"); glUniformMatrix4fv(projectionMatrixLocation, 1, GL_FALSE, &projectionMatrix()[0][0] ); // Send our projection matrix to the shader glUniformMatrix4fv(_viewMatrixLocation, 1, GL_FALSE, &viewMatrix()[0][0]); // Send our view matrix to the shader glUniformMatrix4fv(_modelMatrixLocation, 1, GL_FALSE, &_modelMatrix[0][0]); // Send our model matrix to the shader glUniformMatrix4fv(_inverseViewMatrixLocation, 1, GL_FALSE, &invRotatedViewMatrix()[0][0]); /////////////////////////////////////////////////////////////////////////////////////////////// // Material _material._diffuse_material = glm::vec3(1.0, 0.5, 0.0); _material._ambient_material = glm::vec3(1.0, 0.5, 0.0); _material._specular_material = glm::vec3(1.0, 1.0, 1.0); _material._shininess = 12.0; _material._ambientColorPos = glGetUniformLocation(_program, "ambient_color"); _material._diffuseColorPos = glGetUniformLocation(_program, "diffuse_color"); _material._specularColorPos = glGetUniformLocation(_program, "specular_color"); _material._shininessIdx = glGetUniformLocation(_program, "shininess"); // Send the material to your shader program glUniform3fv(_material._ambientColorPos, 1, &_material._ambient_material[0] ); glUniform3fv(_material._diffuseColorPos, 1, &_material._diffuse_material[0]); glUniform3fv(_material._specularColorPos, 1, &_material._specular_material[0]); glUniform1f(_material._shininessIdx, _material._shininess); /////////////////////////////////////////////////////////////////////////////////////////////// // Light // define the position of the light and send the light position to your shader program _light_source0._lightPos = glm::vec4(50.0,50.0,0.0,1.0); _light_source0._ambient_intensity = 0.5; _light_source0._specular_intensity = 1.0; _light_source0._diffuse_intensity = 1.0; _light_source0._ambientIdx = glGetUniformLocation(_program, "ambient_intensity"); _light_source0._diffuseIdx = glGetUniformLocation(_program, "diffuse_intensity"); _light_source0._specularIdx = glGetUniformLocation(_program, "specular_intensity"); // Send the light information to your shader program glUniform1f(_light_source0._ambientIdx, _light_source0._ambient_intensity ); glUniform1f(_light_source0._diffuseIdx, _light_source0._diffuse_intensity); glUniform1f(_light_source0._specularIdx, _light_source0._specular_intensity); glUniform4fv(_light_source0._lightPosIdx, 1, &_light_source0._lightPos[0]); /////////////////////////////////////////////////////////////////////////////////////////////// // Vertex information / names // bind the to the shader program glBindAttribLocation(_program, 0, "in_Position"); glBindAttribLocation(_program, 1, "in_Normal"); glBindAttribLocation(_program, 2, "in_Color"); glUseProgram(0); }
/** * Vraci program (zkompilovane shadery) pro renderovani nahledoveho krize */ GLuint Shaders::getPreviewProgram() { const char *p_s_vertex_shader = "#version 330\n" "in vec3 v_pos;\n" // atributy - vstup z dat vrcholu "in vec2 v_tex;\n" // souradnice textury "\n" "uniform mat4 t_modelview_projection_matrix;\n" // parametr shaderu - transformacni matice "\n" "out vec2 v_texcoord, v_normal_tex;\n" "\n" "void main()\n" "{\n" " gl_Position = t_modelview_projection_matrix * vec4(v_pos, 1.0);\n" // musime zapsat pozici " v_texcoord = v_tex;\n" " v_normal_tex = v_tex * 2.0 - 1.0;\n" "}\n"; const char *p_s_fragment_shader = "#version 330\n" "in vec2 v_texcoord, v_normal_tex;\n" "\n" "out vec4 frag_color;\n" // vystup do framebufferu "\n" "uniform sampler2D n_tex;\n" "\n" "void main()\n" "{\n" /* " vec4 tex_color = vec4(0.0);" " if(v_texcoord.y >= 0.25 && v_texcoord.y <= 0.75) {" " if(v_texcoord.x < 0.25)" " tex_color = texture(n_tex, (v_texcoord + vec2(0, -.25)) * vec2(0.25/0.25, 0.66667/0.5));" " else if(v_texcoord.x < 0.75)" " tex_color = texture(n_tex, (v_texcoord + vec2(0, -.25)) * vec2(0.5/0.5, 0.66667/0.5));" " else" " tex_color = texture(n_tex, (v_texcoord + vec2(0, -.25)) * vec2(0.25/0.25, 0.66667/0.5));" " } else if(v_texcoord.y < 0.25 && v_texcoord.x >= 0.25 && v_texcoord.x <= 0.75)" " tex_color = texture(n_tex, (v_texcoord + vec2(-.25, 0)) * vec2(0.5/0.5, 0.33333/0.25) + vec2(0.5, 0.66667));" " else if(v_texcoord.y > 0.75 && v_texcoord.x >= 0.25 && v_texcoord.x <= 0.75)" " tex_color = texture(n_tex, (v_texcoord + vec2(-0.25, -.75)) * vec2(0.5/0.5, 0.33333/0.25) + vec2(0.0, 0.66667));" " else discard;" */ " vec4 tex_color = texture(n_tex, v_texcoord);\n" /* " vec4 tex_color = texture(n_tex, v_texcoord) * .5;//vec4(0.0);\n" // precte texturu krabice " float f_tex_length = length(v_normal_tex);\n" " vec3 v_ray = vec3(v_normal_tex, sqrt(1 - f_tex_length));\n" " if(f_tex_length > 1.0)\n" " discard;\n" " vec3 v_ray_abs = abs(v_ray);\n" " if(v_ray.z > v_ray_abs.x && v_ray.z > v_ray_abs.y)\n" // dopredu " tex_color = texture(n_tex, ((v_ray.xy / v_ray.z) * .5 + .5) * vec2(0.5, 0.66667) + vec2(0.25, 0.0));\n" " else if(v_ray_abs.x > v_ray_abs.y) {\n" " if(v_ray.x < 0)\n" // vlevo " tex_color = texture(n_tex, ((v_ray.yz / v_ray.x) * vec2(0.5, 0.5) + vec2(0.5, 0.5)) * vec2(-0.25, -0.66667) + vec2(0.25, 0.66667));\n" " else\n" // vpravo " tex_color = texture(n_tex, ((v_ray.yz / v_ray.x) * vec2(-0.5, 0.5) + vec2(0.5, 0.5)) * vec2(0.25, 0.66667) + vec2(0.75, 0.0));\n" " } else {\n" " if(v_ray.y < 0)\n" // nahoru " tex_color = texture(n_tex, ((v_ray.xz / v_ray.y) * vec2(0.5, 0.5) + vec2(0.5, 0.5)) * vec2(-0.5, -0.33333) + vec2(1.0, 1.0));\n" " else\n" // dolu " tex_color = texture(n_tex, ((v_ray.xz / v_ray.y) * vec2(0.5, -0.5) + vec2(0.5, 0.5)) * vec2(0.5, 0.33333) + vec2(0, 0.66667));\n" " }\n" */ " frag_color = tex_color;\n" "}\n"; // zkompiluje vertex / fragment shader, pripoji je k programu n_preview_vertex_shader = glCreateShader(GL_VERTEX_SHADER); glShaderSource(n_preview_vertex_shader, 1, &p_s_vertex_shader, NULL); glCompileShader(n_preview_vertex_shader); if(!CheckShader(n_preview_vertex_shader, "vertex shader")) return false; n_preview_fragment_shader = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(n_preview_fragment_shader, 1, &p_s_fragment_shader, NULL); glCompileShader(n_preview_fragment_shader); if(!CheckShader(n_preview_fragment_shader, "fragment shader")) return false; n_preview_program_object = glCreateProgram(); glAttachShader(n_preview_program_object, n_preview_vertex_shader); glAttachShader(n_preview_program_object, n_preview_fragment_shader); // nabinduje atributy (propojeni mezi obsahem VBO a vstupem do vertex shaderu) glBindAttribLocation(n_preview_program_object, 0, "v_pos"); glBindAttribLocation(n_preview_program_object, 2, "v_tex"); // nabinduje vystupni promenou (propojeni mezi framebufferem a vystupem fragment shaderu) glBindFragDataLocation(n_preview_program_object, 0, "frag_color"); // slinkuje program glLinkProgram(n_preview_program_object); if(!CheckProgram(n_preview_program_object, "program")) return false; else return n_preview_program_object; }