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 › Texture in 'pure' openGL
Pages: 1 2 
Texture in 'pure' openGL (Read 7308 times)
Texture in 'pure' openGL
Jan 3rd, 2008, 11:24pm
 
Hi, I'm a big fan of talking directly to my graphics card because of the low-level control it gives me and generally that works just fine. But I do have a problem applying textures: I don't know how to get the data from a PImage type into my openGL code correctly.

I've tried stuff like:
...
PImage texmap;
...
texmap = loadImage("uitzicht.jpg");
intBuffer = IntBuffer.wrap(texmap.pixels[]);
...
gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, gl.GL_RGBA, texmap.width, texmap.height, 0, gl.GL_RGBA, gl.GL_UNSIGNED_INT, intBuffer);
...

but the data inside my intbuffer doesn't seem to be configured correctly, somehow. Does somebody here know how to convert this data into a format that the glTexImage2D command understands?

Thanks to anyone who's even willing to think about this question!
Re: Texture in 'pure' openGL
Reply #1 - Jan 4th, 2008, 1:38am
 
If you don't need to access the texture info in a processing style, you can bypass the PImage level entirely, and go with something like:

Code:
import com.sun.opengl.util.texture.*;

Texture tex;

void setup()
{
//... normal setup
try
{
tex=TextureIO.newTexture(new File(dataPath("uitzicht.jpg")),true);
tex.setTexParameteri(GL.GL_TEXTURE_WRAP_R,GL.GL_REPEAT);
tex.setTexParameteri(GL.GL_TEXTURE_WRAP_S,GL.GL_REPEAT);
tex.setTexParameteri(GL.GL_TEXTURE_WRAP_T,GL.GL_REPEAT);
}
catch(Exception e)
{
println(e);
}
}

void draw()
{
// go into pure GL mode
tex.bind();
tex.enable();
gl.glTexCoord2f(0,0); //0.0 -> 1.0 on both axes.
gl.glVertex3f(0,0,0);
//more texcoords/verts
tex.disable();

}


Code unfortunately untested, but cribbed form a program I have that does use them so any errors should just be minor.
Re: Texture in 'pure' openGL
Reply #2 - Jan 11th, 2008, 3:58pm
 
Thanks a lot, that sure gives me a lot more possibilities to work with.

I would still be very glad though if I could also do this with an PImage object (so I can, indeed, do some processing on the image beforehand)

- T
Re: Texture in 'pure' openGL
Reply #3 - Jan 11th, 2008, 8:25pm
 
you can also instanciate a Texture using a java.awt.BufferedImage :

TextureIO.newTexture(java.awt.BufferedImage image, boolean mipmap)

that means, you just need to convert the PImage you're having fun with into a java.awt.BufferedImage
Re: Texture in 'pure' openGL
Reply #4 - Jan 11th, 2008, 8:45pm
 
I believe that there is an internal BufferedImage within a PImage, though whether it's accessible is another matter.
Re: Texture in 'pure' openGL
Reply #5 - Jan 23rd, 2008, 1:02pm
 
Here is my OPENGL / PImage bypass:

Code:

import processing.opengl.*;
import javax.media.opengl.*;
import com.sun.opengl.util.texture.*;

Texture tex;

void setup()
{
 size(1200,800, OPENGL);
 try { tex=TextureIO.newTexture(new File(dataPath("red_circuit.png")),true); }
 catch(Exception e) { println(e); }
}

void draw()
{
 background(0);
 
 PGraphicsOpenGL pgl = (PGraphicsOpenGL) g;
 GL gl = pgl.beginGL();

 tex.bind();
 tex.enable();

 gl.glBegin(GL.GL_QUADS);
 gl.glNormal3f( 0.0f, 0.0f, 1.0f);
 gl.glTexCoord2f(0, 0); gl.glVertex2f(0, 98);
 gl.glTexCoord2f(1, 0); gl.glVertex2f(251, 98);
 gl.glTexCoord2f(1, 1); gl.glVertex2f(251, 0);
 gl.glTexCoord2f(0, 1); gl.glVertex2f(0, 0);
 gl.glEnd();  

 tex.disable();

 pgl.endGL();
}


works, but why is Y flipped?


Re: Texture in 'pure' openGL
Reply #6 - Jan 23rd, 2008, 4:56pm
 
it could be your texture set up to flip y, or it is just opengl that differs and works with a flipped y-axis
simple use a a value to flip your texcoords or object mesh data
Re: Texture in 'pure' openGL
Reply #7 - Feb 1st, 2008, 3:44pm
 
All right, works fine so far. But, the textureloader com.sun.opengl.util.texture converts my png with straight alpha into premultiplied alpha:

tex=TextureIO.newTexture(new File(dataPath("alpha_3.png")),true);

This makes a black halo effect. I know that I can blend it with:

gl.glBlendFunc(GL.GL_ONE, GL.GL_ONE_MINUS_SRC_ALPHA);

instead of:

gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);

But when I use multiple Layers, which are also blended it looks strangly added. Hm, has anyone a link to a Textureloader which does no premultiply?

sorry for my bad english ascorbin


Re: Texture in 'pure' openGL
Reply #8 - Feb 1st, 2008, 5:25pm
 
Unfortunately it's the Texture class that does the premultiply according to the documentation, so you'll have to load the image manually, and faff with lower level GL commands yourself to use it.
Re: Texture in 'pure' openGL
Reply #9 - Feb 1st, 2008, 7:42pm
 
OK, I have it! Who ever will ever need this. Layer your premultiplied alpha pics with:

gl.glBlendFunc(GL.GL_ONE, GL.GL_ONE_MINUS_SRC_ALPHA);

and then you can blend them with:

gl.glColor4f(alpha, alpha, alpha, alpha);

or stay in the normal mode an do:

gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
gl.glColor4f(1, 1, 1, alpha);

It took me some days to find this, I hope someone can use it.

Re: Texture in 'pure' openGL
Reply #10 - Apr 28th, 2008, 5:36pm
 
Instead of messing around with blend modes why not use TextureData instead of Texture? That way you don't have to worry about all the automatic stuff Texture does to your data. I know it might be a little more work since you have to generate mipmaps, filters et cetera, but at the end doesn't this give you more control?
Re: Texture in 'pure' openGL
Reply #11 - Apr 29th, 2008, 1:26am
 
ascorbin wrote on Feb 1st, 2008, 7:42pm:
OK, I have it! Who ever will ever need this. Layer your premultiplied alpha pics with:

gl.glBlendFunc(GL.GL_ONE, GL.GL_ONE_MINUS_SRC_ALPHA);

and then you can blend them with:

gl.glColor4f(alpha, alpha, alpha, alpha);

or stay in the normal mode an do:

gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
gl.glColor4f(1, 1, 1, alpha);

It took me some days to find this, I hope someone can use it.



This sounds good. It would be amazing if you could post a complete example.
Re: Texture in 'pure' openGL
Reply #12 - Apr 29th, 2008, 3:23pm
 
I have modified the code to the following. if I set the org_opengl to false, it seems fine; however, if I set the org_opengl to ture, the texture is distorted.

Is the processing bug???

Plz help

=======================================================
import processing.opengl.*;
import javax.media.opengl.*;
import com.sun.opengl.util.texture.*;  
boolean org_opengl=true;

Texture tex;  
PImage ti;
void setup()  
{  
 size(480,320, OPENGL);
 if(org_opengl)
 {
   ti = loadImage("test.png");
   textureMode(NORMALIZED);
 } else
 {
   try { tex=TextureIO.newTexture(new File(dataPath("test.png")),true); }
   catch(Exception e) { println(e); }
 }
}  

void draw()  
{  
 background(0);

 // the texture size is 300x300
 if(org_opengl)
 {
   beginShape();
   texture(ti);
   vertex(290, 160, 1, 0.5);
   vertex(265, 205, 236.0/300.0, 1);  
   vertex(215, 205, 64.0/300.0, 1);
   vertex(190, 160, 0, 0.5);
   vertex(215, 115, 64.0/300.0, 0);
   vertex(265, 115, 236.0/300.0, 0);
   endShape();
 } else
 {
   PGraphicsOpenGL pgl = (PGraphicsOpenGL) g;  
   GL gl = pgl.beginGL();  

   tex.bind();  
   tex.enable();  

   gl.glBegin(GL.GL_POLYGON);
   gl.glNormal3f( 0.0f, 0.0f, 1.0f);
   gl.glTexCoord2f(1, 0.5);         gl.glVertex2f(290, 160);
   gl.glTexCoord2f(236.0/300.0, 1); gl.glVertex2f(265, 205);
   gl.glTexCoord2f(64.0/300.0, 1);  gl.glVertex2f(215, 205);
   gl.glTexCoord2f(0, 0.5);         gl.glVertex2f(190, 160);
   gl.glTexCoord2f(64.0/300.0, 0);  gl.glVertex2f(215, 115);
   gl.glTexCoord2f(236.0/300.0, 0); gl.glVertex2f(265, 115);
   gl.glEnd();  

   tex.disable();  

   pgl.endGL();
 }
}
Re: Texture in 'pure' openGL
Reply #13 - May 2nd, 2008, 12:33pm
 
ive stumbled upon a texture tutorial on the trusty NeHe site (http://nehe.gamedev.net/) while trying to work out a texture problem in processing.
i wanted to have full control over what commands were made to opengl and not worry about what was under the hood.

anyway i think ive arrived at nice solution that exposes all the opengl calls needed for creating and applying textures. its especially handy when you need to change any texture parameters.

here is link to a version adapted for processing.
http://www.julapy.com/processing/TextureTest.pde

Re: Texture in 'pure' openGL
Reply #14 - May 2nd, 2008, 11:14pm
 
julapy wrote on May 2nd, 2008, 12:33pm:
ive stumbled upon a texture tutorial on the trusty NeHe site (http://nehe.gamedev.net/) while trying to work out a texture problem in processing.
i wanted to have full control over what commands were made to opengl and not worry about what was under the hood.

anyway i think ive arrived at nice solution that exposes all the opengl calls needed for creating and applying textures. its especially handy when you need to change any texture parameters.

here is link to a version adapted for processing.
http://www.julapy.com/processing/TextureTest.pde


This is some amazing code! Thank you.

Anyone have any ideas on how to make the texture transparent
Pages: 1 2