You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I think something goes wrong when I do SetShaderValueTexture(blackhole_shader, iChannel0Loc, combinedRT.texture); so that I can then apply the blackhole shader on the combinedRT.texture (which renders just fine). In the fragment shader it seems like I just don't get the right sampled2D value and I just get black. I can't be sure whether the bug is in the c or the shader file.
Code:
#include<raylib.h>// https://www.raylib.com#include<stdlib.h>#include<stdio.h>#include<time.h>#defineGLSL_VERSION 330
intmain(void) {
constintscreen_width=800;
constintscreen_height=800;
InitWindow(screen_width,screen_height, "kunutza_destroyer2");
// Planet Shader// RLAPI void SetShaderValueTexture(Shader shader, int locIndex, Texture2D texture); // Set shader uniform value for texture (sampler2d)ImageplanetimBlank=GenImageColor(100, 100, BLANK);
Texture2Dplanet_texture=LoadTextureFromImage(planetimBlank); // Load blank texture to fill on shaderUnloadImage(planetimBlank);
Shaderplanet_shader=LoadShader(0, TextFormat("shaders/planet.fs", GLSL_VERSION));
// set the uniform for (vec2 circleCenter) in planet.fsfloatcircleCenter[2] = { (float)planetimBlank.width/2, screen_height- (float)planetimBlank.height/2 };
intcircleCenterLoc=GetShaderLocation(planet_shader, "circleCenter");
SetShaderValue(planet_shader, circleCenterLoc, circleCenter, SHADER_UNIFORM_VEC2);
// set the uniform for (float circleRadius) in planet.fsfloatcircleRadius=50.0;
intcircleRadiusLoc=GetShaderLocation(planet_shader, "circleRadius");
SetShaderValue(planet_shader, circleRadiusLoc, &circleRadius, SHADER_UNIFORM_FLOAT);
// set the uniform for (vec2 u_resolution) in planet.fsfloatiResolution[2] = { screen_width, screen_height};
intiResolutionLoc=GetShaderLocation(planet_shader, "iResolution");
SetShaderValue(planet_shader, iResolutionLoc, iResolution, SHADER_UNIFORM_VEC2);
// set the uniform for θχ (float rotation) in planet.fsfloatrotation=0.0;
introtationLoc=GetShaderLocation(planet_shader, "rotation");
SetShaderValue(planet_shader, rotationLoc, &rotation, SHADER_UNIFORM_FLOAT);
// set the uniform for (float time) in planet.fssrand(time(NULL));
floatctime;
floattime_multiplier=1.0;
ctime=rand() % 10000;
inttimeLoc=GetShaderLocation(planet_shader, "iTime");
SetShaderValue(planet_shader, timeLoc, &ctime, SHADER_UNIFORM_FLOAT);
// set the uniform for (float seed) in planet.fsintseed=rand() % 100000 ;
intseedLoc=GetShaderLocation(planet_shader, "seed");
SetShaderValue(planet_shader, seedLoc, &seed, SHADER_UNIFORM_INT);
// The skymapImageskymapimBlank=GenImageColor(iResolution[0], iResolution[1], BLANK);
Texture2Dskymap_texture=LoadTextureFromImage(skymapimBlank); // Load blank texture to fill on shaderUnloadImage(skymapimBlank);
Shaderskymap_shader=LoadShader(0, TextFormat("shaders/skymap.fs", GLSL_VERSION));
// set the uniform for (vec2 iResolution) in skymap.fsintu_resolutionLoc=GetShaderLocation(skymap_shader, "iResolution");
SetShaderValue(skymap_shader, u_resolutionLoc, iResolution, SHADER_UNIFORM_VEC2);
// set the uniform for (float iTime) in skymap.fs intitimeLoc=GetShaderLocation(skymap_shader, "iTime");
SetShaderValue(skymap_shader, itimeLoc, &ctime, SHADER_UNIFORM_FLOAT);
// Black hole//Image blackholeimBlank = GenImageColor(iResolution[0], iResolution[1], BLANK);//Texture2D blackhole_texture = LoadTextureFromImage(blackholeimBlank);//UnloadImage(blackholeimBlank);Shaderblackhole_shader=LoadShader(0, TextFormat("shaders/black_hole.fs", GLSL_VERSION));
// iResolutionintblackhole_iResLoc=GetShaderLocation(blackhole_shader, "iResolution");
SetShaderValue(blackhole_shader, blackhole_iResLoc, iResolution, SHADER_UNIFORM_VEC2);
// iTimeintblackhole_iTime=GetShaderLocation(blackhole_shader, "iTime");
SetShaderValue(blackhole_shader, blackhole_iTime, &ctime, SHADER_UNIFORM_FLOAT);
// Combined Render Texture for skymap + planetRenderTexture2DcombinedRT=LoadRenderTexture(iResolution[0], iResolution[1]);
// Set iChannel0 in blackhole shaderintiChannel0Loc=GetShaderLocation(blackhole_shader, "iChannel0");
SetShaderValueTexture(blackhole_shader, iChannel0Loc, combinedRT.texture);
// Load texture for rendering (framebuffer), after that I will update itRenderTexture2Dtarget=LoadRenderTexture(iResolution[0], iResolution[1]);
combinedRT.texture.format=PIXELFORMAT_UNCOMPRESSED_R8G8B8;
floatdelta;
SetTargetFPS(60);
while (!WindowShouldClose()) {
delta=GetFrameTime();
if (ctime <= 0) {
time_multiplier=-time_multiplier;
} elseif (ctime >= 100000) {
time_multiplier=-time_multiplier;
}
ctime+=time_multiplier*delta;
SetShaderValue(planet_shader, timeLoc, &ctime, SHADER_UNIFORM_FLOAT);
SetShaderValue(skymap_shader, itimeLoc, &ctime, SHADER_UNIFORM_FLOAT);
SetShaderValue(blackhole_shader, blackhole_iTime, &ctime, SHADER_UNIFORM_FLOAT);
// make the rotation number never gets too bigif (rotation >= 32*PI ) {
rotation-=32*PI ;
}
rotation+=1.0*delta;
SetShaderValue(planet_shader, rotationLoc, &rotation, SHADER_UNIFORM_FLOAT);
// framebuffer// Draw skymap + planet to combinedRTBeginTextureMode(combinedRT);
ClearBackground(BLACK);
BeginShaderMode(skymap_shader);
DrawTexture(skymap_texture, 0, 0, WHITE);
EndShaderMode();
BeginShaderMode(planet_shader);
DrawTexture(planet_texture, 0, 0, WHITE);
EndShaderMode();
EndTextureMode();
SetShaderValueTexture(blackhole_shader, iChannel0Loc, combinedRT.texture);
// Apply blackhole shader on combined resultBeginTextureMode(target);
ClearBackground(BLACK);
BeginShaderMode(blackhole_shader);
DrawTextureRec(combinedRT.texture,
(Rectangle){ 0, 0, (float)combinedRT.texture.width, (float)-combinedRT.texture.height },
(Vector2){ 0, 0 }, WHITE);
EndShaderMode();
EndTextureMode();
BeginDrawing();
ClearBackground(BLACK);
DrawTextureRec(target.texture, (Rectangle){ 0, 0, (float)target.texture.width, (float)-target.texture.height },
(Vector2){ 0, 0 }, WHITE);
EndDrawing();
}
// CleanupUnloadTexture(planet_texture);
UnloadTexture(skymap_texture);
UnloadRenderTexture(combinedRT);
UnloadRenderTexture(target);
UnloadShader(planet_shader);
UnloadShader(skymap_shader);
UnloadShader(blackhole_shader);
CloseWindow();
exit(EXIT_SUCCESS);
}
fs code:
#version330// Input vertex attributes (from vertex shader)invec2 fragTexCoord;
invec4 fragColor;
uniformvec2 iResolution; // viewport resolution (in pixels)uniformfloat iTime; // shader playback time (in seconds)// uniform float iTimeDelta; // render time (in seconds)// uniform float iFrameRate; // shader frame rate// uniform int iFrame; // shader playback frame// uniform float iChannelTime[4]; // channel playback time (in seconds)// uniform vec3 iChannelResolution[4]; // channel resolution (in pixels)// uniform vec4 iMouse; // mouse pixel coords. xy: current (if MLB down), zw: clickuniformsampler2D iChannel0; // input channel. XX = 2D/Cube// uniform vec4 iDate; // (year, month, day, time in seconds)// uniform float iSampleRate; // sound sample rate (i.e., 44100)// Output fragment coloroutvec4 finalColor;
vec2 blackHoleCenter;
// Gravitational field at position pos, given a black hole// of mass m at position center (ignoring G constant)vec2 Fgrav(float m, vec2 center, vec2 pos)
{
vec2 dir = center - pos;
floatdistance=length(dir);
dir /=distance;
return dir * (m / (distance*distance));
}
void main()
{
// void mainImage( out vec4 fragColor, in vec2 fragCoord )// {//// }// vec4 texel = texture(texture0, fragTexCoord); // Get texel color// vec2 texelScale = vec2(0.0);// vec4 color = mix(vec4(0.0), outlineColor, outline);// finalColor = mix(color, texel, texel.a);vec2 circleCenterCoord =vec2(0.5, 0.5);
vec2 uv =gl_FragCoord.xy / iResolution;
float dist =distance(uv, circleCenterCoord);
if (dist <=0.1)
{
finalColor =vec4(1.0, 0.0, 0.0, 0.5); // Red circle
}
else
{
vec4 texColor = texture(iChannel0, uv); // Use UV not fragTexCoord
finalColor = texColor;
finalColor.a =1.0;
}
//vec2 centerOfMass = vec2(pixelCoord.x*0.5, pixelCoord.y*0.5);//blackHoleCenter = centerOfMass;////vec2 netGrav = Fgrav(90.0, blackHoleCenter, fragTexCoord.xy);////float netGravMag = length(netGrav);////if(netGravMag >= 1.0)//{// finalColor = vec4(1.0, 0.0, 0.0, 1.0);//}//else//{// // convert coordinates// //vec2 circleCenterCoord = circleCenter.xy / iResolution;// //float circleWidth = circleRadius / iResolution.x; // If I get some weird with this I can make it a vec2// //float dist = distance(pixelCoord, circleCenterCoord);// vec2 texCoord = (fragTexCoord.xy + netGrav)/iResolution.xy;// vec4 texColor = texture(iChannel0, texCoord);// //texColor *= 0.05;//// //vec2 texCoord = (fragTexCoord.xy + netGrav*50.0)/iResolution.xy;// //vec3 texColor = vec3(texCoord, 1.0);// //texColor *= 0.05;//// vec3 fColor = texColor.rgb;//// //redshift// //fColor.g *= 1.0 - netGravMag*netGravMag*netGravMag*0.25;// //fColor.b *= 1.0 - netGravMag*0.5;//// //finalColor = vec4(pow(fColor, vec3(1.0/2.2)), 1.0);// finalColor = vec4(fColor, 1.0);//}
}
//https://www.shadertoy.com/view/XsfXRS
when drawing the combinedRT I get:
but when I try to draw the changes applied to target, I get:
I want to apply the shader onto the combinedRT's texture. So the correct outcome, in the current code, would be that the sky draws just fine, but where the circle (1.0, 0.0, 0.0, 0,5) appears there will be no sky behind it.
#version330invec2 fragTexCoord;
invec4 fragColor;
uniformvec2 iResolution;
uniformfloat iTime;
outvec4 finalColor;
// 3D Gradient noise from: https://www.shadertoy.com/view/Xsl3Dlvec3 hash( vec3 p ) // replace this by something better
{
p =vec3( dot(p,vec3(127.1,311.7, 74.7)),
dot(p,vec3(269.5,183.3,246.1)),
dot(p,vec3(113.5,271.9,124.6)));
return-1.0+2.0*fract(sin(p)*43758.5453123);
}
float Hash21(vec2 p ){
p =fract(p*vec2(123.234, 234.34));
p +=dot(p, p+213.42);
returnfract(p.x*p.y);
}
float noise( invec3 p )
{
vec3 i =floor( p );
vec3 f =fract( p );
vec3 u = f*f*(3.0-2.0*f);
returnmix( mix( mix( dot( hash( i +vec3(0.0,0.0,0.0) ), f -vec3(0.0,0.0,0.0) ),
dot( hash( i +vec3(1.0,0.0,0.0) ), f -vec3(1.0,0.0,0.0) ), u.x),
mix( dot( hash( i +vec3(0.0,1.0,0.0) ), f -vec3(0.0,1.0,0.0) ),
dot( hash( i +vec3(1.0,1.0,0.0) ), f -vec3(1.0,1.0,0.0) ), u.x), u.y),
mix( mix( dot( hash( i +vec3(0.0,0.0,1.0) ), f -vec3(0.0,0.0,1.0) ),
dot( hash( i +vec3(1.0,0.0,1.0) ), f -vec3(1.0,0.0,1.0) ), u.x),
mix( dot( hash( i +vec3(0.0,1.0,1.0) ), f -vec3(0.0,1.0,1.0) ),
dot( hash( i +vec3(1.0,1.0,1.0) ), f -vec3(1.0,1.0,1.0) ), u.x), u.y), u.z );
}
void main()
{
// Normalized pixel coordinates (from 0 to 1)vec2 uv =gl_FragCoord.xy/iResolution.xy;
// incoorperate coord if want to pixelate//float dx = 2.* (1.0 / iResolution.x);//float dy = 2.* (1.0 / iResolution.y);//vec2 coord = vec2(dx * floor(uv.x / dx), dy * floor(uv.y / dy));// Stars computation:vec3 stars_direction =vec3(uv *2.f -1.0f, 1.0f); // if you change the 2.f next to uv the size of the stars changesfloat stars_threshold =8.0f; // modifies the number of stars that are visiblefloat stars_exposure =200.0f; // modifies the overall strength of the starsfloat stars =pow(clamp(noise(stars_direction *200.0f), 0.0f, 1.0f), stars_threshold) * stars_exposure;
float r = Hash21(vec2(stars));
//stars *= mix(sin(r*6.283185), 2.4, noise(stars_direction * 100.0f + vec3(iTime/2.))); // time based flickering//stars *= mix(sin(r*6.283185)*2., 2.4, noise(stars_direction * 100.0f + vec3(iTime/2.))); //i think this is too much flickering//stars *= mix(1.-0.5*sin(r*6.283185), 2.4, noise(stars_direction * 100.0f + vec3(iTime/2.))); //stars *= mix(.4, 2.4, noise(stars_direction * 100.0f + vec3(iTime/8.))); //good THIS IS WAY BETTER IF YOU WANT TO ZOOM BECAUSE IT EFFECTS THE STARS MORE//stars *= mix(.4, 2.4, noise(stars_direction * 100.0f + vec3(iTime/2.)))/4.; // time based flickering//stars *= mix(0.4, 1.4, noise(stars_direction * 200.0f + vec3(iTime/4.)));//stars *= 1. - sin(2.*pow(sin(r*6.283185*iTime/4.), 2.));
stars *=1. -sin(2.*pow(sin(r*6.283185*iTime/4.), 2.))/2.; // IF ZOOMED STARS 0.5f YOU CCAN BARELY SEE IT// Output to screen
finalColor =vec4(vec3(stars),1.0);
}
// https://www.shadertoy.com/view/NtsBzB
Makefile:
CC = cc -O2 -Wall -std=gnu2x
# Automatically get all .c files and convert them to .o
objects := $(patsubst %.c,%.o,$(wildcard *.c))
all: clean kunutza_destroyer
# Default target
kunutza_destroyer: $(objects)
$(CC) $^ -o $@ -no-pie -lraylib -lm #-lbu what is that
.PHONY: clean
clean:
# rm -f edit $(objects)
rm -f *.o kunutza_destroyer
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
I think something goes wrong when I do SetShaderValueTexture(blackhole_shader, iChannel0Loc, combinedRT.texture); so that I can then apply the blackhole shader on the combinedRT.texture (which renders just fine). In the fragment shader it seems like I just don't get the right sampled2D value and I just get black. I can't be sure whether the bug is in the c or the shader file.
Code:
fs code:
when drawing the combinedRT I get:

but when I try to draw the changes applied to target, I get:

I want to apply the shader onto the combinedRT's texture. So the correct outcome, in the current code, would be that the sky draws just fine, but where the circle (1.0, 0.0, 0.0, 0,5) appears there will be no sky behind it.
here is all the other code if you need it
planet.fs:
skymaps.fs:
Makefile:
Beta Was this translation helpful? Give feedback.
All reactions