GLSL:how to pass values to fragment shader.

Started by
28 comments, last by Nanoha 7 years, 8 months ago

This is tricky because you have a lot of things going on all at once and a lot of older code mixed with a lot of newer code. ftransform() might not even exist in version 4.5 for example. I think it would probably be a very good idea if you followed a modern tutorial that just draws a single triangle to the screen. Once you have that you can make it a quad and once you have that you can put your Julia shader back in.

There's a bunch of tutorials here:

http://ogldev.atspace.co.uk/index.html

Take a look at tutorials 3, 4 and 5.

Is there a work-around for pass thru a gl_position to some other variable without the gl_vertex?

The tutorials above will show you how to do this.

Interested in Fractals? Check out my App, Fractal Scout, free on the Google Play store.

Advertisement

This approach does have some downsides too. It uses floats so you'll be limited to have far you can magnify your image and it also does a lot of work each frame (it is rebuilding the entire thing each time). A better approach might be to render it to a texture once and then simply show that texture each time afterwards using a basic shader.

I heard that before but do not know what that meant. Can you elaborate?

thanks

This approach does have some downsides too. It uses floats so you'll be limited to have far you can magnify your image and it also does a lot of work each frame (it is rebuilding the entire thing each time). A better approach might be to render it to a texture once and then simply show that texture each time afterwards using a basic shader.

I heard that before but do not know what that meant. Can you elaborate?

thanks

Not sure which part of what I have said there that you are referring to.

For the floats part, when you zoom in far enough your image will start to look very blocky, I'm not sure when this will happen but you can zoom in a great deal more if you start to use doubles. If the version of OpenGL/GLSL supports doubles in shaders then it will be easy enough to change later. Earlier versions (and the ES versions) don't have support for doubles but I think you will be ok.

According to wikipedia you'd need OpenGL 4.0 or above to use doubles.

For the render to texture comment. Each time you draw one frame your shader is recreating the entire image, this is potentially a lot of work. Once the image is created you don't really need to build it again unless something changes (the location, the scale etc). The idea then is to build it once, store that image and then keep showing the copy each time instead of remaking it. For that you will need to know how to 'Render to texture'. Right now you have been loading textures from jpg files but it's possible to draw onto them just like you draw onto the screen. First you would set your Julia shader, set the 'render target' (the texture to draw on) and then draw the frame. Then your image is stored in the texture. The next time around you just draw that same texture onto the screen instead. You would probably have to follow a tutorial for this as it can be quite confusing.

Interested in Fractals? Check out my App, Fractal Scout, free on the Google Play store.

hi

I am able to print out the shader and program info log:



#version 450
in vec2 tcoord;
in vec3 vertex_position;

void main() {
  gl_Position = vec4(vertex_position, 1.0);
}


 ***** test.vert compiled with no error. *****



// Julia set fragment shader

#version 450
uniform sampler2D tex;
uniform vec2 cval;
uniform float iter;

void main() {
    vec2 z;
	float color = 0.0;

    z.x = 6.0 * (gl_TexCoord[0].x - 0.5);
    z.y = 3.0 * (gl_TexCoord[0].y - 0.5);

    int i;
    for(; color < iter; color++) {
        float x = (z.x * z.x - z.y * z.y) + cval.x;
        float y = (z.y * z.x + z.x * z.y) + cval.y;

        if((x * x + y * y) > 4.0) break;
        z.x = x;
        z.y = y;
    }
	vec2 cc = vec2(color / iter, 1.0);
    gl_FragColor =texture2D(tex, cc.st);
}


 ***** Julia.frag compiled with no error. *****

program infolog:

GL_LINK_STATUS = 1
GL_ATTACHED_SHADERS = 2
GL_ACTIVE_ATTRIBUTES = 1
0) type: vec3 name: vertex_position location: 0
0) type: vec2 name: cval location: 0
1) type: float name: iter location: 1
2) type: sampler2D name: tex location: 2
linking status = 1, is that a success or failed?
vertex_position and cval are both in loc 0, is that a problem?
thanks

linking status = 1, is that a success or failed?

I'm not sure, where is that value coming from? What function returns that?

vertex_position and cval are both in loc 0, is that a problem?

That sounds ok, one is a uniform and one is an attribute so them both being 0 is acceptable.

What I do notice is you are using gl_TexCoord in your fragment, that probably no longer exists either so that has to go. GL_ACTIVE_ATTRIBUTES = 1, this should read 2 (you have both vertex_position and tcoord). The reason tcoord isn't being registered is because it's not being used in your vertex shader so it's being removed during compilation.

I can't give exact advice because I'm used to using an older version myself but to fix it you need to do something along the lines of:

Add a varying/out tex coord to your vertex shader


#version 450
in vec2 tcoord;
in vec3 vertex_position;
 
varying vec2 fragTexCoord; // this might have to be 'out' instead out 'varying'

void main() 
{
   fragTexCoord = tcoord;
   gl_Position = vec4(vertex_position, 1.0);

}

And then you add a similar thing to the top of your fragment shader

varying vec2 fragTexCoord; // this might have to be 'in' instead of varying

and then each time you have gl_TexCoord[0] just replace it with fragTexCoord.

Looks like you are making some progress though, good work so far.

Interested in Fractals? Check out my App, Fractal Scout, free on the Google Play store.

I'm not sure, where is that value coming from? What function returns that?

I got it from :
glGetProgramiv(programme, GL_LINK_STATUS, &params);
It is very hard to debug in vs 2015. sometimes something did not work and then in the next moment it worked, it is not stabled. for example, my palette files were not working, then I went to dinner and it idled, then without doing anything, it just worked.

glGetProgramiv(programme, GL_LINK_STATUS, ¶ms);

Ah in that case, according to this


GL_LINK_STATUS

params returns GL_TRUE if the last link operation on program was successful, and GL_FALSE otherwise.

Sounds like your shader linked fine if you got 1 from that function.

Interested in Fractals? Check out my App, Fractal Scout, free on the Google Play store.

Like 0 Likes 1 Likes Like Posted 14 August 2016 - 09:08 AM nickme, on 14 Aug 2016 - 07:58 AM, said: I do not understand what do you mean by code layout, I do not understand the context, can you elaborate? Did you notice the boxes in my post that contains code lines? That is "code layout" in the forum. It selects a non-proportional font (all letters are equally wide), and adds syntax colouring to the text. Blocks of code, like your shader code become more readable if you put them in a box too.

thanks for pointing that out.

best regards

For the render to texture comment. Each time you draw one frame your shader is recreating the entire image, this is potentially a lot of work. Once the image is created you don't really need to build it again unless something changes (the location, the scale etc). The idea then is to build it once, store that image and then keep showing the copy each time instead of remaking it. For that you will need to know how to 'Render to texture'. Right now you have been loading textures from jpg files but it's possible to draw onto them just like you draw onto the screen. First you would set your Julia shader, set the 'render target' (the texture to draw on) and then draw the frame. Then your image is stored in the texture. The next time around you just draw that same texture onto the screen instead. You would probably have to follow a tutorial for this as it can be quite confusing.

You mean using glReadPixels()?

For the render to texture comment. Each time you draw one frame your shader is recreating the entire image, this is potentially a lot of work. Once the image is created you don't really need to build it again unless something changes (the location, the scale etc). The idea then is to build it once, store that image and then keep showing the copy each time instead of remaking it. For that you will need to know how to 'Render to texture'. Right now you have been loading textures from jpg files but it's possible to draw onto them just like you draw onto the screen. First you would set your Julia shader, set the 'render target' (the texture to draw on) and then draw the frame. Then your image is stored in the texture. The next time around you just draw that same texture onto the screen instead. You would probably have to follow a tutorial for this as it can be quite confusing.

You mean using glReadPixels()?

Sorry for the delayed reply, hard to keep track of some threads.

You could use glReadPixels but that's the older/slower way to do it. What you want is a 'Frame Buffer Object' (FBO).

Interested in Fractals? Check out my App, Fractal Scout, free on the Google Play store.

This topic is closed to new replies.

Advertisement