glsl

Unable to use '%' in glsl

ⅰ亾dé卋堺 提交于 2019-12-04 04:24:29
While writing a shader program today, I encountered a situation where I have to use % to find the remainder. GLSL gave me an error saying that it is not available in the current version. I've tried several problems. GLSL doesn't support recursive function and while loops, which is needed if I want to create a function that can give me the result of (a % b) . So, I'm currently stuck. Can someone help me with this? Edit . I was trying to emulate a CRT screen using some shader code from this website as reference code. I wanted to modify the color of the pixels at certain row and columns, so I

关于GLSL的gl_FragCoord、gl_FragDepth以及深度计算

♀尐吖头ヾ 提交于 2019-12-04 04:09:53
gl_FragCoord和gl_FragDepth分别是片元着色器的输入和输出变量。 gl_FragCoord是个vec4,四个分量分别对应x, y, z和1/w。其中,x和y是当前片元的窗口相对坐标,不过它们不是整数,小数部分恒为0.5。x - 0.5和y - 0.5分别位于[0, windowWidth - 1]和[0, windowHeight - 1]内。windowWidth和windowHeight都以像素为单位,亦即用glViewPort指定的宽高。w即为乘过了投影矩阵之后点坐标的w,用于perspective divide的那个值。gl_FragCoord.z / gl_FragCoord.w可以得到当前片元和camera之间的距离。参见Fog in GLSL page 4。 gl_FragCoord.z是固定管线计算出的当前片元的深度。它已经考虑了多边形偏移,并经过了投影变换。它位于[0.0, 1.0]之间。如果用gl_FragColor = vec4(vec3(gl_FragCoord.z), 1.0)将其可视化,多半会看到一片白。这是由于变换的非线性,大多数点的深度都非常接近于1。用gl_FragColor = vec4(vec3(pow(gl_FragColor.z, exp)), 1.0)并将exp取为合适的值,就能看到从黑到白的深度变化了

Can't set uniform value in OpenGL shader

≯℡__Kan透↙ 提交于 2019-12-04 04:01:03
问题 I am currently learning OpenGL programming, and I have made some good progress, but I seem to be stuck now. Currently, I am trying to get simple shaders to work. To be more specific, I am trying to set a uniform value of the shader . The shaders compile successfully, and work correctly if I eliminate the uniform value. Here is the (vertex) shader code: #version 440 core layout(location = 0) in vec3 vertex; uniform mat4 mvp; void main() { gl_Position = mvp * vec4(vertex, 1); } And the code I

GLSL: “Invalid call of undeclared identifier 'texture2D'”

£可爱£侵袭症+ 提交于 2019-12-04 02:50:20
I'm on a Mac, using Swift, and using OpenGL 3.2. I'm also using XCode 6.1 Beta so I suppose that's the most likely explanation, because it doesn't seem to me like this makes sense. I can't find any evidence that this shouldn't be supported, but this fragment shader is resulting in the error Invalid call of undeclared identifier 'texture2D' during compilation: #version 150 uniform sampler2D usTexture; in vec2 vTextureCoord; out vec4 FragColor; void main() { vec4 color = texture2D(usTexture, vTextureCoord); FragColor = color; } Cripes. Finally found the answer right after I posted the question.

Textured points in OpenGL ES 2.0?

こ雲淡風輕ζ 提交于 2019-12-04 02:43:29
I'm trying to implement textured points (e.g. point sprites) in OpenGL ES 2.0 for a particle system. Problem I'm having is the points all render as solid black squares, rather than having the texture properly mapped. I have verified that gl_PointCoord is in fact returning x/y values from 0.0 to 1.0, which would map across the entire texture. The texture2D call always seems to return black though. My vertex shader : attribute vec4 aPosition; attribute float aAlpha; attribute float aSize; varying float vAlpha; uniform mat4 uMVPMatrix; void main() { gl_PointSize = aSize; vAlpha = aAlpha; gl

Referencing texels in a data texture using indexes in the shader

こ雲淡風輕ζ 提交于 2019-12-04 02:38:32
问题 I have values in the texels of a DataTexture that I am trying to access using indexes in my shader. The indexes [0, 1, 2, 3, 4, 5, 6... 62, 63] are continuous, while the data texture has a height and width ( uTextureDimension ) of 8. After some research I wrote this function to take a particular index value, and reference the corresponding texel: vec2 customUV = vec2( mod(aIndex, uTextureDimension) / uTextureDimension, floor(aIndex / uTextureDimension) / uTextureDimension ); vec4 texelValues

How to copy depth buffer to a texture on the GPU?

不问归期 提交于 2019-12-04 02:34:51
I want to get the current depth buffer to a texture, to access it in a shader. For various reasons I can't do a separate depth pass, but would need to copy the already-rendered depth. glReadPixels would involve the CPU and potentially kill performance, and as far as I know glBlitFramebuffer can't blit depth-to-color, only depth-to-depth. How to do this on the GPU? The modern way of doing this would be to use a FBO. Attach a color and depth texture to it, render, then disable the FBO and use the textures as inputs to a shader that will render to the default framebuffer. All the details you need

OpenGL es 2.0 Gaussian blur on triangle

大憨熊 提交于 2019-12-04 02:00:40
问题 I recently learn opengl es 2.0, and now I try to make a gaussian blur on triangles generate by myself. I have some difficult to understand examples on the web and most apply the blur on an image. I know I have to use framebuffer but I don't know how to draw triangle on this and apply blur. Is it possible to see a real and complete code in C++ with good explication ? EDIT : #include <stdio.h> #include <stdlib.h> #include <iostream> #define GLFW_INCLUDE_ES2 #include <GLFW/glfw3.h> #include

Enabling an extension on a Three.js shader

可紊 提交于 2019-12-04 01:39:21
How can I enable an extension on a Three.js shader? My code so far: getting extension: var domElement = document.createElement( 'canvas' ); var gl = domElement.getContext('webgl') || domElement.getContext('experimental-webgl'); gl.getExtension('OES_standard_derivatives'); on my shader: fragmentShader: [ "#extension GL_OES_standard_derivatives : enable", "code..." ]... The console output: WARNING: 0:26: extension 'GL_OES_standard_derivatives' is not supported ERROR: 0:32: 'dFdx' : no matching overloaded function found ERROR: 0:32: '=' : cannot convert from 'const mediump float' to '2-component

WebGL: Loop index cannot be compared with non-constant expression

我的梦境 提交于 2019-12-04 01:19:02
问题 I have the a webgl blur shader: precision mediump float; precision mediump int; uniform sampler2D u_image; uniform float blur; uniform int u_horizontalpass; // 0 or 1 to indicate vertical or horizontal pass uniform float sigma; // The sigma value for the gaussian function: higher value means more blur // A good value for 9x9 is around 3 to 5 // A good value for 7x7 is around 2.5 to 4 // A good value for 5x5 is around 2 to 3.5 // ... play around with this based on what you need :) varying vec4