Bladeren bron

slop no longer needs files installed to function, it can still load custom shaders from ~/.config/slop though

naelstrof 7 jaren geleden
bovenliggende
commit
5775c18e1c
13 gewijzigde bestanden met toevoegingen van 56 en 102 verwijderingen
  1. 4
    15
      CMakeLists.txt
  2. 0
    9
      share/slop/simple.frag
  3. 0
    9
      share/slop/simple.vert
  4. 0
    13
      share/slop/textured.frag
  5. 0
    12
      share/slop/textured.vert
  6. 3
    1
      src/framebuffer.cpp
  7. 1
    0
      src/main.cpp
  8. 4
    1
      src/rectangle.cpp
  9. 1
    9
      src/resource.cpp
  10. 0
    1
      src/resource.hpp
  11. 35
    28
      src/shader.cpp
  12. 5
    3
      src/shader.hpp
  13. 3
    1
      src/slop.cpp

+ 4
- 15
CMakeLists.txt Bestand weergeven

@@ -1,21 +1,13 @@
1 1
 #Change this if you need to target a specific CMake version
2 2
 cmake_minimum_required(VERSION 3.1)
3 3
 
4
-add_definitions(-DSLOP_VERSION="v5.3.21")
5
-
6
-if(NOT CMAKE_BUILD_TYPE)
7
-  set(CMAKE_BUILD_TYPE Debug CACHE STRING "Choose the type of build (Debug or Release)" FORCE)
8
-endif()
9
-
10
-if( NOT CMAKE_INSTALL_PREFIX )
11
-    set( CMAKE_INSTALL_PREFIX "/usr" )
12
-endif()
13
-
14
-if( NOT SHADER_PREFIX )
15
-    set( SHADER_PREFIX "${CMAKE_INSTALL_PREFIX}" CACHE PATH "The path where slop thinks it resides in, so that it can find the shaders. This is not necessarily the CMAKE_INSTALL_PREFIX. For example if the shaders will exist in /usr/share/slop, SHADER_PREFIX should be \"/usr\"." )
4
+set(CMAKE_BUILD_TYPE Debug CACHE STRING "Choose the type of build (Debug or Release)")
5
+if ( NOT CMAKE_INSTALL_PREFIX )
6
+    set(CMAKE_INSTALL_PREFIX "/usr")
16 7
 endif()
17 8
 
18 9
 project(slop)
10
+add_definitions(-DSLOP_VERSION="v5.3.21")
19 11
 
20 12
 set(CMAKE_CXX_STANDARD 14)
21 13
 set(CMAKE_CXX_STANDARD_REQUIRED on)
@@ -66,6 +58,3 @@ target_link_libraries(${EXECUTABLE_NAME} ${X11_LIBRARIES}
66 58
 
67 59
 # Install targets
68 60
 install( TARGETS ${EXECUTABLE_NAME} DESTINATION "${CMAKE_INSTALL_PREFIX}/bin" )
69
-install( DIRECTORY "${CMAKE_SOURCE_DIR}/share" DESTINATION "${CMAKE_INSTALL_PREFIX}" )
70
-
71
-add_definitions(-DSHADER_PREFIX="${SHADER_PREFIX}")

+ 0
- 9
share/slop/simple.frag Bestand weergeven

@@ -1,9 +0,0 @@
1
-#version 130
2
-
3
-uniform vec4 color;
4
-out vec4 outColor;
5
-
6
-void main()
7
-{
8
-    outColor = color;
9
-}

+ 0
- 9
share/slop/simple.vert Bestand weergeven

@@ -1,9 +0,0 @@
1
-#version 130
2
-
3
-in vec2 position;
4
-uniform mat4 projection;
5
-
6
-void main()
7
-{
8
-    gl_Position = projection*vec4(position,0,1);
9
-}

+ 0
- 13
share/slop/textured.frag Bestand weergeven

@@ -1,13 +0,0 @@
1
-#version 130
2
-
3
-uniform sampler2D texture;
4
-
5
-varying vec2 uvCoord;
6
-
7
-out vec4 outColor;
8
-
9
-void main()
10
-{
11
-    outColor = texture2D( texture, uvCoord );
12
-}
13
-

+ 0
- 12
share/slop/textured.vert Bestand weergeven

@@ -1,12 +0,0 @@
1
-#version 130
2
-
3
-attribute vec2 position;
4
-attribute vec2 uv;
5
-
6
-varying vec2 uvCoord;
7
-
8
-void main()
9
-{
10
-    uvCoord = uv;
11
-    gl_Position = vec4(position,0,1);
12
-}

+ 3
- 1
src/framebuffer.cpp Bestand weergeven

@@ -1,7 +1,9 @@
1 1
 #include "framebuffer.hpp"
2 2
 
3 3
 Framebuffer::Framebuffer( int w, int h ) {
4
-    shader = new Shader( "textured.vert", "textured.frag" );
4
+    std::string vert = "#version 130\nattribute vec2 position;\nattribute vec2 uv;\nvarying vec2 uvCoord;\nvoid main()\n{\nuvCoord = uv;\ngl_Position = vec4(position,0,1);\n}\n";
5
+    std::string frag = "#version 130\nuniform sampler2D texture;\nvarying vec2 uvCoord;\nout vec4 outColor;void main()\n{\noutColor = texture2D( texture, uvCoord );\n}\n";
6
+    shader = new Shader( vert, frag, false );
5 7
     glGenFramebuffers( 1, &fbuffer );
6 8
     glBindFramebuffer( GL_FRAMEBUFFER, fbuffer );
7 9
     glGenTextures(1, &image);

+ 1
- 0
src/main.cpp Bestand weergeven

@@ -31,6 +31,7 @@ SlopOptions* getOptions( Options& options ) {
31 31
     glm::vec4 color = glm::vec4( foo->r, foo->g, foo->b, foo->a );
32 32
     options.getColor("color", 'c', color);
33 33
     options.getString( "xdisplay", 'x', foo->xdisplay );
34
+    options.getString( "shader", 's', foo->shader );
34 35
     foo->r = color.r;
35 36
     foo->g = color.g;
36 37
     foo->b = color.b;

+ 4
- 1
src/rectangle.cpp Bestand weergeven

@@ -21,7 +21,10 @@ Rectangle::Rectangle( glm::vec2 p1, glm::vec2 p2, float border, float padding, g
21 21
     our = ur + glm::vec2(border,border);
22 22
     obr = br + glm::vec2(border,-border);
23 23
     generateBuffers();
24
-    shader = new Shader( "simple.vert", "simple.frag" );
24
+    std::string vert = "#version 130\nin vec2 position;\nuniform mat4 projection;\nvoid main() {\ngl_Position = projection*vec4(position,0,1);\n }";
25
+    std::string frag = "#version 130\nuniform vec4 color;\nout vec4 outColor;\nvoid main() {\noutColor = color;\n}";
26
+
27
+    shader = new Shader( vert, frag, false );
25 28
 }
26 29
 
27 30
 void Rectangle::setPoints( glm::vec2 p1, glm::vec2 p2 ) {

+ 1
- 9
src/resource.cpp Bestand weergeven

@@ -28,25 +28,17 @@ Resource::Resource() {
28 28
         char* home = getpwuid(getuid())->pw_dir;
29 29
         usrconfig += home;
30 30
         usrconfig += "/.config/slop/";
31
-        // SHADER_PREFIX is defined by the user within CMake, defaults to `/usr`.
32
-        sysconfig = SHADER_PREFIX;
33
-        sysconfig += "/share/slop/";
34 31
         return;
35 32
     }
36 33
     usrconfig += config;
37 34
     usrconfig += "/slop/";
38
-    sysconfig = SHADER_PREFIX;
39
-    sysconfig += "/share/slop/";
40 35
 }
41 36
 
42 37
 std::string Resource::getRealPath( std::string localpath ) {
43 38
     if ( validatePath( usrconfig + localpath ) ) {
44 39
         return usrconfig + localpath;
45 40
     }
46
-    if ( validatePath( sysconfig + localpath ) ) {
47
-        return sysconfig + localpath;
48
-    }
49
-    std::string err = "The file or folder " + localpath + " was not found in either " + sysconfig + " or " + usrconfig + "\n";
41
+    std::string err = "The file or folder " + localpath + " was not found in " + usrconfig + "\n";
50 42
     throw new std::runtime_error(err);
51 43
     return localpath;
52 44
 }

+ 0
- 1
src/resource.hpp Bestand weergeven

@@ -40,7 +40,6 @@ public:
40 40
 private:
41 41
     bool validatePath( std::string path );
42 42
     std::string usrconfig;
43
-    std::string sysconfig;
44 43
 };
45 44
 
46 45
 extern Resource* resource;

+ 35
- 28
src/shader.cpp Bestand weergeven

@@ -1,20 +1,27 @@
1 1
 #include "shader.hpp"
2 2
 
3
-Shader::Shader( std::string vert, std::string frag) {
4
-    vert = resource->getRealPath(vert);
5
-    frag = resource->getRealPath(frag);
6
-    // Create the program to link to.
7
-    m_program = glCreateProgram();
8
-
9
-    std::ifstream v( vert.c_str() );
10
-    std::string vert_contents((std::istreambuf_iterator<char>(v)),
11
-                               std::istreambuf_iterator<char>());
12
-    std::ifstream f( frag.c_str() );
13
-    std::string frag_contents((std::istreambuf_iterator<char>(f)),
14
-                              std::istreambuf_iterator<char>());
3
+Shader::Shader( std::string vert, std::string frag, bool file ) {
4
+    std::string vert_contents;
5
+    std::string frag_contents;
6
+    if ( file ) {
7
+        vert = resource->getRealPath(vert);
8
+        frag = resource->getRealPath(frag);
9
+        std::ifstream v( vert.c_str() );
10
+        vert_contents = std::string((std::istreambuf_iterator<char>(v)),
11
+                                   std::istreambuf_iterator<char>());
12
+        std::ifstream f( frag.c_str() );
13
+        frag_contents = std::string((std::istreambuf_iterator<char>(f)),
14
+                                  std::istreambuf_iterator<char>());
15
+    } else {
16
+        vert_contents = vert;
17
+        frag_contents = frag;
18
+    }
15 19
 
16 20
     const char* vertsrc = vert_contents.c_str();
17 21
     const char* fragsrc = frag_contents.c_str();
22
+
23
+    // Create the program to link to.
24
+    program = glCreateProgram();
18 25
     
19 26
     if ( vert_contents.length() <= 0 ) {
20 27
         std::string errstring = "Failed to open file (or is empty) `" + vert + "`.\n";
@@ -67,15 +74,15 @@ Shader::Shader( std::string vert, std::string frag) {
67 74
 }
68 75
 
69 76
 Shader::~Shader() {
70
-    glDeleteProgram( m_program );
77
+    glDeleteProgram( program );
71 78
 }
72 79
 
73 80
 unsigned int Shader::getProgram() {
74
-    return m_program;
81
+    return program;
75 82
 }
76 83
 
77 84
 void Shader::bind() {
78
-    glUseProgram( m_program );
85
+    glUseProgram( program );
79 86
 }
80 87
 
81 88
 int Shader::compile( unsigned int shader, std::string& error ) {
@@ -97,18 +104,18 @@ int Shader::compile( unsigned int shader, std::string& error ) {
97 104
 }
98 105
 
99 106
 int Shader::link( unsigned int vertshader, unsigned int fragshader, std::string& error ) {
100
-    glAttachShader( m_program, vertshader );
101
-    glAttachShader( m_program, fragshader );
102
-    glLinkProgram( m_program );
107
+    glAttachShader( program, vertshader );
108
+    glAttachShader( program, fragshader );
109
+    glLinkProgram( program );
103 110
 
104 111
     // Linking the shader is the easy part, all this junk down here is for printing the error it might generate.
105 112
     int result = GL_FALSE;
106 113
     int logLength;
107
-    glGetProgramiv( m_program, GL_LINK_STATUS, &result);
108
-    glGetProgramiv( m_program, GL_INFO_LOG_LENGTH, &logLength);
114
+    glGetProgramiv( program, GL_LINK_STATUS, &result);
115
+    glGetProgramiv( program, GL_INFO_LOG_LENGTH, &logLength);
109 116
     if ( result == GL_FALSE ) {
110 117
         char* errormsg = new char[ logLength ];
111
-        glGetProgramInfoLog( m_program, logLength, NULL, errormsg );
118
+        glGetProgramInfoLog( program, logLength, NULL, errormsg );
112 119
         error = errormsg;
113 120
         delete[] errormsg;
114 121
         return 1;
@@ -117,8 +124,8 @@ int Shader::link( unsigned int vertshader, unsigned int fragshader, std::string&
117 124
 }
118 125
 
119 126
 unsigned int Shader::getUniformLocation( std::string name ) {
120
-    glUseProgram( m_program );
121
-    return glGetUniformLocation( m_program, name.c_str() );
127
+    glUseProgram( program );
128
+    return glGetUniformLocation( program, name.c_str() );
122 129
 }
123 130
 
124 131
 void Shader::setParameter( std::string name, int foo ) {
@@ -142,17 +149,17 @@ void Shader::setParameter( std::string name, glm::vec2 foo ) {
142 149
 }
143 150
 
144 151
 void Shader::setAttribute( std::string name, unsigned int buffer, unsigned int stepsize ) {
145
-    unsigned int a = glGetAttribLocation( m_program, name.c_str() );
152
+    unsigned int a = glGetAttribLocation( program, name.c_str() );
146 153
     glEnableVertexAttribArray( a );
147 154
     glBindBuffer( GL_ARRAY_BUFFER, buffer );
148 155
     glVertexAttribPointer( a, stepsize, GL_FLOAT, GL_FALSE, 0, NULL );
149
-    m_activeattribs.push_back( a );
156
+    activeAttributes.push_back( a );
150 157
 }
151 158
 
152 159
 void Shader::unbind() {
153
-    for ( unsigned int i=0; i<m_activeattribs.size(); i++ ) {
154
-        glDisableVertexAttribArray( m_activeattribs[i] );
160
+    for ( unsigned int i=0; i<activeAttributes.size(); i++ ) {
161
+        glDisableVertexAttribArray( activeAttributes[i] );
155 162
     }
156
-    m_activeattribs.clear();
163
+    activeAttributes.clear();
157 164
     glUseProgram( 0 );
158 165
 }

+ 5
- 3
src/shader.hpp Bestand weergeven

@@ -36,7 +36,7 @@
36 36
 
37 37
 class Shader {
38 38
 public:
39
-    Shader( std::string vert, std::string frag );
39
+    Shader( std::string vert, std::string frag, bool file = true );
40 40
     ~Shader();
41 41
     unsigned int    getProgram();
42 42
     void            bind();
@@ -48,11 +48,13 @@ public:
48 48
     void            setParameter( std::string name, glm::vec2 foo );
49 49
     void            setAttribute( std::string name, unsigned int buffer, unsigned int stepsize );
50 50
 private:
51
-    std::vector<unsigned int>   m_activeattribs;
51
+    void loadFromFile( std::string vert, std::string frag );
52
+    void loadFromMemory( std::string vert, std::string frag );
53
+    std::vector<unsigned int>   activeAttributes;
52 54
     unsigned int                getUniformLocation( std::string );
53 55
     int                         compile( unsigned int shader, std::string& error );
54 56
     int                         link( unsigned int vert, unsigned int frag, std::string& error );
55
-    unsigned int                m_program;
57
+    unsigned int                program;
56 58
 };
57 59
 
58 60
 #endif // N_SHADER_H_

+ 3
- 1
src/slop.cpp Bestand weergeven

@@ -84,7 +84,9 @@ SlopSelection SlopSelect( SlopOptions* options, bool* cancelled ) {
84 84
     // Set up window with GL context
85 85
     SlopWindow* window = new SlopWindow();
86 86
 
87
-    window->framebuffer->setShader( options->shader );
87
+    if ( options->shader != "textured" ) {
88
+        window->framebuffer->setShader( options->shader );
89
+    }
88 90
 
89 91
     // Init our little state machine, memory is a tad of a misnomer
90 92
     SlopMemory memory( options );