Subrect drawing to texture FBO within OpenGL ES Fragment Shader - opengl-es

I'm trying to draw to a subrect of a texture based FBO, but am having difficulty. The FBO has dimensions of say 500x500 and I am trying to have the fragment shader only redraw say a 20x20 pixel subrect. Modiyfing the full texture works without difficulty.
At first I tried setting glViewport to the needed subrect, but it doesn't look to be that simple. I'm suspecting that the Vertex attributes affecting gl_Position and the varying texture coordinates are involved, but I can't figure out how.

Turns out that I was trying to modify the texture coordinate attributes, but was more easily able to just modify the viewport using glViewport and gl_FlagCoord within the shader.

Related

Improve texture resolution in GLSL fragment shader

Is there a way to icrease, or improve resolution of a texture using GLSL fragment shader processing? Let's say, I have 512x424 px source image, and want to have 1024x848 px as a result, with smooth pixels.
Update.
Under "improvement" I mean enlarging using some sort of resampling algorithm.
Create FBO, attach large destination texture with desired dimensions
Render "full-screen" textured quad with small source texture bound

three.js flat color using FlatShading and a texture

I'm creating a terrain with three.js and texturing with some grass texture I found, and applying FlatShading so it looks low poly, but the shading only modifies that and I'm still seeing the texture applied, I need each face to have a flat color and not the texture, like this picture:
http://i.ytimg.com/vi/9WFBnc_gPMo/maxresdefault.jpg
Unity using PolyWorld asset, from a terrain with a grass texture for example, it applies flat shading and also uses a flat color as texture (predominant color?)
What you seem to want is inefficient, but one way to accomplish it would be to make sure that all three faceVertexUV values for each triangle face are the same: that is for a triangle ABC the UV coordinates are all, say (.4,.6),(.4,.6),(.4,.6)
This means that all pixels of the rendered triangle will have that one uniform UV and you'll always get the same texture color across the triangle (some filtering notwithstanding in very extreme foreshortening cases)

Three.js - get image dimensions and pass to fragment shader

I'm sending a bitmap as a uniform to a fragment shader with THREE.ImageUtils.loadTexture(). What's the simplest way for the fragment shader to access the size of the image in pixels?
Pass the values you want to the shader as uniforms.

How to access the depth value of the neighbor pixels in WebGL?

I want to make a toon border effect. For it, I'll use the depth value of the neighbor pixels of each pixel to determine if it is or isn't supposed to be blacked. How can I access that information inside the fragment shader?
When you render your scene in a normal way (vertex shader, then fragment shader - single pass) then in the fragment shader there is no way to access depth values for another pixels.
But:
You can render scene twice and perform some postprocessing effects. In the first run you store depth values and others (like normals, etc) in RenderTarget (in texture) then you use those textures in the second pass.
Here you have effect from XNA, but can be quickly ported to GLSL: http://xnameetingpoint.weebly.com/shader7f31.html
Here some link about Render to Texture: http://learningwebgl.com/blog/?p=1786
Hint: depth values will not be enough for border detection, you have to use normals as well. But it is covered in the above tutorial from XNA.

How to draw a border around a texture using GLSL

I want to create some textured rectangles (I guess the jargon for this is 'quads" :D) with OpenGL ES 2.0 and move them on screen following mouse pointer.
But now comes the "advanced" part: I want that all these rectangles to have a border around them; I could do this by simply overpainting the texture images in software to draw the borders on top of them and after that pass the modified (sw "bordered") texture data to the shaders; But I want to do this in hardware, in the shaders (either vertex or fragment shader or both).
Is this possible? If yes can someone post the GLSL shaders code for this?
One idea would be to test if either coordinate of the UV is less than 0.1 or greater than 0.9, and then replace the texture texel with a border color if the test is true.

Resources