Home | History | Annotate | Download | only in app
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef SHADER_PROJECTED_TEX_H
     18 #define SHADER_PROJECTED_TEX_H
     19 
     20 // This shader is used to project a sensors image onto wold space geometry
     21 // as if it were projected from the original sensor's point of view in the world.
     22 
     23 const char vtxShader_projectedTexture[] = ""
     24         "#version 300 es                            \n"
     25         "layout(location = 0) in vec4 pos;          \n"
     26         "uniform mat4 cameraMat;                    \n"
     27         "uniform mat4 projectionMat;                \n"
     28         "out vec4 projectionSpace;                  \n"
     29         "void main()                                \n"
     30         "{                                          \n"
     31         "   gl_Position = cameraMat * pos;          \n"
     32         "   projectionSpace = projectionMat * pos;  \n"
     33         "}                                          \n";
     34 
     35 const char pixShader_projectedTexture[] =
     36         "#version 300 es                                        \n"
     37         "precision mediump float;                               \n"
     38         "uniform sampler2D tex;                                 \n"
     39         "in vec4 projectionSpace;                               \n"
     40         "out vec4 color;                                        \n"
     41         "void main()                                            \n"
     42         "{                                                      \n"
     43         "    const vec2 zero = vec2(0.0f, 0.0f);                \n"
     44         "    const vec2 one  = vec2(1.0f, 1.0f);                \n"
     45         "                                                       \n"
     46         "    // Compute perspective correct texture coordinates \n"
     47         "    // in the sensor map                               \n"
     48         "    vec2 cs = projectionSpace.xy / projectionSpace.w;  \n"
     49         "                                                       \n"
     50         "    // flip the texture!                               \n"
     51         "    cs.y = -cs.y;                                      \n"
     52         "                                                       \n"
     53         "    // scale from -1/1 clip space to 0/1 uv space      \n"
     54         "    vec2 uv = (cs + 1.0f) * 0.5f;                      \n"
     55         "                                                       \n"
     56         "    // Bail if we don't have a valid projection        \n"
     57         "    if ((projectionSpace.w <= 0.0f) ||                 \n"
     58         "        any(greaterThan(uv, one)) ||                   \n"
     59         "        any(lessThan(uv, zero))) {                     \n"
     60         "        discard;                                       \n"
     61         "    }                                                  \n"
     62         "    color = texture(tex, uv);                          \n"
     63         "}                                                      \n";
     64 
     65 #endif // SHADER_PROJECTED_TEX_H