Skip to content

Commit

Permalink
added README + added view menu
Browse files Browse the repository at this point in the history
  • Loading branch information
EddieBreeg committed Jan 12, 2023
1 parent 88efa9c commit 18c6d93
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 21 deletions.
66 changes: 66 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# GLSL Explorer

Simple OpenGL based GLSL shader visualizer.

## What does it do?

GLSL Explorer simply allows you to import any GLSL fragment shader from a file,
and visualize it on a 2D plane. It supports viewport navigation with panning and zooming,
custom uniforms, multiple render passes and framebuffer export.

## How do I build it?

The project is self contained, all dependencies (glew, glfw, ImGui and stb_image) are directly integrated into it. Clone this repo anywhere you like, then open the folder in your favorite terminal and do:
```
cmake -S . -B build
cmake --build ./build
```

## How do I use it?

### Writing a shader

All you technically need is to write the main function for your fragment shader, like this:
```glsl
void main(){
frag0 = vec4(1);
}
```

The fragment shader has 8 outputs, frag0 to frag7. It also provides you with the following inputs:
```glsl
in vec4 pos; // the fragment position
in vec2 uv; // normalized screen space coordinates
uniform float scale; // the viewport scale parameter
uniform vec4 offset; // the viewport position parameter
uniform float time; // runtime
uniform vec2 res; // dimensions of the screen, in pixels
uniform sampler2D texNoise; // a white noise texture
```

To add your own inputs, simply add a uniform in your shader file, it will automatically appear in the UI,
in the "Shader parameters" section.
Only the following data types are supported: float, vec2, vec3, vec4, int, ivec2, ivec3, ivec4, bool,
bvec2, bvec3, bvec4.

### General settings

In this section your can find the general setup of the program:
- **Shader file**: the path to the GLSL file to load
- **Scale**: The global viewport scale level (decreasing the value "zooms in")
- **Position**: 4D offset to pan around
- **Mouse sensitivity**: Scale and position adjustment sensitivity
- **Slider sensitivity**: How sensitive should the sliders be for the custom shader parameters
- **Render pass**: Selects the output texture currently being displayed on screen

### Navigation

To pan the view, simply click and drag. To zoom in and out, hold shift and click and drag to the right/left.

### Screenshots

The "Save screenshot" button exports the contents of the current frame to file named "out.png" in the current working directory.

## Going further

The [examples](examples) folder contains a few premade shaders.
2 changes: 1 addition & 1 deletion examples/blank.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ void main(){
frag4 = vec4(1, 0, 0, 1);
frag5 = vec4(1, 0, 1, 1);
frag6 = vec4(1, 1, 0, 1);
frag7 = vec4(1, 1, 1, 1);
frag7 = vec4(1);
}
3 changes: 2 additions & 1 deletion examples/mandelbrot.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ vec4 palette[16] = {
};

void main(){
vec2 C = pos.xy;
vec2 C = pos.xy * scale;
C.x *= res.x/res.y;
C += offset.xy;
vec2 Z = vec2(0);
int i = 0;
while(i++ < maxIter && dot(Z, Z) <= 4){
Expand Down
7 changes: 0 additions & 7 deletions examples/palette.py

This file was deleted.

32 changes: 20 additions & 12 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class GLExplorer: public GLBase::Application
GLBase::Texture _textures[8];
GLBase::Texture _noise;


int _selectedRenderPass = 0;
static constexpr const char *_renderPasses[] = {
"Pass 0",
Expand All @@ -40,6 +39,7 @@ class GLExplorer: public GLBase::Application
"Pass 7",
};

bool _showInspector = true;
bool _vsync = 0;
std::chrono::steady_clock::time_point _start;
float _scale = 1.0f;
Expand Down Expand Up @@ -229,7 +229,26 @@ class GLExplorer: public GLBase::Application
ImGui::Text("Couldn't save frame: %s", strerror(errno));
ImGui::End();
}
if(ImGui::BeginMainMenuBar()){
if(ImGui::BeginMenu("View")){
ImGui::MenuItem("Show inspector", "Ctrl+1", &_showInspector);
ImGui::EndMenu();
}
ImGui::EndMainMenuBar();
}
if(_glslStatus.errStatus){
ImGui::Begin("GLSL Error");
ImGui::TextColored(ImVec4{1, 0, 0, 1}, "%s", _glslStatus.message.c_str());
ImGui::End();
}
if(_fileErrorPopup){
ImGui::Begin("Error", &_fileErrorPopup);
ImGui::TextColored(ImVec4{1, 0, 0, 1}, "Couldn't load %s: %s",
_filePath.c_str(), strerror(errno));
ImGui::End();
}

if(!_showInspector) return;
ImGui::Begin("Inspector");
ImGui::Text("%f FPS", ImGui::GetIO().Framerate);
if(ImGui::TreeNode("General settings")){
Expand Down Expand Up @@ -264,17 +283,6 @@ class GLExplorer: public GLBase::Application

_saveFrame = ImGui::Button("Save screenshot");
ImGui::End();
if(_glslStatus.errStatus){
ImGui::Begin("GLSL Error");
ImGui::TextColored(ImVec4{1, 0, 0, 1}, "%s", _glslStatus.message.c_str());
ImGui::End();
}
if(_fileErrorPopup){
ImGui::Begin("Error", &_fileErrorPopup);
ImGui::TextColored(ImVec4{1, 0, 0, 1}, "Couldn't load %s: %s",
_filePath.c_str(), strerror(errno));
ImGui::End();
}
}
bool loadFragmentShaderFromFile(const std::string& path) {
std::ifstream f(path, std::ios_base::binary);
Expand Down
Binary file added out.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 18c6d93

Please sign in to comment.