• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Shaders Shader Samplers

Gamer-15

Member
Why is the output of the following codes not the same?

Test1:

Draw event:
Code:
 draw_rectangle_color(0,0,room_width,room_height,c_blue,c_blue,c_red,c_red,0);
Screen_1.png

Test2:

Create Event:
Code:
surface_gradient=-1;

sampler_gradient = shader_get_sampler_index(test_shader,"sampler_gradient");
Draw event:
Code:
if !surface_exists(surface_gradient)
surface_gradient = surface_create(room_width,room_height); 

if surface_exists(surface_gradient)
{
 surface_set_target(surface_gradient);
 draw_rectangle_color(0,0,room_width,room_height,c_blue,c_blue,c_red,c_red,0);
 surface_reset_target();
   
 texture_gradient = surface_get_texture(surface_gradient);

 shader_set(test_shader);
 texture_set_stage(sampler_gradient,texture_gradient);
 draw_rectangle(0,0,room_width,room_height,0);
 shader_reset();
}
Fragment Shader:
Code:
varying vec2 v_vTexcoord;
varying vec4 v_vColour;

uniform sampler2D sampler_gradient;

void main()
{
 gl_FragColor = texture2D(sampler_gradient,v_vTexcoord);
}
Screen_1.png

So in test2 I am just trying to draw this vertical gradient as can be seen in test1, but this time I am passing it as an sampler to a shader.
 

Gamer-15

Member
So I tried it with a textured primitive:

Create event:
Code:
surface_gradient=-1;

sampler_gradient = shader_get_sampler_index(test_shader,"sampler_gradient");
Draw event:
Code:
if !surface_exists(surface_gradient)
surface_gradient = surface_create(room_width,room_height);

if surface_exists(surface_gradient)
{
 surface_set_target(surface_gradient);
    draw_set_colour(c_white);
    var tex = sprite_get_texture(sprite0,0);
    draw_primitive_begin_texture(pr_trianglestrip, tex);
    draw_vertex_texture_color(0,room_height,0,1,c_red,1);
    draw_vertex_texture_color(room_width,room_height,1,1,c_red,1);
    draw_vertex_texture_color(0,0,0,0,c_blue,1);
    draw_vertex_texture_color(room_width,0,1,0,c_blue,1);
    draw_primitive_end(); 
 surface_reset_target();
 
 texture_gradient = surface_get_texture(surface_gradient);

 shader_set(test_shader);
 texture_set_stage(sampler_gradient,texture_gradient);
 draw_rectangle(0,0,room_width,room_height,0);
 shader_reset();
}
Fragment shader:
Code:
varying vec2 v_vTexcoord;
varying vec4 v_vColour;

uniform sampler2D sampler_gradient;

void main()
{
 gl_FragColor = texture2D(sampler_gradient,v_vTexcoord);
}
Screen_3.png

I don't understand what I need to do with the coordinates in the shader.
 

Gamer-15

Member
Do you mean like this:

Code:
if !surface_exists(surface_gradient)
surface_gradient = surface_create(room_width,room_height);

if surface_exists(surface_gradient)
{
 surface_set_target(surface_gradient);

 surface_reset_target();
 
 texture_gradient = surface_get_texture(surface_gradient);

 shader_set(test_shader);
 texture_set_stage(sampler_gradient,texture_gradient);
 draw_rectangle(0,0,room_width,room_height,0);
 shader_reset();
}
I don't understand.
 

hamdrax24

Member
Draw the surface instead of the rectangle. The surface has accurate texture coordinates so it should sample correctly.

Edit: Also change the sampler back to your original gradient texture.
 
Last edited:

Gamer-15

Member
Is this what you mean, or could you write the code if I understand it wrong:

Code:
if !surface_exists(surface_gradient)
surface_gradient = surface_create(room_width,room_height);

if surface_exists(surface_gradient)
{
 surface_set_target(surface_gradient);
        draw_set_colour(c_white);
        var tex = sprite_get_texture(sprite0,0);
        draw_primitive_begin_texture(pr_trianglestrip, tex);
        draw_vertex_texture_color(0,room_height,0,1,c_red,1);
        draw_vertex_texture_color(room_width,room_height,1,1,c_red,1);
        draw_vertex_texture_color(0,0,0,0,c_blue,1);
        draw_vertex_texture_color(room_width,0,1,0,c_blue,1);
        draw_primitive_end();
    draw_primitive_end();
 surface_reset_target();
 
 texture_gradient = surface_get_texture(surface_gradient);

 shader_set(test_shader);
 texture_set_stage(sampler_gradient,texture_gradient);
 draw_surface(surface_gradient,0,0);//draw_rectangle(0,0,room_width,room_height,0);
 shader_reset();
}
I actually don't want to draw the surface, I only would like to pass the surface to the shader so that I can use it in the shader and also change it's rgb color values.
 

hamdrax24

Member
Code:
if surface_exists(!surface_gradient)
{
surface_gradient = surface_create(room_width,room_height);
}

var tex = sprite_get_texture(sprite0,0);

 shader_set(test_shader);
 texture_set_stage(sampler_gradient,tex);
 draw_surface(surface_gradient,0,0);
 shader_reset();
}
Something like this.
 

Gamer-15

Member
I don't think this is the solution for my problem, while I don't want to draw the surface itself but only pass it to the shader so that I can use it with the shader( in my game I want to modify the color values rgb in the shader, but this is not in the code yet to simplify the problem.).

Does someone know how to change the code (in the shader?) in my first or second post so that I get the vertical gradient and not the diagonal one.
 

sylvain_l

Member
Does someone know how to change the code (in the shader?) in my first or second post so that I get the vertical gradient and not the diagonal one.
(I'm really bad at getting it right, but just my own experience:)
if you are not working with a quad, you need to take into account that a rectangle (or sprite) is composed of 2 tris.
 

Gamer-15

Member
Ok, thanks!
Your comment made me change the primitive code and now it works.

Draw event:
Code:
if !surface_exists(surface_gradient)
surface_gradient = surface_create(room_width,room_height);

if surface_exists(surface_gradient)
{
 surface_set_target(surface_gradient);
        draw_set_colour(c_white);
        var tex = sprite_get_texture(sprite0,0);
        draw_primitive_begin_texture(pr_trianglelist, tex);
        draw_vertex_texture_color(0,room_height,1,0,c_red,1);
        draw_vertex_texture_color(room_width,room_height,0,0,c_red,1);
        draw_vertex_texture_color(0,0,1,1,c_blue,1);
        draw_primitive_end();
        draw_primitive_begin_texture(pr_trianglelist, tex);
        draw_vertex_texture_color(room_width,room_height,1,1,c_red,1);
        draw_vertex_texture_color(room_width,0,0,1,c_blue,1);     
        draw_vertex_texture_color(0,0,0,0,c_blue,1);     
        draw_primitive_end();     
 surface_reset_target();
 
 texture_gradient = surface_get_texture(surface_gradient);

 shader_set(test_shader);
 texture_set_stage(sampler_gradient,texture_gradient);
 draw_rectangle(0,0,room_width,room_height,0);
 shader_reset();
}
 
Top