glsl

Drawing a border on a 2d polygon with a fragment shader

大城市里の小女人 提交于 2019-11-26 19:20:02
问题 I have some simple polygons (fewer than 20 vertices) rendering flat on a simple xy plane, using GL_TRIANGLES and a flat color, a 2d simulation. I would like to add a border of variable thickness and a different color to these polygons. I have something implemented using the same vertices and glLineWidth/GL_LINE_LOOP, which works, but is another rendering pass and repeats all the vertex transforms. I think I should be able to do this in the fragment shader using gl_FragCoord and the vertex

How to change hue of a texture with GLSL?

落花浮王杯 提交于 2019-11-26 19:12:37
问题 Is there a way to efficiently change hue of a 2D OpenGL texture using GLSL (fragment shader)? Do someone have some code for it? UPDATE: This is the code resulting from user1118321 suggestion: uniform sampler2DRect texture; const mat3 rgb2yiq = mat3(0.299, 0.587, 0.114, 0.595716, -0.274453, -0.321263, 0.211456, -0.522591, 0.311135); const mat3 yiq2rgb = mat3(1.0, 0.9563, 0.6210, 1.0, -0.2721, -0.6474, 1.0, -1.1070, 1.7046); uniform float hue; void main() { vec3 yColor = rgb2yiq * texture2DRect

Creating a GLSL Arrays of Uniforms?

*爱你&永不变心* 提交于 2019-11-26 19:05:01
问题 I would like to leave OpenGL's lights and make my own. I would like my shaders to allow for a variable number of lights. Can we declare an array of uniforms in GLSL shaders? If so, how would we set the values of those uniforms? 回答1: Yes this is possible. You declare uniform arrays similar to how you'd do it in C, e.g. uniform float v[10]; Then you can set their values using glUniform{1,2,3,4}{f,i}v GLfloat v[10] = {...}; glUniform1fv(glGetUniformLocation(program, "v"), 10, v); 回答2: Yes it is

Why is the sprite not rendering in OpenGL?

自闭症网瘾萝莉.ら 提交于 2019-11-26 18:34:53
问题 I am trying to render a 2D(Screen coordinated) sprite in OpenGL. Yet, when I compile it, it does not show up. I see that the code is fine (There are not even any shader compilation errors nor any other errors). I also have also the matrices set up(which I doubt is causing the problem, and that's where starts the CONFUSION!!) Here is the source code, by the way(without debugging, to make it short):- main.cpp // Including all required headers here... #include <iostream> #define GLEW_STATIC

How to correctly linearize depth in OpenGL ES in iOS?

妖精的绣舞 提交于 2019-11-26 18:01:18
I'm trying to render a forrest scene for an iOS App with OpenGL. To make it a little bit nicer, I'd like to implement a depth effect into the scene. However I need a linearized depth value from the OpenGL depth buffer to do so. Currently I am using a computation in the fragment shader (which I found here ). Therefore my terrain fragment shader looks like this: #version 300 es precision mediump float; layout(location = 0) out lowp vec4 out_color; float linearizeDepth(float depth) { return 2.0 * nearz / (farz + nearz - depth * (farz - nearz)); } void main(void) { float depth = gl_FragCoord.z;

Why transforming normals with the transpose of the inverse of the modelview matrix?

我与影子孤独终老i 提交于 2019-11-26 17:15:02
I am working on some shaders, and I need to transform normals. I read in few tutorials the way you transform normals is you multiply them with the transpose of the inverse of the modelview matrix . But I can't find explanation of why is that so, and what is the logic behind that? Invalid Take a look at this tutorial: https://paroj.github.io/gltut/Illumination/Tut09%20Normal%20Transformation.html You can imagine that when the surface of a sphere stretches (so the sphere is scaled along one axis or something similar) the normals of that surface will all 'bend' towards each other. It turns out

Frequency of shader invocations in rendering commands

时光总嘲笑我的痴心妄想 提交于 2019-11-26 16:56:28
问题 Shaders have invocations, which each are (usually) given a unique set of input data, and each (usually) write to their own separate output data. When you issue a rendering command, how many times does each shader get invoked? 回答1: Each shader stage has its own frequency of invocations. I will use the OpenGL terminology, but D3D works the same way (since they're both modelling the same hardware relationships). Vertex Shaders These are the second most complicated. They execute once for every

How does this faking the light work on aerotwist?

让人想犯罪 __ 提交于 2019-11-26 16:44:17
I am trying to read up this tutorial: https://aerotwist.com/tutorials/an-introduction-to-shaders-part-2/ but I am not able to follow up. Basically the code creates a directional light by using shaders that run directly on the GPU. This is the code: // same name and type as VS varying vec3 vNormal; void main() { // calc the dot product and clamp // 0 -> 1 rather than -1 -> 1 vec3 light = vec3(0.5,0.2,1.0); // ensure it's normalized light = normalize(light); // calculate the dot product of // the light to the vertex normal float dProd = max(0.0, dot(vNormal, light)); // feed into our frag colour

GLSL - Using custom output attribute instead of gl_Position

北城以北 提交于 2019-11-26 16:32:42
问题 I am currently learning OpenGL with shaders (3.3). There is one thing i can't seem to work out though. I have read that using built-in variables like gl_Position and gl_FragCoords is deprecated in OpenGL 3+, therefore I wanted to use my own output variable. So instead of this: #version 330\n layout(location=0) in vec2 i_position; out vec4 o_position; void main() { gl_Position = vec4(i_position, 0.0, 1.0); }; I wrote this: #version 330\n layout(location=0) in vec2 i_position; out vec4 o

How can I improve the performance of my custom OpenGL ES 2.0 depth texture generation?

谁说胖子不能爱 提交于 2019-11-26 15:09:51
问题 I have an open source iOS application that uses custom OpenGL ES 2.0 shaders to display 3-D representations of molecular structures. It does this by using procedurally generated sphere and cylinder impostors drawn over rectangles, instead of these same shapes built using lots of vertices. The downside to this approach is that the depth values for each fragment of these impostor objects needs to be calculated in a fragment shader, to be used when objects overlap. Unfortunately, OpenGL ES 2.0