Is it possible to combine texture map and vertex color in one glsl shader?
in
Programming Questions
•
4 months ago
I'm trying to write a GLSL shader that will let me combine per-vertex color information with a texture map (essentially multiplying the texture map's color values with the colors from the vertices.) But I can't seem to make it pay attention to both at once! If I turn on the texture, it ignores the per-vertex colors entirely.
Am I missing something obvious here? Or does Processing somehow prevent you from combining textures with per-vertex coloring? (And if so, is there any kind of workaround for this limitation?)
Here's the example sketch I'm using:
- PImage label;
- PShape can;
- float angle;
- PShader texShader;
- void setup() {
- size(640, 360, P3D);
- label = loadImage("deer.jpg");
- can = createCan(100, 200, 32, label);
- texShader = loadShader("texfrag.glsl", "texvert.glsl");
- //texShader.set("color", color(255, 0, 0));
- }
- void draw() {
- background(0);
- shader(texShader);
- translate(width/2, height/2);
- rotateY(angle);
- shape(can);
- angle += 0.01;
- }
- PShape createCan(float r, float h, int detail, PImage tex) {
- textureMode(NORMAL);
- PShape sh = createShape();
- sh.beginShape(QUAD_STRIP);
- sh.noStroke();
- sh.texture(tex); // comment out this line to see the vertex colors!
- for (int i = 0; i <= detail; i++) {
- float angle = TWO_PI / detail;
- float x = sin(i * angle);
- float z = cos(i * angle);
- float u = float(i) / detail;
- sh.fill(random(0, 255), random(0, 255), random(0, 255));
- sh.normal(x, 0, z);
- sh.vertex(x * r, -h/2, z * r, u, 0);
- sh.vertex(x * r, +h/2, z * r, u, 1);
- }
- sh.endShape();
- return sh;
- }
- #define PROCESSING_TEXTURE_SHADER
- uniform mat4 transform;
- uniform mat4 texMatrix;
- attribute vec4 vertex;
- attribute vec4 color;
- attribute vec2 texCoord;
- varying vec4 vertColor;
- varying vec4 vertTexCoord;
- void main() {
- gl_Position = transform * vertex;
- vertColor = color;
- vertTexCoord = texMatrix * vec4(texCoord, 1.0, 1.0);
- }
- #ifdef GL_ES
- precision mediump float;
- precision mediump int;
- #endif
- uniform sampler2D texture;
- varying vec4 vertColor;
- varying vec4 vertTexCoord;
- void main() {
- gl_FragColor = texture2D(texture, vertTexCoord.st) * vertColor;
- }
If I comment out the "sh.texture(tex)" command, Processing ignores my shader entirely, and the per-vertex colors (random colors) show up. But when that line is present, the vertex colors are ignored, and I just see the texture map of "deer.jpg" wrapped around the cylinder.
1