Convert fragment shader (in GLSL) to Metal Shader code [closed] - glsl

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last month.
Improve this question
The following code needs to be converted to Metal for Xcode:
uniform sampler2D emissionTexture;
vec3 light = _lightingContribution.diffuse;
float lum = max(0.0, 1 - (0.2126*light.r + 0.7152*light.g + 0.0722*light.b));
vec4 emission = texture2D(emissionTexture, _surface.diffuseTexcoord) * lum;
_output.color += emission;

There are a number of free tools for converting GLSL to MSL automatically.
For instance Shader Playground.
And both MoltenVK and MoltenGL contain automatic GLSL to MSL converters.

Related

What's the relationship between the barycentric coordinates of triangle in clip space and the one in screen space [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 months ago.
Improve this question
Suppose I have a triangle say(PC0,PC1,PC2), and its barycentric coordinates is((1,0),(0,1),(0,0)), they are all in clip space.
And now I want calculate the interpolated barycentric coordinates in screen space, how can I do that so it could be correct? I have see something like perspective correct interpolation, but I cant find a good mapping relationship bewteen them.
The conversion from screen-space barycentric (b0,b1,b2) to clip-space barycentric (B0,B1,B2) is given by:
(b0/w0, b1/w1, b2/w2)
(B0, B1, B2) = -----------------------
b0/w0 + b1/w1 + b2/w2
Where (w0,w1,w2) are the w-coordinates of the three vertices of the triangle, as set by the vertex shader. (See How exactly does OpenGL do perspectively correct linear interpolation?).
The inverse transformation is given by:
(B0*w0, B1*w1, B2*w2)
(b0, b1, b2) = -----------------------
B0*w0 + B1*w1 + B2*w2

How to handle texture animation in OpenGL? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I started using OpenGL with SDL2 and GLEW not so long ago, and I got two questions:
How to pass a texture to GLSL's uniform sampler2D ? It seems to do it automatically when binded, but it confuses me.
And the main question here:
What's the best way to handle texture animation?
Should I do it by GL_TEXTURE_3D? By loading all frames into multiple GL_TEXTURE_2D? Is there a built-in way for animation?
glActiveTexture and glBindTexture calls in this order will bind a texture to the given sampler slot.
Use a spritesheet as a TEXTURE_2D, and change the texture coordinates for swapping the animation frames.

Simple Shadertoy to regular glsl [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I want the code in glslSandBox to a regular glsl. Suppose I have a quad mesh 3D displaced in a 3D scene. I want to create this "shadertoy-like" texture and apply to it. I'm aware about the transformations it requires : Screen - NDC - CLIPSPACE - WORLDSPACE, but still I'm struggling. http://glslsandbox.com/e#61091.0 this is an extremely simple shader, can you please demonstrate the normal vs and fs shader it would take to apply it to a 3D mesh in a 3D scene?
The shader you linked is a fragment shader drawing on a flat screen plane.
Typically, the corresponding vertex setup would be two triangles (potentially as a strip, meaning 4 vertices in total), covering the entire screen. In fact, you don't really have to be concerning yourself with any transformations, especially given that the fs uses gl_FragCoord and the resolution is being passed as a uniform.
VS:
#version 450
in vec4 position;
void main() {
gl_Position = position
}
Vertices (example, use with GL_TRIANGLE_STRIP):
-1, -1
-1, 1
1, -1
1, 1
After you cover the entire screen, you can now just switch this setup to render to texture; create a framebuffer, attach a texture to it, and render in the same way. Then you'll be able to use that texture on your 3D model. This will work well if the generated image rarely changes.
If you actually wanted to draw that in one pass, then you'll need to pass the texture coordinate as a varying variable, and use it instead of gl_FragCoord; no other changes should be necessary.

change specific pixel color via fragment/pixel shader?(opengl) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Is there any way to change specific pixel color via fragment/pixel shader? (like a uniform variable?)
To be specific, im trying to implement ray-traced shadows.
If by pixel you mean an actual pixel on the screen, then you can achieve that with multistage rendering. Render your scene to an FBO (to a texture), then render the quad onto a screen with that texture.
If the FBO texture size will be exactly the same as screen size, every invocation of the fragment shader will nicely correspond to the screen position.

GLSL Editor program [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
I'm looking for a GLSL editor program. I did find some by googling, but I wanna know if there are any preferred ones based on user experience.
Possible features:
Syntax Highlighting
Intellisense
Automatic compile and link
P.S.
I'm not even sure if it's meaningful/possible for GLSL to be compiled automatically (any comments?).
EDIT:
Here's what I found:
Shader Maker
Try out KickJS's Shader Editor. It currently supports syntax highlight and compiles the code as you write.
http://www.kickjs.org/example/shader_editor/shader_editor.html
If you are running OS/X you should try out the OpenGL Shader Builder, even though this tool feels a little out-dated:
/Developer/Applications/Graphics Tools/OpenGL Shader Builder.app
There is also the GLman, which maybe is more a GLSL sandbox environment than a editor. A good introduction to the program is found in the excellent book: 'Graphics Shaders - Theory and practice - Second edition'.
http://web.engr.oregonstate.edu/~mjb/glman/
I found shader toy to be helpful. Contains some predefined shaders you can tweak and see instant results. All online and covers WebGL, OpenGL ES 1.1 / (some) 2.0, probably OpenGL various versions too.
https://www.shadertoy.com/
It passes in some predefined uniforms as well as up to 4 textures you can hyperlink too.
Here are the following inputs:
uniform vec4 mouse: xy contain the current pixel coords (if LMB is down). zw contain the click pixel.
uniform vec2 resolution: the rendering vieport resolution.
uniform float time: current time in seconds.
uniform sampler2D tex0: sampler for input texture 0.
uniform sampler2D tex1: sampler for input texture 1.
uniform sampler2D tex2: sampler for input texture 2.
uniform sampler2D tex3: sampler for input texture 3.
I've found http://glsl.heroku.com interesting, you can edit only the fragment shader, but it's quite useful for testing some effects.
And it's open source! You can get the source on github: https://github.com/mrdoob/glsl-sandbox
Example of a shader using this editor: http://glsl.heroku.com/e#7310.0 (it's not mine, btw)