GLVis  v4.2
Accurate and flexible finite element visualization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
shader.hpp
Go to the documentation of this file.
1 // Copyright (c) 2010-2022, Lawrence Livermore National Security, LLC. Produced
2 // at the Lawrence Livermore National Laboratory. All Rights reserved. See files
3 // LICENSE and NOTICE for details. LLNL-CODE-443271.
4 //
5 // This file is part of the GLVis visualization tool and library. For more
6 // information and source code availability see https://glvis.org.
7 //
8 // GLVis is free software; you can redistribute it and/or modify it under the
9 // terms of the BSD-3 license. We welcome feedback and contributions, see file
10 // CONTRIBUTING.md for details.
11 
12 #ifndef GLVIS_SHADER_HPP
13 #define GLVIS_SHADER_HPP
14 
15 #include "renderer.hpp"
16 
17 #include <unordered_map>
18 
19 namespace gl3
20 {
22 {
23 public:
25  {
26  program_id = glCreateProgram();
27  if (program_id == 0)
28  {
29  std::cerr << "Failed to create an OpenGL program object." << std::endl;
30  }
31  }
32 
33  bool create(std::string vertexShader,
34  std::string fragmentShader,
35  std::unordered_map<int, std::string> inAttributes,
36  int numOutputs);
37 
38  bool isCompiled() const { return is_compiled; }
39 
40  int uniform(std::string uniformName) const
41  {
42  auto unifId = uniform_idx.find(uniformName);
43  if (unifId != uniform_idx.end())
44  {
45  return unifId->second;
46  }
47  return -1;
48  }
49 
50  std::unordered_map<std::string, GLuint> getUniformMap() const
51  {
52  return uniform_idx;
53  }
54 
55  GLuint getProgramId() const { return program_id; }
56 
57  void bind() const { glUseProgram(program_id); }
58 
59 private:
60  static void GetGLSLVersion();
61 
62  std::string formatShader(const std::string& inShader, GLenum shaderType);
63  GLuint compileShader(const std::string& inShader, GLenum shaderType);
64  bool linkShaders(const std::vector<GLuint>& shaders);
65  void mapShaderUniforms();
66 
67  static int glsl_version;
68  const static bool glsl_is_es;
69  std::unordered_map<int, std::string> attrib_idx;
70  int num_outputs = 0;
71 
72  ShaderPrgmHandle program_id = 0;
73  ShaderHandle vertex_shader = 0;
74  ShaderHandle fragment_shader = 0;
75  bool is_compiled = false;
76  std::unordered_map<std::string, GLuint> uniform_idx;
77 };
78 
79 }
80 
81 #endif // GLVIS_SHADER_HPP
std::unordered_map< std::string, GLuint > getUniformMap() const
Definition: shader.hpp:50
GLuint getProgramId() const
Definition: shader.hpp:55
bool create(std::string vertexShader, std::string fragmentShader, std::unordered_map< int, std::string > inAttributes, int numOutputs)
Definition: shader.cpp:18
void bind() const
Definition: shader.hpp:57
bool isCompiled() const
Definition: shader.hpp:38
int uniform(std::string uniformName) const
Definition: shader.hpp:40