fragment-shader

Access to 3D array in fragment shader

我的未来我决定 提交于 2019-12-01 22:31:32
I'm trying to provide access to a 3-dimensional array of scalar data to a fragment shader, from a Python program using PyOpenGL. In the fragment shader, I declare the a 3d sampler uniform uniform sampler3D vol; and in the Python program I have the following code to set up a scalar 3d texture vol = numpy.random.rand(3, 3, 3).astype(np.float32) texture = glGenTextures(1) glUniform1i(glGetUniformLocation(program, "vol"), 0) glActiveTexture(GL_TEXTURE0 + 0) glBindTexture(GL_TEXTURE_3D, texture) glTexImage3D(GL_TEXTURE_3D, 0, GL_RED, 3, 3, 3, 0, GL_RED, GL_FLOAT, vol) glEnable(GL_TEXTURE_3D)

How vertex and fragment shaders communicate in OpenGL?

人盡茶涼 提交于 2019-12-01 08:54:46
问题 I really do not understand how fragment shader works. I know that vertex shader runs once per vertices fragment shader runs once per fragment Since fragment shader does not work per vertex but per fragment how can it send data to the fragment shader? The amount of vertices and amount of fragments are not equal. How can it decide which fragment belong to which vertex? 回答1: To make sense of this, you'll need to consider the whole render pipeline. The outputs of the vertex shader (besides the

LookAt function: I'm going crazy

时光毁灭记忆、已成空白 提交于 2019-12-01 03:44:43
I must do a homework and I try to implement a lookAt function. I tried in many ways but the only result I got is a blue screen. The rest of my program works greatly, infact if I use glm::lookAt all is good. This is my code: mat4 Transform::lookAt(const vec3 &eye, const vec3 &center, const vec3 &up) { vec3 w(glm::normalize(eye - center)) ; vec3 u(glm::normalize(glm::cross(up, w))); vec3 v(glm::cross(w, u)) ; mat4 ret = mat4 ( vec4 (u.x,v.x,w.x,0), vec4 (u.y,v.y,w.y,0), vec4 (u.z,v.z,w.z,0), vec4 (-u.x*eye.x-u.y*eye.y-u.z*eye.z, -v.x*eye.x-v.y*eye.y-v.z*eye.z, -w.x*eye.x-w.y*eye.y-w.z*eye.z, 1)

Count pixels by color in webgl fragment shader

一笑奈何 提交于 2019-11-30 20:36:22
问题 I have 2d texture S and want to return 3d texture H, such that pixel H[r,g,b] is equal to number of pixels of color rgb in texture S. Basically histogram of colors in texture S. I know about occlusion queries, but it's only available in webgl2, and IIUC even there only with boolean results, and besides I would need to do separate query for each color. Ideally I'd like to do this in one fragment shader pass. Is there any way to do reduce (fold) operations in fragment shaders? Why I need this

Compute normals from displacement map in three.js r.58?

好久不见. 提交于 2019-11-30 10:22:08
I'm using the normal shader in three.js r.58, which I understand requires a normal map . However, I'm using a dynamic displacement map, so a pre-computed normal map won't work in this situation. All the examples I've found of lit displacement maps either use flat shading or pre-computed normal maps. Is it possible to calculate the normals dynamically based on the displaced vertices instead? Edit: I've posted a demo of a sphere with a displacement map showing flat normals : Here's a link to the github repo with all of my examples illustrating this problem, and the solutions I eventually found:

How to define constant array in GLSL (OpenGL ES 2.0)?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-30 08:49:14
I just want to store an array of weights that needs to every fragment calculation. This: float weights[5] = float[5](3.4, 4.2, 5.0, 5.2, 1.1); Just throws this: ERROR: 0:30: ']' : syntax error syntax error ERROR: 0:30: ';' : syntax error syntax error Stefan Hanke From the OpenGL ES SL 1.0 spec , paragraph 4.1.9 Arrays (p. 24): There is no mechanism for initializing arrays at declaration time from within a shader. Note that this has been intentionally left out . According to this post , the OpenGL ES SL version for OpenGL ES 2 is based on OpenGL SL 1.2 . The same paragraph (p. 20) contains:

GLSL: How to get pixel x,y,z world position?

ⅰ亾dé卋堺 提交于 2019-11-29 23:02:19
I want to adjust the colors depending on which xyz position they are in the world. I tried this in my fragment shader: varying vec4 verpos; void main(){ vec4 c; c.x = verpos.x; c.y = verpos.y; c.z = verpos.z; c.w = 1.0; gl_FragColor = c; } but it seems that the colors change depending on my camera angle/position, how do i make the coords independent from my camera position/angle? Heres my vertex shader: varying vec4 verpos; void main(){ gl_Position = ftransform(); verpos = gl_ModelViewMatrix*gl_Vertex; } Edit2: changed title, so i want world coords, not screen coords! Edit3: added my full code

Compute normals from displacement map in three.js r.58?

人走茶凉 提交于 2019-11-29 15:39:36
问题 I'm using the normal shader in three.js r.58, which I understand requires a normal map. However, I'm using a dynamic displacement map, so a pre-computed normal map won't work in this situation. All the examples I've found of lit displacement maps either use flat shading or pre-computed normal maps. Is it possible to calculate the normals dynamically based on the displaced vertices instead? Edit: I've posted a demo of a sphere with a displacement map showing flat normals: Here's a link to the

How to define constant array in GLSL (OpenGL ES 2.0)?

亡梦爱人 提交于 2019-11-29 12:04:44
问题 I just want to store an array of weights that needs to every fragment calculation. This: float weights[5] = float[5](3.4, 4.2, 5.0, 5.2, 1.1); Just throws this: ERROR: 0:30: ']' : syntax error syntax error ERROR: 0:30: ';' : syntax error syntax error 回答1: From the OpenGL ES SL 1.0 spec, paragraph 4.1.9 Arrays (p. 24): There is no mechanism for initializing arrays at declaration time from within a shader. Note that this has been intentionally left out . According to this post, the OpenGL ES SL

GLSL: Replace large uniform int array with buffer or texture

蹲街弑〆低调 提交于 2019-11-29 06:57:13
Right now I am trying to pass an array of ints into the fragment shader, and am doing this through a uniform array: uniform int myArray[300]; And filling it outside the shader with glUniform1iv . Unfortunately, uniform arrays larger than ~400 fail. I understand that I can use a "uniform buffer" instead, but can't seem to a find a full example of passing a large 1D array into a fragment shader with buffers or otherwise. Could anyone supply such an example? This should get you started using a Uniform Buffer Object to store an array. Note that GL requires UBOs to have a minimum capacity of 16 KiB