【问题标题】:Texture Not Displayed: OpenGL (JOGL)未显示纹理:OpenGL (JOGL)
【发布时间】:2014-06-01 22:40:21
【问题描述】:

我正在尝试在屏幕上绘制多个图像。但是,不显示图像本身。只有它们必须存在的区域与背景颜色不同。你能帮我找出错误吗?我看到的主题很少,其中类似的问题是通过从对面观察纹理引起的。但我不明白如何正确换边。

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 


} 

【问题讨论】:

    标签: java opengl jogl


    【解决方案1】:

    您使用的是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 绘制到窗口的世界的方式一样有意义。

    【讨论】:

      猜你喜欢
      • 2012-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多