Quantcast
Channel: Processing Forum
Viewing all articles
Browse latest Browse all 1768

Re : How to use pgl.drawElement ?

$
0
0
Hello All !

I'm far from my initial question but I found the solution I was looking for ! :)

It's actually possible to add custom attributes that affect Vertex & Fragment shader, and it's very easy to do !
Here is an exemple based on the 'TextureQuad' example :

  1. import java.nio.*;

  2. PImage img;
  3. PShader testShader;
  4. PGL pgl;

  5. float[] scales;
  6. float[] colors;

  7. FloatBuffer scaleData;
  8. FloatBuffer colorData;

  9. int scaleLoc;
  10. int colorLoc;

  11. void setup() {
  12.   size(640, 360, P3D);
  13.   img = loadImage("Desert.jpg");
  14.   testShader = loadShader("fragment.glsl","vertex.glsl");
  15.   
  16.   scales = new float[16];
  17.   scales[0] = scales[1] = scales[2] = scales[3] = scales[4] = scales[5] = scales[6] = scales[7] = scales[8] = scales[9] = scales[10] = scales[11] = scales[12] = scales[13] = scales[14] = scales[15] = 0.5;
  18.   
  19.   colors = new float[16];
  20.   colors[0] = colors[4] = colors[8] = colors[12] = 1;
  21.   colors[1] = colors[5] = colors[9] = colors[13] = 0;
  22.   colors[2] = colors[6] = colors[10] = colors[14] = 0;
  23.   colors[3] = colors[7] = colors[11] = colors[15] = 1;
  24.   
  25.   
  26.   scaleData = allocateDirectFloatBuffer(16);
  27.   scaleData.put(scales);
  28.   scaleData.position(0);  
  29.   
  30.   
  31.   colorData = allocateDirectFloatBuffer(16);
  32.   colorData.put(colors);
  33.   colorData.position(0);  
  34.   
  35.   noStroke();
  36.  
  37. }

  38. void draw() {
  39.   background(0);
  40.   translate(width / 2, height / 2);
  41.   rotateY(map(mouseX, 0, width, -PI, PI));
  42.   rotateZ(PI/6);
  43.   
  44.   pgl = beginPGL();
  45.   
  46.   scaleLoc = pgl.getAttribLocation(testShader.glProgram, "inScale");
  47.   pgl.enableVertexAttribArray(scaleLoc);
  48.   pgl.vertexAttribPointer(scaleLoc, 4, PGL.FLOAT, false, 0, scaleData);
  49.   
  50.   colorLoc = pgl.getAttribLocation(testShader.glProgram, "inColour");
  51.   pgl.enableVertexAttribArray(colorLoc);
  52.   pgl.vertexAttribPointer(colorLoc, 4, PGL.FLOAT, false, 0, colorData);
  53.   
  54.   endPGL();
  55.   
  56.   shader(testShader);
  57.   
  58.   beginShape();
  59.   
  60.   texture(img);
  61.   vertex(-100, -100, 0, 0, 0);
  62.   vertex(100, -100, 0, img.width, 0);
  63.   vertex(100, 100, 0, img.width, img.height);
  64.   vertex(-100, 100, 0, 0, img.height);
  65.   
  66.   endShape();
  67.   
  68.   
  69.   resetShader();
  70. }

  71. FloatBuffer allocateDirectFloatBuffer(int n) {
  72.   return ByteBuffer.allocateDirect(n * Float.SIZE/8).order(ByteOrder.nativeOrder()).asFloatBuffer();
  73. }

My vertexShader 
  1. uniform mat4 projmodelviewMatrix;

  2. //custom attributes
  3. attribute vec4 inScale;
  4. attribute vec4 inColour; //inColor cannot be overrided (it always a white color)
  5.                          //so I renamed it 'colour' for the test.                         
  6. attribute vec4 inVertex;
  7. attribute vec2 inTexcoord;

  8. varying vec2 vertTexcoord;
  9. varying vec4 vertColor;

  10. void main() {
  11.   // Vertex in clip coordinates
  12.   vec4 pos = projmodelviewMatrix * inVertex;
  13.   pos.xy *= inScale.xy;
  14.   gl_Position = pos;
  15.   
  16.   vertTexcoord = inTexcoord;
  17.   vertColor = inColour;
  18. }
and my fragmentShader
  1. #ifdef GL_ES
  2. precision mediump float;
  3. precision mediump int;
  4. #endif

  5. uniform sampler2D textureSampler;
  6. varying vec2 vertTexcoord;
  7. varying vec4 vertColor;

  8. void main() {
  9.   vec4 texture = texture2D(textureSampler, vertTexcoord);
  10.   texture.xyz *= vertColor.xyz;
  11.   gl_FragColor = texture;
  12. }


The whole code draw a textured-colorTransformed-scaled quad. The color used in the colorTransform operation come from the 'fill' instruction. 

It's much more easy than Flash, I'm impressed ! :)


EDIT : I have an error "OpenGL error 1281 at top endDraw(): invalid value" but all works fine. Other people already had these error but they didn't know why (all works fine for them too)


Viewing all articles
Browse latest Browse all 1768

Trending Articles