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 › OpenGL Texture 101 help
Page Index Toggle Pages: 1
OpenGL Texture 101 help (Read 1644 times)
OpenGL Texture 101 help
Aug 6th, 2009, 2:24am
 
Hi,
I am trying to apply a texture to a quad, obviously I fail, I browse the forum but I dont understand the procedure on getting this to work, so here is what I got

Main sketch
Code:
// tutorial from
// http://nehe.gamedev.net/
// under OpenGL Tutorials section


import processing.opengl.PGraphicsOpenGL;
import javax.media.opengl.GL;
import javax.media.opengl.glu.GLU;

GLU glu;
PGraphicsOpenGL pgl;
GL gl;

PImage boxTexture;
Cube cube;

void setup() {
 size(320, 240, OPENGL);
 
 boxTexture = loadImage("box-up.jpg");
 
 glu = new GLU();
 pgl = (PGraphicsOpenGL) g;
 
 cube = new Cube(boxTexture);
}

void draw() {
 background(0);

 gl = pgl.beginGL();
 
 gl.glMatrixMode(GL.GL_PROJECTION);
 gl.glLoadIdentity();
 // calc aspect ratio of the window
 glu.gluPerspective(45.0f, width / height, 0.1f, 100.0f);
 gl.glMatrixMode(GL.GL_MODELVIEW);
 // clear the screen and the depth buffer
 gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
 
 drawGLScene();
 
 pgl.endGL();
}

void drawGLScene() {
 gl.glLoadIdentity();
 gl.glTranslatef(1.5f, 0.0f, -8.0f);
 
 cube.setGL(gl);
 cube.update();
 cube.display();
}


Cube class
Code:
class Cube {
 
 GL gl;
 
 PImage boxTexture;
 int texSize;
 int[] tex = new int[1];
 
 float rquad;
 
 
 Cube(PImage _texture) {
   boxTexture = _texture;
   texSize = boxTexture.width;
   //println(tex);
 }
 
 
 void setGL(GL _gl) {
   gl = _gl;
 }
 
 void update() {
   // rotate square
   rquad += 0.5f;
   gl.glRotatef(rquad, 1.0f, 0.0f, 0.0f);
   gl.glRotatef(rquad, 0.0f, 1.0f, 0.0f);
   gl.glRotatef(rquad, 0.0f, 0.0f, 1.0f);
 }

 void display() {
   gl.glEnable(GL.GL_TEXTURE_2D);
   gl.glGenTextures(1, tex, 0);
   gl.glBindTexture(GL.GL_TEXTURE_2D, tex[0]);
   gl.glTexImage2D(GL.GL_TEXTURE_2D,
                   0,
                   3,
                   boxTexture.width,
                   boxTexture.height,
                   0,
                   GL.GL_RGB,
                   GL.GL_UNSIGNED_BYTE,
                   null);
   gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR);
   gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
   
   gl.glBegin(GL.GL_QUADS);
     // top face
     gl.glTexCoord2f(0.0f, 0.0f);  gl.glVertex3f(-1.0f, -1.0f,  1.0f);
     gl.glTexCoord2f(1.0f, 0.0f);  gl.glVertex3f( 1.0f, -1.0f,  1.0f);
     gl.glTexCoord2f(1.0f, 1.0f);  gl.glVertex3f( 1.0f,  1.0f,  1.0f);
     gl.glTexCoord2f(0.0f, 1.0f);  gl.glVertex3f(-1.0f,  1.0f,  1.0f);
     
     
   gl.glEnd();
 }

}


To be honest I just trow an arrow there, and hope for the best, I just dont get this 3 methods
glGenTextures
glBindTexture
glTexImage2D


Thanks
rS
Re: OpenGL Texture 101 help
Reply #1 - Aug 6th, 2009, 8:02am
 
Found it!

First you need to import
Code:
import com.sun.opengl.util.texture.Texture;
import com.sun.opengl.util.texture.TextureIO;


So you can load the texture like
Code:

Texture tex;
tex = TextureIO.newTexture(new File(dataPath(boxTexture)), true);



And finally you just need to call
Code:
tex.enable();
tex.bind();
// draw the 3D stuff here
tex.disable();


If you can explain how to do it the other way will be great!

Cheers
rS
Re: OpenGL Texture 101 help
Reply #2 - Aug 6th, 2009, 8:37am
 
Hi nardove,

The method above works in the IDE, but when you try to go online with the sketch, you won't be able to use dataPath or sketchPath.  I recently dealt with this, here's my answer:

Code:

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

 InputStream texStream = openStream("texture.png");
 if (tex != null) tex.dispose();
 try{
   texLoad = TextureIO.newTextureData(texStream,true,TextureIO.PNG);
 }
 catch(Exception e){
   System.out.println("ERROR LOADING TEXTURE: " + e);
 }
 if (texLoad != null) tex=TextureIO.newTexture(texLoad);
 else println ("TEXTURE WAS NULL");


The reason I include tex.dispose() is that I have a habit of including a reset button capable of running setup twice.  Textures don't like being loaded twice...

After this, you won't need:
gl.glGenTextures(1, tex, 0);
gl.glBindTexture(GL.GL_TEXTURE_2D, tex[0]);
gl.glTexImage2D(GL.GL_TEXTURE_2D, ...)
gl.glEnable(GL.GL_TEXTURE_2D);
int[] tex = new int[1];

^ as far as I know, these methods are useful if you're trying to generate and bind a texture on the fly from a set of primitives.

And to clarify the alternatives that you found:
tex.bind() //<-- once, during setup: the most time-consuming operation
tex.enable(); //<-- before drawing the texture
tex.disable(); //<-- after drawing the texture, to allow untextured primitives to be drawn again -- and as far as I know, you can even omit this and enable the texture only once, during setup, if all you are drawing is the textured quad.

your:
gl.glTexParameteri
calls can be run once, during setup().

This is a minor thing, but I would mentally categorize glLoadIdentity with glMatrixMode, and group them together.  They both deal with setting the identity matrix.

And the cube.setGL(_gl) doesn't do anything for you; the gl=pgl.beginGL() is fine.  I Believe that clearing the color/depth buffers is not necessary with Processing, but I'd be happy to be corrected on any of this.  If you want parts of your texture to be transparent, there are some difficulties -- I've found this article very helpful:
http://www.opengl.org/wiki/Alpha_Blending

GL, Ben
Re: OpenGL Texture 101 help
Reply #3 - Aug 6th, 2009, 9:26am
 
Hi Ben, thanks for the explanations, helps a lot, now you said that my setGL method in the Cube Class doesnt do anything, but I need it to pass the gl to the class in order to be able to call the gl stuff, is it any other way to work around this?

Cheers
rS
Re: OpenGL Texture 101 help
Reply #4 - Aug 6th, 2009, 9:48am
 
Oh -- you need to get rid of the duplicate GL gl inside the cube class.  Then it will inherit the "gl" you define in setup.
Cheers, Ben
Page Index Toggle Pages: 1