Show half portion of image side by side - OpenGL
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have created two textures for two images. Now I want to show this textures in opengl in an order that left part of image2, full image1, right part of image2. I have done like below. Image1 shows at the center of opengl screen. But left and right part of screen is not correct (which should show left part of image2 and right part of image2 respectively)
Vertex shader:
attribute vec3 a_position;
varying vec2 vTexCoord;
void main() {
vTexCoord = (a_position.xy + 1) / 2;
gl_Position = vec4(a_position,1);
}
Fragment shader:
precision highp float;
uniform sampler2D sTexture;//texture for image 1
uniform sampler2D sTexture1;//texture for image 2
varying vec2 vTexCoord;
void main () {
if ( vTexCoord.x<=0.25 )
gl_FragColor = texture2D (sTexture1, vec2(vTexCoord.x/2.0, vTexCoord.y));
else if ( vTexCoord.x>0.25 && vTexCoord.x<0.75 )
gl_FragColor = texture2D (sTexture, vec2(vTexCoord.x*2.0, vTexCoord.y));
else if(vTexCoord.x>=0.75 )
gl_FragColor = texture2D (sTexture1, vec2(vTexCoord.x*2.0-1.0, vTexCoord.y));
}
Compiling shaders:
void CreateShaders() {
/***********Vert Shader********************/
vertShader = GL.CreateShader(ShaderType.VertexShader);
GL.ShaderSource(vertShader, vertexShaderSource);
GL.CompileShader(vertShader);
/***********Frag Shader ****************/
fragShader = GL.CreateShader(ShaderType.FragmentShader);
GL.ShaderSource(fragShader, fragmentShaderSource);
GL.CompileShader(fragShader);
}
opengl glsl opentk
add a comment |
I have created two textures for two images. Now I want to show this textures in opengl in an order that left part of image2, full image1, right part of image2. I have done like below. Image1 shows at the center of opengl screen. But left and right part of screen is not correct (which should show left part of image2 and right part of image2 respectively)
Vertex shader:
attribute vec3 a_position;
varying vec2 vTexCoord;
void main() {
vTexCoord = (a_position.xy + 1) / 2;
gl_Position = vec4(a_position,1);
}
Fragment shader:
precision highp float;
uniform sampler2D sTexture;//texture for image 1
uniform sampler2D sTexture1;//texture for image 2
varying vec2 vTexCoord;
void main () {
if ( vTexCoord.x<=0.25 )
gl_FragColor = texture2D (sTexture1, vec2(vTexCoord.x/2.0, vTexCoord.y));
else if ( vTexCoord.x>0.25 && vTexCoord.x<0.75 )
gl_FragColor = texture2D (sTexture, vec2(vTexCoord.x*2.0, vTexCoord.y));
else if(vTexCoord.x>=0.75 )
gl_FragColor = texture2D (sTexture1, vec2(vTexCoord.x*2.0-1.0, vTexCoord.y));
}
Compiling shaders:
void CreateShaders() {
/***********Vert Shader********************/
vertShader = GL.CreateShader(ShaderType.VertexShader);
GL.ShaderSource(vertShader, vertexShaderSource);
GL.CompileShader(vertShader);
/***********Frag Shader ****************/
fragShader = GL.CreateShader(ShaderType.FragmentShader);
GL.ShaderSource(fragShader, fragmentShaderSource);
GL.CompileShader(fragShader);
}
opengl glsl opentk
Can you please reformat this code so that it's actually readable?
– Bartek Banachewicz
Nov 22 '18 at 11:23
please check now
– nsds
Nov 22 '18 at 11:27
What do you mean by "But left and right part of screen is not correct"? You need to provide a full detail on what is going on. "not correct" can be anything from being black, being green, seeing garbage, seeing the wrong texture with correct coordinates, seeing correct texture with wrong coordinates (not the correct part of it)...
– Matic Oblak
Nov 22 '18 at 12:14
Also define what is your expected result. Are you expecting to see all 3 images as a whole stacked one next to another or do you want to simply overlay the middle part ofimage1
withimage2
?
– Matic Oblak
Nov 22 '18 at 12:16
add a comment |
I have created two textures for two images. Now I want to show this textures in opengl in an order that left part of image2, full image1, right part of image2. I have done like below. Image1 shows at the center of opengl screen. But left and right part of screen is not correct (which should show left part of image2 and right part of image2 respectively)
Vertex shader:
attribute vec3 a_position;
varying vec2 vTexCoord;
void main() {
vTexCoord = (a_position.xy + 1) / 2;
gl_Position = vec4(a_position,1);
}
Fragment shader:
precision highp float;
uniform sampler2D sTexture;//texture for image 1
uniform sampler2D sTexture1;//texture for image 2
varying vec2 vTexCoord;
void main () {
if ( vTexCoord.x<=0.25 )
gl_FragColor = texture2D (sTexture1, vec2(vTexCoord.x/2.0, vTexCoord.y));
else if ( vTexCoord.x>0.25 && vTexCoord.x<0.75 )
gl_FragColor = texture2D (sTexture, vec2(vTexCoord.x*2.0, vTexCoord.y));
else if(vTexCoord.x>=0.75 )
gl_FragColor = texture2D (sTexture1, vec2(vTexCoord.x*2.0-1.0, vTexCoord.y));
}
Compiling shaders:
void CreateShaders() {
/***********Vert Shader********************/
vertShader = GL.CreateShader(ShaderType.VertexShader);
GL.ShaderSource(vertShader, vertexShaderSource);
GL.CompileShader(vertShader);
/***********Frag Shader ****************/
fragShader = GL.CreateShader(ShaderType.FragmentShader);
GL.ShaderSource(fragShader, fragmentShaderSource);
GL.CompileShader(fragShader);
}
opengl glsl opentk
I have created two textures for two images. Now I want to show this textures in opengl in an order that left part of image2, full image1, right part of image2. I have done like below. Image1 shows at the center of opengl screen. But left and right part of screen is not correct (which should show left part of image2 and right part of image2 respectively)
Vertex shader:
attribute vec3 a_position;
varying vec2 vTexCoord;
void main() {
vTexCoord = (a_position.xy + 1) / 2;
gl_Position = vec4(a_position,1);
}
Fragment shader:
precision highp float;
uniform sampler2D sTexture;//texture for image 1
uniform sampler2D sTexture1;//texture for image 2
varying vec2 vTexCoord;
void main () {
if ( vTexCoord.x<=0.25 )
gl_FragColor = texture2D (sTexture1, vec2(vTexCoord.x/2.0, vTexCoord.y));
else if ( vTexCoord.x>0.25 && vTexCoord.x<0.75 )
gl_FragColor = texture2D (sTexture, vec2(vTexCoord.x*2.0, vTexCoord.y));
else if(vTexCoord.x>=0.75 )
gl_FragColor = texture2D (sTexture1, vec2(vTexCoord.x*2.0-1.0, vTexCoord.y));
}
Compiling shaders:
void CreateShaders() {
/***********Vert Shader********************/
vertShader = GL.CreateShader(ShaderType.VertexShader);
GL.ShaderSource(vertShader, vertexShaderSource);
GL.CompileShader(vertShader);
/***********Frag Shader ****************/
fragShader = GL.CreateShader(ShaderType.FragmentShader);
GL.ShaderSource(fragShader, fragmentShaderSource);
GL.CompileShader(fragShader);
}
opengl glsl opentk
opengl glsl opentk
edited Nov 22 '18 at 12:04
Matic Oblak
11.3k11632
11.3k11632
asked Nov 22 '18 at 11:17
nsdsnsds
3082620
3082620
Can you please reformat this code so that it's actually readable?
– Bartek Banachewicz
Nov 22 '18 at 11:23
please check now
– nsds
Nov 22 '18 at 11:27
What do you mean by "But left and right part of screen is not correct"? You need to provide a full detail on what is going on. "not correct" can be anything from being black, being green, seeing garbage, seeing the wrong texture with correct coordinates, seeing correct texture with wrong coordinates (not the correct part of it)...
– Matic Oblak
Nov 22 '18 at 12:14
Also define what is your expected result. Are you expecting to see all 3 images as a whole stacked one next to another or do you want to simply overlay the middle part ofimage1
withimage2
?
– Matic Oblak
Nov 22 '18 at 12:16
add a comment |
Can you please reformat this code so that it's actually readable?
– Bartek Banachewicz
Nov 22 '18 at 11:23
please check now
– nsds
Nov 22 '18 at 11:27
What do you mean by "But left and right part of screen is not correct"? You need to provide a full detail on what is going on. "not correct" can be anything from being black, being green, seeing garbage, seeing the wrong texture with correct coordinates, seeing correct texture with wrong coordinates (not the correct part of it)...
– Matic Oblak
Nov 22 '18 at 12:14
Also define what is your expected result. Are you expecting to see all 3 images as a whole stacked one next to another or do you want to simply overlay the middle part ofimage1
withimage2
?
– Matic Oblak
Nov 22 '18 at 12:16
Can you please reformat this code so that it's actually readable?
– Bartek Banachewicz
Nov 22 '18 at 11:23
Can you please reformat this code so that it's actually readable?
– Bartek Banachewicz
Nov 22 '18 at 11:23
please check now
– nsds
Nov 22 '18 at 11:27
please check now
– nsds
Nov 22 '18 at 11:27
What do you mean by "But left and right part of screen is not correct"? You need to provide a full detail on what is going on. "not correct" can be anything from being black, being green, seeing garbage, seeing the wrong texture with correct coordinates, seeing correct texture with wrong coordinates (not the correct part of it)...
– Matic Oblak
Nov 22 '18 at 12:14
What do you mean by "But left and right part of screen is not correct"? You need to provide a full detail on what is going on. "not correct" can be anything from being black, being green, seeing garbage, seeing the wrong texture with correct coordinates, seeing correct texture with wrong coordinates (not the correct part of it)...
– Matic Oblak
Nov 22 '18 at 12:14
Also define what is your expected result. Are you expecting to see all 3 images as a whole stacked one next to another or do you want to simply overlay the middle part of
image1
with image2
?– Matic Oblak
Nov 22 '18 at 12:16
Also define what is your expected result. Are you expecting to see all 3 images as a whole stacked one next to another or do you want to simply overlay the middle part of
image1
with image2
?– Matic Oblak
Nov 22 '18 at 12:16
add a comment |
1 Answer
1
active
oldest
votes
I want to show this textures in opengl in an order that left part of image2, full image1, right part of image2
If the texture coordinate is less than 0.25 then you want to show the rang [0, 0.5] of image2. You have to map the x component of the texture coordinate from [0.0, 0.25] to [0, 0.5]:
vec2( vTexCoord.x*2.0, vTexCoord.y );
If the texture coordinate is grater then you want to show the rang [0.5, 1] of image2. You have to map the x component of the texture coordinate from [0.75, 1.0] to [0.5, 1.0]:
vec2( (vTexCoord.x-0.75)*2.0 + 0.5, vTexCoord.y );
or
vec2( vTexCoord.x*2.0 - 1.0, vTexCoord.y );
If the texture coordinate is grater than 0.25 and less than 0.75 then you want to show the full range of image1. You have to map the x component of the texture coordinate from [0.25, 0.75] to [0.0, 1.0]:
vec2( vTexCoord.x*2.0 - 0.5, vTexCoord.y );
The shader code my look like this
precision highp float;
uniform sampler2D sTexture;//texture for image 1
uniform sampler2D sTexture1;//texture for image 2
varying vec2 vTexCoord;
void main ()
{
float u = vTexCoord.x*2.0;
float a = 0.0;
if( vTexCoord.x > 0.75 )
{
u -= 1.0;
}
else if ( vTexCoord.x >= 0.25 )
{
u -= 0.5;
a = 1.0;
}
vec4 color1 = texture2D(sTexture1, vec2(u, texCoord.y));
vec4 color2 = texture2D(sTexture, vec2(u, texCoord.y));
gl_FragColor = mix(color2, color1, a);
}
Extension to the answer:
if I want to show only a portion of right image,for example (0.6,0.8).
You want to map [0.75, 1.0] to [0.6, 0.8]:
[0.75, 1.0] to [0, 1]:
u' = (u-0.75)/0.25
[0, 1] to [0.6, 0.8]:
u'' = u'*0.2+0.6
This leads to:
u = (vTexCoord.x-0.75)*0.2/0.25 + 0.6;
when mapping [0.75, 1.0] to [0.6, 0.8] i wish to display the remain portion of [0.75, 1.0] with black/white color. when using (vTexCoord.x-0.75)*0.2/0.25 + 0.6 image with portion [.6,.8] is filled in [0.75, 1.0]
You have to convert the RGB color to grayscal. I recommend to use the a formula for Relative luminance:
float grayscale = dot(color1, vec3(0.2126, 0.7152, 0.0722));
color1.rgb = vec3(grayscale);
Hi, I want to to flip the left portion. tried like below.but failed. if ( vTexCoord.x <= 0.25 ) { gl_FragColor = texture2D(sTexture1, vec2(1.0- (vTexCoord.x*2.0), vTexCoord.y)); }
– nsds
Nov 23 '18 at 10:51
1
The coordinates of the left part are in range [0, 0.5], so it has to be0.5-vTexCoord.x*2.0
-gl_FragColor = texture2D(sTexture1, vec2(0.5-vTexCoord.x*2.0, vTexCoord.y));
– Rabbid76
Nov 23 '18 at 10:53
hi, if I want to show only a portion of right image,for example (0.6,0.8). I have tried like below . else if(vTexCoord.x >= 0.75 ) { vec2( (vTexCoord.x-0.75)*2.0 + 0.6, vTexCoord.y ); } But it shows a blurred area only.
– nsds
Nov 26 '18 at 6:20
1
@nsds I never saidu'=(u-0.75)*2.0
I saidu' = (u-0.75)/0.25
! To map fro [0, 0.25] to [0, 1] you have to multiply by 4 respectively divide by 0.25 because1/4 = 0.25
and4 == 1/0.25
. I used 0.25 because the size of the range is 0.25 and can be calculated by1-0.75
. I tried to point out how the values of the range change the formula.
– Rabbid76
Nov 26 '18 at 7:38
1
Well, "this reason" at least could be fixed by editing it to use the explicitLod
versions. However, these are not allowed in the FS in the early GLSL versions, and that code seems to use 1.10 or 1.20, so that option isn't really there, either. The other quick-fix would be to always sample both textures and store the results in two different variables, and only conditionally decide between those. That would be well-defined, at least.
– derhass
Nov 26 '18 at 14:51
|
show 9 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53429788%2fshow-half-portion-of-image-side-by-side-opengl%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I want to show this textures in opengl in an order that left part of image2, full image1, right part of image2
If the texture coordinate is less than 0.25 then you want to show the rang [0, 0.5] of image2. You have to map the x component of the texture coordinate from [0.0, 0.25] to [0, 0.5]:
vec2( vTexCoord.x*2.0, vTexCoord.y );
If the texture coordinate is grater then you want to show the rang [0.5, 1] of image2. You have to map the x component of the texture coordinate from [0.75, 1.0] to [0.5, 1.0]:
vec2( (vTexCoord.x-0.75)*2.0 + 0.5, vTexCoord.y );
or
vec2( vTexCoord.x*2.0 - 1.0, vTexCoord.y );
If the texture coordinate is grater than 0.25 and less than 0.75 then you want to show the full range of image1. You have to map the x component of the texture coordinate from [0.25, 0.75] to [0.0, 1.0]:
vec2( vTexCoord.x*2.0 - 0.5, vTexCoord.y );
The shader code my look like this
precision highp float;
uniform sampler2D sTexture;//texture for image 1
uniform sampler2D sTexture1;//texture for image 2
varying vec2 vTexCoord;
void main ()
{
float u = vTexCoord.x*2.0;
float a = 0.0;
if( vTexCoord.x > 0.75 )
{
u -= 1.0;
}
else if ( vTexCoord.x >= 0.25 )
{
u -= 0.5;
a = 1.0;
}
vec4 color1 = texture2D(sTexture1, vec2(u, texCoord.y));
vec4 color2 = texture2D(sTexture, vec2(u, texCoord.y));
gl_FragColor = mix(color2, color1, a);
}
Extension to the answer:
if I want to show only a portion of right image,for example (0.6,0.8).
You want to map [0.75, 1.0] to [0.6, 0.8]:
[0.75, 1.0] to [0, 1]:
u' = (u-0.75)/0.25
[0, 1] to [0.6, 0.8]:
u'' = u'*0.2+0.6
This leads to:
u = (vTexCoord.x-0.75)*0.2/0.25 + 0.6;
when mapping [0.75, 1.0] to [0.6, 0.8] i wish to display the remain portion of [0.75, 1.0] with black/white color. when using (vTexCoord.x-0.75)*0.2/0.25 + 0.6 image with portion [.6,.8] is filled in [0.75, 1.0]
You have to convert the RGB color to grayscal. I recommend to use the a formula for Relative luminance:
float grayscale = dot(color1, vec3(0.2126, 0.7152, 0.0722));
color1.rgb = vec3(grayscale);
Hi, I want to to flip the left portion. tried like below.but failed. if ( vTexCoord.x <= 0.25 ) { gl_FragColor = texture2D(sTexture1, vec2(1.0- (vTexCoord.x*2.0), vTexCoord.y)); }
– nsds
Nov 23 '18 at 10:51
1
The coordinates of the left part are in range [0, 0.5], so it has to be0.5-vTexCoord.x*2.0
-gl_FragColor = texture2D(sTexture1, vec2(0.5-vTexCoord.x*2.0, vTexCoord.y));
– Rabbid76
Nov 23 '18 at 10:53
hi, if I want to show only a portion of right image,for example (0.6,0.8). I have tried like below . else if(vTexCoord.x >= 0.75 ) { vec2( (vTexCoord.x-0.75)*2.0 + 0.6, vTexCoord.y ); } But it shows a blurred area only.
– nsds
Nov 26 '18 at 6:20
1
@nsds I never saidu'=(u-0.75)*2.0
I saidu' = (u-0.75)/0.25
! To map fro [0, 0.25] to [0, 1] you have to multiply by 4 respectively divide by 0.25 because1/4 = 0.25
and4 == 1/0.25
. I used 0.25 because the size of the range is 0.25 and can be calculated by1-0.75
. I tried to point out how the values of the range change the formula.
– Rabbid76
Nov 26 '18 at 7:38
1
Well, "this reason" at least could be fixed by editing it to use the explicitLod
versions. However, these are not allowed in the FS in the early GLSL versions, and that code seems to use 1.10 or 1.20, so that option isn't really there, either. The other quick-fix would be to always sample both textures and store the results in two different variables, and only conditionally decide between those. That would be well-defined, at least.
– derhass
Nov 26 '18 at 14:51
|
show 9 more comments
I want to show this textures in opengl in an order that left part of image2, full image1, right part of image2
If the texture coordinate is less than 0.25 then you want to show the rang [0, 0.5] of image2. You have to map the x component of the texture coordinate from [0.0, 0.25] to [0, 0.5]:
vec2( vTexCoord.x*2.0, vTexCoord.y );
If the texture coordinate is grater then you want to show the rang [0.5, 1] of image2. You have to map the x component of the texture coordinate from [0.75, 1.0] to [0.5, 1.0]:
vec2( (vTexCoord.x-0.75)*2.0 + 0.5, vTexCoord.y );
or
vec2( vTexCoord.x*2.0 - 1.0, vTexCoord.y );
If the texture coordinate is grater than 0.25 and less than 0.75 then you want to show the full range of image1. You have to map the x component of the texture coordinate from [0.25, 0.75] to [0.0, 1.0]:
vec2( vTexCoord.x*2.0 - 0.5, vTexCoord.y );
The shader code my look like this
precision highp float;
uniform sampler2D sTexture;//texture for image 1
uniform sampler2D sTexture1;//texture for image 2
varying vec2 vTexCoord;
void main ()
{
float u = vTexCoord.x*2.0;
float a = 0.0;
if( vTexCoord.x > 0.75 )
{
u -= 1.0;
}
else if ( vTexCoord.x >= 0.25 )
{
u -= 0.5;
a = 1.0;
}
vec4 color1 = texture2D(sTexture1, vec2(u, texCoord.y));
vec4 color2 = texture2D(sTexture, vec2(u, texCoord.y));
gl_FragColor = mix(color2, color1, a);
}
Extension to the answer:
if I want to show only a portion of right image,for example (0.6,0.8).
You want to map [0.75, 1.0] to [0.6, 0.8]:
[0.75, 1.0] to [0, 1]:
u' = (u-0.75)/0.25
[0, 1] to [0.6, 0.8]:
u'' = u'*0.2+0.6
This leads to:
u = (vTexCoord.x-0.75)*0.2/0.25 + 0.6;
when mapping [0.75, 1.0] to [0.6, 0.8] i wish to display the remain portion of [0.75, 1.0] with black/white color. when using (vTexCoord.x-0.75)*0.2/0.25 + 0.6 image with portion [.6,.8] is filled in [0.75, 1.0]
You have to convert the RGB color to grayscal. I recommend to use the a formula for Relative luminance:
float grayscale = dot(color1, vec3(0.2126, 0.7152, 0.0722));
color1.rgb = vec3(grayscale);
Hi, I want to to flip the left portion. tried like below.but failed. if ( vTexCoord.x <= 0.25 ) { gl_FragColor = texture2D(sTexture1, vec2(1.0- (vTexCoord.x*2.0), vTexCoord.y)); }
– nsds
Nov 23 '18 at 10:51
1
The coordinates of the left part are in range [0, 0.5], so it has to be0.5-vTexCoord.x*2.0
-gl_FragColor = texture2D(sTexture1, vec2(0.5-vTexCoord.x*2.0, vTexCoord.y));
– Rabbid76
Nov 23 '18 at 10:53
hi, if I want to show only a portion of right image,for example (0.6,0.8). I have tried like below . else if(vTexCoord.x >= 0.75 ) { vec2( (vTexCoord.x-0.75)*2.0 + 0.6, vTexCoord.y ); } But it shows a blurred area only.
– nsds
Nov 26 '18 at 6:20
1
@nsds I never saidu'=(u-0.75)*2.0
I saidu' = (u-0.75)/0.25
! To map fro [0, 0.25] to [0, 1] you have to multiply by 4 respectively divide by 0.25 because1/4 = 0.25
and4 == 1/0.25
. I used 0.25 because the size of the range is 0.25 and can be calculated by1-0.75
. I tried to point out how the values of the range change the formula.
– Rabbid76
Nov 26 '18 at 7:38
1
Well, "this reason" at least could be fixed by editing it to use the explicitLod
versions. However, these are not allowed in the FS in the early GLSL versions, and that code seems to use 1.10 or 1.20, so that option isn't really there, either. The other quick-fix would be to always sample both textures and store the results in two different variables, and only conditionally decide between those. That would be well-defined, at least.
– derhass
Nov 26 '18 at 14:51
|
show 9 more comments
I want to show this textures in opengl in an order that left part of image2, full image1, right part of image2
If the texture coordinate is less than 0.25 then you want to show the rang [0, 0.5] of image2. You have to map the x component of the texture coordinate from [0.0, 0.25] to [0, 0.5]:
vec2( vTexCoord.x*2.0, vTexCoord.y );
If the texture coordinate is grater then you want to show the rang [0.5, 1] of image2. You have to map the x component of the texture coordinate from [0.75, 1.0] to [0.5, 1.0]:
vec2( (vTexCoord.x-0.75)*2.0 + 0.5, vTexCoord.y );
or
vec2( vTexCoord.x*2.0 - 1.0, vTexCoord.y );
If the texture coordinate is grater than 0.25 and less than 0.75 then you want to show the full range of image1. You have to map the x component of the texture coordinate from [0.25, 0.75] to [0.0, 1.0]:
vec2( vTexCoord.x*2.0 - 0.5, vTexCoord.y );
The shader code my look like this
precision highp float;
uniform sampler2D sTexture;//texture for image 1
uniform sampler2D sTexture1;//texture for image 2
varying vec2 vTexCoord;
void main ()
{
float u = vTexCoord.x*2.0;
float a = 0.0;
if( vTexCoord.x > 0.75 )
{
u -= 1.0;
}
else if ( vTexCoord.x >= 0.25 )
{
u -= 0.5;
a = 1.0;
}
vec4 color1 = texture2D(sTexture1, vec2(u, texCoord.y));
vec4 color2 = texture2D(sTexture, vec2(u, texCoord.y));
gl_FragColor = mix(color2, color1, a);
}
Extension to the answer:
if I want to show only a portion of right image,for example (0.6,0.8).
You want to map [0.75, 1.0] to [0.6, 0.8]:
[0.75, 1.0] to [0, 1]:
u' = (u-0.75)/0.25
[0, 1] to [0.6, 0.8]:
u'' = u'*0.2+0.6
This leads to:
u = (vTexCoord.x-0.75)*0.2/0.25 + 0.6;
when mapping [0.75, 1.0] to [0.6, 0.8] i wish to display the remain portion of [0.75, 1.0] with black/white color. when using (vTexCoord.x-0.75)*0.2/0.25 + 0.6 image with portion [.6,.8] is filled in [0.75, 1.0]
You have to convert the RGB color to grayscal. I recommend to use the a formula for Relative luminance:
float grayscale = dot(color1, vec3(0.2126, 0.7152, 0.0722));
color1.rgb = vec3(grayscale);
I want to show this textures in opengl in an order that left part of image2, full image1, right part of image2
If the texture coordinate is less than 0.25 then you want to show the rang [0, 0.5] of image2. You have to map the x component of the texture coordinate from [0.0, 0.25] to [0, 0.5]:
vec2( vTexCoord.x*2.0, vTexCoord.y );
If the texture coordinate is grater then you want to show the rang [0.5, 1] of image2. You have to map the x component of the texture coordinate from [0.75, 1.0] to [0.5, 1.0]:
vec2( (vTexCoord.x-0.75)*2.0 + 0.5, vTexCoord.y );
or
vec2( vTexCoord.x*2.0 - 1.0, vTexCoord.y );
If the texture coordinate is grater than 0.25 and less than 0.75 then you want to show the full range of image1. You have to map the x component of the texture coordinate from [0.25, 0.75] to [0.0, 1.0]:
vec2( vTexCoord.x*2.0 - 0.5, vTexCoord.y );
The shader code my look like this
precision highp float;
uniform sampler2D sTexture;//texture for image 1
uniform sampler2D sTexture1;//texture for image 2
varying vec2 vTexCoord;
void main ()
{
float u = vTexCoord.x*2.0;
float a = 0.0;
if( vTexCoord.x > 0.75 )
{
u -= 1.0;
}
else if ( vTexCoord.x >= 0.25 )
{
u -= 0.5;
a = 1.0;
}
vec4 color1 = texture2D(sTexture1, vec2(u, texCoord.y));
vec4 color2 = texture2D(sTexture, vec2(u, texCoord.y));
gl_FragColor = mix(color2, color1, a);
}
Extension to the answer:
if I want to show only a portion of right image,for example (0.6,0.8).
You want to map [0.75, 1.0] to [0.6, 0.8]:
[0.75, 1.0] to [0, 1]:
u' = (u-0.75)/0.25
[0, 1] to [0.6, 0.8]:
u'' = u'*0.2+0.6
This leads to:
u = (vTexCoord.x-0.75)*0.2/0.25 + 0.6;
when mapping [0.75, 1.0] to [0.6, 0.8] i wish to display the remain portion of [0.75, 1.0] with black/white color. when using (vTexCoord.x-0.75)*0.2/0.25 + 0.6 image with portion [.6,.8] is filled in [0.75, 1.0]
You have to convert the RGB color to grayscal. I recommend to use the a formula for Relative luminance:
float grayscale = dot(color1, vec3(0.2126, 0.7152, 0.0722));
color1.rgb = vec3(grayscale);
edited Nov 26 '18 at 16:05
answered Nov 22 '18 at 12:35
Rabbid76Rabbid76
43.6k123354
43.6k123354
Hi, I want to to flip the left portion. tried like below.but failed. if ( vTexCoord.x <= 0.25 ) { gl_FragColor = texture2D(sTexture1, vec2(1.0- (vTexCoord.x*2.0), vTexCoord.y)); }
– nsds
Nov 23 '18 at 10:51
1
The coordinates of the left part are in range [0, 0.5], so it has to be0.5-vTexCoord.x*2.0
-gl_FragColor = texture2D(sTexture1, vec2(0.5-vTexCoord.x*2.0, vTexCoord.y));
– Rabbid76
Nov 23 '18 at 10:53
hi, if I want to show only a portion of right image,for example (0.6,0.8). I have tried like below . else if(vTexCoord.x >= 0.75 ) { vec2( (vTexCoord.x-0.75)*2.0 + 0.6, vTexCoord.y ); } But it shows a blurred area only.
– nsds
Nov 26 '18 at 6:20
1
@nsds I never saidu'=(u-0.75)*2.0
I saidu' = (u-0.75)/0.25
! To map fro [0, 0.25] to [0, 1] you have to multiply by 4 respectively divide by 0.25 because1/4 = 0.25
and4 == 1/0.25
. I used 0.25 because the size of the range is 0.25 and can be calculated by1-0.75
. I tried to point out how the values of the range change the formula.
– Rabbid76
Nov 26 '18 at 7:38
1
Well, "this reason" at least could be fixed by editing it to use the explicitLod
versions. However, these are not allowed in the FS in the early GLSL versions, and that code seems to use 1.10 or 1.20, so that option isn't really there, either. The other quick-fix would be to always sample both textures and store the results in two different variables, and only conditionally decide between those. That would be well-defined, at least.
– derhass
Nov 26 '18 at 14:51
|
show 9 more comments
Hi, I want to to flip the left portion. tried like below.but failed. if ( vTexCoord.x <= 0.25 ) { gl_FragColor = texture2D(sTexture1, vec2(1.0- (vTexCoord.x*2.0), vTexCoord.y)); }
– nsds
Nov 23 '18 at 10:51
1
The coordinates of the left part are in range [0, 0.5], so it has to be0.5-vTexCoord.x*2.0
-gl_FragColor = texture2D(sTexture1, vec2(0.5-vTexCoord.x*2.0, vTexCoord.y));
– Rabbid76
Nov 23 '18 at 10:53
hi, if I want to show only a portion of right image,for example (0.6,0.8). I have tried like below . else if(vTexCoord.x >= 0.75 ) { vec2( (vTexCoord.x-0.75)*2.0 + 0.6, vTexCoord.y ); } But it shows a blurred area only.
– nsds
Nov 26 '18 at 6:20
1
@nsds I never saidu'=(u-0.75)*2.0
I saidu' = (u-0.75)/0.25
! To map fro [0, 0.25] to [0, 1] you have to multiply by 4 respectively divide by 0.25 because1/4 = 0.25
and4 == 1/0.25
. I used 0.25 because the size of the range is 0.25 and can be calculated by1-0.75
. I tried to point out how the values of the range change the formula.
– Rabbid76
Nov 26 '18 at 7:38
1
Well, "this reason" at least could be fixed by editing it to use the explicitLod
versions. However, these are not allowed in the FS in the early GLSL versions, and that code seems to use 1.10 or 1.20, so that option isn't really there, either. The other quick-fix would be to always sample both textures and store the results in two different variables, and only conditionally decide between those. That would be well-defined, at least.
– derhass
Nov 26 '18 at 14:51
Hi, I want to to flip the left portion. tried like below.but failed. if ( vTexCoord.x <= 0.25 ) { gl_FragColor = texture2D(sTexture1, vec2(1.0- (vTexCoord.x*2.0), vTexCoord.y)); }
– nsds
Nov 23 '18 at 10:51
Hi, I want to to flip the left portion. tried like below.but failed. if ( vTexCoord.x <= 0.25 ) { gl_FragColor = texture2D(sTexture1, vec2(1.0- (vTexCoord.x*2.0), vTexCoord.y)); }
– nsds
Nov 23 '18 at 10:51
1
1
The coordinates of the left part are in range [0, 0.5], so it has to be
0.5-vTexCoord.x*2.0
- gl_FragColor = texture2D(sTexture1, vec2(0.5-vTexCoord.x*2.0, vTexCoord.y));
– Rabbid76
Nov 23 '18 at 10:53
The coordinates of the left part are in range [0, 0.5], so it has to be
0.5-vTexCoord.x*2.0
- gl_FragColor = texture2D(sTexture1, vec2(0.5-vTexCoord.x*2.0, vTexCoord.y));
– Rabbid76
Nov 23 '18 at 10:53
hi, if I want to show only a portion of right image,for example (0.6,0.8). I have tried like below . else if(vTexCoord.x >= 0.75 ) { vec2( (vTexCoord.x-0.75)*2.0 + 0.6, vTexCoord.y ); } But it shows a blurred area only.
– nsds
Nov 26 '18 at 6:20
hi, if I want to show only a portion of right image,for example (0.6,0.8). I have tried like below . else if(vTexCoord.x >= 0.75 ) { vec2( (vTexCoord.x-0.75)*2.0 + 0.6, vTexCoord.y ); } But it shows a blurred area only.
– nsds
Nov 26 '18 at 6:20
1
1
@nsds I never said
u'=(u-0.75)*2.0
I said u' = (u-0.75)/0.25
! To map fro [0, 0.25] to [0, 1] you have to multiply by 4 respectively divide by 0.25 because 1/4 = 0.25
and 4 == 1/0.25
. I used 0.25 because the size of the range is 0.25 and can be calculated by 1-0.75
. I tried to point out how the values of the range change the formula.– Rabbid76
Nov 26 '18 at 7:38
@nsds I never said
u'=(u-0.75)*2.0
I said u' = (u-0.75)/0.25
! To map fro [0, 0.25] to [0, 1] you have to multiply by 4 respectively divide by 0.25 because 1/4 = 0.25
and 4 == 1/0.25
. I used 0.25 because the size of the range is 0.25 and can be calculated by 1-0.75
. I tried to point out how the values of the range change the formula.– Rabbid76
Nov 26 '18 at 7:38
1
1
Well, "this reason" at least could be fixed by editing it to use the explicit
Lod
versions. However, these are not allowed in the FS in the early GLSL versions, and that code seems to use 1.10 or 1.20, so that option isn't really there, either. The other quick-fix would be to always sample both textures and store the results in two different variables, and only conditionally decide between those. That would be well-defined, at least.– derhass
Nov 26 '18 at 14:51
Well, "this reason" at least could be fixed by editing it to use the explicit
Lod
versions. However, these are not allowed in the FS in the early GLSL versions, and that code seems to use 1.10 or 1.20, so that option isn't really there, either. The other quick-fix would be to always sample both textures and store the results in two different variables, and only conditionally decide between those. That would be well-defined, at least.– derhass
Nov 26 '18 at 14:51
|
show 9 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53429788%2fshow-half-portion-of-image-side-by-side-opengl%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Can you please reformat this code so that it's actually readable?
– Bartek Banachewicz
Nov 22 '18 at 11:23
please check now
– nsds
Nov 22 '18 at 11:27
What do you mean by "But left and right part of screen is not correct"? You need to provide a full detail on what is going on. "not correct" can be anything from being black, being green, seeing garbage, seeing the wrong texture with correct coordinates, seeing correct texture with wrong coordinates (not the correct part of it)...
– Matic Oblak
Nov 22 '18 at 12:14
Also define what is your expected result. Are you expecting to see all 3 images as a whole stacked one next to another or do you want to simply overlay the middle part of
image1
withimage2
?– Matic Oblak
Nov 22 '18 at 12:16