We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOpenGL and 3D Libraries › Vertex Shader in OpenGL
Page Index Toggle Pages: 1
Vertex Shader in OpenGL (Read 4277 times)
Vertex Shader in OpenGL
Mar 27th, 2006, 6:19pm
 
I have managed to load and use a Vertex Shader (In GL Shader Language) with processing.

Not all that useful, because of the processing/OpenGL different view matrix things, but someoen may be able to use it for a cool effect in certain situations.

Anyway, here's the code. It's fairly long unfortunately.

Quote:
import processing.opengl.*;
import javax.media.opengl.*;
import java.nio.*;
import com.sun.opengl.util.BufferUtil;


float phase;
float div;
String shaderSource;
GL gl;
int programObject;
int vsPhase;
boolean vertexShaderEnabled;
boolean vertexShaderSupported;

void setup()
{
 size(800,600,OPENGL);
 gl=((PGraphicsGL)g).gl;
 div=50;
 
 String extensions = gl.glGetString(GL.GL_EXTENSIONS);
 vertexShaderSupported = extensions.indexOf("GL_ARB_vertex_shader") != -1;
 vertexShaderEnabled = true;

 if (vertexShaderSupported)
 {
   String[] lines=loadStrings("wave.glsl");
   shaderSource=join(lines,"\n");

   if (shaderSource != null)
   {
     int shader = gl.glCreateShaderObjectARB(GL.GL_VERTEX_SHADER_ARB);

     gl.glShaderSourceARB(shader, 1, new String[]{shaderSource},(int[]) null, 0);
     gl.glCompileShaderARB(shader);
     checkLogInfo(gl, shader);

     programObject = gl.glCreateProgramObjectARB();

     gl.glAttachObjectARB(programObject, shader);
     gl.glLinkProgramARB(programObject);
     gl.glValidateProgramARB(programObject);
     checkLogInfo(gl, programObject);

     vsPhase=gl.glGetAttribLocationARB(programObject, "phase");
   }
 }
}

void checkLogInfo(GL gl, int obj)
{
 IntBuffer iVal = BufferUtil.newIntBuffer(1);
 gl.glGetObjectParameterivARB(obj, GL.GL_OBJECT_INFO_LOG_LENGTH_ARB, iVal);

 int length = iVal.get();
 if (length <= 1)
 {
   return;
 }
 ByteBuffer infoLog = BufferUtil.newByteBuffer(length);
 iVal.flip();
 gl.glGetInfoLogARB(obj, length, iVal, infoLog);
 byte[] infoBytes = new byte[length];
 infoLog.get(infoBytes);
 println("GLSL Validation >> " + new String(infoBytes));
}

void draw()
{
 phase-=0.01;
 if(phase<0)
   phase+=TWO_PI;
 if(phase>TWO_PI)
   phase-=TWO_PI;
 
 stroke(0,255,30);
//  fill(255,0,0);
 noFill();
 background(0);
 camera(1800,600,600,600,0,600,0,-1,0);
 if (vertexShaderEnabled)
 {
   gl.glUseProgramObjectARB(programObject);
   gl.glVertexAttrib1fARB(vsPhase, phase);
 }
  beginShape(QUADS);
  for (int x = 0; x < div; x++)
  {
    for (int z = 0; z < div; z++)
    {
       
      vertex((x)*20.0, 0, (z)*20.0);        // Draw Vertex
      vertex(((x+1))*20.0,0 , (z)*20.0);        // Draw Vertex
      vertex((x+1)*20.0, 0, ((z+1))*20.0);        // Draw Vertex
      vertex((x)*20.0, 0, ((z+1))*20.0);        // Draw Vertex
    }
  }
  endShape();
  if (vertexShaderEnabled) {
    gl.glUseProgramObjectARB(0);
  }
 if(frameCount%30==29)
   println(framerate);
}


And the contents of wave.glsl:

Code:
attribute float phase;
void main()
{
vec4 vertex = gl_Vertex;
float val;
float vx=vertex.x;
float vz=vertex.z;
val=sin(sqrt(vx*vx+vz*vz)+phase);
vertex.y=vertex.y+30.0*val;
gl_Position = gl_ModelViewProjectionMatrix * vertex;
gl_FrontColor = gl_Color;
gl_BackColor = gl_Color;
}
Re: Vertex Shader in OpenGL
Reply #1 - Mar 28th, 2006, 12:50am
 
Neat!

I haven't really had a chance to work with processing much as of late, but does anyone know why I'm getting a BufferOverflowException when I add a "lights()" line to either setup or draw?

I'm using the 110 beta.

Marcello

Quote:
Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: java.nio.BufferOverflowException
at processing.opengl.PGraphicsGL.requestDisplay(PGraphicsGL.java:247)
at processing.core.PApplet.run(PApplet.java:1142)
at java.lang.Thread.run(Unknown Source)
Caused by: java.nio.BufferOverflowException
at java.nio.DirectFloatBufferU.put(Unknown Source)
at java.nio.FloatBuffer.put(Unknown Source)
at processing.opengl.PGraphicsGL.glLightSpecular(PGraphicsGL.java:1542)
at processing.opengl.PGraphicsGL.lightSpecular(PGraphicsGL.java:1448)

java.lang.RuntimeException: java.nio.BufferOverflowException
at processing.opengl.PGraphicsGL.requestDisplay(PGraphicsGL.java:247)
at processing.core.PApplet.run(PApplet.java:1142)
at java.lang.Thread.run(Unknown Source)
Caused by: java.nio.BufferOverflowException
at java.nio.DirectFloatBufferU.put(Unknown Source)
at java.nio.FloatBuffer.put(Unknown Source)
at processing.opengl.PGraphicsGL.glLightSpecular(PGraphicsGL.java:1542)
at processing.opengl.PGraphicsGL.lightSpecular(PGraphicsGL.java:1448)
at processing.core.PGraphics3.beginFrame(PGraphics3.java:315)
at processing.opengl.PGraphicsGL.beginFrame(PGraphicsGL.java:319)
at processing.core.PApplet.display(PApplet.java:1248)
at processing.opengl.PGraphicsGL$1.display(PGraphicsGL.java:135)
at com.sun.opengl.impl.GLDrawableHelper.display(GLDrawableHelper.java:78)
at javax.media.opengl.GLCanvas$DisplayAction.run(GLCanvas.java:270)
at com.sun.opengl.impl.GLDrawableHelper.invokeGL(GLDrawableHelper.java:145)
at javax.media.opengl.GLCanvas$DisplayOnEventDispatchThreadAction.run(GLCanvas.java
:287)
at com.sun.opengl.impl.GLWorkerThread$WorkerRunnable.run(GLWorkerThread.java:241)
... 1 more

at processing.core.PGraphics3.beginFrame(PGraphics3.java:315)
at processing.opengl.PGraphicsGL.beginFrame(PGraphicsGL.java:319)
at processing.core.PApplet.display(PApplet.java:1248)
at processing.opengl.PGraphicsGL$1.display(PGraphicsGL.java:135)
at com.sun.opengl.impl.GLDrawableHelper.display(GLDrawableHelper.java:78)
at javax.media.opengl.GLCanvas$DisplayAction.run(GLCanvas.java:270)
at com.sun.opengl.impl.GLDrawableHelper.invokeGL(GLDrawableHelper.java:145)
at javax.media.opengl.GLCanvas$DisplayOnEventDispatchThreadAction.run(GLCanvas.java
:287)
at com.sun.opengl.impl.GLWorkerThread$WorkerRunnable.run(GLWorkerThread.java:241)
... 1 more
javax.media.opengl.GLException: java.nio.BufferOverflowException
at javax.media.opengl.Threading.invokeOnOpenGLThread(Threading.java:268)
at javax.media.opengl.GLCanvas.maybeDoSingleThreadedWorkaround(GLCanvas.java:245)
at javax.media.opengl.GLCanvas.display(GLCanvas.java:130)
at javax.media.opengl.GLCanvas.paint(GLCanvas.java:142)
at sun.awt.RepaintArea.paintComponent(Unknown Source)
at sun.awt.RepaintArea.paint(Unknown Source)
at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.nio.BufferOverflowException
at java.nio.DirectFloatBufferU.put(Unknown Source)
at java.nio.FloatBuffer.put(Unknown Source)
at processing.opengl.PGraphicsGL.glLightDirection(PGraphicsGL.java:1500)
at processing.opengl.PGraphicsGL.directionalLight(PGraphicsGL.java:1394)
at processing.core.PGraphics3.lights(PGraphics3.java:3632)
at processing.opengl.PGraphicsGL.lights(PGraphicsGL.java:1358)
at processing.core.PApplet.lights(PApplet.java:7062)
at Temporary_5600_1482.draw(Temporary_5600_1482.java:80)
at processing.core.PApplet.display(PApplet.java:1326)
at processing.opengl.PGraphicsGL$1.display(PGraphicsGL.java:135)
at com.sun.opengl.impl.GLDrawableHelper.display(GLDrawableHelper.java:78)
at javax.media.opengl.GLCanvas$DisplayAction.run(GLCanvas.java:270)
at com.sun.opengl.impl.GLDrawableHelper.invokeGL(GLDrawableHelper.java:145)
at javax.media.opengl.GLCanvas$DisplayOnEventDispatchThreadAction.run(GLCanvas.java
:287)
at com.sun.opengl.impl.GLWorkerThread$WorkerRunnable.run(GLWorkerThread.java:241)
at java.lang.Thread.run(Unknown Source)


edit: removed smilies ;)
Re: Vertex Shader in OpenGL
Reply #2 - Mar 28th, 2006, 4:50pm
 
it's a bug reported over here:

http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Collaboration;action=display;num=1143077846;start=15#15
Re: Vertex Shader in OpenGL
Reply #3 - Apr 5th, 2006, 11:29am
 
Sad but im no openGL coder myself.. im just wondering:
why does the vertex shader remove textures assigned in processing via textures(PImage)?

gl_FrontColor = gl_Color; has anything to do with that?

removing that line from the shader script results in... well.. some crap on the screen blinking sometimes.
Re: Vertex Shader in OpenGL
Reply #4 - Apr 5th, 2006, 12:23pm
 
If you use textures the fragment/vertex shader have to handle them, it's an all or nothing thing.. if you want to use a shadre and textures the shader has to say how the texture is used.


EDIT: After a quick fiddle, it seems fairly trivial to use textures in the shader, but you need to have a fragment shader as well as the vertex one.

It should be noted that shaders aren't actually that useful in processing, since processing munges the vertex locations before GL ever sees them, and so the projection matrixes etc won't be what they should be, which will cause problems with anything but the most basic shaders unfortunately.
Re: Vertex Shader in OpenGL
Reply #5 - Oct 17th, 2006, 2:38pm
 
You say trivial, could you supply some indication of how to do it, please.
Re: Vertex Shader in OpenGL
Reply #6 - Oct 26th, 2006, 8:37am
 
I have tried the code, and i really want to see the result of Vertex shader in Opengl.
However, I have couple errors.

like
1)
javax.media.opengl.GLException: Method "glUseProgramObjectARB" not available
at com.sun.opengl.impl.GLImpl.glUseProgramObjectARB(GLImpl.java:20962)

2)
Caused by: javax.media.opengl.GLException: Method "glUseProgramObjectARB" not available


plz help
Re: Vertex Shader in OpenGL
Reply #7 - Oct 26th, 2006, 11:08am
 
You could try removing the "ARB" from the end of the function name.

However it could also be that your video card doesn't support openGL shaders...
Re: Vertex Shader in OpenGL
Reply #8 - Nov 6th, 2006, 11:16pm
 
Is there any possibility of a flag to disable Processing's pre-OpenGL transformations in order to allow more advanced users the shader capabilities?

I would imagine doing the transformations off the processor would be way faster as well.

Jack
Re: Vertex Shader in OpenGL
Reply #9 - Nov 7th, 2006, 1:18am
 
It is possible to (effectively) remove processing's internal transformations by setting the view matrix the identity matrix by doing: camera(0,0,0,0,0,-1,0,1,0);

However you then have to do everything in OpenGL directly, or things will go screwey.

Another option is (I think) to use the proGL library which makes quite a lot more happen in GL rather than in processing.
Re: Vertex Shader in OpenGL
Reply #10 - Nov 7th, 2006, 1:22am
 
Oh cool, that'll help! Thanks!
Page Index Toggle Pages: 1