纹理不显示:OpenGL(JOGL)

时间:2014-06-01 22:40:21

标签: java opengl jogl

我想在屏幕上绘制几张图片。但是,不显示自己的图像。只有它们必须具有的区域与背景颜色不同。你能帮我找错吗?我看到几个主题,其中simillar projlem是通过从oppoisite方面看纹理引起的。但我不明白如何改变方面。

package game; 

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.DisplayMode; 
import java.awt.GraphicsDevice; 
import java.awt.GraphicsEnvironment; 
import java.awt.Point; 
import java.awt.Rectangle; 
import java.awt.Toolkit; 
import java.awt.event.ActionEvent; 
import java.awt.event.KeyEvent; 
import java.awt.event.WindowAdapter; 
import java.awt.event.WindowEvent; 
import java.io.File; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.Collections; 



import javax.media.opengl.GL2; 
import javax.media.opengl.GLAutoDrawable; 
import javax.media.opengl.GLCapabilities; 
import javax.media.opengl.GLEventListener; 
import javax.media.opengl.GLProfile; 
import javax.media.opengl.awt.GLCanvas; 
import javax.media.opengl.glu.GLU; 
import javax.swing.AbstractAction; 
import javax.swing.ActionMap; 
import javax.swing.InputMap; 
import javax.swing.JFrame; 
import javax.swing.JOptionPane; 
import javax.swing.JPanel; 
import javax.swing.KeyStroke; 

import com.jogamp.opengl.util.FPSAnimator; 
import com.jogamp.opengl.util.texture.Texture; 
import com.jogamp.opengl.util.texture.TextureIO; 

public class StartingClass implements GLEventListener { 
        private static GraphicsEnvironment graphicsEnviorment; 

        private GLU glu = new GLU(); 

        private int texture; 
        private static int B_WIDTH = 800; 
        private static int B_HEIGHT = 600; 
        private Dog dog; 
        private Wolf wolf; 
        private ArrayList<Sheep> sheeps; 
        private boolean ingame; 

        @Override 
        public void display(GLAutoDrawable drawable) { 
                // TODO Auto-generated method stub 
                final GL2 gl = drawable.getGL().getGL2(); 
                gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT); 
                // Clear // The // Screen // And // The 
                // Depth 
                // Buffer 
                gl.glLoadIdentity(); // Reset The View 
                gl.glBindTexture(GL2.GL_TEXTURE_2D, dog.getTexture()); 
                gl.glBegin(GL2.GL_QUADS); 
                // Front Face 
                Rectangle rect = dog.getBounds(); 
                gl.glTexCoord2f(rect.x, rect.y); 
                gl.glVertex3f(rect.x, rect.y, 0.0f); 
                gl.glTexCoord2f(rect.x + rect.width, rect.y); 
                gl.glVertex3f(rect.x + rect.width, rect.y, 0.0f); 
                gl.glTexCoord2f(rect.x + rect.width, rect.y + rect.height); 
                gl.glVertex3f(rect.x + rect.width, rect.y + rect.height, 0.0f); 
                gl.glTexCoord2f(rect.x, rect.y + rect.height); 
                gl.glVertex3f(rect.x, rect.y + rect.height, 0.0f); 
                gl.glEnd(); 


                gl.glFlush(); 

        } 

        @Override 
        public void dispose(GLAutoDrawable drawable) { 
                // TODO Auto-generated method stub 

        } 

        @Override 
        public void init(GLAutoDrawable drawable) { 
                dog = new Dog(B_WIDTH / 2, B_HEIGHT / 2); 

                final GL2 gl = drawable.getGL().getGL2(); 

                gl.glEnable(GL2.GL_TEXTURE_2D); 
                try { 
                        File im = new File("src/game/dog.jpg"); 
                        Texture t = TextureIO.newTexture(im, true); 
                        dog.setTexture(t.getTextureObject(gl)); 
                } catch (IOException e) { 
                        e.printStackTrace(); 
                } 

        } 

        @Override 
        public void reshape(GLAutoDrawable drawable, int x, int y, int width, 
                        int height) { 
                // TODO Auto-generated method stub 
                final GL2 gl = drawable.getGL().getGL2(); 

                if (height <= 0) 
                        height = 1; 
                final float h = (float) width / (float) height; 
                gl.glViewport(0, 0, width, height); 
                gl.glMatrixMode(GL2.GL_PROJECTION); 
                gl.glLoadIdentity(); 
                gl.glOrtho(0, width, height, 0, 0, 1); 

                gl.glMatrixMode(GL2.GL_MODELVIEW); 
                gl.glLoadIdentity(); 
        } 

        /** 
         * @param args 
         */ 
        public static void main(String[] args) { 
                // TODO Auto-generated method stub 
                // setUp open GL version 2 
                final GLProfile profile = GLProfile.get(GLProfile.GL2); 
                GLCapabilities capabilities = new GLCapabilities(profile); 

                // The canvas 
                final GLCanvas glcanvas = new GLCanvas(capabilities); 
                StartingClass r = new StartingClass(); 
                glcanvas.addGLEventListener(r); 
                glcanvas.setSize(B_WIDTH, B_HEIGHT); 

                final FPSAnimator animator = new FPSAnimator(glcanvas, 300, true); 

                final JFrame frame = new JFrame("sheppard"); 

                frame.getContentPane().add(glcanvas); 

                // Shutdown 
                frame.addWindowListener(new java.awt.event.WindowAdapter() { 
                        @Override 
                        public void windowClosing(java.awt.event.WindowEvent windowEvent) { 
                                if (JOptionPane.showConfirmDialog(frame, 
                                                "Are you sure to close this window?", 
                                                "Really Closing?", JOptionPane.YES_NO_OPTION, 
                                                JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) { 
                                        if (animator.isStarted()) 
                                                animator.stop(); 
                                        System.exit(0); 
                                } 
                        } 
                }); 

                frame.setSize(frame.getContentPane().getPreferredSize()); 
                /** 
                 * Centers the screen on start up 
                 * 
                 */ 
                graphicsEnviorment = GraphicsEnvironment.getLocalGraphicsEnvironment(); 

                GraphicsDevice[] devices = graphicsEnviorment.getScreenDevices(); 

                Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 

                int windowX = Math.max(0, (screenSize.width - frame.getWidth()) / 2); 
                int windowY = Math.max(0, (screenSize.height - frame.getHeight()) / 2); 

                frame.setLocation(windowX, windowY); 
                /** 
                                 * 
                                 */ 
                frame.setVisible(true); 
                /* 
                 * Time to add Button Control 
                 */ 
                JPanel p = new JPanel(); 
                p.setPreferredSize(new Dimension(0, 0)); 
                frame.add(p, BorderLayout.SOUTH); 
                frame.setResizable(false); 
                animator.start(); 
        } 

        //methods to generate sheeps' positions 


} 

1 个答案:

答案 0 :(得分:0)

您正在使用GL_TEXTURE_2D。从dog.getBounds()回来的界限是什么?使用GL_TEXTURE_2D时,纹理坐标应该标准化(介于0和1之间)。既然您已将当前矩阵(我假设是ModelView矩阵?)设置为标识,则整个窗口应在x和y的-1到1范围内。

在大多数情况下,我不希望图像的边界有用作纹理坐标和顶点坐标。例如,图像可能是640x480,但您希望将其显示在恰好位于坐标(-5,-5)并延伸到(15,10)的矩形中。在这种情况下,纹理坐标在GL_TEXTURE_2D的两个方向上仍然是0-1。

我会检查所涉及的所有坐标是什么,看看它们是否有意义,就像你设置OpenGL正在绘制到窗口的世界一样。