├── imgui.ini ├── src ├── parser │ ├── operator.h │ ├── operator.cpp │ ├── modulator.h │ ├── equation.h │ ├── unit.h │ ├── modulator.cpp │ ├── equation.cpp │ └── unit.cpp ├── renderer │ ├── shader.h │ ├── grid.h │ ├── line.h │ ├── imgui │ │ ├── LICENSE.txt │ │ ├── imgui_impl_sdl.h │ │ ├── imgui_impl_opengl3.h │ │ ├── imconfig.h │ │ ├── imgui_impl_sdl.cpp │ │ ├── imstb_rectpack.h │ │ └── imgui_impl_opengl3.cpp │ ├── line.cpp │ ├── renderer.h │ ├── shader.cpp │ ├── grid.cpp │ └── renderer.cpp └── main.cpp ├── .gitignore ├── makefile ├── README.md └── LICENSE /imgui.ini: -------------------------------------------------------------------------------- 1 | [Window][Debug##Default] 2 | Pos=60,60 3 | Size=400,400 4 | Collapsed=0 5 | 6 | [Window][Grapher] 7 | Pos=4,7 8 | Size=309,302 9 | Collapsed=0 10 | 11 | -------------------------------------------------------------------------------- /src/parser/operator.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | class Operator { 4 | public: 5 | Operator(char c); 6 | ~Operator(); 7 | 8 | // get the type of the operator 9 | char getOp() { return op; } 10 | 11 | // evaluate an operator expression 12 | double evalOp(double lhs, double rhs); 13 | 14 | private: 15 | // operator type 16 | char op; 17 | }; 18 | -------------------------------------------------------------------------------- /src/renderer/shader.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | class Shader { 6 | public: 7 | Shader(); 8 | ~Shader(); 9 | 10 | void useProgram(); 11 | void setUniform3f(const char *name, float one, float two, float three); 12 | void setUniform1f(const char *name, float val); 13 | 14 | private: 15 | GLuint shaderProgram; 16 | }; 17 | -------------------------------------------------------------------------------- /src/renderer/grid.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "line.h" 4 | #include "shader.h" 5 | 6 | class Grid { 7 | public: 8 | Grid(); 9 | ~Grid(); 10 | 11 | void updateGrid(double xOffset, double yOffset, double scale); 12 | void renderGrid(Shader *s); 13 | 14 | private: 15 | std::vector vertLines; 16 | std::vector horizLines; 17 | Line *yAxis; 18 | Line *xAxis; 19 | bool xAct, yAct; 20 | }; 21 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | glGraph 34 | 35 | #ide stuff 36 | .vscode 37 | vgcore.* 38 | *.swp 39 | -------------------------------------------------------------------------------- /src/parser/operator.cpp: -------------------------------------------------------------------------------- 1 | #include "operator.h" 2 | 3 | #include 4 | 5 | Operator::Operator(char c) { op = c; } 6 | 7 | Operator::~Operator() {} 8 | 9 | // evaluate the operator 10 | double Operator::evalOp(double lhs, double rhs) { 11 | switch (op) { 12 | case '+': 13 | return lhs + rhs; 14 | case '-': 15 | return lhs - rhs; 16 | case '/': 17 | return lhs / rhs; 18 | case '*': 19 | return lhs * rhs; 20 | case '^': 21 | return std::pow(lhs, rhs); 22 | default: 23 | return lhs + rhs; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /makefile: -------------------------------------------------------------------------------- 1 | PNAME := glGraph 2 | 3 | PCXXSRC := $(wildcard src/*.cpp) 4 | PCXXSRC += $(wildcard src/parser/*.cpp) 5 | PCXXSRC += $(wildcard src/renderer/*.cpp) 6 | PCXXSRC += $(wildcard src/renderer/imgui/*.cpp) 7 | 8 | POBJS := $(PCXXSRC:.cpp=.o) 9 | 10 | CXXFLAGS += -std=c++17 11 | 12 | LDLIBS += -lSDL2 -lGLEW -lGL 13 | 14 | .SILENT: all $(POBJS) 15 | .PHONY: all 16 | 17 | all: $(PNAME) 18 | 19 | $(PNAME): $(POBJS) 20 | @-$(LINK.cc) $(POBJS) -o $(PNAME) $(LDLIBS) 21 | @-$(RM) $(POBJS) 22 | 23 | clean: 24 | @- $(RM) $(PNAME) 25 | @- $(RM) $(POBJS) 26 | -------------------------------------------------------------------------------- /src/parser/modulator.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | enum class mod { NONE = 0, SIN = 1, COS = 2, TAN = 3, SQRT = 4 }; 6 | 7 | class Modulator { 8 | public: 9 | Modulator(); 10 | ~Modulator(); 11 | 12 | // get and set type 13 | mod getM() const { return m; } 14 | void setM(mod modulator) { m = modulator; } 15 | 16 | // apply the function 17 | double applyMod(double x); 18 | 19 | // allow printing of the modulator (mostly for debug purposes) 20 | friend std::ostream &operator<<(std::ostream &os, const Modulator &m); 21 | 22 | private: 23 | // type of modulator 24 | mod m; 25 | }; 26 | -------------------------------------------------------------------------------- /src/renderer/line.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include 6 | 7 | class Line { 8 | public: 9 | Line(std::vector verts); 10 | Line(double *verts, int numVerts); 11 | ~Line(); 12 | 13 | void draw(); 14 | 15 | float getColorR() { return color[0]; } 16 | float getColorG() { return color[1]; } 17 | float getColorB() { return color[2]; } 18 | 19 | void setColor(float r, float g, float b); 20 | 21 | bool isSelected() { return selected; } 22 | void setSelected(bool val) { selected = val; } 23 | 24 | private: 25 | void init(); 26 | 27 | GLuint VAO, VBO; 28 | std::vector vertices; 29 | float color[3]; 30 | bool selected; 31 | }; 32 | -------------------------------------------------------------------------------- /src/parser/equation.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | #include "unit.h" 7 | 8 | class Equation { 9 | public: 10 | Equation(std::string eq); 11 | ~Equation(); 12 | 13 | // evaluate at a single x-value 14 | double evalAtX(double x, bool useDeg); 15 | 16 | // evaluate over a range of x-values 17 | std::map exportRange(double xPos, double dist, 18 | double resolution, bool scaleRes, 19 | bool useDeg); 20 | 21 | private: 22 | std::string origEq; 23 | Unit *baseUnit; 24 | 25 | std::string processEq(std::string eq); 26 | 27 | bool isModStart(char c); 28 | bool isModulator(std::string t); 29 | bool isOperator(char c); 30 | }; 31 | -------------------------------------------------------------------------------- /src/parser/unit.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include "modulator.h" 9 | #include "operator.h" 10 | 11 | class Unit { 12 | public: 13 | Unit(std::string eq); 14 | ~Unit(); 15 | 16 | // evaluate the unit at an x-value 17 | // and choose whether or not to use degrees (for trigonometric functions) 18 | double evalUnit(double x, bool useDeg); 19 | 20 | std::string getEqStr() { return eqStr; } 21 | 22 | private: 23 | std::string eqStr; 24 | bool hasVar; 25 | double noVarValDeg; 26 | double noVarValRad; 27 | 28 | bool modExp; 29 | Modulator m; 30 | std::pair modUnit; 31 | 32 | bool isOperator(char c); 33 | std::vector> exp; 34 | 35 | // evaluate operator expressions 36 | double evalOps(std::vector> opVec); 37 | }; 38 | -------------------------------------------------------------------------------- /src/parser/modulator.cpp: -------------------------------------------------------------------------------- 1 | #include "modulator.h" 2 | 3 | #include 4 | #include 5 | 6 | Modulator::Modulator() { m = mod::NONE; } 7 | 8 | Modulator::~Modulator(){}; 9 | 10 | // apply the function 11 | double Modulator::applyMod(double x) { 12 | switch (m) { 13 | case mod::SIN: 14 | return std::sin(x); 15 | case mod::COS: 16 | return std::cos(x); 17 | case mod::TAN: 18 | return std::tan(x); 19 | case mod::SQRT: 20 | return std::sqrt(x); 21 | case mod::NONE: 22 | return x; 23 | default: 24 | return x; 25 | } 26 | } 27 | 28 | // print the modulator type (used for debug) 29 | std::ostream &operator<<(std::ostream &os, const Modulator &m) { 30 | std::string ret = ""; 31 | switch (m.getM()) { 32 | case mod::SIN: 33 | ret = "sin"; 34 | break; 35 | case mod::COS: 36 | ret = "cos"; 37 | break; 38 | case mod::TAN: 39 | ret = "tan"; 40 | break; 41 | case mod::SQRT: 42 | ret = "sqrt"; 43 | break; 44 | case mod::NONE: 45 | break; 46 | default: 47 | break; 48 | } 49 | return os << ret; 50 | } 51 | -------------------------------------------------------------------------------- /src/renderer/imgui/LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014-2021 Omar Cornut 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/renderer/line.cpp: -------------------------------------------------------------------------------- 1 | #include "line.h" 2 | 3 | #include 4 | 5 | Line::Line(std::vector verts) { 6 | vertices = verts; 7 | init(); 8 | } 9 | 10 | Line::Line(double *verts, int numVerts) { 11 | for (int i = 0; i < numVerts; i++) { 12 | vertices.push_back(verts[i]); 13 | } 14 | init(); 15 | } 16 | 17 | void Line::init() { 18 | color[0] = 1.0f; 19 | color[1] = 0.0f; 20 | color[2] = 0.0f; 21 | 22 | selected = false; 23 | glGenVertexArrays(1, &VAO); 24 | glGenBuffers(1, &VBO); 25 | glBindVertexArray(VAO); 26 | glBindBuffer(GL_ARRAY_BUFFER, VBO); 27 | glVertexAttribPointer(0, 3, GL_DOUBLE, GL_FALSE, 0, (void *)0); 28 | glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(double), 29 | vertices.data(), GL_DYNAMIC_DRAW); 30 | glEnableVertexAttribArray(0); 31 | } 32 | 33 | Line::~Line() { 34 | glDeleteVertexArrays(1, &VAO); 35 | glDeleteBuffers(1, &VBO); 36 | } 37 | 38 | void Line::draw() { 39 | glBindVertexArray(VAO); 40 | glDrawArrays(GL_LINE_STRIP, 0, vertices.size() / 3); 41 | } 42 | 43 | void Line::setColor(float r, float g, float b) { 44 | color[0] = r; 45 | color[1] = g; 46 | color[2] = b; 47 | } 48 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # glGraph 2 | 3 | A 2d Graphing Calculator using Modern OpenGL 4 | 5 | ## Demo 6 | https://user-images.githubusercontent.com/5031736/122145371-1c5d8400-ce23-11eb-9e6e-e537469c06df.mp4 7 | 8 | ## Information 9 | 10 | - This has only been tested on Fedora 34, it should work on other OS's but I make no guarantees. 11 | - This uses C++17 features. 12 | - Controls: WASD to move around, QE to zoom 13 | 14 | ## What can it do so far? 15 | 16 | - Basic operations (+, -, *, /, ^) 17 | - Order of operations (parentheses, etc) 18 | - Functions: sin, cos, tan, sqrt 19 | - Switch between radians and degrees 20 | - Multiple lines, ability to change line color, and ability to remove lines 21 | - Live equation updating (not shown in example) 22 | 23 | ## Known issues 24 | 25 | - floating point precision errors 26 | - functions with undefined values (e.g. divides by 0 or goes to infinity) sometimes display strange behavior 27 | - glLineWidth may not work on some (maybe all) versions of macOS because of limited GL_LINE_WIDTH_RANGE 28 | - invalid character sequences may cause program to crash 29 | 30 | ## Dependencies 31 | 32 | - OpenGL 3.3 core 33 | - SDL2 34 | - GLEW 35 | - Dear ImGui 36 | -------------------------------------------------------------------------------- /src/renderer/renderer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #define SDL_MAIN_HANDLED 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include "imgui/imgui.h" 12 | #include "imgui/imgui_impl_opengl3.h" 13 | #include "imgui/imgui_impl_sdl.h" 14 | 15 | #include "grid.h" 16 | #include "line.h" 17 | #include "shader.h" 18 | 19 | class Renderer { 20 | public: 21 | Renderer(); 22 | ~Renderer(); 23 | 24 | bool isRunning() { return running; } 25 | 26 | void clear(); 27 | void update(double *xPos, double *scale, std::vector *equs, 28 | double *resolution, bool *scaleRes, bool *updateEq, 29 | bool *updatePos, bool *useDeg); 30 | 31 | void graphPoint(double x, double y); 32 | void graphLine(std::map points, int index, double scale); 33 | 34 | private: 35 | bool running; 36 | int screenWidth = 1024; 37 | int screenHeight = 600; 38 | 39 | bool eqUpdate; 40 | std::vector eqBuf; 41 | 42 | double yOffset; 43 | double xOffset; 44 | 45 | int mouseX, mouseY; 46 | bool showMouse; 47 | 48 | SDL_Window *gWindow; 49 | SDL_GLContext gContext; 50 | 51 | Shader *s; 52 | std::vector lines; 53 | bool lineAct; 54 | int numLines; 55 | float newColor[3]; 56 | int lInd; 57 | int currSel; 58 | 59 | Grid *g; 60 | }; 61 | -------------------------------------------------------------------------------- /src/renderer/imgui/imgui_impl_sdl.h: -------------------------------------------------------------------------------- 1 | // dear imgui: Platform Backend for SDL2 2 | // This needs to be used along with a Renderer (e.g. DirectX11, OpenGL3, Vulkan..) 3 | // (Info: SDL2 is a cross-platform general purpose library for handling windows, inputs, graphics context creation, etc.) 4 | 5 | // Implemented features: 6 | // [X] Platform: Mouse cursor shape and visibility. Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'. 7 | // [X] Platform: Clipboard support. 8 | // [X] Platform: Keyboard arrays indexed using SDL_SCANCODE_* codes, e.g. ImGui::IsKeyPressed(SDL_SCANCODE_SPACE). 9 | // [X] Platform: Gamepad support. Enabled with 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad'. 10 | // Missing features: 11 | // [ ] Platform: SDL2 handling of IME under Windows appears to be broken and it explicitly disable the regular Windows IME. You can restore Windows IME by compiling SDL with SDL_DISABLE_WINDOWS_IME. 12 | 13 | // You can copy and use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this. 14 | // If you are new to Dear ImGui, read documentation from the docs/ folder + read the top of imgui.cpp. 15 | // Read online: https://github.com/ocornut/imgui/tree/master/docs 16 | 17 | #pragma once 18 | #include "imgui.h" // IMGUI_IMPL_API 19 | 20 | struct SDL_Window; 21 | typedef union SDL_Event SDL_Event; 22 | 23 | IMGUI_IMPL_API bool ImGui_ImplSDL2_InitForOpenGL(SDL_Window* window, void* sdl_gl_context); 24 | IMGUI_IMPL_API bool ImGui_ImplSDL2_InitForVulkan(SDL_Window* window); 25 | IMGUI_IMPL_API bool ImGui_ImplSDL2_InitForD3D(SDL_Window* window); 26 | IMGUI_IMPL_API bool ImGui_ImplSDL2_InitForMetal(SDL_Window* window); 27 | IMGUI_IMPL_API void ImGui_ImplSDL2_Shutdown(); 28 | IMGUI_IMPL_API void ImGui_ImplSDL2_NewFrame(SDL_Window* window); 29 | IMGUI_IMPL_API bool ImGui_ImplSDL2_ProcessEvent(const SDL_Event* event); 30 | -------------------------------------------------------------------------------- /src/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "parser/equation.h" 4 | #include "renderer/renderer.h" 5 | 6 | int main(int argc, char *argv[]) { 7 | 8 | std::vector currEqs = {""}; 9 | std::vector eqs = {new Equation("")}; 10 | Renderer r; 11 | 12 | // viewport controls 13 | double xPos = 0.0f; 14 | double resolution = 0.1f; 15 | double scale = 1.0f; 16 | bool scaleRes = true; 17 | 18 | // misc calculator controls 19 | bool useDeg = false; 20 | 21 | while (r.isRunning()) { 22 | r.clear(); 23 | 24 | bool updateEq = false; 25 | bool updatePos = false; 26 | 27 | r.update(&xPos, &scale, &currEqs, &resolution, &scaleRes, &updateEq, 28 | &updatePos, &useDeg); 29 | 30 | // update changed equations 31 | if (updateEq) { 32 | for (int i = 0; i < std::min(currEqs.size(), eqs.size()); i++) { 33 | delete eqs.at(i); 34 | eqs.at(i) = new Equation(currEqs.at(i)); 35 | } 36 | // if an equation should be removed, remove it 37 | if (currEqs.size() < eqs.size()) { 38 | delete eqs.at(eqs.size() - 1); 39 | eqs.erase(eqs.end() - 1); 40 | } 41 | // if an equation should be added, add it 42 | if (currEqs.size() > eqs.size()) { 43 | eqs.push_back(new Equation(currEqs.at(currEqs.size() - 1))); 44 | } 45 | } 46 | 47 | // redraw the equations when the camera position updates 48 | if (updatePos) { 49 | int index = 0; 50 | for (auto s : currEqs) { 51 | if (s != "") { 52 | std::map map = eqs.at(index)->exportRange( 53 | xPos, scale, resolution, scaleRes, useDeg); 54 | r.graphLine(map, index, scale); 55 | /*for (auto [x, y] : map) { 56 | std::cout << "x=" << x << " " 57 | << "y=" << y << std::endl; 58 | }*/ 59 | } 60 | index++; 61 | } 62 | } 63 | } 64 | 65 | for (int i = 0; i < eqs.size(); i++) 66 | delete eqs.at(i); 67 | 68 | return 0; 69 | } 70 | -------------------------------------------------------------------------------- /src/renderer/shader.cpp: -------------------------------------------------------------------------------- 1 | #include "shader.h" 2 | 3 | #include 4 | 5 | Shader::Shader() { 6 | const char *vertSrc = R""( 7 | #version 330 core 8 | in vec3 fPos; 9 | void main(){ 10 | gl_Position = vec4(fPos, 1.0); 11 | } 12 | )""; 13 | 14 | const char *fragSrc = R""( 15 | #version 330 core 16 | out vec4 outColor; 17 | uniform vec3 color; 18 | uniform float opacity; 19 | void main(){ 20 | outColor = vec4(color, opacity); 21 | } 22 | )""; 23 | 24 | int success; 25 | char infoLog[512]; 26 | 27 | GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER); 28 | glShaderSource(vertexShader, 1, &vertSrc, NULL); 29 | glCompileShader(vertexShader); 30 | glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success); 31 | if (!success) { 32 | glGetShaderInfoLog(vertexShader, 512, NULL, infoLog); 33 | std::cout << "Vertex: " << infoLog << std::endl; 34 | } 35 | 36 | GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); 37 | glShaderSource(fragmentShader, 1, &fragSrc, NULL); 38 | glCompileShader(fragmentShader); 39 | glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success); 40 | if (!success) { 41 | glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog); 42 | std::cout << "Fragment: " << infoLog << std::endl; 43 | } 44 | 45 | shaderProgram = glCreateProgram(); 46 | glAttachShader(shaderProgram, vertexShader); 47 | glAttachShader(shaderProgram, fragmentShader); 48 | glLinkProgram(shaderProgram); 49 | glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success); 50 | if (!success) { 51 | glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog); 52 | std::cout << "Program: " << infoLog << std::endl; 53 | } 54 | 55 | glDeleteShader(vertexShader); 56 | glDeleteShader(fragmentShader); 57 | } 58 | 59 | Shader::~Shader() { glDeleteProgram(shaderProgram); } 60 | 61 | void Shader::useProgram() { glUseProgram(shaderProgram); } 62 | 63 | void Shader::setUniform3f(const char *name, float one, float two, float three) { 64 | GLuint loc = glGetUniformLocation(shaderProgram, name); 65 | glUniform3f(loc, one, two, three); 66 | } 67 | 68 | void Shader::setUniform1f(const char *name, float val) { 69 | GLuint loc = glGetUniformLocation(shaderProgram, name); 70 | glUniform1f(loc, val); 71 | } 72 | -------------------------------------------------------------------------------- /src/renderer/grid.cpp: -------------------------------------------------------------------------------- 1 | #include "grid.h" 2 | 3 | #include 4 | #include 5 | 6 | Grid::Grid() { 7 | xAct = false; 8 | yAct = false; 9 | updateGrid(0.0f, 0.0f, 1.0f); 10 | } 11 | 12 | Grid::~Grid() { 13 | delete yAxis; 14 | delete xAxis; 15 | for (int i = 0; i < horizLines.size(); i++) { 16 | delete horizLines.at(i); 17 | } 18 | for (int i = 0; i < vertLines.size(); i++) { 19 | delete vertLines.at(i); 20 | } 21 | } 22 | 23 | void Grid::updateGrid(double xOffset, double yOffset, double scale) { 24 | for (int i = 0; i < horizLines.size(); i++) { 25 | delete horizLines.at(i); 26 | } 27 | horizLines.clear(); 28 | for (float i = yOffset; i <= scale + yOffset; i += 1.0f / scale) { 29 | std::vector tVerts = {-1.0f, i, 0.0f, 1.0f, i, 0.0f}; 30 | horizLines.push_back(new Line(tVerts)); 31 | } 32 | 33 | for (float i = yOffset; i >= -1.0f * scale + yOffset; i -= 1.0f / scale) { 34 | std::vector tVerts = {-1.0f, i, 0.0f, 1.0f, i, 0.0f}; 35 | horizLines.push_back(new Line(tVerts)); 36 | } 37 | 38 | if (xAct) 39 | delete xAxis; 40 | double x = (0.0f + xOffset) / scale; 41 | double v[] = {x, 1.0f, 0.0f, x, -1.0f, 0.0f}; 42 | xAxis = new Line(v, 6); 43 | xAct = true; 44 | 45 | for (int i = 0; i < vertLines.size(); i++) { 46 | delete vertLines.at(i); 47 | } 48 | vertLines.clear(); 49 | 50 | for (float i = xOffset / scale; i <= scale + xOffset; i += 1.0f / scale) { 51 | std::vector tVerts = {i, -1.0f, 0.0f, i, 1.0f, 0.0f}; 52 | vertLines.push_back(new Line(tVerts)); 53 | } 54 | for (float i = xOffset / scale; i >= -1.0 * scale + xOffset; 55 | i -= 1.0f / scale) { 56 | std::vector tVerts = {i, -1.0f, 0.0f, i, 1.0f, 0.0f}; 57 | vertLines.push_back(new Line(tVerts)); 58 | } 59 | 60 | if (yAct) 61 | delete yAxis; 62 | double y = (0.0f + yOffset); 63 | double vy[] = {-1.0f, y, 0.0f, 1.0f, y, 0.0f}; 64 | yAxis = new Line(vy, 6); 65 | yAct = true; 66 | } 67 | 68 | void Grid::renderGrid(Shader *s) { 69 | s->setUniform3f("color", 0.5f, 0.5f, 0.5f); 70 | glLineWidth(1.0f); 71 | for (auto l : vertLines) { 72 | l->draw(); 73 | } 74 | for (auto l : horizLines) { 75 | l->draw(); 76 | } 77 | 78 | glLineWidth(4.0f); 79 | s->setUniform3f("color", 0.0f, 0.0f, 0.0f); 80 | yAxis->draw(); 81 | xAxis->draw(); 82 | } 83 | -------------------------------------------------------------------------------- /src/renderer/imgui/imgui_impl_opengl3.h: -------------------------------------------------------------------------------- 1 | // dear imgui: Renderer Backend for modern OpenGL with shaders / programmatic pipeline 2 | // - Desktop GL: 2.x 3.x 4.x 3 | // - Embedded GL: ES 2.0 (WebGL 1.0), ES 3.0 (WebGL 2.0) 4 | // This needs to be used along with a Platform Backend (e.g. GLFW, SDL, Win32, custom..) 5 | 6 | // Implemented features: 7 | // [X] Renderer: User texture binding. Use 'GLuint' OpenGL texture identifier as void*/ImTextureID. Read the FAQ about ImTextureID! 8 | // [x] Renderer: Desktop GL only: Support for large meshes (64k+ vertices) with 16-bit indices. 9 | 10 | // You can copy and use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this. 11 | // If you are new to Dear ImGui, read documentation from the docs/ folder + read the top of imgui.cpp. 12 | // Read online: https://github.com/ocornut/imgui/tree/master/docs 13 | 14 | // About Desktop OpenGL function loaders: 15 | // Modern Desktop OpenGL doesn't have a standard portable header file to load OpenGL function pointers. 16 | // Helper libraries are often used for this purpose! Here we are supporting a few common ones (gl3w, glew, glad). 17 | // You may use another loader/header of your choice (glext, glLoadGen, etc.), or chose to manually implement your own. 18 | 19 | // About GLSL version: 20 | // The 'glsl_version' initialization parameter should be NULL (default) or a "#version XXX" string. 21 | // On computer platform the GLSL version default to "#version 130". On OpenGL ES 3 platform it defaults to "#version 300 es" 22 | // Only override if your GL version doesn't handle this GLSL version. See GLSL version table at the top of imgui_impl_opengl3.cpp. 23 | 24 | #pragma once 25 | #include "imgui.h" // IMGUI_IMPL_API 26 | 27 | // Backend API 28 | IMGUI_IMPL_API bool ImGui_ImplOpenGL3_Init(const char* glsl_version = NULL); 29 | IMGUI_IMPL_API void ImGui_ImplOpenGL3_Shutdown(); 30 | IMGUI_IMPL_API void ImGui_ImplOpenGL3_NewFrame(); 31 | IMGUI_IMPL_API void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data); 32 | 33 | // (Optional) Called by Init/NewFrame/Shutdown 34 | IMGUI_IMPL_API bool ImGui_ImplOpenGL3_CreateFontsTexture(); 35 | IMGUI_IMPL_API void ImGui_ImplOpenGL3_DestroyFontsTexture(); 36 | IMGUI_IMPL_API bool ImGui_ImplOpenGL3_CreateDeviceObjects(); 37 | IMGUI_IMPL_API void ImGui_ImplOpenGL3_DestroyDeviceObjects(); 38 | 39 | // Specific OpenGL ES versions 40 | //#define IMGUI_IMPL_OPENGL_ES2 // Auto-detected on Emscripten 41 | //#define IMGUI_IMPL_OPENGL_ES3 // Auto-detected on iOS/Android 42 | 43 | // Attempt to auto-detect the default Desktop GL loader based on available header files. 44 | // If auto-detection fails or doesn't select the same GL loader file as used by your application, 45 | // you are likely to get a crash in ImGui_ImplOpenGL3_Init(). 46 | // You can explicitly select a loader by using one of the '#define IMGUI_IMPL_OPENGL_LOADER_XXX' in imconfig.h or compiler command-line. 47 | #if !defined(IMGUI_IMPL_OPENGL_ES2) \ 48 | && !defined(IMGUI_IMPL_OPENGL_ES3) \ 49 | && !defined(IMGUI_IMPL_OPENGL_LOADER_GL3W) \ 50 | && !defined(IMGUI_IMPL_OPENGL_LOADER_GLEW) \ 51 | && !defined(IMGUI_IMPL_OPENGL_LOADER_GLAD) \ 52 | && !defined(IMGUI_IMPL_OPENGL_LOADER_GLAD2) \ 53 | && !defined(IMGUI_IMPL_OPENGL_LOADER_GLBINDING2) \ 54 | && !defined(IMGUI_IMPL_OPENGL_LOADER_GLBINDING3) \ 55 | && !defined(IMGUI_IMPL_OPENGL_LOADER_CUSTOM) 56 | 57 | // Try to detect GLES on matching platforms 58 | #if defined(__APPLE__) 59 | #include "TargetConditionals.h" 60 | #endif 61 | #if (defined(__APPLE__) && (TARGET_OS_IOS || TARGET_OS_TV)) || (defined(__ANDROID__)) 62 | #define IMGUI_IMPL_OPENGL_ES3 // iOS, Android -> GL ES 3, "#version 300 es" 63 | #elif defined(__EMSCRIPTEN__) 64 | #define IMGUI_IMPL_OPENGL_ES2 // Emscripten -> GL ES 2, "#version 100" 65 | 66 | // Otherwise try to detect supported Desktop OpenGL loaders.. 67 | #elif defined(__has_include) 68 | #if __has_include() 69 | #define IMGUI_IMPL_OPENGL_LOADER_GLEW 70 | #elif __has_include() 71 | #define IMGUI_IMPL_OPENGL_LOADER_GLAD 72 | #elif __has_include() 73 | #define IMGUI_IMPL_OPENGL_LOADER_GLAD2 74 | #elif __has_include() 75 | #define IMGUI_IMPL_OPENGL_LOADER_GL3W 76 | #elif __has_include() 77 | #define IMGUI_IMPL_OPENGL_LOADER_GLBINDING3 78 | #elif __has_include() 79 | #define IMGUI_IMPL_OPENGL_LOADER_GLBINDING2 80 | #else 81 | #error "Cannot detect OpenGL loader!" 82 | #endif 83 | #else 84 | #define IMGUI_IMPL_OPENGL_LOADER_GL3W // Default to GL3W embedded in our repository 85 | #endif 86 | 87 | #endif 88 | -------------------------------------------------------------------------------- /src/parser/equation.cpp: -------------------------------------------------------------------------------- 1 | #include "equation.h" 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | Equation::Equation(std::string eq) { 8 | // std::cout << "Original: " << eq << std::endl; 9 | origEq = processEq(eq); 10 | // std::cout << "Processed: " << origEq << std::endl; 11 | 12 | baseUnit = new Unit(origEq); 13 | } 14 | 15 | Equation::~Equation() { delete baseUnit; } 16 | 17 | // is the char the start of a function (sqrt, sin, cos, tan) 18 | bool Equation::isModStart(char c) { 19 | switch (c) { 20 | case 's': 21 | case 'c': 22 | case 't': 23 | return true; 24 | default: 25 | return false; 26 | } 27 | } 28 | 29 | // is the string a function (sqrt, sin, cos, tan) 30 | bool Equation::isModulator(std::string t) { 31 | if (t.length() == 3) { 32 | if (t.compare("sin") == 0 || t.compare("cos") == 0 || 33 | t.compare("tan") == 0) { 34 | return true; 35 | } 36 | } else if (t.length() == 4) { 37 | if (t.compare("sqrt") == 0) { 38 | return true; 39 | } 40 | } 41 | return false; 42 | } 43 | 44 | // is the char an operator (space is counted as an operator here because spaces 45 | // have not been removed yet when this function is used 46 | bool Equation::isOperator(char c) { 47 | switch (c) { 48 | case '+': 49 | case '-': 50 | case '/': 51 | case '*': 52 | case '^': 53 | case ' ': 54 | return true; 55 | default: 56 | return false; 57 | } 58 | } 59 | 60 | // process the equation into something that the computer can understand 61 | std::string Equation::processEq(std::string eq) { 62 | // split equation into units and surround with parentheses 63 | // what is a unit? 64 | // number: 3.5, 5 65 | // variable: x, y 66 | // modulator expression: sin(x), sin(x+2), cos(3) 67 | std::string stepOneOut = ""; 68 | std::string curr = ""; 69 | std::vector exitDepths; 70 | bool inMod = false; 71 | int parenDepth = 0; 72 | for (auto it = eq.begin(); it < eq.end(); it++) { 73 | if (isOperator(*it)) { 74 | if (curr != "") { 75 | stepOneOut += "(" + curr + ")"; 76 | curr = ""; 77 | } 78 | stepOneOut += *it; 79 | } else if (isModStart(*it)) { 80 | std::string test = ""; 81 | test += *it; 82 | test += *(it + 1); 83 | test += *(it + 2); 84 | if (isModulator(test)) { 85 | if (curr != "") { 86 | stepOneOut += "(" + curr + ")"; 87 | curr = ""; 88 | } 89 | stepOneOut += "(" + test + "("; 90 | exitDepths.push_back(parenDepth); 91 | inMod = true; 92 | it += 2; 93 | } else { 94 | test += *(it + 3); 95 | if (isModulator(test)) { 96 | if (curr != "") { 97 | stepOneOut += "(" + curr + ")"; 98 | curr = ""; 99 | } 100 | 101 | stepOneOut += "(" + test; 102 | exitDepths.push_back(parenDepth); 103 | inMod = true; 104 | it += 3; 105 | } else { 106 | return ""; 107 | } 108 | } 109 | } else if (*it == 'x' || *it == 'y') { 110 | if (curr != "") { 111 | stepOneOut += "(" + curr + ")"; 112 | curr = ""; 113 | } 114 | stepOneOut += "("; 115 | stepOneOut += *it; 116 | stepOneOut += ")"; 117 | } else if (*it == '(') { 118 | parenDepth++; 119 | if (curr != "") { 120 | curr += "("; 121 | } else { 122 | stepOneOut += "("; 123 | } 124 | } else if (*it == ')') { 125 | parenDepth--; 126 | if (curr != "") { 127 | curr += ")"; 128 | } else { 129 | stepOneOut += ")"; 130 | } 131 | } else { 132 | curr += *it; 133 | } 134 | 135 | // deal with units inside of a modulator 136 | if (inMod && exitDepths.size() > 0 && *(it + 1) != '(') { 137 | auto f = std::find(exitDepths.begin(), exitDepths.end(), parenDepth); 138 | if (f != exitDepths.end()) { 139 | if (curr != "") { 140 | stepOneOut += "(" + curr + ")"; 141 | curr = ""; 142 | } 143 | stepOneOut += "))"; 144 | exitDepths.erase(f); 145 | inMod = false; 146 | } 147 | } 148 | } 149 | if (curr != "") { 150 | stepOneOut += "(" + curr + ")"; 151 | curr = ""; 152 | } 153 | 154 | // bad hack to fix issue with parentheses not adding up 155 | int numEnd = 0; 156 | int numBegin = 0; 157 | for (auto c : stepOneOut) { 158 | if (c == ')') 159 | numEnd++; 160 | if (c == '(') 161 | numBegin++; 162 | } 163 | 164 | if (numBegin > numEnd) { 165 | for (int i = 0; i < numBegin - numEnd; i++) { 166 | stepOneOut += ")"; 167 | } 168 | } 169 | 170 | // remove spaces 171 | std::string stepTwoOut = ""; 172 | for (auto c : stepOneOut) { 173 | if (c != ' ') { 174 | stepTwoOut += c; 175 | } 176 | } 177 | 178 | // add implied multiplication and subtraction 179 | // implied multiplication is when 3x = (3)*(x) 180 | // implied subtraction is when -2 = (0) - (2) 181 | std::string stepThreeOut = ""; 182 | for (auto it = stepTwoOut.begin(); it < stepTwoOut.end(); it++) { 183 | if (*it == '-') { 184 | if (it == stepTwoOut.begin()) { 185 | stepThreeOut += "(0)-"; 186 | } else if (isOperator(*(it - 1))) { 187 | stepThreeOut += "(0)-"; 188 | } else { 189 | stepThreeOut += "-"; 190 | } 191 | } else if (*it == '(') { 192 | if (it != stepTwoOut.begin() && *(it - 1) == ')') { 193 | stepThreeOut += "*("; 194 | } else { 195 | stepThreeOut += "("; 196 | } 197 | } else { 198 | stepThreeOut += *it; 199 | } 200 | } 201 | 202 | return stepThreeOut; 203 | } 204 | 205 | // evaluate at a single x-value 206 | double Equation::evalAtX(double x, bool useDeg) { 207 | double ret = 0; 208 | ret = baseUnit->evalUnit(x, useDeg); 209 | return ret; 210 | } 211 | 212 | // evaluate over a range of x-values 213 | std::map Equation::exportRange(double xPos, double dist, 214 | double resolution, bool scaleRes, 215 | bool useDeg) { 216 | std::map ret; 217 | 218 | if (resolution == 0.0f) 219 | resolution = 0.1f; 220 | 221 | // scale the resolution based upon the zoom of the camera 222 | if (scaleRes) 223 | resolution = resolution * pow(dist, 0.5f); 224 | 225 | double startX = xPos - dist; 226 | 227 | for (double i = 0; i <= dist * 2 + resolution; i += resolution) { 228 | ret[startX + i] = evalAtX(startX + i, useDeg); 229 | } 230 | 231 | return ret; 232 | } 233 | -------------------------------------------------------------------------------- /src/parser/unit.cpp: -------------------------------------------------------------------------------- 1 | #include "unit.h" 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | // is a character an operator expression 8 | bool Unit::isOperator(char c) { 9 | switch (c) { 10 | case '+': 11 | case '-': 12 | case '/': 13 | case '*': 14 | case '^': 15 | return true; 16 | default: 17 | return false; 18 | } 19 | } 20 | 21 | // create the unit expression 22 | Unit::Unit(std::string eq) { 23 | eqStr = eq; 24 | modExp = false; 25 | 26 | // deal with empty unit 27 | if (eq == "()") { 28 | hasVar = false; 29 | noVarValDeg = 0; 30 | noVarValRad = 0; 31 | return; 32 | } 33 | 34 | // check if the expression has a function 35 | if (eq.size() >= 3) { 36 | if (eq.at(0) == 's' || eq.at(0) == 't' || eq.at(0) == 'c') { 37 | modExp = true; 38 | } 39 | } 40 | 41 | // process the expression if it has a function 42 | if (modExp) { 43 | mod t = mod::NONE; 44 | int subPos = 3; 45 | switch (eq.at(1)) { 46 | case 'q': 47 | t = mod::SQRT; 48 | subPos = 4; 49 | break; 50 | case 'i': 51 | t = mod::SIN; 52 | break; 53 | case 'o': 54 | t = mod::COS; 55 | break; 56 | case 'a': 57 | t = mod::TAN; 58 | break; 59 | } 60 | Modulator temp; 61 | temp.setM(t); 62 | modUnit = 63 | std::make_pair(temp, new Unit(eq.substr(subPos, eq.length() - subPos))); 64 | } else { 65 | // process the expression if it doesn't have a function 66 | int parenDepth = 0; 67 | std::string curr = ""; 68 | for (auto c : eq) { 69 | if (c == '(') { 70 | parenDepth++; 71 | } else if (c == ')') { 72 | parenDepth--; 73 | } 74 | if (parenDepth == 0) { 75 | if (curr != "") { 76 | Unit temp(curr); 77 | exp.push_back(temp); 78 | curr = ""; 79 | } 80 | if (isOperator(c)) { 81 | Operator o(c); 82 | exp.push_back(o); 83 | } 84 | } else { 85 | if (!(parenDepth == 1 && c == '(')) { 86 | curr += c; 87 | } 88 | } 89 | } 90 | } 91 | 92 | // detect if the expression has a variable 93 | // if it does have a variable, then we let it be 94 | // otherwise, we evaluate it so we don't have to re-evaluate it every time 95 | bool tempHV = false; 96 | for (auto c : eqStr) { 97 | if (c == 'x' || c == 'y') { 98 | tempHV = true; 99 | break; 100 | } 101 | } 102 | 103 | if (!tempHV) { 104 | hasVar = true; 105 | noVarValDeg = this->evalUnit(0, true); 106 | noVarValRad = this->evalUnit(0, false); 107 | } 108 | hasVar = tempHV; 109 | } 110 | 111 | Unit::~Unit() {} 112 | 113 | double Unit::evalUnit(double x, bool useDeg) { 114 | // if the expression does not have a variable, then it will be the same 115 | // every time and we can just return that value 116 | if (!hasVar) { 117 | return useDeg ? noVarValDeg : noVarValRad; 118 | } 119 | // std::cout << eqStr << std::endl; 120 | 121 | // evaluate expression if it has a function 122 | if (modExp) { 123 | 124 | double val = modUnit.second->evalUnit(x, useDeg); 125 | if (useDeg) { 126 | const double pi = 3.14159f; 127 | val = val * pi; 128 | val = val / 180.0f; 129 | } 130 | 131 | return modUnit.first.applyMod(val); 132 | } else { 133 | // evaluate expression if it does not have a function 134 | // return if its just x 135 | if (eqStr == "x") { 136 | return x; 137 | } 138 | // return if its just a number 139 | // regex matches anything that follows the patterns d.d, .d, d., and d where 140 | // d is any number of digits 0-9 141 | std::smatch match; 142 | std::regex isNum("^(\\d*)(.?)(\\d*)$"); 143 | if (std::regex_match(eqStr, match, isNum)) { 144 | return atof(match[0].str().c_str()); 145 | } 146 | // otherwise evaluate expression 147 | std::vector> opVec; 148 | for (auto i : exp) { 149 | if (std::holds_alternative(i)) { 150 | opVec.push_back(std::get(i).evalUnit(x, useDeg)); 151 | } else if (std::holds_alternative(i)) { 152 | opVec.push_back(std::get(i)); 153 | } else if (std::holds_alternative(i)) { 154 | opVec.push_back(std::get(i)); 155 | } 156 | } 157 | 158 | return evalOps(opVec); 159 | } 160 | return x; 161 | } 162 | 163 | // evaluate any operator expressions inside the unit 164 | double Unit::evalOps(std::vector> opVec) { 165 | // handle invalid expressions 166 | for (auto it = opVec.begin(); it < opVec.end(); it++) { 167 | if (std::holds_alternative(*it)) { 168 | if (it == opVec.end() - 1) { 169 | return 0; 170 | } 171 | if (!std::holds_alternative(*(it - 1))) { 172 | return 0; 173 | } 174 | if (!std::holds_alternative(*(it + 1))) { 175 | return 0; 176 | } 177 | } 178 | } 179 | // order of operations: exponents, multiplication and division, addition and 180 | // subtraction 181 | // 182 | // exponents: 183 | for (auto it = opVec.begin(); it < opVec.end(); it++) { 184 | if (std::holds_alternative(*it)) { 185 | Operator o = std::get(*it); 186 | if (o.getOp() == '^') { 187 | double lhs, rhs; 188 | lhs = std::get(*(it - 1)); 189 | rhs = std::get(*(it + 1)); 190 | auto er = (it - 1); 191 | opVec.erase(er); 192 | opVec.erase(er); 193 | opVec.erase(er); 194 | opVec.insert(er, o.evalOp(lhs, rhs)); 195 | } 196 | } 197 | } 198 | 199 | // multiplication and division: 200 | for (auto it = opVec.begin(); it < opVec.end(); it++) { 201 | if (std::holds_alternative(*it)) { 202 | Operator o = std::get(*it); 203 | if (o.getOp() == '*' || o.getOp() == '/') { 204 | double lhs, rhs; 205 | lhs = std::get(*(it - 1)); 206 | rhs = std::get(*(it + 1)); 207 | auto er = (it - 1); 208 | opVec.erase(er); 209 | opVec.erase(er); 210 | opVec.erase(er); 211 | opVec.insert(er, o.evalOp(lhs, rhs)); 212 | } 213 | } 214 | } 215 | 216 | // addition and subtraction: 217 | for (auto it = opVec.begin(); it < opVec.end(); it++) { 218 | if (std::holds_alternative(*it)) { 219 | Operator o = std::get(*it); 220 | if (o.getOp() == '+' || o.getOp() == '-') { 221 | double lhs, rhs; 222 | lhs = std::get(*(it - 1)); 223 | rhs = std::get(*(it + 1)); 224 | auto er = (it - 1); 225 | opVec.erase(er); 226 | opVec.erase(er); 227 | opVec.erase(er); 228 | opVec.insert(er, o.evalOp(lhs, rhs)); 229 | } 230 | } 231 | } 232 | 233 | // if there is only one number left in the expression, return that number 234 | if (opVec.size() == 1 && std::holds_alternative(*opVec.begin())) { 235 | return std::get(*opVec.begin()); 236 | } else { 237 | if (opVec.size() == 1) { 238 | std::cout << "lonely operator!" << std::endl; 239 | return 0; 240 | } 241 | // if there are still operators left after processing, re-process them until 242 | // there is one number left 243 | return evalOps(opVec); 244 | } 245 | } 246 | -------------------------------------------------------------------------------- /src/renderer/renderer.cpp: -------------------------------------------------------------------------------- 1 | #include "renderer.h" 2 | #include "imgui/imgui.h" 3 | 4 | #include 5 | 6 | Renderer::Renderer() { 7 | running = true; 8 | lineAct = false; 9 | 10 | eqUpdate = false; 11 | 12 | //fixes problem on Windows where SDL already defines a main function 13 | SDL_SetMainReady(); 14 | 15 | SDL_Init(SDL_INIT_VIDEO); 16 | 17 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); 18 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3); 19 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); 20 | SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8); 21 | 22 | gWindow = SDL_CreateWindow("glGraph", SDL_WINDOWPOS_CENTERED, 23 | SDL_WINDOWPOS_CENTERED, screenWidth, screenHeight, 24 | SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL); 25 | gContext = SDL_GL_CreateContext(gWindow); 26 | SDL_GL_MakeCurrent(gWindow, gContext); 27 | 28 | glewExperimental = true; 29 | glewInit(); 30 | 31 | // Imgui stuff 32 | IMGUI_CHECKVERSION(); 33 | ImGui::CreateContext(); 34 | ImGuiIO &io = ImGui::GetIO(); 35 | (void)io; 36 | 37 | ImGui::StyleColorsDark(); 38 | 39 | ImGui_ImplSDL2_InitForOpenGL(gWindow, gContext); 40 | ImGui_ImplOpenGL3_Init(); 41 | 42 | s = new Shader(); 43 | g = new Grid(); 44 | 45 | numLines = 1; 46 | eqBuf.push_back(new char[512]); 47 | for (int i = 0; i < 512; i++) { 48 | *(eqBuf.at(0) + i) = 0; 49 | } 50 | yOffset = 0.0f; 51 | xOffset = 0.0f; 52 | 53 | newColor[0] = 0; 54 | newColor[1] = 0; 55 | newColor[2] = 0; 56 | lInd = 0; 57 | currSel = -1; 58 | 59 | showMouse = false; 60 | 61 | glLineWidth(4.0f); 62 | } 63 | 64 | Renderer::~Renderer() { 65 | delete s; 66 | delete g; 67 | if (lineAct) { 68 | for (int i = 0; i < lines.size(); i++) { 69 | delete lines.at(i); 70 | } 71 | } 72 | for (int i = 0; i < eqBuf.size(); i++) { 73 | delete[] eqBuf.at(i); 74 | } 75 | ImGui_ImplOpenGL3_Shutdown(); 76 | ImGui_ImplSDL2_Shutdown(); 77 | ImGui::DestroyContext(); 78 | 79 | SDL_GL_DeleteContext(gContext); 80 | SDL_DestroyWindow(gWindow); 81 | SDL_Quit(); 82 | } 83 | 84 | void Renderer::clear() { 85 | glClearColor(0.75f, 0.75f, 0.75f, 1.0f); 86 | glClear(GL_COLOR_BUFFER_BIT); 87 | } 88 | 89 | void Renderer::update(double *xPos, double *scale, 90 | std::vector *equs, double *resolution, 91 | bool *scaleRes, bool *updateEq, bool *updatePos, 92 | bool *useDeg) { 93 | std::vector oldEqBuf; 94 | for (auto e : eqBuf) { 95 | oldEqBuf.push_back(std::string(e)); 96 | } 97 | 98 | s->useProgram(); 99 | g->renderGrid(s); 100 | if (lineAct) { 101 | for (auto l : lines) { 102 | if (l->isSelected()) { 103 | s->setUniform3f("color", 1.0f, 1.0f, 0.0f); 104 | glLineWidth(8.0f); 105 | l->draw(); 106 | glLineWidth(4.0f); 107 | } 108 | s->setUniform3f("color", l->getColorR(), l->getColorG(), l->getColorB()); 109 | l->draw(); 110 | } 111 | } 112 | SDL_GetMouseState(&mouseX, &mouseY); 113 | 114 | ImGui_ImplOpenGL3_NewFrame(); 115 | ImGui_ImplSDL2_NewFrame(gWindow); 116 | ImGui::NewFrame(); 117 | 118 | ImGui::Begin("Grapher"); 119 | ImGui::Text("Resolution: "); 120 | ImGui::SameLine(); 121 | ImGui::InputDouble("res", resolution); 122 | ImGui::Checkbox("Scale Resolution", scaleRes); 123 | ImGui::Checkbox("Use Degrees", useDeg); 124 | ImGui::Text("Enter equations: "); 125 | int lToR = -1; 126 | for (int i = 0; i < numLines; i++) { 127 | ImGui::InputTextWithHint(std::to_string(i).c_str(), "equation", eqBuf.at(i), 128 | 512); 129 | if (ImGui::Button(std::string("Remove " + std::to_string(i)).c_str())) { 130 | lToR = i; 131 | } 132 | } 133 | 134 | if (lToR != -1 && lToR < lines.size()) { 135 | numLines--; 136 | lines.erase(lines.begin() + lToR); 137 | eqBuf.erase(eqBuf.begin() + lToR); 138 | equs->erase(equs->begin() + lToR); 139 | *updateEq = true; 140 | *updatePos = true; 141 | } 142 | if (ImGui::Button("Add line")) { 143 | eqBuf.push_back(new char[512]); 144 | for (int i = 0; i < 512; i++) { 145 | *(eqBuf.at(eqBuf.size() - 1) + i) = 0; 146 | } 147 | equs->push_back(""); 148 | numLines++; 149 | *updateEq = true; 150 | *updatePos = true; 151 | } 152 | /*ImGui::SameLine(); 153 | if (ImGui::Button("Graph")) { 154 | for (int i = 0; i < eqBuf.size(); i++) { 155 | equs->at(i) = std::string(eqBuf.at(i)); 156 | } 157 | *updateEq = true; 158 | *updatePos = true; 159 | }*/ 160 | // on-the-fly equation handling 161 | for (int i = 0; i < eqBuf.size(); i++) { 162 | if (i >= oldEqBuf.size() || 163 | std::string(eqBuf.at(i)).compare(oldEqBuf.at(i))) { 164 | equs->at(i) = std::string(eqBuf.at(i)); 165 | *updateEq = true; 166 | *updatePos = true; 167 | } 168 | } 169 | ImGui::Text("Line index:"); 170 | ImGui::SameLine(); 171 | ImGui::InputInt("", &lInd); 172 | if (lInd < 0) 173 | lInd = lines.size() - 1; 174 | if (lInd >= lines.size()) 175 | lInd = 0; 176 | if (lines.size() > 0) { 177 | if (currSel != -1) 178 | lines.at(currSel)->setSelected(false); 179 | currSel = lInd; 180 | lines.at(currSel)->setSelected(true); 181 | } 182 | ImGui::Text("Input line color:"); 183 | ImGui::InputFloat3("rgb", newColor); 184 | if (ImGui::Button("Update line color")) { 185 | lines.at(lInd)->setColor(newColor[0], newColor[1], newColor[2]); 186 | } 187 | ImGui::Checkbox("Show stats", &showMouse); 188 | if (showMouse) { 189 | ImGui::Text("Camera Y: %f", -1.0f * yOffset); 190 | ImGui::Text("Camera X: %f", -1.0f * xOffset); 191 | ImGui::Text("Scale: %f", *scale); 192 | 193 | ImGui::Text("Mouse: (%f, %f)", 194 | double(mouseX / double(screenWidth / 2.0f) - 1.0f) - xOffset, 195 | double(-1.0f * (mouseY) / double(screenHeight / 2.0f) + 1.0f) - 196 | yOffset); 197 | } 198 | ImGui::End(); 199 | 200 | ImGui::Render(); 201 | ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); 202 | SDL_GL_SwapWindow(gWindow); 203 | 204 | SDL_Event e; 205 | while (SDL_PollEvent(&e)) { 206 | ImGui_ImplSDL2_ProcessEvent(&e); 207 | if (e.type == SDL_QUIT) { 208 | running = false; 209 | } else if (e.type == SDL_KEYDOWN) { 210 | if (e.key.keysym.scancode == SDL_SCANCODE_ESCAPE) { 211 | running = false; 212 | } else { 213 | double mSpeed = 0.05f * (*scale); 214 | switch (e.key.keysym.scancode) { 215 | case SDL_SCANCODE_W: 216 | *updatePos = true; 217 | yOffset -= mSpeed; 218 | break; 219 | case SDL_SCANCODE_S: 220 | *updatePos = true; 221 | yOffset += mSpeed; 222 | break; 223 | case SDL_SCANCODE_A: 224 | *updatePos = true; 225 | *xPos -= mSpeed; 226 | xOffset += mSpeed; 227 | break; 228 | case SDL_SCANCODE_D: 229 | *updatePos = true; 230 | *xPos += mSpeed; 231 | xOffset -= mSpeed; 232 | break; 233 | case SDL_SCANCODE_Q: 234 | *updatePos = true; 235 | *scale *= 1.1f; 236 | break; 237 | case SDL_SCANCODE_E: 238 | *updatePos = true; 239 | *scale /= 1.1f; 240 | break; 241 | default: 242 | break; 243 | } 244 | } 245 | } 246 | } 247 | if (*scale <= 0.0f) { 248 | *scale = 0.1f; 249 | } 250 | if (*updatePos) { 251 | g->updateGrid(xOffset, yOffset, *scale); 252 | } 253 | } 254 | 255 | void Renderer::graphPoint(double x, double y) {} 256 | 257 | void Renderer::graphLine(std::map points, int index, 258 | double scale) { 259 | // make sure line color and selection status persists between line updates 260 | bool select = false; 261 | float color[3] = {1.0f, 0.0f, 0.0f}; 262 | if (lineAct && index < lines.size()) { 263 | select = lines.at(index)->isSelected(); 264 | color[0] = lines.at(index)->getColorR(); 265 | color[1] = lines.at(index)->getColorG(); 266 | color[2] = lines.at(index)->getColorB(); 267 | delete lines.at(index); 268 | } 269 | lineAct = true; 270 | 271 | // add the vertices that make up the line 272 | std::vector verts; 273 | 274 | // int numPoints = points.size(); 275 | // double step = 2.0f / (numPoints - 2); 276 | 277 | // int i = 0; 278 | for (auto [x, y] : points) { 279 | verts.push_back((x + xOffset) / scale); 280 | // verts.push_back(i * step - 1.0); 281 | verts.push_back(y / scale + yOffset); 282 | verts.push_back(0.0f); 283 | // i++; 284 | } 285 | 286 | // either update the line or create a new one 287 | if (index < lines.size()) { 288 | lines.at(index) = new Line(verts); 289 | lines.at(index)->setSelected(select); 290 | lines.at(index)->setColor(color[0], color[1], color[2]); 291 | } else { 292 | lines.push_back(new Line(verts)); 293 | lines.at(lines.size() - 1)->setColor(color[0], color[1], color[2]); 294 | } 295 | } 296 | -------------------------------------------------------------------------------- /src/renderer/imgui/imconfig.h: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // COMPILE-TIME OPTIONS FOR DEAR IMGUI 3 | // Runtime options (clipboard callbacks, enabling various features, etc.) can generally be set via the ImGuiIO structure. 4 | // You can use ImGui::SetAllocatorFunctions() before calling ImGui::CreateContext() to rewire memory allocation functions. 5 | //----------------------------------------------------------------------------- 6 | // A) You may edit imconfig.h (and not overwrite it when updating Dear ImGui, or maintain a patch/rebased branch with your modifications to it) 7 | // B) or '#define IMGUI_USER_CONFIG "my_imgui_config.h"' in your project and then add directives in your own file without touching this template. 8 | //----------------------------------------------------------------------------- 9 | // You need to make sure that configuration settings are defined consistently _everywhere_ Dear ImGui is used, which include the imgui*.cpp 10 | // files but also _any_ of your code that uses Dear ImGui. This is because some compile-time options have an affect on data structures. 11 | // Defining those options in imconfig.h will ensure every compilation unit gets to see the same data structure layouts. 12 | // Call IMGUI_CHECKVERSION() from your .cpp files to verify that the data structures your files are using are matching the ones imgui.cpp is using. 13 | //----------------------------------------------------------------------------- 14 | 15 | #pragma once 16 | 17 | //---- Define assertion handler. Defaults to calling assert(). 18 | // If your macro uses multiple statements, make sure is enclosed in a 'do { .. } while (0)' block so it can be used as a single statement. 19 | //#define IM_ASSERT(_EXPR) MyAssert(_EXPR) 20 | //#define IM_ASSERT(_EXPR) ((void)(_EXPR)) // Disable asserts 21 | 22 | //---- Define attributes of all API symbols declarations, e.g. for DLL under Windows 23 | // Using Dear ImGui via a shared library is not recommended, because of function call overhead and because we don't guarantee backward nor forward ABI compatibility. 24 | // DLL users: heaps and globals are not shared across DLL boundaries! You will need to call SetCurrentContext() + SetAllocatorFunctions() 25 | // for each static/DLL boundary you are calling from. Read "Context and Memory Allocators" section of imgui.cpp for more details. 26 | //#define IMGUI_API __declspec( dllexport ) 27 | //#define IMGUI_API __declspec( dllimport ) 28 | 29 | //---- Don't define obsolete functions/enums/behaviors. Consider enabling from time to time after updating to avoid using soon-to-be obsolete function/names. 30 | //#define IMGUI_DISABLE_OBSOLETE_FUNCTIONS 31 | 32 | //---- Disable all of Dear ImGui or don't implement standard windows. 33 | // It is very strongly recommended to NOT disable the demo windows during development. Please read comments in imgui_demo.cpp. 34 | //#define IMGUI_DISABLE // Disable everything: all headers and source files will be empty. 35 | //#define IMGUI_DISABLE_DEMO_WINDOWS // Disable demo windows: ShowDemoWindow()/ShowStyleEditor() will be empty. Not recommended. 36 | //#define IMGUI_DISABLE_METRICS_WINDOW // Disable metrics/debugger window: ShowMetricsWindow() will be empty. 37 | 38 | //---- Don't implement some functions to reduce linkage requirements. 39 | //#define IMGUI_DISABLE_WIN32_DEFAULT_CLIPBOARD_FUNCTIONS // [Win32] Don't implement default clipboard handler. Won't use and link with OpenClipboard/GetClipboardData/CloseClipboard etc. (user32.lib/.a, kernel32.lib/.a) 40 | //#define IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCTIONS // [Win32] Don't implement default IME handler. Won't use and link with ImmGetContext/ImmSetCompositionWindow. (imm32.lib/.a) 41 | //#define IMGUI_DISABLE_WIN32_FUNCTIONS // [Win32] Won't use and link with any Win32 function (clipboard, ime). 42 | //#define IMGUI_ENABLE_OSX_DEFAULT_CLIPBOARD_FUNCTIONS // [OSX] Implement default OSX clipboard handler (need to link with '-framework ApplicationServices', this is why this is not the default). 43 | //#define IMGUI_DISABLE_DEFAULT_FORMAT_FUNCTIONS // Don't implement ImFormatString/ImFormatStringV so you can implement them yourself (e.g. if you don't want to link with vsnprintf) 44 | //#define IMGUI_DISABLE_DEFAULT_MATH_FUNCTIONS // Don't implement ImFabs/ImSqrt/ImPow/ImFmod/ImCos/ImSin/ImAcos/ImAtan2 so you can implement them yourself. 45 | //#define IMGUI_DISABLE_FILE_FUNCTIONS // Don't implement ImFileOpen/ImFileClose/ImFileRead/ImFileWrite and ImFileHandle at all (replace them with dummies) 46 | //#define IMGUI_DISABLE_DEFAULT_FILE_FUNCTIONS // Don't implement ImFileOpen/ImFileClose/ImFileRead/ImFileWrite and ImFileHandle so you can implement them yourself if you don't want to link with fopen/fclose/fread/fwrite. This will also disable the LogToTTY() function. 47 | //#define IMGUI_DISABLE_DEFAULT_ALLOCATORS // Don't implement default allocators calling malloc()/free() to avoid linking with them. You will need to call ImGui::SetAllocatorFunctions(). 48 | 49 | //---- Include imgui_user.h at the end of imgui.h as a convenience 50 | //#define IMGUI_INCLUDE_IMGUI_USER_H 51 | 52 | //---- Pack colors to BGRA8 instead of RGBA8 (to avoid converting from one to another) 53 | //#define IMGUI_USE_BGRA_PACKED_COLOR 54 | 55 | //---- Use 32-bit for ImWchar (default is 16-bit) to support unicode planes 1-16. (e.g. point beyond 0xFFFF like emoticons, dingbats, symbols, shapes, ancient languages, etc...) 56 | //#define IMGUI_USE_WCHAR32 57 | 58 | //---- Avoid multiple STB libraries implementations, or redefine path/filenames to prioritize another version 59 | // By default the embedded implementations are declared static and not available outside of Dear ImGui sources files. 60 | //#define IMGUI_STB_TRUETYPE_FILENAME "my_folder/stb_truetype.h" 61 | //#define IMGUI_STB_RECT_PACK_FILENAME "my_folder/stb_rect_pack.h" 62 | //#define IMGUI_DISABLE_STB_TRUETYPE_IMPLEMENTATION 63 | //#define IMGUI_DISABLE_STB_RECT_PACK_IMPLEMENTATION 64 | 65 | //---- Use stb_printf's faster implementation of vsnprintf instead of the one from libc (unless IMGUI_DISABLE_DEFAULT_FORMAT_FUNCTIONS is defined) 66 | // Requires 'stb_sprintf.h' to be available in the include path. Compatibility checks of arguments and formats done by clang and GCC will be disabled in order to support the extra formats provided by STB sprintf. 67 | // #define IMGUI_USE_STB_SPRINTF 68 | 69 | //---- Use FreeType to build and rasterize the font atlas (instead of stb_truetype which is embedded by default in Dear ImGui) 70 | // Requires FreeType headers to be available in the include path. Requires program to be compiled with 'misc/freetype/imgui_freetype.cpp' (in this repository) + the FreeType library (not provided). 71 | // On Windows you may use vcpkg with 'vcpkg install freetype' + 'vcpkg integrate install'. 72 | //#define IMGUI_ENABLE_FREETYPE 73 | 74 | //---- Use stb_truetype to build and rasterize the font atlas (default) 75 | // The only purpose of this define is if you want force compilation of the stb_truetype backend ALONG with the FreeType backend. 76 | //#define IMGUI_ENABLE_STB_TRUETYPE 77 | 78 | //---- Define constructor and implicit cast operators to convert back<>forth between your math types and ImVec2/ImVec4. 79 | // This will be inlined as part of ImVec2 and ImVec4 class declarations. 80 | /* 81 | #define IM_VEC2_CLASS_EXTRA \ 82 | ImVec2(const MyVec2& f) { x = f.x; y = f.y; } \ 83 | operator MyVec2() const { return MyVec2(x,y); } 84 | 85 | #define IM_VEC4_CLASS_EXTRA \ 86 | ImVec4(const MyVec4& f) { x = f.x; y = f.y; z = f.z; w = f.w; } \ 87 | operator MyVec4() const { return MyVec4(x,y,z,w); } 88 | */ 89 | 90 | //---- Use 32-bit vertex indices (default is 16-bit) is one way to allow large meshes with more than 64K vertices. 91 | // Your renderer backend will need to support it (most example renderer backends support both 16/32-bit indices). 92 | // Another way to allow large meshes while keeping 16-bit indices is to handle ImDrawCmd::VtxOffset in your renderer. 93 | // Read about ImGuiBackendFlags_RendererHasVtxOffset for details. 94 | //#define ImDrawIdx unsigned int 95 | 96 | //---- Override ImDrawCallback signature (will need to modify renderer backends accordingly) 97 | //struct ImDrawList; 98 | //struct ImDrawCmd; 99 | //typedef void (*MyImDrawCallback)(const ImDrawList* draw_list, const ImDrawCmd* cmd, void* my_renderer_user_data); 100 | //#define ImDrawCallback MyImDrawCallback 101 | 102 | //---- Debug Tools: Macro to break in Debugger 103 | // (use 'Metrics->Tools->Item Picker' to pick widgets with the mouse and break into them for easy debugging.) 104 | //#define IM_DEBUG_BREAK IM_ASSERT(0) 105 | //#define IM_DEBUG_BREAK __debugbreak() 106 | 107 | //---- Debug Tools: Have the Item Picker break in the ItemAdd() function instead of ItemHoverable(), 108 | // (which comes earlier in the code, will catch a few extra items, allow picking items other than Hovered one.) 109 | // This adds a small runtime cost which is why it is not enabled by default. 110 | //#define IMGUI_DEBUG_TOOL_ITEM_PICKER_EX 111 | 112 | //---- Debug Tools: Enable slower asserts 113 | //#define IMGUI_DEBUG_PARANOID 114 | 115 | //---- Tip: You can add extra functions within the ImGui:: namespace, here or in your own headers files. 116 | /* 117 | namespace ImGui 118 | { 119 | void MyFunction(const char* name, const MyMatrix44& v); 120 | } 121 | */ 122 | -------------------------------------------------------------------------------- /src/renderer/imgui/imgui_impl_sdl.cpp: -------------------------------------------------------------------------------- 1 | // dear imgui: Platform Backend for SDL2 2 | // This needs to be used along with a Renderer (e.g. DirectX11, OpenGL3, Vulkan..) 3 | // (Info: SDL2 is a cross-platform general purpose library for handling windows, inputs, graphics context creation, etc.) 4 | // (Requires: SDL 2.0. Prefer SDL 2.0.4+ for full feature support.) 5 | 6 | // Implemented features: 7 | // [X] Platform: Mouse cursor shape and visibility. Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'. 8 | // [X] Platform: Clipboard support. 9 | // [X] Platform: Keyboard arrays indexed using SDL_SCANCODE_* codes, e.g. ImGui::IsKeyPressed(SDL_SCANCODE_SPACE). 10 | // [X] Platform: Gamepad support. Enabled with 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad'. 11 | // Missing features: 12 | // [ ] Platform: SDL2 handling of IME under Windows appears to be broken and it explicitly disable the regular Windows IME. You can restore Windows IME by compiling SDL with SDL_DISABLE_WINDOWS_IME. 13 | 14 | // You can copy and use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this. 15 | // If you are new to Dear ImGui, read documentation from the docs/ folder + read the top of imgui.cpp. 16 | // Read online: https://github.com/ocornut/imgui/tree/master/docs 17 | 18 | // CHANGELOG 19 | // (minor and older changes stripped away, please see git history for details) 20 | // 2021-03-22: Rework global mouse pos availability check listing supported platforms explicitly, effectively fixing mouse access on Raspberry Pi. (#2837, #3950) 21 | // 2020-05-25: Misc: Report a zero display-size when window is minimized, to be consistent with other backends. 22 | // 2020-02-20: Inputs: Fixed mapping for ImGuiKey_KeyPadEnter (using SDL_SCANCODE_KP_ENTER instead of SDL_SCANCODE_RETURN2). 23 | // 2019-12-17: Inputs: On Wayland, use SDL_GetMouseState (because there is no global mouse state). 24 | // 2019-12-05: Inputs: Added support for ImGuiMouseCursor_NotAllowed mouse cursor. 25 | // 2019-07-21: Inputs: Added mapping for ImGuiKey_KeyPadEnter. 26 | // 2019-04-23: Inputs: Added support for SDL_GameController (if ImGuiConfigFlags_NavEnableGamepad is set by user application). 27 | // 2019-03-12: Misc: Preserve DisplayFramebufferScale when main window is minimized. 28 | // 2018-12-21: Inputs: Workaround for Android/iOS which don't seem to handle focus related calls. 29 | // 2018-11-30: Misc: Setting up io.BackendPlatformName so it can be displayed in the About Window. 30 | // 2018-11-14: Changed the signature of ImGui_ImplSDL2_ProcessEvent() to take a 'const SDL_Event*'. 31 | // 2018-08-01: Inputs: Workaround for Emscripten which doesn't seem to handle focus related calls. 32 | // 2018-06-29: Inputs: Added support for the ImGuiMouseCursor_Hand cursor. 33 | // 2018-06-08: Misc: Extracted imgui_impl_sdl.cpp/.h away from the old combined SDL2+OpenGL/Vulkan examples. 34 | // 2018-06-08: Misc: ImGui_ImplSDL2_InitForOpenGL() now takes a SDL_GLContext parameter. 35 | // 2018-05-09: Misc: Fixed clipboard paste memory leak (we didn't call SDL_FreeMemory on the data returned by SDL_GetClipboardText). 36 | // 2018-03-20: Misc: Setup io.BackendFlags ImGuiBackendFlags_HasMouseCursors flag + honor ImGuiConfigFlags_NoMouseCursorChange flag. 37 | // 2018-02-16: Inputs: Added support for mouse cursors, honoring ImGui::GetMouseCursor() value. 38 | // 2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves. 39 | // 2018-02-06: Inputs: Added mapping for ImGuiKey_Space. 40 | // 2018-02-05: Misc: Using SDL_GetPerformanceCounter() instead of SDL_GetTicks() to be able to handle very high framerate (1000+ FPS). 41 | // 2018-02-05: Inputs: Keyboard mapping is using scancodes everywhere instead of a confusing mixture of keycodes and scancodes. 42 | // 2018-01-20: Inputs: Added Horizontal Mouse Wheel support. 43 | // 2018-01-19: Inputs: When available (SDL 2.0.4+) using SDL_CaptureMouse() to retrieve coordinates outside of client area when dragging. Otherwise (SDL 2.0.3 and before) testing for SDL_WINDOW_INPUT_FOCUS instead of SDL_WINDOW_MOUSE_FOCUS. 44 | // 2018-01-18: Inputs: Added mapping for ImGuiKey_Insert. 45 | // 2017-08-25: Inputs: MousePos set to -FLT_MAX,-FLT_MAX when mouse is unavailable/missing (instead of -1,-1). 46 | // 2016-10-15: Misc: Added a void* user_data parameter to Clipboard function handlers. 47 | 48 | #include "imgui.h" 49 | #include "imgui_impl_sdl.h" 50 | 51 | // SDL 52 | #include 53 | #include 54 | #if defined(__APPLE__) 55 | #include "TargetConditionals.h" 56 | #endif 57 | 58 | #define SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE SDL_VERSION_ATLEAST(2,0,4) 59 | #define SDL_HAS_VULKAN SDL_VERSION_ATLEAST(2,0,6) 60 | 61 | // Data 62 | static SDL_Window* g_Window = NULL; 63 | static Uint64 g_Time = 0; 64 | static bool g_MousePressed[3] = { false, false, false }; 65 | static SDL_Cursor* g_MouseCursors[ImGuiMouseCursor_COUNT] = {}; 66 | static char* g_ClipboardTextData = NULL; 67 | static bool g_MouseCanUseGlobalState = true; 68 | 69 | static const char* ImGui_ImplSDL2_GetClipboardText(void*) 70 | { 71 | if (g_ClipboardTextData) 72 | SDL_free(g_ClipboardTextData); 73 | g_ClipboardTextData = SDL_GetClipboardText(); 74 | return g_ClipboardTextData; 75 | } 76 | 77 | static void ImGui_ImplSDL2_SetClipboardText(void*, const char* text) 78 | { 79 | SDL_SetClipboardText(text); 80 | } 81 | 82 | // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs. 83 | // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application. 84 | // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application. 85 | // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags. 86 | // If you have multiple SDL events and some of them are not meant to be used by dear imgui, you may need to filter events based on their windowID field. 87 | bool ImGui_ImplSDL2_ProcessEvent(const SDL_Event* event) 88 | { 89 | ImGuiIO& io = ImGui::GetIO(); 90 | switch (event->type) 91 | { 92 | case SDL_MOUSEWHEEL: 93 | { 94 | if (event->wheel.x > 0) io.MouseWheelH += 1; 95 | if (event->wheel.x < 0) io.MouseWheelH -= 1; 96 | if (event->wheel.y > 0) io.MouseWheel += 1; 97 | if (event->wheel.y < 0) io.MouseWheel -= 1; 98 | return true; 99 | } 100 | case SDL_MOUSEBUTTONDOWN: 101 | { 102 | if (event->button.button == SDL_BUTTON_LEFT) g_MousePressed[0] = true; 103 | if (event->button.button == SDL_BUTTON_RIGHT) g_MousePressed[1] = true; 104 | if (event->button.button == SDL_BUTTON_MIDDLE) g_MousePressed[2] = true; 105 | return true; 106 | } 107 | case SDL_TEXTINPUT: 108 | { 109 | io.AddInputCharactersUTF8(event->text.text); 110 | return true; 111 | } 112 | case SDL_KEYDOWN: 113 | case SDL_KEYUP: 114 | { 115 | int key = event->key.keysym.scancode; 116 | IM_ASSERT(key >= 0 && key < IM_ARRAYSIZE(io.KeysDown)); 117 | io.KeysDown[key] = (event->type == SDL_KEYDOWN); 118 | io.KeyShift = ((SDL_GetModState() & KMOD_SHIFT) != 0); 119 | io.KeyCtrl = ((SDL_GetModState() & KMOD_CTRL) != 0); 120 | io.KeyAlt = ((SDL_GetModState() & KMOD_ALT) != 0); 121 | #ifdef _WIN32 122 | io.KeySuper = false; 123 | #else 124 | io.KeySuper = ((SDL_GetModState() & KMOD_GUI) != 0); 125 | #endif 126 | return true; 127 | } 128 | } 129 | return false; 130 | } 131 | 132 | static bool ImGui_ImplSDL2_Init(SDL_Window* window) 133 | { 134 | g_Window = window; 135 | 136 | // Setup backend capabilities flags 137 | ImGuiIO& io = ImGui::GetIO(); 138 | io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional) 139 | io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos; // We can honor io.WantSetMousePos requests (optional, rarely used) 140 | io.BackendPlatformName = "imgui_impl_sdl"; 141 | 142 | // Keyboard mapping. Dear ImGui will use those indices to peek into the io.KeysDown[] array. 143 | io.KeyMap[ImGuiKey_Tab] = SDL_SCANCODE_TAB; 144 | io.KeyMap[ImGuiKey_LeftArrow] = SDL_SCANCODE_LEFT; 145 | io.KeyMap[ImGuiKey_RightArrow] = SDL_SCANCODE_RIGHT; 146 | io.KeyMap[ImGuiKey_UpArrow] = SDL_SCANCODE_UP; 147 | io.KeyMap[ImGuiKey_DownArrow] = SDL_SCANCODE_DOWN; 148 | io.KeyMap[ImGuiKey_PageUp] = SDL_SCANCODE_PAGEUP; 149 | io.KeyMap[ImGuiKey_PageDown] = SDL_SCANCODE_PAGEDOWN; 150 | io.KeyMap[ImGuiKey_Home] = SDL_SCANCODE_HOME; 151 | io.KeyMap[ImGuiKey_End] = SDL_SCANCODE_END; 152 | io.KeyMap[ImGuiKey_Insert] = SDL_SCANCODE_INSERT; 153 | io.KeyMap[ImGuiKey_Delete] = SDL_SCANCODE_DELETE; 154 | io.KeyMap[ImGuiKey_Backspace] = SDL_SCANCODE_BACKSPACE; 155 | io.KeyMap[ImGuiKey_Space] = SDL_SCANCODE_SPACE; 156 | io.KeyMap[ImGuiKey_Enter] = SDL_SCANCODE_RETURN; 157 | io.KeyMap[ImGuiKey_Escape] = SDL_SCANCODE_ESCAPE; 158 | io.KeyMap[ImGuiKey_KeyPadEnter] = SDL_SCANCODE_KP_ENTER; 159 | io.KeyMap[ImGuiKey_A] = SDL_SCANCODE_A; 160 | io.KeyMap[ImGuiKey_C] = SDL_SCANCODE_C; 161 | io.KeyMap[ImGuiKey_V] = SDL_SCANCODE_V; 162 | io.KeyMap[ImGuiKey_X] = SDL_SCANCODE_X; 163 | io.KeyMap[ImGuiKey_Y] = SDL_SCANCODE_Y; 164 | io.KeyMap[ImGuiKey_Z] = SDL_SCANCODE_Z; 165 | 166 | io.SetClipboardTextFn = ImGui_ImplSDL2_SetClipboardText; 167 | io.GetClipboardTextFn = ImGui_ImplSDL2_GetClipboardText; 168 | io.ClipboardUserData = NULL; 169 | 170 | // Load mouse cursors 171 | g_MouseCursors[ImGuiMouseCursor_Arrow] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW); 172 | g_MouseCursors[ImGuiMouseCursor_TextInput] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_IBEAM); 173 | g_MouseCursors[ImGuiMouseCursor_ResizeAll] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZEALL); 174 | g_MouseCursors[ImGuiMouseCursor_ResizeNS] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENS); 175 | g_MouseCursors[ImGuiMouseCursor_ResizeEW] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZEWE); 176 | g_MouseCursors[ImGuiMouseCursor_ResizeNESW] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENESW); 177 | g_MouseCursors[ImGuiMouseCursor_ResizeNWSE] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENWSE); 178 | g_MouseCursors[ImGuiMouseCursor_Hand] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_HAND); 179 | g_MouseCursors[ImGuiMouseCursor_NotAllowed] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_NO); 180 | 181 | // Check and store if we are on a SDL backend that supports global mouse position 182 | // ("wayland" and "rpi" don't support it, but we chose to use a white-list instead of a black-list) 183 | const char* sdl_backend = SDL_GetCurrentVideoDriver(); 184 | const char* global_mouse_whitelist[] = { "windows", "cocoa", "x11", "DIVE", "VMAN" }; 185 | g_MouseCanUseGlobalState = false; 186 | for (int n = 0; n < IM_ARRAYSIZE(global_mouse_whitelist); n++) 187 | if (strncmp(sdl_backend, global_mouse_whitelist[n], strlen(global_mouse_whitelist[n])) == 0) 188 | g_MouseCanUseGlobalState = true; 189 | 190 | #ifdef _WIN32 191 | SDL_SysWMinfo wmInfo; 192 | SDL_VERSION(&wmInfo.version); 193 | SDL_GetWindowWMInfo(window, &wmInfo); 194 | io.ImeWindowHandle = wmInfo.info.win.window; 195 | #else 196 | (void)window; 197 | #endif 198 | 199 | return true; 200 | } 201 | 202 | bool ImGui_ImplSDL2_InitForOpenGL(SDL_Window* window, void* sdl_gl_context) 203 | { 204 | (void)sdl_gl_context; // Viewport branch will need this. 205 | return ImGui_ImplSDL2_Init(window); 206 | } 207 | 208 | bool ImGui_ImplSDL2_InitForVulkan(SDL_Window* window) 209 | { 210 | #if !SDL_HAS_VULKAN 211 | IM_ASSERT(0 && "Unsupported"); 212 | #endif 213 | return ImGui_ImplSDL2_Init(window); 214 | } 215 | 216 | bool ImGui_ImplSDL2_InitForD3D(SDL_Window* window) 217 | { 218 | #if !defined(_WIN32) 219 | IM_ASSERT(0 && "Unsupported"); 220 | #endif 221 | return ImGui_ImplSDL2_Init(window); 222 | } 223 | 224 | bool ImGui_ImplSDL2_InitForMetal(SDL_Window* window) 225 | { 226 | return ImGui_ImplSDL2_Init(window); 227 | } 228 | 229 | void ImGui_ImplSDL2_Shutdown() 230 | { 231 | g_Window = NULL; 232 | 233 | // Destroy last known clipboard data 234 | if (g_ClipboardTextData) 235 | SDL_free(g_ClipboardTextData); 236 | g_ClipboardTextData = NULL; 237 | 238 | // Destroy SDL mouse cursors 239 | for (ImGuiMouseCursor cursor_n = 0; cursor_n < ImGuiMouseCursor_COUNT; cursor_n++) 240 | SDL_FreeCursor(g_MouseCursors[cursor_n]); 241 | memset(g_MouseCursors, 0, sizeof(g_MouseCursors)); 242 | } 243 | 244 | static void ImGui_ImplSDL2_UpdateMousePosAndButtons() 245 | { 246 | ImGuiIO& io = ImGui::GetIO(); 247 | 248 | // Set OS mouse position if requested (rarely used, only when ImGuiConfigFlags_NavEnableSetMousePos is enabled by user) 249 | if (io.WantSetMousePos) 250 | SDL_WarpMouseInWindow(g_Window, (int)io.MousePos.x, (int)io.MousePos.y); 251 | else 252 | io.MousePos = ImVec2(-FLT_MAX, -FLT_MAX); 253 | 254 | int mx, my; 255 | Uint32 mouse_buttons = SDL_GetMouseState(&mx, &my); 256 | io.MouseDown[0] = g_MousePressed[0] || (mouse_buttons & SDL_BUTTON(SDL_BUTTON_LEFT)) != 0; // If a mouse press event came, always pass it as "mouse held this frame", so we don't miss click-release events that are shorter than 1 frame. 257 | io.MouseDown[1] = g_MousePressed[1] || (mouse_buttons & SDL_BUTTON(SDL_BUTTON_RIGHT)) != 0; 258 | io.MouseDown[2] = g_MousePressed[2] || (mouse_buttons & SDL_BUTTON(SDL_BUTTON_MIDDLE)) != 0; 259 | g_MousePressed[0] = g_MousePressed[1] = g_MousePressed[2] = false; 260 | 261 | #if SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE && !defined(__EMSCRIPTEN__) && !defined(__ANDROID__) && !(defined(__APPLE__) && TARGET_OS_IOS) 262 | SDL_Window* focused_window = SDL_GetKeyboardFocus(); 263 | if (g_Window == focused_window) 264 | { 265 | if (g_MouseCanUseGlobalState) 266 | { 267 | // SDL_GetMouseState() gives mouse position seemingly based on the last window entered/focused(?) 268 | // The creation of a new windows at runtime and SDL_CaptureMouse both seems to severely mess up with that, so we retrieve that position globally. 269 | // Won't use this workaround on SDL backends that have no global mouse position, like Wayland or RPI 270 | int wx, wy; 271 | SDL_GetWindowPosition(focused_window, &wx, &wy); 272 | SDL_GetGlobalMouseState(&mx, &my); 273 | mx -= wx; 274 | my -= wy; 275 | } 276 | io.MousePos = ImVec2((float)mx, (float)my); 277 | } 278 | 279 | // SDL_CaptureMouse() let the OS know e.g. that our imgui drag outside the SDL window boundaries shouldn't e.g. trigger the OS window resize cursor. 280 | // The function is only supported from SDL 2.0.4 (released Jan 2016) 281 | bool any_mouse_button_down = ImGui::IsAnyMouseDown(); 282 | SDL_CaptureMouse(any_mouse_button_down ? SDL_TRUE : SDL_FALSE); 283 | #else 284 | if (SDL_GetWindowFlags(g_Window) & SDL_WINDOW_INPUT_FOCUS) 285 | io.MousePos = ImVec2((float)mx, (float)my); 286 | #endif 287 | } 288 | 289 | static void ImGui_ImplSDL2_UpdateMouseCursor() 290 | { 291 | ImGuiIO& io = ImGui::GetIO(); 292 | if (io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange) 293 | return; 294 | 295 | ImGuiMouseCursor imgui_cursor = ImGui::GetMouseCursor(); 296 | if (io.MouseDrawCursor || imgui_cursor == ImGuiMouseCursor_None) 297 | { 298 | // Hide OS mouse cursor if imgui is drawing it or if it wants no cursor 299 | SDL_ShowCursor(SDL_FALSE); 300 | } 301 | else 302 | { 303 | // Show OS mouse cursor 304 | SDL_SetCursor(g_MouseCursors[imgui_cursor] ? g_MouseCursors[imgui_cursor] : g_MouseCursors[ImGuiMouseCursor_Arrow]); 305 | SDL_ShowCursor(SDL_TRUE); 306 | } 307 | } 308 | 309 | static void ImGui_ImplSDL2_UpdateGamepads() 310 | { 311 | ImGuiIO& io = ImGui::GetIO(); 312 | memset(io.NavInputs, 0, sizeof(io.NavInputs)); 313 | if ((io.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) == 0) 314 | return; 315 | 316 | // Get gamepad 317 | SDL_GameController* game_controller = SDL_GameControllerOpen(0); 318 | if (!game_controller) 319 | { 320 | io.BackendFlags &= ~ImGuiBackendFlags_HasGamepad; 321 | return; 322 | } 323 | 324 | // Update gamepad inputs 325 | #define MAP_BUTTON(NAV_NO, BUTTON_NO) { io.NavInputs[NAV_NO] = (SDL_GameControllerGetButton(game_controller, BUTTON_NO) != 0) ? 1.0f : 0.0f; } 326 | #define MAP_ANALOG(NAV_NO, AXIS_NO, V0, V1) { float vn = (float)(SDL_GameControllerGetAxis(game_controller, AXIS_NO) - V0) / (float)(V1 - V0); if (vn > 1.0f) vn = 1.0f; if (vn > 0.0f && io.NavInputs[NAV_NO] < vn) io.NavInputs[NAV_NO] = vn; } 327 | const int thumb_dead_zone = 8000; // SDL_gamecontroller.h suggests using this value. 328 | MAP_BUTTON(ImGuiNavInput_Activate, SDL_CONTROLLER_BUTTON_A); // Cross / A 329 | MAP_BUTTON(ImGuiNavInput_Cancel, SDL_CONTROLLER_BUTTON_B); // Circle / B 330 | MAP_BUTTON(ImGuiNavInput_Menu, SDL_CONTROLLER_BUTTON_X); // Square / X 331 | MAP_BUTTON(ImGuiNavInput_Input, SDL_CONTROLLER_BUTTON_Y); // Triangle / Y 332 | MAP_BUTTON(ImGuiNavInput_DpadLeft, SDL_CONTROLLER_BUTTON_DPAD_LEFT); // D-Pad Left 333 | MAP_BUTTON(ImGuiNavInput_DpadRight, SDL_CONTROLLER_BUTTON_DPAD_RIGHT); // D-Pad Right 334 | MAP_BUTTON(ImGuiNavInput_DpadUp, SDL_CONTROLLER_BUTTON_DPAD_UP); // D-Pad Up 335 | MAP_BUTTON(ImGuiNavInput_DpadDown, SDL_CONTROLLER_BUTTON_DPAD_DOWN); // D-Pad Down 336 | MAP_BUTTON(ImGuiNavInput_FocusPrev, SDL_CONTROLLER_BUTTON_LEFTSHOULDER); // L1 / LB 337 | MAP_BUTTON(ImGuiNavInput_FocusNext, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER); // R1 / RB 338 | MAP_BUTTON(ImGuiNavInput_TweakSlow, SDL_CONTROLLER_BUTTON_LEFTSHOULDER); // L1 / LB 339 | MAP_BUTTON(ImGuiNavInput_TweakFast, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER); // R1 / RB 340 | MAP_ANALOG(ImGuiNavInput_LStickLeft, SDL_CONTROLLER_AXIS_LEFTX, -thumb_dead_zone, -32768); 341 | MAP_ANALOG(ImGuiNavInput_LStickRight, SDL_CONTROLLER_AXIS_LEFTX, +thumb_dead_zone, +32767); 342 | MAP_ANALOG(ImGuiNavInput_LStickUp, SDL_CONTROLLER_AXIS_LEFTY, -thumb_dead_zone, -32767); 343 | MAP_ANALOG(ImGuiNavInput_LStickDown, SDL_CONTROLLER_AXIS_LEFTY, +thumb_dead_zone, +32767); 344 | 345 | io.BackendFlags |= ImGuiBackendFlags_HasGamepad; 346 | #undef MAP_BUTTON 347 | #undef MAP_ANALOG 348 | } 349 | 350 | void ImGui_ImplSDL2_NewFrame(SDL_Window* window) 351 | { 352 | ImGuiIO& io = ImGui::GetIO(); 353 | IM_ASSERT(io.Fonts->IsBuilt() && "Font atlas not built! It is generally built by the renderer backend. Missing call to renderer _NewFrame() function? e.g. ImGui_ImplOpenGL3_NewFrame()."); 354 | 355 | // Setup display size (every frame to accommodate for window resizing) 356 | int w, h; 357 | int display_w, display_h; 358 | SDL_GetWindowSize(window, &w, &h); 359 | if (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED) 360 | w = h = 0; 361 | SDL_GL_GetDrawableSize(window, &display_w, &display_h); 362 | io.DisplaySize = ImVec2((float)w, (float)h); 363 | if (w > 0 && h > 0) 364 | io.DisplayFramebufferScale = ImVec2((float)display_w / w, (float)display_h / h); 365 | 366 | // Setup time step (we don't use SDL_GetTicks() because it is using millisecond resolution) 367 | static Uint64 frequency = SDL_GetPerformanceFrequency(); 368 | Uint64 current_time = SDL_GetPerformanceCounter(); 369 | io.DeltaTime = g_Time > 0 ? (float)((double)(current_time - g_Time) / frequency) : (float)(1.0f / 60.0f); 370 | g_Time = current_time; 371 | 372 | ImGui_ImplSDL2_UpdateMousePosAndButtons(); 373 | ImGui_ImplSDL2_UpdateMouseCursor(); 374 | 375 | // Update game controllers (if enabled and available) 376 | ImGui_ImplSDL2_UpdateGamepads(); 377 | } 378 | -------------------------------------------------------------------------------- /src/renderer/imgui/imstb_rectpack.h: -------------------------------------------------------------------------------- 1 | // [DEAR IMGUI] 2 | // This is a slightly modified version of stb_rect_pack.h 1.00. 3 | // Those changes would need to be pushed into nothings/stb: 4 | // - Added STBRP__CDECL 5 | // Grep for [DEAR IMGUI] to find the changes. 6 | 7 | // stb_rect_pack.h - v1.00 - public domain - rectangle packing 8 | // Sean Barrett 2014 9 | // 10 | // Useful for e.g. packing rectangular textures into an atlas. 11 | // Does not do rotation. 12 | // 13 | // Not necessarily the awesomest packing method, but better than 14 | // the totally naive one in stb_truetype (which is primarily what 15 | // this is meant to replace). 16 | // 17 | // Has only had a few tests run, may have issues. 18 | // 19 | // More docs to come. 20 | // 21 | // No memory allocations; uses qsort() and assert() from stdlib. 22 | // Can override those by defining STBRP_SORT and STBRP_ASSERT. 23 | // 24 | // This library currently uses the Skyline Bottom-Left algorithm. 25 | // 26 | // Please note: better rectangle packers are welcome! Please 27 | // implement them to the same API, but with a different init 28 | // function. 29 | // 30 | // Credits 31 | // 32 | // Library 33 | // Sean Barrett 34 | // Minor features 35 | // Martins Mozeiko 36 | // github:IntellectualKitty 37 | // 38 | // Bugfixes / warning fixes 39 | // Jeremy Jaussaud 40 | // Fabian Giesen 41 | // 42 | // Version history: 43 | // 44 | // 1.00 (2019-02-25) avoid small space waste; gracefully fail too-wide rectangles 45 | // 0.99 (2019-02-07) warning fixes 46 | // 0.11 (2017-03-03) return packing success/fail result 47 | // 0.10 (2016-10-25) remove cast-away-const to avoid warnings 48 | // 0.09 (2016-08-27) fix compiler warnings 49 | // 0.08 (2015-09-13) really fix bug with empty rects (w=0 or h=0) 50 | // 0.07 (2015-09-13) fix bug with empty rects (w=0 or h=0) 51 | // 0.06 (2015-04-15) added STBRP_SORT to allow replacing qsort 52 | // 0.05: added STBRP_ASSERT to allow replacing assert 53 | // 0.04: fixed minor bug in STBRP_LARGE_RECTS support 54 | // 0.01: initial release 55 | // 56 | // LICENSE 57 | // 58 | // See end of file for license information. 59 | 60 | ////////////////////////////////////////////////////////////////////////////// 61 | // 62 | // INCLUDE SECTION 63 | // 64 | 65 | #ifndef STB_INCLUDE_STB_RECT_PACK_H 66 | #define STB_INCLUDE_STB_RECT_PACK_H 67 | 68 | #define STB_RECT_PACK_VERSION 1 69 | 70 | #ifdef STBRP_STATIC 71 | #define STBRP_DEF static 72 | #else 73 | #define STBRP_DEF extern 74 | #endif 75 | 76 | #ifdef __cplusplus 77 | extern "C" { 78 | #endif 79 | 80 | typedef struct stbrp_context stbrp_context; 81 | typedef struct stbrp_node stbrp_node; 82 | typedef struct stbrp_rect stbrp_rect; 83 | 84 | #ifdef STBRP_LARGE_RECTS 85 | typedef int stbrp_coord; 86 | #else 87 | typedef unsigned short stbrp_coord; 88 | #endif 89 | 90 | STBRP_DEF int stbrp_pack_rects (stbrp_context *context, stbrp_rect *rects, int num_rects); 91 | // Assign packed locations to rectangles. The rectangles are of type 92 | // 'stbrp_rect' defined below, stored in the array 'rects', and there 93 | // are 'num_rects' many of them. 94 | // 95 | // Rectangles which are successfully packed have the 'was_packed' flag 96 | // set to a non-zero value and 'x' and 'y' store the minimum location 97 | // on each axis (i.e. bottom-left in cartesian coordinates, top-left 98 | // if you imagine y increasing downwards). Rectangles which do not fit 99 | // have the 'was_packed' flag set to 0. 100 | // 101 | // You should not try to access the 'rects' array from another thread 102 | // while this function is running, as the function temporarily reorders 103 | // the array while it executes. 104 | // 105 | // To pack into another rectangle, you need to call stbrp_init_target 106 | // again. To continue packing into the same rectangle, you can call 107 | // this function again. Calling this multiple times with multiple rect 108 | // arrays will probably produce worse packing results than calling it 109 | // a single time with the full rectangle array, but the option is 110 | // available. 111 | // 112 | // The function returns 1 if all of the rectangles were successfully 113 | // packed and 0 otherwise. 114 | 115 | struct stbrp_rect 116 | { 117 | // reserved for your use: 118 | int id; 119 | 120 | // input: 121 | stbrp_coord w, h; 122 | 123 | // output: 124 | stbrp_coord x, y; 125 | int was_packed; // non-zero if valid packing 126 | 127 | }; // 16 bytes, nominally 128 | 129 | 130 | STBRP_DEF void stbrp_init_target (stbrp_context *context, int width, int height, stbrp_node *nodes, int num_nodes); 131 | // Initialize a rectangle packer to: 132 | // pack a rectangle that is 'width' by 'height' in dimensions 133 | // using temporary storage provided by the array 'nodes', which is 'num_nodes' long 134 | // 135 | // You must call this function every time you start packing into a new target. 136 | // 137 | // There is no "shutdown" function. The 'nodes' memory must stay valid for 138 | // the following stbrp_pack_rects() call (or calls), but can be freed after 139 | // the call (or calls) finish. 140 | // 141 | // Note: to guarantee best results, either: 142 | // 1. make sure 'num_nodes' >= 'width' 143 | // or 2. call stbrp_allow_out_of_mem() defined below with 'allow_out_of_mem = 1' 144 | // 145 | // If you don't do either of the above things, widths will be quantized to multiples 146 | // of small integers to guarantee the algorithm doesn't run out of temporary storage. 147 | // 148 | // If you do #2, then the non-quantized algorithm will be used, but the algorithm 149 | // may run out of temporary storage and be unable to pack some rectangles. 150 | 151 | STBRP_DEF void stbrp_setup_allow_out_of_mem (stbrp_context *context, int allow_out_of_mem); 152 | // Optionally call this function after init but before doing any packing to 153 | // change the handling of the out-of-temp-memory scenario, described above. 154 | // If you call init again, this will be reset to the default (false). 155 | 156 | 157 | STBRP_DEF void stbrp_setup_heuristic (stbrp_context *context, int heuristic); 158 | // Optionally select which packing heuristic the library should use. Different 159 | // heuristics will produce better/worse results for different data sets. 160 | // If you call init again, this will be reset to the default. 161 | 162 | enum 163 | { 164 | STBRP_HEURISTIC_Skyline_default=0, 165 | STBRP_HEURISTIC_Skyline_BL_sortHeight = STBRP_HEURISTIC_Skyline_default, 166 | STBRP_HEURISTIC_Skyline_BF_sortHeight 167 | }; 168 | 169 | 170 | ////////////////////////////////////////////////////////////////////////////// 171 | // 172 | // the details of the following structures don't matter to you, but they must 173 | // be visible so you can handle the memory allocations for them 174 | 175 | struct stbrp_node 176 | { 177 | stbrp_coord x,y; 178 | stbrp_node *next; 179 | }; 180 | 181 | struct stbrp_context 182 | { 183 | int width; 184 | int height; 185 | int align; 186 | int init_mode; 187 | int heuristic; 188 | int num_nodes; 189 | stbrp_node *active_head; 190 | stbrp_node *free_head; 191 | stbrp_node extra[2]; // we allocate two extra nodes so optimal user-node-count is 'width' not 'width+2' 192 | }; 193 | 194 | #ifdef __cplusplus 195 | } 196 | #endif 197 | 198 | #endif 199 | 200 | ////////////////////////////////////////////////////////////////////////////// 201 | // 202 | // IMPLEMENTATION SECTION 203 | // 204 | 205 | #ifdef STB_RECT_PACK_IMPLEMENTATION 206 | #ifndef STBRP_SORT 207 | #include 208 | #define STBRP_SORT qsort 209 | #endif 210 | 211 | #ifndef STBRP_ASSERT 212 | #include 213 | #define STBRP_ASSERT assert 214 | #endif 215 | 216 | // [DEAR IMGUI] Added STBRP__CDECL 217 | #ifdef _MSC_VER 218 | #define STBRP__NOTUSED(v) (void)(v) 219 | #define STBRP__CDECL __cdecl 220 | #else 221 | #define STBRP__NOTUSED(v) (void)sizeof(v) 222 | #define STBRP__CDECL 223 | #endif 224 | 225 | enum 226 | { 227 | STBRP__INIT_skyline = 1 228 | }; 229 | 230 | STBRP_DEF void stbrp_setup_heuristic(stbrp_context *context, int heuristic) 231 | { 232 | switch (context->init_mode) { 233 | case STBRP__INIT_skyline: 234 | STBRP_ASSERT(heuristic == STBRP_HEURISTIC_Skyline_BL_sortHeight || heuristic == STBRP_HEURISTIC_Skyline_BF_sortHeight); 235 | context->heuristic = heuristic; 236 | break; 237 | default: 238 | STBRP_ASSERT(0); 239 | } 240 | } 241 | 242 | STBRP_DEF void stbrp_setup_allow_out_of_mem(stbrp_context *context, int allow_out_of_mem) 243 | { 244 | if (allow_out_of_mem) 245 | // if it's ok to run out of memory, then don't bother aligning them; 246 | // this gives better packing, but may fail due to OOM (even though 247 | // the rectangles easily fit). @TODO a smarter approach would be to only 248 | // quantize once we've hit OOM, then we could get rid of this parameter. 249 | context->align = 1; 250 | else { 251 | // if it's not ok to run out of memory, then quantize the widths 252 | // so that num_nodes is always enough nodes. 253 | // 254 | // I.e. num_nodes * align >= width 255 | // align >= width / num_nodes 256 | // align = ceil(width/num_nodes) 257 | 258 | context->align = (context->width + context->num_nodes-1) / context->num_nodes; 259 | } 260 | } 261 | 262 | STBRP_DEF void stbrp_init_target(stbrp_context *context, int width, int height, stbrp_node *nodes, int num_nodes) 263 | { 264 | int i; 265 | #ifndef STBRP_LARGE_RECTS 266 | STBRP_ASSERT(width <= 0xffff && height <= 0xffff); 267 | #endif 268 | 269 | for (i=0; i < num_nodes-1; ++i) 270 | nodes[i].next = &nodes[i+1]; 271 | nodes[i].next = NULL; 272 | context->init_mode = STBRP__INIT_skyline; 273 | context->heuristic = STBRP_HEURISTIC_Skyline_default; 274 | context->free_head = &nodes[0]; 275 | context->active_head = &context->extra[0]; 276 | context->width = width; 277 | context->height = height; 278 | context->num_nodes = num_nodes; 279 | stbrp_setup_allow_out_of_mem(context, 0); 280 | 281 | // node 0 is the full width, node 1 is the sentinel (lets us not store width explicitly) 282 | context->extra[0].x = 0; 283 | context->extra[0].y = 0; 284 | context->extra[0].next = &context->extra[1]; 285 | context->extra[1].x = (stbrp_coord) width; 286 | #ifdef STBRP_LARGE_RECTS 287 | context->extra[1].y = (1<<30); 288 | #else 289 | context->extra[1].y = 65535; 290 | #endif 291 | context->extra[1].next = NULL; 292 | } 293 | 294 | // find minimum y position if it starts at x1 295 | static int stbrp__skyline_find_min_y(stbrp_context *c, stbrp_node *first, int x0, int width, int *pwaste) 296 | { 297 | stbrp_node *node = first; 298 | int x1 = x0 + width; 299 | int min_y, visited_width, waste_area; 300 | 301 | STBRP__NOTUSED(c); 302 | 303 | STBRP_ASSERT(first->x <= x0); 304 | 305 | #if 0 306 | // skip in case we're past the node 307 | while (node->next->x <= x0) 308 | ++node; 309 | #else 310 | STBRP_ASSERT(node->next->x > x0); // we ended up handling this in the caller for efficiency 311 | #endif 312 | 313 | STBRP_ASSERT(node->x <= x0); 314 | 315 | min_y = 0; 316 | waste_area = 0; 317 | visited_width = 0; 318 | while (node->x < x1) { 319 | if (node->y > min_y) { 320 | // raise min_y higher. 321 | // we've accounted for all waste up to min_y, 322 | // but we'll now add more waste for everything we've visted 323 | waste_area += visited_width * (node->y - min_y); 324 | min_y = node->y; 325 | // the first time through, visited_width might be reduced 326 | if (node->x < x0) 327 | visited_width += node->next->x - x0; 328 | else 329 | visited_width += node->next->x - node->x; 330 | } else { 331 | // add waste area 332 | int under_width = node->next->x - node->x; 333 | if (under_width + visited_width > width) 334 | under_width = width - visited_width; 335 | waste_area += under_width * (min_y - node->y); 336 | visited_width += under_width; 337 | } 338 | node = node->next; 339 | } 340 | 341 | *pwaste = waste_area; 342 | return min_y; 343 | } 344 | 345 | typedef struct 346 | { 347 | int x,y; 348 | stbrp_node **prev_link; 349 | } stbrp__findresult; 350 | 351 | static stbrp__findresult stbrp__skyline_find_best_pos(stbrp_context *c, int width, int height) 352 | { 353 | int best_waste = (1<<30), best_x, best_y = (1 << 30); 354 | stbrp__findresult fr; 355 | stbrp_node **prev, *node, *tail, **best = NULL; 356 | 357 | // align to multiple of c->align 358 | width = (width + c->align - 1); 359 | width -= width % c->align; 360 | STBRP_ASSERT(width % c->align == 0); 361 | 362 | // if it can't possibly fit, bail immediately 363 | if (width > c->width || height > c->height) { 364 | fr.prev_link = NULL; 365 | fr.x = fr.y = 0; 366 | return fr; 367 | } 368 | 369 | node = c->active_head; 370 | prev = &c->active_head; 371 | while (node->x + width <= c->width) { 372 | int y,waste; 373 | y = stbrp__skyline_find_min_y(c, node, node->x, width, &waste); 374 | if (c->heuristic == STBRP_HEURISTIC_Skyline_BL_sortHeight) { // actually just want to test BL 375 | // bottom left 376 | if (y < best_y) { 377 | best_y = y; 378 | best = prev; 379 | } 380 | } else { 381 | // best-fit 382 | if (y + height <= c->height) { 383 | // can only use it if it first vertically 384 | if (y < best_y || (y == best_y && waste < best_waste)) { 385 | best_y = y; 386 | best_waste = waste; 387 | best = prev; 388 | } 389 | } 390 | } 391 | prev = &node->next; 392 | node = node->next; 393 | } 394 | 395 | best_x = (best == NULL) ? 0 : (*best)->x; 396 | 397 | // if doing best-fit (BF), we also have to try aligning right edge to each node position 398 | // 399 | // e.g, if fitting 400 | // 401 | // ____________________ 402 | // |____________________| 403 | // 404 | // into 405 | // 406 | // | | 407 | // | ____________| 408 | // |____________| 409 | // 410 | // then right-aligned reduces waste, but bottom-left BL is always chooses left-aligned 411 | // 412 | // This makes BF take about 2x the time 413 | 414 | if (c->heuristic == STBRP_HEURISTIC_Skyline_BF_sortHeight) { 415 | tail = c->active_head; 416 | node = c->active_head; 417 | prev = &c->active_head; 418 | // find first node that's admissible 419 | while (tail->x < width) 420 | tail = tail->next; 421 | while (tail) { 422 | int xpos = tail->x - width; 423 | int y,waste; 424 | STBRP_ASSERT(xpos >= 0); 425 | // find the left position that matches this 426 | while (node->next->x <= xpos) { 427 | prev = &node->next; 428 | node = node->next; 429 | } 430 | STBRP_ASSERT(node->next->x > xpos && node->x <= xpos); 431 | y = stbrp__skyline_find_min_y(c, node, xpos, width, &waste); 432 | if (y + height <= c->height) { 433 | if (y <= best_y) { 434 | if (y < best_y || waste < best_waste || (waste==best_waste && xpos < best_x)) { 435 | best_x = xpos; 436 | STBRP_ASSERT(y <= best_y); 437 | best_y = y; 438 | best_waste = waste; 439 | best = prev; 440 | } 441 | } 442 | } 443 | tail = tail->next; 444 | } 445 | } 446 | 447 | fr.prev_link = best; 448 | fr.x = best_x; 449 | fr.y = best_y; 450 | return fr; 451 | } 452 | 453 | static stbrp__findresult stbrp__skyline_pack_rectangle(stbrp_context *context, int width, int height) 454 | { 455 | // find best position according to heuristic 456 | stbrp__findresult res = stbrp__skyline_find_best_pos(context, width, height); 457 | stbrp_node *node, *cur; 458 | 459 | // bail if: 460 | // 1. it failed 461 | // 2. the best node doesn't fit (we don't always check this) 462 | // 3. we're out of memory 463 | if (res.prev_link == NULL || res.y + height > context->height || context->free_head == NULL) { 464 | res.prev_link = NULL; 465 | return res; 466 | } 467 | 468 | // on success, create new node 469 | node = context->free_head; 470 | node->x = (stbrp_coord) res.x; 471 | node->y = (stbrp_coord) (res.y + height); 472 | 473 | context->free_head = node->next; 474 | 475 | // insert the new node into the right starting point, and 476 | // let 'cur' point to the remaining nodes needing to be 477 | // stiched back in 478 | 479 | cur = *res.prev_link; 480 | if (cur->x < res.x) { 481 | // preserve the existing one, so start testing with the next one 482 | stbrp_node *next = cur->next; 483 | cur->next = node; 484 | cur = next; 485 | } else { 486 | *res.prev_link = node; 487 | } 488 | 489 | // from here, traverse cur and free the nodes, until we get to one 490 | // that shouldn't be freed 491 | while (cur->next && cur->next->x <= res.x + width) { 492 | stbrp_node *next = cur->next; 493 | // move the current node to the free list 494 | cur->next = context->free_head; 495 | context->free_head = cur; 496 | cur = next; 497 | } 498 | 499 | // stitch the list back in 500 | node->next = cur; 501 | 502 | if (cur->x < res.x + width) 503 | cur->x = (stbrp_coord) (res.x + width); 504 | 505 | #ifdef _DEBUG 506 | cur = context->active_head; 507 | while (cur->x < context->width) { 508 | STBRP_ASSERT(cur->x < cur->next->x); 509 | cur = cur->next; 510 | } 511 | STBRP_ASSERT(cur->next == NULL); 512 | 513 | { 514 | int count=0; 515 | cur = context->active_head; 516 | while (cur) { 517 | cur = cur->next; 518 | ++count; 519 | } 520 | cur = context->free_head; 521 | while (cur) { 522 | cur = cur->next; 523 | ++count; 524 | } 525 | STBRP_ASSERT(count == context->num_nodes+2); 526 | } 527 | #endif 528 | 529 | return res; 530 | } 531 | 532 | // [DEAR IMGUI] Added STBRP__CDECL 533 | static int STBRP__CDECL rect_height_compare(const void *a, const void *b) 534 | { 535 | const stbrp_rect *p = (const stbrp_rect *) a; 536 | const stbrp_rect *q = (const stbrp_rect *) b; 537 | if (p->h > q->h) 538 | return -1; 539 | if (p->h < q->h) 540 | return 1; 541 | return (p->w > q->w) ? -1 : (p->w < q->w); 542 | } 543 | 544 | // [DEAR IMGUI] Added STBRP__CDECL 545 | static int STBRP__CDECL rect_original_order(const void *a, const void *b) 546 | { 547 | const stbrp_rect *p = (const stbrp_rect *) a; 548 | const stbrp_rect *q = (const stbrp_rect *) b; 549 | return (p->was_packed < q->was_packed) ? -1 : (p->was_packed > q->was_packed); 550 | } 551 | 552 | #ifdef STBRP_LARGE_RECTS 553 | #define STBRP__MAXVAL 0xffffffff 554 | #else 555 | #define STBRP__MAXVAL 0xffff 556 | #endif 557 | 558 | STBRP_DEF int stbrp_pack_rects(stbrp_context *context, stbrp_rect *rects, int num_rects) 559 | { 560 | int i, all_rects_packed = 1; 561 | 562 | // we use the 'was_packed' field internally to allow sorting/unsorting 563 | for (i=0; i < num_rects; ++i) { 564 | rects[i].was_packed = i; 565 | } 566 | 567 | // sort according to heuristic 568 | STBRP_SORT(rects, num_rects, sizeof(rects[0]), rect_height_compare); 569 | 570 | for (i=0; i < num_rects; ++i) { 571 | if (rects[i].w == 0 || rects[i].h == 0) { 572 | rects[i].x = rects[i].y = 0; // empty rect needs no space 573 | } else { 574 | stbrp__findresult fr = stbrp__skyline_pack_rectangle(context, rects[i].w, rects[i].h); 575 | if (fr.prev_link) { 576 | rects[i].x = (stbrp_coord) fr.x; 577 | rects[i].y = (stbrp_coord) fr.y; 578 | } else { 579 | rects[i].x = rects[i].y = STBRP__MAXVAL; 580 | } 581 | } 582 | } 583 | 584 | // unsort 585 | STBRP_SORT(rects, num_rects, sizeof(rects[0]), rect_original_order); 586 | 587 | // set was_packed flags and all_rects_packed status 588 | for (i=0; i < num_rects; ++i) { 589 | rects[i].was_packed = !(rects[i].x == STBRP__MAXVAL && rects[i].y == STBRP__MAXVAL); 590 | if (!rects[i].was_packed) 591 | all_rects_packed = 0; 592 | } 593 | 594 | // return the all_rects_packed status 595 | return all_rects_packed; 596 | } 597 | #endif 598 | 599 | /* 600 | ------------------------------------------------------------------------------ 601 | This software is available under 2 licenses -- choose whichever you prefer. 602 | ------------------------------------------------------------------------------ 603 | ALTERNATIVE A - MIT License 604 | Copyright (c) 2017 Sean Barrett 605 | Permission is hereby granted, free of charge, to any person obtaining a copy of 606 | this software and associated documentation files (the "Software"), to deal in 607 | the Software without restriction, including without limitation the rights to 608 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 609 | of the Software, and to permit persons to whom the Software is furnished to do 610 | so, subject to the following conditions: 611 | The above copyright notice and this permission notice shall be included in all 612 | copies or substantial portions of the Software. 613 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 614 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 615 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 616 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 617 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 618 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 619 | SOFTWARE. 620 | ------------------------------------------------------------------------------ 621 | ALTERNATIVE B - Public Domain (www.unlicense.org) 622 | This is free and unencumbered software released into the public domain. 623 | Anyone is free to copy, modify, publish, use, compile, sell, or distribute this 624 | software, either in source code form or as a compiled binary, for any purpose, 625 | commercial or non-commercial, and by any means. 626 | In jurisdictions that recognize copyright laws, the author or authors of this 627 | software dedicate any and all copyright interest in the software to the public 628 | domain. We make this dedication for the benefit of the public at large and to 629 | the detriment of our heirs and successors. We intend this dedication to be an 630 | overt act of relinquishment in perpetuity of all present and future rights to 631 | this software under copyright law. 632 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 633 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 634 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 635 | AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 636 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 637 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 638 | ------------------------------------------------------------------------------ 639 | */ 640 | -------------------------------------------------------------------------------- /src/renderer/imgui/imgui_impl_opengl3.cpp: -------------------------------------------------------------------------------- 1 | // dear imgui: Renderer Backend for modern OpenGL with shaders / programmatic pipeline 2 | // - Desktop GL: 2.x 3.x 4.x 3 | // - Embedded GL: ES 2.0 (WebGL 1.0), ES 3.0 (WebGL 2.0) 4 | // This needs to be used along with a Platform Backend (e.g. GLFW, SDL, Win32, custom..) 5 | 6 | // Implemented features: 7 | // [X] Renderer: User texture binding. Use 'GLuint' OpenGL texture identifier as void*/ImTextureID. Read the FAQ about ImTextureID! 8 | // [x] Renderer: Desktop GL only: Support for large meshes (64k+ vertices) with 16-bit indices. 9 | 10 | // You can copy and use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this. 11 | // If you are new to Dear ImGui, read documentation from the docs/ folder + read the top of imgui.cpp. 12 | // Read online: https://github.com/ocornut/imgui/tree/master/docs 13 | 14 | // CHANGELOG 15 | // (minor and older changes stripped away, please see git history for details) 16 | // 2021-05-19: OpenGL: Replaced direct access to ImDrawCmd::TextureId with a call to ImDrawCmd::GetTexID(). (will become a requirement) 17 | // 2021-04-06: OpenGL: Don't try to read GL_CLIP_ORIGIN unless we're OpenGL 4.5 or greater. 18 | // 2021-02-18: OpenGL: Change blending equation to preserve alpha in output buffer. 19 | // 2021-01-03: OpenGL: Backup, setup and restore GL_STENCIL_TEST state. 20 | // 2020-10-23: OpenGL: Backup, setup and restore GL_PRIMITIVE_RESTART state. 21 | // 2020-10-15: OpenGL: Use glGetString(GL_VERSION) instead of glGetIntegerv(GL_MAJOR_VERSION, ...) when the later returns zero (e.g. Desktop GL 2.x) 22 | // 2020-09-17: OpenGL: Fix to avoid compiling/calling glBindSampler() on ES or pre 3.3 context which have the defines set by a loader. 23 | // 2020-07-10: OpenGL: Added support for glad2 OpenGL loader. 24 | // 2020-05-08: OpenGL: Made default GLSL version 150 (instead of 130) on OSX. 25 | // 2020-04-21: OpenGL: Fixed handling of glClipControl(GL_UPPER_LEFT) by inverting projection matrix. 26 | // 2020-04-12: OpenGL: Fixed context version check mistakenly testing for 4.0+ instead of 3.2+ to enable ImGuiBackendFlags_RendererHasVtxOffset. 27 | // 2020-03-24: OpenGL: Added support for glbinding 2.x OpenGL loader. 28 | // 2020-01-07: OpenGL: Added support for glbinding 3.x OpenGL loader. 29 | // 2019-10-25: OpenGL: Using a combination of GL define and runtime GL version to decide whether to use glDrawElementsBaseVertex(). Fix building with pre-3.2 GL loaders. 30 | // 2019-09-22: OpenGL: Detect default GL loader using __has_include compiler facility. 31 | // 2019-09-16: OpenGL: Tweak initialization code to allow application calling ImGui_ImplOpenGL3_CreateFontsTexture() before the first NewFrame() call. 32 | // 2019-05-29: OpenGL: Desktop GL only: Added support for large mesh (64K+ vertices), enable ImGuiBackendFlags_RendererHasVtxOffset flag. 33 | // 2019-04-30: OpenGL: Added support for special ImDrawCallback_ResetRenderState callback to reset render state. 34 | // 2019-03-29: OpenGL: Not calling glBindBuffer more than necessary in the render loop. 35 | // 2019-03-15: OpenGL: Added a GL call + comments in ImGui_ImplOpenGL3_Init() to detect uninitialized GL function loaders early. 36 | // 2019-03-03: OpenGL: Fix support for ES 2.0 (WebGL 1.0). 37 | // 2019-02-20: OpenGL: Fix for OSX not supporting OpenGL 4.5, we don't try to read GL_CLIP_ORIGIN even if defined by the headers/loader. 38 | // 2019-02-11: OpenGL: Projecting clipping rectangles correctly using draw_data->FramebufferScale to allow multi-viewports for retina display. 39 | // 2019-02-01: OpenGL: Using GLSL 410 shaders for any version over 410 (e.g. 430, 450). 40 | // 2018-11-30: Misc: Setting up io.BackendRendererName so it can be displayed in the About Window. 41 | // 2018-11-13: OpenGL: Support for GL 4.5's glClipControl(GL_UPPER_LEFT) / GL_CLIP_ORIGIN. 42 | // 2018-08-29: OpenGL: Added support for more OpenGL loaders: glew and glad, with comments indicative that any loader can be used. 43 | // 2018-08-09: OpenGL: Default to OpenGL ES 3 on iOS and Android. GLSL version default to "#version 300 ES". 44 | // 2018-07-30: OpenGL: Support for GLSL 300 ES and 410 core. Fixes for Emscripten compilation. 45 | // 2018-07-10: OpenGL: Support for more GLSL versions (based on the GLSL version string). Added error output when shaders fail to compile/link. 46 | // 2018-06-08: Misc: Extracted imgui_impl_opengl3.cpp/.h away from the old combined GLFW/SDL+OpenGL3 examples. 47 | // 2018-06-08: OpenGL: Use draw_data->DisplayPos and draw_data->DisplaySize to setup projection matrix and clipping rectangle. 48 | // 2018-05-25: OpenGL: Removed unnecessary backup/restore of GL_ELEMENT_ARRAY_BUFFER_BINDING since this is part of the VAO state. 49 | // 2018-05-14: OpenGL: Making the call to glBindSampler() optional so 3.2 context won't fail if the function is a NULL pointer. 50 | // 2018-03-06: OpenGL: Added const char* glsl_version parameter to ImGui_ImplOpenGL3_Init() so user can override the GLSL version e.g. "#version 150". 51 | // 2018-02-23: OpenGL: Create the VAO in the render function so the setup can more easily be used with multiple shared GL context. 52 | // 2018-02-16: Misc: Obsoleted the io.RenderDrawListsFn callback and exposed ImGui_ImplSdlGL3_RenderDrawData() in the .h file so you can call it yourself. 53 | // 2018-01-07: OpenGL: Changed GLSL shader version from 330 to 150. 54 | // 2017-09-01: OpenGL: Save and restore current bound sampler. Save and restore current polygon mode. 55 | // 2017-05-01: OpenGL: Fixed save and restore of current blend func state. 56 | // 2017-05-01: OpenGL: Fixed save and restore of current GL_ACTIVE_TEXTURE. 57 | // 2016-09-05: OpenGL: Fixed save and restore of current scissor rectangle. 58 | // 2016-07-29: OpenGL: Explicitly setting GL_UNPACK_ROW_LENGTH to reduce issues because SDL changes it. (#752) 59 | 60 | //---------------------------------------- 61 | // OpenGL GLSL GLSL 62 | // version version string 63 | //---------------------------------------- 64 | // 2.0 110 "#version 110" 65 | // 2.1 120 "#version 120" 66 | // 3.0 130 "#version 130" 67 | // 3.1 140 "#version 140" 68 | // 3.2 150 "#version 150" 69 | // 3.3 330 "#version 330 core" 70 | // 4.0 400 "#version 400 core" 71 | // 4.1 410 "#version 410 core" 72 | // 4.2 420 "#version 410 core" 73 | // 4.3 430 "#version 430 core" 74 | // ES 2.0 100 "#version 100" = WebGL 1.0 75 | // ES 3.0 300 "#version 300 es" = WebGL 2.0 76 | //---------------------------------------- 77 | 78 | #if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS) 79 | #define _CRT_SECURE_NO_WARNINGS 80 | #endif 81 | 82 | #include "imgui.h" 83 | #include "imgui_impl_opengl3.h" 84 | #include 85 | #if defined(_MSC_VER) && _MSC_VER <= 1500 // MSVC 2008 or earlier 86 | #include // intptr_t 87 | #else 88 | #include // intptr_t 89 | #endif 90 | 91 | // GL includes 92 | #if defined(IMGUI_IMPL_OPENGL_ES2) 93 | #include 94 | #elif defined(IMGUI_IMPL_OPENGL_ES3) 95 | #if (defined(__APPLE__) && (TARGET_OS_IOS || TARGET_OS_TV)) 96 | #include // Use GL ES 3 97 | #else 98 | #include // Use GL ES 3 99 | #endif 100 | #else 101 | // About Desktop OpenGL function loaders: 102 | // Modern desktop OpenGL doesn't have a standard portable header file to load OpenGL function pointers. 103 | // Helper libraries are often used for this purpose! Here we are supporting a few common ones (gl3w, glew, glad). 104 | // You may use another loader/header of your choice (glext, glLoadGen, etc.), or chose to manually implement your own. 105 | #if defined(IMGUI_IMPL_OPENGL_LOADER_GL3W) 106 | #include // Needs to be initialized with gl3wInit() in user's code 107 | #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLEW) 108 | #include // Needs to be initialized with glewInit() in user's code. 109 | #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLAD) 110 | #include // Needs to be initialized with gladLoadGL() in user's code. 111 | #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLAD2) 112 | #include // Needs to be initialized with gladLoadGL(...) or gladLoaderLoadGL() in user's code. 113 | #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLBINDING2) 114 | #ifndef GLFW_INCLUDE_NONE 115 | #define GLFW_INCLUDE_NONE // GLFW including OpenGL headers causes ambiguity or multiple definition errors. 116 | #endif 117 | #include // Needs to be initialized with glbinding::Binding::initialize() in user's code. 118 | #include 119 | using namespace gl; 120 | #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLBINDING3) 121 | #ifndef GLFW_INCLUDE_NONE 122 | #define GLFW_INCLUDE_NONE // GLFW including OpenGL headers causes ambiguity or multiple definition errors. 123 | #endif 124 | #include // Needs to be initialized with glbinding::initialize() in user's code. 125 | #include 126 | using namespace gl; 127 | #else 128 | #include IMGUI_IMPL_OPENGL_LOADER_CUSTOM 129 | #endif 130 | #endif 131 | 132 | // Desktop GL 3.2+ has glDrawElementsBaseVertex() which GL ES and WebGL don't have. 133 | #if !defined(IMGUI_IMPL_OPENGL_ES2) && !defined(IMGUI_IMPL_OPENGL_ES3) && defined(GL_VERSION_3_2) 134 | #define IMGUI_IMPL_OPENGL_MAY_HAVE_VTX_OFFSET 135 | #endif 136 | 137 | // Desktop GL 3.3+ has glBindSampler() 138 | #if !defined(IMGUI_IMPL_OPENGL_ES2) && !defined(IMGUI_IMPL_OPENGL_ES3) && defined(GL_VERSION_3_3) 139 | #define IMGUI_IMPL_OPENGL_MAY_HAVE_BIND_SAMPLER 140 | #endif 141 | 142 | // Desktop GL 3.1+ has GL_PRIMITIVE_RESTART state 143 | #if !defined(IMGUI_IMPL_OPENGL_ES2) && !defined(IMGUI_IMPL_OPENGL_ES3) && defined(GL_VERSION_3_1) 144 | #define IMGUI_IMPL_OPENGL_MAY_HAVE_PRIMITIVE_RESTART 145 | #endif 146 | 147 | // OpenGL Data 148 | static GLuint g_GlVersion = 0; // Extracted at runtime using GL_MAJOR_VERSION, GL_MINOR_VERSION queries (e.g. 320 for GL 3.2) 149 | static char g_GlslVersionString[32] = ""; // Specified by user or detected based on compile time GL settings. 150 | static GLuint g_FontTexture = 0; 151 | static GLuint g_ShaderHandle = 0, g_VertHandle = 0, g_FragHandle = 0; 152 | static GLint g_AttribLocationTex = 0, g_AttribLocationProjMtx = 0; // Uniforms location 153 | static GLuint g_AttribLocationVtxPos = 0, g_AttribLocationVtxUV = 0, g_AttribLocationVtxColor = 0; // Vertex attributes location 154 | static unsigned int g_VboHandle = 0, g_ElementsHandle = 0; 155 | 156 | // Functions 157 | bool ImGui_ImplOpenGL3_Init(const char* glsl_version) 158 | { 159 | // Query for GL version (e.g. 320 for GL 3.2) 160 | #if !defined(IMGUI_IMPL_OPENGL_ES2) 161 | GLint major = 0; 162 | GLint minor = 0; 163 | glGetIntegerv(GL_MAJOR_VERSION, &major); 164 | glGetIntegerv(GL_MINOR_VERSION, &minor); 165 | if (major == 0 && minor == 0) 166 | { 167 | // Query GL_VERSION in desktop GL 2.x, the string will start with "." 168 | const char* gl_version = (const char*)glGetString(GL_VERSION); 169 | sscanf(gl_version, "%d.%d", &major, &minor); 170 | } 171 | g_GlVersion = (GLuint)(major * 100 + minor * 10); 172 | #else 173 | g_GlVersion = 200; // GLES 2 174 | #endif 175 | 176 | // Setup backend capabilities flags 177 | ImGuiIO& io = ImGui::GetIO(); 178 | io.BackendRendererName = "imgui_impl_opengl3"; 179 | #ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_VTX_OFFSET 180 | if (g_GlVersion >= 320) 181 | io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes. 182 | #endif 183 | 184 | // Store GLSL version string so we can refer to it later in case we recreate shaders. 185 | // Note: GLSL version is NOT the same as GL version. Leave this to NULL if unsure. 186 | #if defined(IMGUI_IMPL_OPENGL_ES2) 187 | if (glsl_version == NULL) 188 | glsl_version = "#version 100"; 189 | #elif defined(IMGUI_IMPL_OPENGL_ES3) 190 | if (glsl_version == NULL) 191 | glsl_version = "#version 300 es"; 192 | #elif defined(__APPLE__) 193 | if (glsl_version == NULL) 194 | glsl_version = "#version 150"; 195 | #else 196 | if (glsl_version == NULL) 197 | glsl_version = "#version 130"; 198 | #endif 199 | IM_ASSERT((int)strlen(glsl_version) + 2 < IM_ARRAYSIZE(g_GlslVersionString)); 200 | strcpy(g_GlslVersionString, glsl_version); 201 | strcat(g_GlslVersionString, "\n"); 202 | 203 | // Debugging construct to make it easily visible in the IDE and debugger which GL loader has been selected. 204 | // The code actually never uses the 'gl_loader' variable! It is only here so you can read it! 205 | // If auto-detection fails or doesn't select the same GL loader file as used by your application, 206 | // you are likely to get a crash below. 207 | // You can explicitly select a loader by using '#define IMGUI_IMPL_OPENGL_LOADER_XXX' in imconfig.h or compiler command-line. 208 | const char* gl_loader = "Unknown"; 209 | IM_UNUSED(gl_loader); 210 | #if defined(IMGUI_IMPL_OPENGL_LOADER_GL3W) 211 | gl_loader = "GL3W"; 212 | #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLEW) 213 | gl_loader = "GLEW"; 214 | #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLAD) 215 | gl_loader = "GLAD"; 216 | #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLAD2) 217 | gl_loader = "GLAD2"; 218 | #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLBINDING2) 219 | gl_loader = "glbinding2"; 220 | #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLBINDING3) 221 | gl_loader = "glbinding3"; 222 | #elif defined(IMGUI_IMPL_OPENGL_LOADER_CUSTOM) 223 | gl_loader = "custom"; 224 | #else 225 | gl_loader = "none"; 226 | #endif 227 | 228 | // Make an arbitrary GL call (we don't actually need the result) 229 | // IF YOU GET A CRASH HERE: it probably means that you haven't initialized the OpenGL function loader used by this code. 230 | // Desktop OpenGL 3/4 need a function loader. See the IMGUI_IMPL_OPENGL_LOADER_xxx explanation above. 231 | GLint current_texture; 232 | glGetIntegerv(GL_TEXTURE_BINDING_2D, ¤t_texture); 233 | 234 | return true; 235 | } 236 | 237 | void ImGui_ImplOpenGL3_Shutdown() 238 | { 239 | ImGui_ImplOpenGL3_DestroyDeviceObjects(); 240 | } 241 | 242 | void ImGui_ImplOpenGL3_NewFrame() 243 | { 244 | if (!g_ShaderHandle) 245 | ImGui_ImplOpenGL3_CreateDeviceObjects(); 246 | } 247 | 248 | static void ImGui_ImplOpenGL3_SetupRenderState(ImDrawData* draw_data, int fb_width, int fb_height, GLuint vertex_array_object) 249 | { 250 | // Setup render state: alpha-blending enabled, no face culling, no depth testing, scissor enabled, polygon fill 251 | glEnable(GL_BLEND); 252 | glBlendEquation(GL_FUNC_ADD); 253 | glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA); 254 | glDisable(GL_CULL_FACE); 255 | glDisable(GL_DEPTH_TEST); 256 | glDisable(GL_STENCIL_TEST); 257 | glEnable(GL_SCISSOR_TEST); 258 | #ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_PRIMITIVE_RESTART 259 | if (g_GlVersion >= 310) 260 | glDisable(GL_PRIMITIVE_RESTART); 261 | #endif 262 | #ifdef GL_POLYGON_MODE 263 | glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); 264 | #endif 265 | 266 | // Support for GL 4.5 rarely used glClipControl(GL_UPPER_LEFT) 267 | #if defined(GL_CLIP_ORIGIN) 268 | bool clip_origin_lower_left = true; 269 | if (g_GlVersion >= 450) 270 | { 271 | GLenum current_clip_origin = 0; glGetIntegerv(GL_CLIP_ORIGIN, (GLint*)¤t_clip_origin); 272 | if (current_clip_origin == GL_UPPER_LEFT) 273 | clip_origin_lower_left = false; 274 | } 275 | #endif 276 | 277 | // Setup viewport, orthographic projection matrix 278 | // Our visible imgui space lies from draw_data->DisplayPos (top left) to draw_data->DisplayPos+data_data->DisplaySize (bottom right). DisplayPos is (0,0) for single viewport apps. 279 | glViewport(0, 0, (GLsizei)fb_width, (GLsizei)fb_height); 280 | float L = draw_data->DisplayPos.x; 281 | float R = draw_data->DisplayPos.x + draw_data->DisplaySize.x; 282 | float T = draw_data->DisplayPos.y; 283 | float B = draw_data->DisplayPos.y + draw_data->DisplaySize.y; 284 | #if defined(GL_CLIP_ORIGIN) 285 | if (!clip_origin_lower_left) { float tmp = T; T = B; B = tmp; } // Swap top and bottom if origin is upper left 286 | #endif 287 | const float ortho_projection[4][4] = 288 | { 289 | { 2.0f/(R-L), 0.0f, 0.0f, 0.0f }, 290 | { 0.0f, 2.0f/(T-B), 0.0f, 0.0f }, 291 | { 0.0f, 0.0f, -1.0f, 0.0f }, 292 | { (R+L)/(L-R), (T+B)/(B-T), 0.0f, 1.0f }, 293 | }; 294 | glUseProgram(g_ShaderHandle); 295 | glUniform1i(g_AttribLocationTex, 0); 296 | glUniformMatrix4fv(g_AttribLocationProjMtx, 1, GL_FALSE, &ortho_projection[0][0]); 297 | 298 | #ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_BIND_SAMPLER 299 | if (g_GlVersion >= 330) 300 | glBindSampler(0, 0); // We use combined texture/sampler state. Applications using GL 3.3 may set that otherwise. 301 | #endif 302 | 303 | (void)vertex_array_object; 304 | #ifndef IMGUI_IMPL_OPENGL_ES2 305 | glBindVertexArray(vertex_array_object); 306 | #endif 307 | 308 | // Bind vertex/index buffers and setup attributes for ImDrawVert 309 | glBindBuffer(GL_ARRAY_BUFFER, g_VboHandle); 310 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_ElementsHandle); 311 | glEnableVertexAttribArray(g_AttribLocationVtxPos); 312 | glEnableVertexAttribArray(g_AttribLocationVtxUV); 313 | glEnableVertexAttribArray(g_AttribLocationVtxColor); 314 | glVertexAttribPointer(g_AttribLocationVtxPos, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (GLvoid*)IM_OFFSETOF(ImDrawVert, pos)); 315 | glVertexAttribPointer(g_AttribLocationVtxUV, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (GLvoid*)IM_OFFSETOF(ImDrawVert, uv)); 316 | glVertexAttribPointer(g_AttribLocationVtxColor, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(ImDrawVert), (GLvoid*)IM_OFFSETOF(ImDrawVert, col)); 317 | } 318 | 319 | // OpenGL3 Render function. 320 | // Note that this implementation is little overcomplicated because we are saving/setting up/restoring every OpenGL state explicitly. 321 | // This is in order to be able to run within an OpenGL engine that doesn't do so. 322 | void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data) 323 | { 324 | // Avoid rendering when minimized, scale coordinates for retina displays (screen coordinates != framebuffer coordinates) 325 | int fb_width = (int)(draw_data->DisplaySize.x * draw_data->FramebufferScale.x); 326 | int fb_height = (int)(draw_data->DisplaySize.y * draw_data->FramebufferScale.y); 327 | if (fb_width <= 0 || fb_height <= 0) 328 | return; 329 | 330 | // Backup GL state 331 | GLenum last_active_texture; glGetIntegerv(GL_ACTIVE_TEXTURE, (GLint*)&last_active_texture); 332 | glActiveTexture(GL_TEXTURE0); 333 | GLuint last_program; glGetIntegerv(GL_CURRENT_PROGRAM, (GLint*)&last_program); 334 | GLuint last_texture; glGetIntegerv(GL_TEXTURE_BINDING_2D, (GLint*)&last_texture); 335 | #ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_BIND_SAMPLER 336 | GLuint last_sampler; if (g_GlVersion >= 330) { glGetIntegerv(GL_SAMPLER_BINDING, (GLint*)&last_sampler); } else { last_sampler = 0; } 337 | #endif 338 | GLuint last_array_buffer; glGetIntegerv(GL_ARRAY_BUFFER_BINDING, (GLint*)&last_array_buffer); 339 | #ifndef IMGUI_IMPL_OPENGL_ES2 340 | GLuint last_vertex_array_object; glGetIntegerv(GL_VERTEX_ARRAY_BINDING, (GLint*)&last_vertex_array_object); 341 | #endif 342 | #ifdef GL_POLYGON_MODE 343 | GLint last_polygon_mode[2]; glGetIntegerv(GL_POLYGON_MODE, last_polygon_mode); 344 | #endif 345 | GLint last_viewport[4]; glGetIntegerv(GL_VIEWPORT, last_viewport); 346 | GLint last_scissor_box[4]; glGetIntegerv(GL_SCISSOR_BOX, last_scissor_box); 347 | GLenum last_blend_src_rgb; glGetIntegerv(GL_BLEND_SRC_RGB, (GLint*)&last_blend_src_rgb); 348 | GLenum last_blend_dst_rgb; glGetIntegerv(GL_BLEND_DST_RGB, (GLint*)&last_blend_dst_rgb); 349 | GLenum last_blend_src_alpha; glGetIntegerv(GL_BLEND_SRC_ALPHA, (GLint*)&last_blend_src_alpha); 350 | GLenum last_blend_dst_alpha; glGetIntegerv(GL_BLEND_DST_ALPHA, (GLint*)&last_blend_dst_alpha); 351 | GLenum last_blend_equation_rgb; glGetIntegerv(GL_BLEND_EQUATION_RGB, (GLint*)&last_blend_equation_rgb); 352 | GLenum last_blend_equation_alpha; glGetIntegerv(GL_BLEND_EQUATION_ALPHA, (GLint*)&last_blend_equation_alpha); 353 | GLboolean last_enable_blend = glIsEnabled(GL_BLEND); 354 | GLboolean last_enable_cull_face = glIsEnabled(GL_CULL_FACE); 355 | GLboolean last_enable_depth_test = glIsEnabled(GL_DEPTH_TEST); 356 | GLboolean last_enable_stencil_test = glIsEnabled(GL_STENCIL_TEST); 357 | GLboolean last_enable_scissor_test = glIsEnabled(GL_SCISSOR_TEST); 358 | #ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_PRIMITIVE_RESTART 359 | GLboolean last_enable_primitive_restart = (g_GlVersion >= 310) ? glIsEnabled(GL_PRIMITIVE_RESTART) : GL_FALSE; 360 | #endif 361 | 362 | // Setup desired GL state 363 | // Recreate the VAO every time (this is to easily allow multiple GL contexts to be rendered to. VAO are not shared among GL contexts) 364 | // The renderer would actually work without any VAO bound, but then our VertexAttrib calls would overwrite the default one currently bound. 365 | GLuint vertex_array_object = 0; 366 | #ifndef IMGUI_IMPL_OPENGL_ES2 367 | glGenVertexArrays(1, &vertex_array_object); 368 | #endif 369 | ImGui_ImplOpenGL3_SetupRenderState(draw_data, fb_width, fb_height, vertex_array_object); 370 | 371 | // Will project scissor/clipping rectangles into framebuffer space 372 | ImVec2 clip_off = draw_data->DisplayPos; // (0,0) unless using multi-viewports 373 | ImVec2 clip_scale = draw_data->FramebufferScale; // (1,1) unless using retina display which are often (2,2) 374 | 375 | // Render command lists 376 | for (int n = 0; n < draw_data->CmdListsCount; n++) 377 | { 378 | const ImDrawList* cmd_list = draw_data->CmdLists[n]; 379 | 380 | // Upload vertex/index buffers 381 | glBufferData(GL_ARRAY_BUFFER, (GLsizeiptr)cmd_list->VtxBuffer.Size * (int)sizeof(ImDrawVert), (const GLvoid*)cmd_list->VtxBuffer.Data, GL_STREAM_DRAW); 382 | glBufferData(GL_ELEMENT_ARRAY_BUFFER, (GLsizeiptr)cmd_list->IdxBuffer.Size * (int)sizeof(ImDrawIdx), (const GLvoid*)cmd_list->IdxBuffer.Data, GL_STREAM_DRAW); 383 | 384 | for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++) 385 | { 386 | const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i]; 387 | if (pcmd->UserCallback != NULL) 388 | { 389 | // User callback, registered via ImDrawList::AddCallback() 390 | // (ImDrawCallback_ResetRenderState is a special callback value used by the user to request the renderer to reset render state.) 391 | if (pcmd->UserCallback == ImDrawCallback_ResetRenderState) 392 | ImGui_ImplOpenGL3_SetupRenderState(draw_data, fb_width, fb_height, vertex_array_object); 393 | else 394 | pcmd->UserCallback(cmd_list, pcmd); 395 | } 396 | else 397 | { 398 | // Project scissor/clipping rectangles into framebuffer space 399 | ImVec4 clip_rect; 400 | clip_rect.x = (pcmd->ClipRect.x - clip_off.x) * clip_scale.x; 401 | clip_rect.y = (pcmd->ClipRect.y - clip_off.y) * clip_scale.y; 402 | clip_rect.z = (pcmd->ClipRect.z - clip_off.x) * clip_scale.x; 403 | clip_rect.w = (pcmd->ClipRect.w - clip_off.y) * clip_scale.y; 404 | 405 | if (clip_rect.x < fb_width && clip_rect.y < fb_height && clip_rect.z >= 0.0f && clip_rect.w >= 0.0f) 406 | { 407 | // Apply scissor/clipping rectangle 408 | glScissor((int)clip_rect.x, (int)(fb_height - clip_rect.w), (int)(clip_rect.z - clip_rect.x), (int)(clip_rect.w - clip_rect.y)); 409 | 410 | // Bind texture, Draw 411 | glBindTexture(GL_TEXTURE_2D, (GLuint)(intptr_t)pcmd->GetTexID()); 412 | #ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_VTX_OFFSET 413 | if (g_GlVersion >= 320) 414 | glDrawElementsBaseVertex(GL_TRIANGLES, (GLsizei)pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, (void*)(intptr_t)(pcmd->IdxOffset * sizeof(ImDrawIdx)), (GLint)pcmd->VtxOffset); 415 | else 416 | #endif 417 | glDrawElements(GL_TRIANGLES, (GLsizei)pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, (void*)(intptr_t)(pcmd->IdxOffset * sizeof(ImDrawIdx))); 418 | } 419 | } 420 | } 421 | } 422 | 423 | // Destroy the temporary VAO 424 | #ifndef IMGUI_IMPL_OPENGL_ES2 425 | glDeleteVertexArrays(1, &vertex_array_object); 426 | #endif 427 | 428 | // Restore modified GL state 429 | glUseProgram(last_program); 430 | glBindTexture(GL_TEXTURE_2D, last_texture); 431 | #ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_BIND_SAMPLER 432 | if (g_GlVersion >= 330) 433 | glBindSampler(0, last_sampler); 434 | #endif 435 | glActiveTexture(last_active_texture); 436 | #ifndef IMGUI_IMPL_OPENGL_ES2 437 | glBindVertexArray(last_vertex_array_object); 438 | #endif 439 | glBindBuffer(GL_ARRAY_BUFFER, last_array_buffer); 440 | glBlendEquationSeparate(last_blend_equation_rgb, last_blend_equation_alpha); 441 | glBlendFuncSeparate(last_blend_src_rgb, last_blend_dst_rgb, last_blend_src_alpha, last_blend_dst_alpha); 442 | if (last_enable_blend) glEnable(GL_BLEND); else glDisable(GL_BLEND); 443 | if (last_enable_cull_face) glEnable(GL_CULL_FACE); else glDisable(GL_CULL_FACE); 444 | if (last_enable_depth_test) glEnable(GL_DEPTH_TEST); else glDisable(GL_DEPTH_TEST); 445 | if (last_enable_stencil_test) glEnable(GL_STENCIL_TEST); else glDisable(GL_STENCIL_TEST); 446 | if (last_enable_scissor_test) glEnable(GL_SCISSOR_TEST); else glDisable(GL_SCISSOR_TEST); 447 | #ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_PRIMITIVE_RESTART 448 | if (g_GlVersion >= 310) { if (last_enable_primitive_restart) glEnable(GL_PRIMITIVE_RESTART); else glDisable(GL_PRIMITIVE_RESTART); } 449 | #endif 450 | 451 | #ifdef GL_POLYGON_MODE 452 | glPolygonMode(GL_FRONT_AND_BACK, (GLenum)last_polygon_mode[0]); 453 | #endif 454 | glViewport(last_viewport[0], last_viewport[1], (GLsizei)last_viewport[2], (GLsizei)last_viewport[3]); 455 | glScissor(last_scissor_box[0], last_scissor_box[1], (GLsizei)last_scissor_box[2], (GLsizei)last_scissor_box[3]); 456 | } 457 | 458 | bool ImGui_ImplOpenGL3_CreateFontsTexture() 459 | { 460 | // Build texture atlas 461 | ImGuiIO& io = ImGui::GetIO(); 462 | unsigned char* pixels; 463 | int width, height; 464 | io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height); // Load as RGBA 32-bit (75% of the memory is wasted, but default font is so small) because it is more likely to be compatible with user's existing shaders. If your ImTextureId represent a higher-level concept than just a GL texture id, consider calling GetTexDataAsAlpha8() instead to save on GPU memory. 465 | 466 | // Upload texture to graphics system 467 | GLint last_texture; 468 | glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture); 469 | glGenTextures(1, &g_FontTexture); 470 | glBindTexture(GL_TEXTURE_2D, g_FontTexture); 471 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 472 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 473 | #ifdef GL_UNPACK_ROW_LENGTH 474 | glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); 475 | #endif 476 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels); 477 | 478 | // Store our identifier 479 | io.Fonts->SetTexID((ImTextureID)(intptr_t)g_FontTexture); 480 | 481 | // Restore state 482 | glBindTexture(GL_TEXTURE_2D, last_texture); 483 | 484 | return true; 485 | } 486 | 487 | void ImGui_ImplOpenGL3_DestroyFontsTexture() 488 | { 489 | if (g_FontTexture) 490 | { 491 | ImGuiIO& io = ImGui::GetIO(); 492 | glDeleteTextures(1, &g_FontTexture); 493 | io.Fonts->SetTexID(0); 494 | g_FontTexture = 0; 495 | } 496 | } 497 | 498 | // If you get an error please report on github. You may try different GL context version or GLSL version. See GL<>GLSL version table at the top of this file. 499 | static bool CheckShader(GLuint handle, const char* desc) 500 | { 501 | GLint status = 0, log_length = 0; 502 | glGetShaderiv(handle, GL_COMPILE_STATUS, &status); 503 | glGetShaderiv(handle, GL_INFO_LOG_LENGTH, &log_length); 504 | if ((GLboolean)status == GL_FALSE) 505 | fprintf(stderr, "ERROR: ImGui_ImplOpenGL3_CreateDeviceObjects: failed to compile %s!\n", desc); 506 | if (log_length > 1) 507 | { 508 | ImVector buf; 509 | buf.resize((int)(log_length + 1)); 510 | glGetShaderInfoLog(handle, log_length, NULL, (GLchar*)buf.begin()); 511 | fprintf(stderr, "%s\n", buf.begin()); 512 | } 513 | return (GLboolean)status == GL_TRUE; 514 | } 515 | 516 | // If you get an error please report on GitHub. You may try different GL context version or GLSL version. 517 | static bool CheckProgram(GLuint handle, const char* desc) 518 | { 519 | GLint status = 0, log_length = 0; 520 | glGetProgramiv(handle, GL_LINK_STATUS, &status); 521 | glGetProgramiv(handle, GL_INFO_LOG_LENGTH, &log_length); 522 | if ((GLboolean)status == GL_FALSE) 523 | fprintf(stderr, "ERROR: ImGui_ImplOpenGL3_CreateDeviceObjects: failed to link %s! (with GLSL '%s')\n", desc, g_GlslVersionString); 524 | if (log_length > 1) 525 | { 526 | ImVector buf; 527 | buf.resize((int)(log_length + 1)); 528 | glGetProgramInfoLog(handle, log_length, NULL, (GLchar*)buf.begin()); 529 | fprintf(stderr, "%s\n", buf.begin()); 530 | } 531 | return (GLboolean)status == GL_TRUE; 532 | } 533 | 534 | bool ImGui_ImplOpenGL3_CreateDeviceObjects() 535 | { 536 | // Backup GL state 537 | GLint last_texture, last_array_buffer; 538 | glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture); 539 | glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &last_array_buffer); 540 | #ifndef IMGUI_IMPL_OPENGL_ES2 541 | GLint last_vertex_array; 542 | glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &last_vertex_array); 543 | #endif 544 | 545 | // Parse GLSL version string 546 | int glsl_version = 130; 547 | sscanf(g_GlslVersionString, "#version %d", &glsl_version); 548 | 549 | const GLchar* vertex_shader_glsl_120 = 550 | "uniform mat4 ProjMtx;\n" 551 | "attribute vec2 Position;\n" 552 | "attribute vec2 UV;\n" 553 | "attribute vec4 Color;\n" 554 | "varying vec2 Frag_UV;\n" 555 | "varying vec4 Frag_Color;\n" 556 | "void main()\n" 557 | "{\n" 558 | " Frag_UV = UV;\n" 559 | " Frag_Color = Color;\n" 560 | " gl_Position = ProjMtx * vec4(Position.xy,0,1);\n" 561 | "}\n"; 562 | 563 | const GLchar* vertex_shader_glsl_130 = 564 | "uniform mat4 ProjMtx;\n" 565 | "in vec2 Position;\n" 566 | "in vec2 UV;\n" 567 | "in vec4 Color;\n" 568 | "out vec2 Frag_UV;\n" 569 | "out vec4 Frag_Color;\n" 570 | "void main()\n" 571 | "{\n" 572 | " Frag_UV = UV;\n" 573 | " Frag_Color = Color;\n" 574 | " gl_Position = ProjMtx * vec4(Position.xy,0,1);\n" 575 | "}\n"; 576 | 577 | const GLchar* vertex_shader_glsl_300_es = 578 | "precision mediump float;\n" 579 | "layout (location = 0) in vec2 Position;\n" 580 | "layout (location = 1) in vec2 UV;\n" 581 | "layout (location = 2) in vec4 Color;\n" 582 | "uniform mat4 ProjMtx;\n" 583 | "out vec2 Frag_UV;\n" 584 | "out vec4 Frag_Color;\n" 585 | "void main()\n" 586 | "{\n" 587 | " Frag_UV = UV;\n" 588 | " Frag_Color = Color;\n" 589 | " gl_Position = ProjMtx * vec4(Position.xy,0,1);\n" 590 | "}\n"; 591 | 592 | const GLchar* vertex_shader_glsl_410_core = 593 | "layout (location = 0) in vec2 Position;\n" 594 | "layout (location = 1) in vec2 UV;\n" 595 | "layout (location = 2) in vec4 Color;\n" 596 | "uniform mat4 ProjMtx;\n" 597 | "out vec2 Frag_UV;\n" 598 | "out vec4 Frag_Color;\n" 599 | "void main()\n" 600 | "{\n" 601 | " Frag_UV = UV;\n" 602 | " Frag_Color = Color;\n" 603 | " gl_Position = ProjMtx * vec4(Position.xy,0,1);\n" 604 | "}\n"; 605 | 606 | const GLchar* fragment_shader_glsl_120 = 607 | "#ifdef GL_ES\n" 608 | " precision mediump float;\n" 609 | "#endif\n" 610 | "uniform sampler2D Texture;\n" 611 | "varying vec2 Frag_UV;\n" 612 | "varying vec4 Frag_Color;\n" 613 | "void main()\n" 614 | "{\n" 615 | " gl_FragColor = Frag_Color * texture2D(Texture, Frag_UV.st);\n" 616 | "}\n"; 617 | 618 | const GLchar* fragment_shader_glsl_130 = 619 | "uniform sampler2D Texture;\n" 620 | "in vec2 Frag_UV;\n" 621 | "in vec4 Frag_Color;\n" 622 | "out vec4 Out_Color;\n" 623 | "void main()\n" 624 | "{\n" 625 | " Out_Color = Frag_Color * texture(Texture, Frag_UV.st);\n" 626 | "}\n"; 627 | 628 | const GLchar* fragment_shader_glsl_300_es = 629 | "precision mediump float;\n" 630 | "uniform sampler2D Texture;\n" 631 | "in vec2 Frag_UV;\n" 632 | "in vec4 Frag_Color;\n" 633 | "layout (location = 0) out vec4 Out_Color;\n" 634 | "void main()\n" 635 | "{\n" 636 | " Out_Color = Frag_Color * texture(Texture, Frag_UV.st);\n" 637 | "}\n"; 638 | 639 | const GLchar* fragment_shader_glsl_410_core = 640 | "in vec2 Frag_UV;\n" 641 | "in vec4 Frag_Color;\n" 642 | "uniform sampler2D Texture;\n" 643 | "layout (location = 0) out vec4 Out_Color;\n" 644 | "void main()\n" 645 | "{\n" 646 | " Out_Color = Frag_Color * texture(Texture, Frag_UV.st);\n" 647 | "}\n"; 648 | 649 | // Select shaders matching our GLSL versions 650 | const GLchar* vertex_shader = NULL; 651 | const GLchar* fragment_shader = NULL; 652 | if (glsl_version < 130) 653 | { 654 | vertex_shader = vertex_shader_glsl_120; 655 | fragment_shader = fragment_shader_glsl_120; 656 | } 657 | else if (glsl_version >= 410) 658 | { 659 | vertex_shader = vertex_shader_glsl_410_core; 660 | fragment_shader = fragment_shader_glsl_410_core; 661 | } 662 | else if (glsl_version == 300) 663 | { 664 | vertex_shader = vertex_shader_glsl_300_es; 665 | fragment_shader = fragment_shader_glsl_300_es; 666 | } 667 | else 668 | { 669 | vertex_shader = vertex_shader_glsl_130; 670 | fragment_shader = fragment_shader_glsl_130; 671 | } 672 | 673 | // Create shaders 674 | const GLchar* vertex_shader_with_version[2] = { g_GlslVersionString, vertex_shader }; 675 | g_VertHandle = glCreateShader(GL_VERTEX_SHADER); 676 | glShaderSource(g_VertHandle, 2, vertex_shader_with_version, NULL); 677 | glCompileShader(g_VertHandle); 678 | CheckShader(g_VertHandle, "vertex shader"); 679 | 680 | const GLchar* fragment_shader_with_version[2] = { g_GlslVersionString, fragment_shader }; 681 | g_FragHandle = glCreateShader(GL_FRAGMENT_SHADER); 682 | glShaderSource(g_FragHandle, 2, fragment_shader_with_version, NULL); 683 | glCompileShader(g_FragHandle); 684 | CheckShader(g_FragHandle, "fragment shader"); 685 | 686 | g_ShaderHandle = glCreateProgram(); 687 | glAttachShader(g_ShaderHandle, g_VertHandle); 688 | glAttachShader(g_ShaderHandle, g_FragHandle); 689 | glLinkProgram(g_ShaderHandle); 690 | CheckProgram(g_ShaderHandle, "shader program"); 691 | 692 | g_AttribLocationTex = glGetUniformLocation(g_ShaderHandle, "Texture"); 693 | g_AttribLocationProjMtx = glGetUniformLocation(g_ShaderHandle, "ProjMtx"); 694 | g_AttribLocationVtxPos = (GLuint)glGetAttribLocation(g_ShaderHandle, "Position"); 695 | g_AttribLocationVtxUV = (GLuint)glGetAttribLocation(g_ShaderHandle, "UV"); 696 | g_AttribLocationVtxColor = (GLuint)glGetAttribLocation(g_ShaderHandle, "Color"); 697 | 698 | // Create buffers 699 | glGenBuffers(1, &g_VboHandle); 700 | glGenBuffers(1, &g_ElementsHandle); 701 | 702 | ImGui_ImplOpenGL3_CreateFontsTexture(); 703 | 704 | // Restore modified GL state 705 | glBindTexture(GL_TEXTURE_2D, last_texture); 706 | glBindBuffer(GL_ARRAY_BUFFER, last_array_buffer); 707 | #ifndef IMGUI_IMPL_OPENGL_ES2 708 | glBindVertexArray(last_vertex_array); 709 | #endif 710 | 711 | return true; 712 | } 713 | 714 | void ImGui_ImplOpenGL3_DestroyDeviceObjects() 715 | { 716 | if (g_VboHandle) { glDeleteBuffers(1, &g_VboHandle); g_VboHandle = 0; } 717 | if (g_ElementsHandle) { glDeleteBuffers(1, &g_ElementsHandle); g_ElementsHandle = 0; } 718 | if (g_ShaderHandle && g_VertHandle) { glDetachShader(g_ShaderHandle, g_VertHandle); } 719 | if (g_ShaderHandle && g_FragHandle) { glDetachShader(g_ShaderHandle, g_FragHandle); } 720 | if (g_VertHandle) { glDeleteShader(g_VertHandle); g_VertHandle = 0; } 721 | if (g_FragHandle) { glDeleteShader(g_FragHandle); g_FragHandle = 0; } 722 | if (g_ShaderHandle) { glDeleteProgram(g_ShaderHandle); g_ShaderHandle = 0; } 723 | 724 | ImGui_ImplOpenGL3_DestroyFontsTexture(); 725 | } 726 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | --------------------------------------------------------------------------------