Hi,guys. I hava a problem about texture,after texture mapping, I can't draw any elements like QUADS and so on.
Here is my codes, I will really appreciate if any one can help me solve the problem. package xmu.edu.software.clm.shader; import java.io.File; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.IntBuffer; import java.util.ArrayList; import java.util.Date; import java.util.Timer; import javax.media.opengl.GL; import javax.media.opengl.GL2; import javax.media.opengl.GLAutoDrawable; import javax.media.opengl.GLEventListener; import javax.media.opengl.GLException; import javax.media.opengl.awt.GLCanvas; import javax.media.opengl.glu.GLU; import com.sun.opengl.util.awt.Screenshot; import common.FileReader; public class TextureRender implements GLEventListener{ private GLCanvas canvas; private GLU glu=new GLU(); private int imgLocation; private int ssLocation; private ByteBuffer buffer=ByteBuffer.allocate(512*512*3); public static ArrayList<Point> points=new ArrayList<Point>(); public static float gScale=1.0f; public static int layer=8; public static int[] textureInt=new int[layer]; public TextureRender(GLCanvas canvas){ this.canvas=canvas; points.add(new Point(128,384)); points.add(new Point(384,128)); } @Override public void display(GLAutoDrawable drawable) { System.out.println("size="+points.size()); GL2 gl=drawable.getGL().getGL2(); gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT); gl.glLoadIdentity(); gl.glTranslatef(0.0f, 0.0f, -2.0f); gl.glBegin(GL2.GL_LINES); for(int i=0;i<points.size()-1;i++){ float x1=((float)points.get(i).getX()*gScale/512)*4-2; float y1=((512.0f-points.get(i).getY()*gScale)/512)*4-2; float x2=((float)points.get(i+1).getX()*gScale/512)*4-2; float y2=((512.0f-points.get(i+1).getY()*gScale)/512)*4-2; gl.glColor3f(1.0f, 0.0f, 1.0f); gl.glVertex2f(x1-0.01f, y1+0.01f); gl.glVertex2f(x2-0.01f, y2+0.01f); gl.glColor3f(1.0f, 1.0f, 0.0f); gl.glVertex2f(x1+0.01f, y1-0.01f); gl.glVertex2f(x2+0.01f, y2-0.01f); } gl.glEnd(); diffuse(gl); gl.glLoadIdentity(); gl.glTranslatef(0.0f, 0.0f, -2.0f); gl.glColor3f(0.0f, 1.0f, 1.0f); gl.glBegin(GL2.GL_QUADS);//it did't draw here gl.glVertex2f(-1.0f, -1.0f); gl.glVertex2f(-1.0f, 1.0f); gl.glVertex2f(1.0f, 1.0f); gl.glVertex2f(1.0f, -1.0f); gl.glEnd(); gl.glFlush(); } @Override public void dispose(GLAutoDrawable arg0) { GL2 gl=arg0.getGL().getGL2(); gl.glDeleteTextures(layer, textureInt, 0); } @Override public void init(GLAutoDrawable drawable) { GL2 gl=drawable.getGL().getGL2(); gl.glShadeModel(GL2.GL_SMOOTH); gl.glClearColor(0.6f, 0.6f, 0.6f, 1.0f); gl.glClearDepth(1.0f); gl.glEnable(GL2.GL_DEPTH_TEST); gl.glDepthFunc(GL2.GL_LEQUAL); gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NEAREST); gl.glGenTextures(layer, textureInt, 0); genTexture(gl,layer); } @Override public void reshape(GLAutoDrawable gLDrawable, int x, int y, int width, int height) { final GL2 gl = gLDrawable.getGL().getGL2(); if (height <= 0) // avoid a divide by zero error! { height = 1; } int side=width<height?width:height; gl.glViewport((width - side) / 2, (height - side) / 2, side, side); gl.glMatrixMode(GL2.GL_PROJECTION); gl.glLoadIdentity(); glu.gluPerspective(90.0f, 1, 1.0, 1000.0); gl.glMatrixMode(GL2.GL_MODELVIEW); gl.glLoadIdentity(); } public void diffuse(GL2 gl){ long costTime = 0; long texTime=0; int accup=setAccuShader(gl); int ss=1; gl.glUniform1i(imgLocation, 0); for(int i=0;i<TextureRender.layer;i++){ gl.glUniform1i(ssLocation, ss); gl.glActiveTexture(GL2.GL_TEXTURE0); gl.glBindTexture(GL2.GL_TEXTURE_2D, TextureRender.textureInt[i]); gl.glReadPixels(0, 0, 512, 512, GL2.GL_RGB, GL2.GL_BYTE, buffer); gl.glTexImage2D(GL2.GL_TEXTURE_2D, 0, 3, 512, 512, 0, GL2.GL_RGB, GL2.GL_BYTE, buffer); gl.glEnable(GL2.GL_TEXTURE_2D); gl.glBegin(GL2.GL_QUADS); gl.glTexCoord2f(0.0f,0.0f); gl.glVertex2f(-2.0f, -2.0f); gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex2f(2.0f, -2.0f); gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex2f(2.0f, 2.0f); gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex2f(-2.0f, 2.0f); gl.glEnd(); gl.glDisable(GL2.GL_TEXTURE_2D); ss*=2; } gl.glDeleteProgram(accup); } public void genTexture(GL2 gl,int num){ for (int i = 0; i < num; i++) { gl.glBindTexture(GL2.GL_TEXTURE_2D, textureInt[i]); gl.glTexImage2D(GL2.GL_TEXTURE_2D, 0, 3, 512, 512, 0, GL2.GL_RGB, GL2.GL_BYTE, null); gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR); gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR); gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_S, GL2.GL_CLAMP_TO_EDGE); gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_T, GL2.GL_CLAMP_TO_EDGE); gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_R, GL2.GL_CLAMP_TO_EDGE); } } public int setAccuShader(GL2 gl){ int f=gl.glCreateShader(GL2.GL_FRAGMENT_SHADER); String[] fragString=FileReader.readFile("shaders/shaderAccu.frag"); gl.glShaderSource(f, 1, fragString, null, 0); gl.glCompileShader(f); int p=gl.glCreateProgram(); gl.glAttachShader(p, f); gl.glLinkProgram(p); gl.glValidateProgram(p); IntBuffer intBuffer = IntBuffer.allocate(1); gl.glGetProgramiv(p, GL2.GL_LINK_STATUS, intBuffer); if(intBuffer.get(0) != 1){ gl.glGetProgramiv(p, GL2.GL_INFO_LOG_LENGTH, intBuffer); int size = intBuffer.get(0); System.err.println("Program link error: "); if (size > 0) { ByteBuffer byteBuffer = ByteBuffer.allocate(size); gl.glGetProgramInfoLog(p, size, intBuffer, byteBuffer); for (byte b : byteBuffer.array()) { System.err.print((char) b); } } else { System.out.println("Unknown"); } System.exit(1); } imgLocation=gl.glGetUniformLocation(p, "img"); ssLocation=gl.glGetUniformLocation(p, "scale"); gl.glUseProgram(p); System.out.println("accu shader called!"); return p; } } |
Administrator
|
Hi
Are you sure that the use of textures breaks your example? Rather enable and disable GL_TEXTURE_2D once in diffuse(), don't do it inside the loop, don't mix immediate mode with shaders (it is not reliable on some drivers). Use com.jogamp.opengl.util.ImmModeSink or explicitly switch to VBOs. Don't use glActiveTexture() if you always use only one texture unit. Don't use glFlush(), it is useless in your case. Maybe call glUseProgram(0) when you don't need to use your shader any more.
Julien Gouesse | Personal blog | Website
|
Thanks very much. And, you are right, when I call glUseProgram(0), it's OK now.
|
Administrator
|
You're welcome, I'm really happy for you.
Julien Gouesse | Personal blog | Website
|
Free forum by Nabble | Edit this page |