How to use a custom shader? #3230
-
|
When importing ShaderProgram, VertexShader, and FragmentShader, I cannot get them to display my GLMeshItem. gl.shaders.Shaders.append(ShaderProgram('hilight',
[
VertexShader("""
uniform mat4 u_mvp;
attribute vec4 a_position;
attribute vec3 a_normal;
attribute vec4 a_color;
varying vec4 v_color;
varying vec3 normal;
void main() {
// compute here for use in fragment shader
normal = normalize(gl_NormalMatrix * gl_Normal);
v_color = a_color;
gl_Position = u_mvp * a_position;
}
"""),
FragmentShader("""
#ifdef GL_ES
precision mediump float;
#endif
varying vec4 v_color;
varying vec3 normal;
void main() {
vec4 color = v_color;
color.x = (normal.x + 1.0) * 0.5;
color.y = (normal.y + 1.0) * 0.5;
color.z = (normal.z + 1.0) * 0.5;
gl_FragColor = color;
}
""")
]))Then calling self.current_plot = gl.GLMeshItem(
meshdata=mesh_data,
shader='hilight', # Set shader here
smooth=True,
drawFaces=True,
computeNormals=True
)This fails but the same thing with print("Shader program ID:", self.shader.program()) # Output 6What's strange is that when I edit GLMeshItem's paint function like this: def paint(self):
self.setupGLState()
self.parseMeshData()
if self.opts['drawFaces']:
with self.shader():
# Add debug lines
print("Position: " + str(glGetAttribLocation(self.shader().program(), 'a_position')))
print("Normal: " + str(glGetAttribLocation(self.shader().program(), 'a_normal')))
print("Color: " + str(glGetAttribLocation(self.shader().program(), 'a_color')))
# Rest of code...With but when This is counterintuitive since the latter is the shader that works. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
The following works on the current import sys
import pyqtgraph as pg
import pyqtgraph.opengl as gl
from pyqtgraph.opengl import shaders
import trimesh
vert_shader = """
#version 140
uniform mat4 u_mvp;
uniform mat3 u_normal;
in vec4 a_position;
in vec3 a_normal;
in vec4 a_color;
out vec4 v_color;
out vec3 v_normal;
void main() {
v_normal = normalize(u_normal * a_normal);
v_color = a_color;
gl_Position = u_mvp * a_position;
}
"""
frag_shader = """
#version 140
in vec4 v_color;
in vec3 v_normal;
out vec4 FragColor;
void main() {
float p = max(v_normal.z, 0.0);
FragColor = vec4(v_color.rgb * p, v_color.a);
}
"""
custom = shaders.ShaderProgram('custom-private-name', [
shaders.VertexShader(vert_shader),
shaders.FragmentShader(frag_shader),
])
mesh = trimesh.load_mesh(sys.argv[1])
mesh.apply_translation(-mesh.bounds.mean(axis=0))
dist = (mesh.extents**2).sum()**0.5
pg.mkQApp()
win = gl.GLViewWidget(rotationMethod='quaternion')
win.setCameraPosition(distance=dist)
win.show()
md = gl.MeshData(vertexes=mesh.vertices, faces=mesh.faces)
if mesh.visual.defined:
# pyqtgraph current master branch now supports ubyte colors
visual = mesh.visual.to_color() if mesh.visual.kind == 'texture' else mesh.visual
md.setVertexColors(visual.vertex_colors)
mitem = gl.GLMeshItem(meshdata=md, shader=custom)
win.addItem(mitem)
pg.exec() |
Beta Was this translation helpful? Give feedback.
masterbranch (0.14.0dev). (The #ifdef GL_ES is not in 0.13.7)The following works on the current
masterbranch (0.14.0dev)A sample STL file can be found at https://upload.wikimedia.org/wikipedia/commons/9/93/Utah_teapot_%28solid%29.stl