Open texture from fragment shader - glsl

I'm trying to write a shader for Minecraft bedrock edition, and since I don't have access to the code of the game, just the GLSLes shaders, I was wondering if there was any way for me to access a texture from the fragment shader without having to pass it through the c++ code.

The answer is as stated in the comments above : there's no way. GLSL doesn't work this way.

Related

GLSL (WebGL) equivalent for RWTexture2D<float4>

tl;dr:
I'm a shader newbie and I'm trying to port an HLSL shader to GLSL.
What is the GLSL equivalent for RWTexture2D<float4> whatever; ?
I need to programmatically create a texture inside a shader.
Longer version:
I'm trying to port the "slime shader" from this video to GLSL, and load it in a web page (at the moment I'm using Three.js).
I managed to code the pseudo-random hash function and display the noise on screen, but now I'm stuck.
(Here's the HLSL shader code)
In the original shader there is this: RWTexture2D<float4> TrailMap; and I can't find a way to make something similar in my shader. All the info that I have found online is about loading external textures, but what I need is a texture that is created and modified inside the shader (and it seems to me that the way GLSL handles textures is not very beginner friendly).
I also tried using this converter. What I get is uniform image2D TrailMap; but it gives me this error:
'image2D' : Illegal use of reserved word
What am I missing?
WebGL doesn't have access to image load/store, the ability to modify image data arbitrarily within shaders. The converter is doing the right thing, but WebGL simply doesn't provide access to this hardware functionality.

OpenGL: Fragment vs Vertex shader for gradients?

I'm new to OpenGL, and I'm trying to understand vertex and fragment shaders. It seems you can use a vertex shader to make a gradient if you define the color you want each of the vertices to be, but it seems you can also make gradients using a fragment shader if you use the FragCoord variable, for example.
My question is, since you seem to be able to make color gradients using both kinds of shaders, which one is better to use? I'm guessing vertex shaders are faster or something since everyone seems to use them, but I just want to make sure.
... since everyone seems to use them
Using vertex and fragment shaders are mandatory in modern OpenGL for rendering absolutely everything.† So everyone uses both. It's the vertex shader responsibility to compute the color at the vertices, OpenGL's to interpolate it between them, and fragment shader's to write the interpolated value to the output color attachment.
† OK, you can also use a compute shader with imageStore, but I'm talking about the rasterization pipeline here.

GLSL - A do-nothing vertex shader?

So I have an opengl program that draws a group on objects. When I draw these objects I want to use my shader program is a vertex shader and a vertex shader exclusively. Basically, I am aiming to adjust the height of the model inside the vertex shader depending on a texture calculation. And that is it. Otherwise I want the object to be drawn as if using naked openGL (no shaders). I do not want to implement a fragment shader.
However I haven't been able to find how to make it so I can have a shader program with only a vertex shader and nothing else. Forgetting the part about adjust my model's height, so far I have:
gl_FrontColor = gl_Color;
gl_Position = modelViewProjectionMain * Position;
It transforms the object to the correct position alright, however when I do this I loose texture coordinates and also lighting information (normals are lost). What am I missing? How do I write a "do-nothing" vertex shader? That is, a vertex shader you could turn off and on when drawing a textured .obj with normals, and there would be no difference?
You can't write a shader with partial implementation. Either you do everything in a shader or completely rely on fixed functionality(deprecated) for a given object.
What you can do is this:
glUseProgram(handle)
// draw objects with shader
glUseProgram(0)
// draw objects with fixed functionality
To expand a little on the entirely correct answer by Abhishek Bansal, what you want to do would be nice but is not actually possible. You're going to have to write your own vertex and fragment shaders.
From your post, by "naked OpenGL" you mean the fixed-function pipeline in OpenGL 1 and 2, which included built-in lighting and texturing. Shaders in OpenGL entirely replace the fixed-function pipeline rather than extending it. And in OpenGL 3+ the old functionality has been removed, so now they're compulsory.
The good news is that vertex/fragment shaders to perform the same function as the original OpenGL lighting and texturing are easy to find and easy to modify for your purpose. The OpenGL Shading Language book by Rost, Licea-Kane, etc has a whole chapter "Emulating OpenGL Fixed Functionality" Or you could get a copy of the 5th edition OpenGL SuperBible book and code (not the 6th edition) which came with a bunch of useful predefined shaders. Or if you prefer online resources to books, there are the NeHe tutorials.
Writing shaders seems a bit daunting at first, but it's easier than you might think, and the extra flexibility is well worth it.

Three.js and GLSL - Kaleidoscope shader

I'm starting and experimenting with GLSL and WebGL. Using Three.js r52.
I have a simple plane geom mesh with a Shader material.
I'm already passing a texture as uniform property, and I was able to manipulate it in different ways.
But, when I try to experiment the shaders from Shader Toy, more specifically this one http://www.iquilezles.org/apps/shadertoy/?p=Kaleidoscope , doesn't work the same way.
I think has something to do with positions.
Anyone have a clue about what could be the problem?

GLSL with openGL2 setup problem

I am trying to use GLSL with openGL 2.0.
Can anyone give me a good tutorial to follow, so that I can setup GLSL properly.
Regards
Zeeshan
Depending on what you are trying to achieve and what is your current knowledge, you can take different approaches.
If you are trying to learn OpenGL 2.0 while also learning GLSL, I suggest getting the Red book and the Orange book as a set, as they go hand in hand.
If you want a less comprehensive guide that will just get you started, check out the OpenGL bible.
If I misunderstood your question and you already know OpenGL, and want to study more about GLSL in particular, here's a good phong shading example that shows the basics.
Compiling a shader source is really simple,
First you need to allocate a shader slot for your source, just like you allocate a texture, using glCreateShader:
GLuint vtxShader = glCreateShader(GL_VERTEX_SHADER);
GLuint pxlShader = glCreateShader(GL_FRAGMENT_SHADER);
After that you need to load your source code somehow. Since this is really a platform dependent solution, this is up to you.
After obtaining the source, you set it using glShaderSource:
glShaderSource(vtxShader, 1, &vsSource, 0);
glShaderSource(pxlShader, 1, &psSource, 0);
Then you compile your sources with glCompileShader:
glCompileShader(vtxShader);
glCompileShader(pxlShader);
Link the shaders to each other, first allocate a program using glCreateProgram, attach the shaders into the program using glAttachShader, and link them using glLinkProgram:
GLuint shaderId = glCreateProgram();
glAttachShader(shaderId, vtxShader);
glAttachShader(shaderId, pxlShader);
glLinkProgram(shaderId);
Then, just like a texture, you bind it to the current rendering stage using glUseProgram:
glUseProgram(shaderId);
To unbind it, use an id of 0 or another shader ID.
For cleanup:
glDetachShader(shaderId, vtxShader);
glDetachShader(shaderId, pxlShader);
glDeleteShader(vtxShader);
glDeleteShader(pxlShader);
glDeleteProgram(shaderId);
And that's mostly everything to it, you can use the glUniform function family alongside with glGetUniform to set parameters as well.