移植到JOGL的超级样本代码,但不起作用

时间:2015-02-27 09:23:11

标签: java opengl glsl jogl

我写了一些示例代码,从极好的(6th,P27-P30)到JOGL,但它没有用(应该有一个三角形,但只有绿色背景)。请看下面的代码。非常感谢任何帮助!! 原始代码:

 /*
 * Copyright ?2012-2013 Graham Sellers
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 */

#include <sb6.h>

class singlepoint_app : public sb6::application
{
    void init()
    {
        static const char title[] = "OpenGL SuperBible - Single Triangle";

        sb6::application::init();

        memcpy(info.title, title, sizeof(title));
    }

    virtual void startup()
    {
        static const char * vs_source[] =
        {
            "#version 420 core                                                 \n"
            "                                                                  \n"
            "void main(void)                                                   \n"
            "{                                                                 \n"
            "    const vec4 vertices[] = vec4[](vec4( 0.25, -0.25, 0.5, 1.0),  \n"
            "                                   vec4(-0.25, -0.25, 0.5, 1.0),  \n"
            "                                   vec4( 0.25,  0.25, 0.5, 1.0)); \n"
            "                                                                  \n"
            "    gl_Position = vertices[gl_VertexID];                          \n"
            "}                                                                 \n"
        };

        static const char * fs_source[] =
        {
            "#version 420 core                                                 \n"
            "                                                                  \n"
            "out vec4 color;                                                   \n"
            "                                                                  \n"
            "void main(void)                                                   \n"
            "{                                                                 \n"
            "    color = vec4(0.0, 0.8, 1.0, 1.0);                             \n"
            "}                                                                 \n"
        };

        program = glCreateProgram();
        GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
        glShaderSource(fs, 1, fs_source, NULL);
        glCompileShader(fs);

        GLuint vs = glCreateShader(GL_VERTEX_SHADER);
        glShaderSource(vs, 1, vs_source, NULL);
        glCompileShader(vs);

        glAttachShader(program, vs);
        glAttachShader(program, fs);

        glLinkProgram(program);

        glGenVertexArrays(1, &vao);
        glBindVertexArray(vao);
    }

    virtual void render(double currentTime)
    {
        static const GLfloat green[] = { 0.0f, 0.25f, 0.0f, 1.0f };
        glClearBufferfv(GL_COLOR, 0, green);

        glUseProgram(program);
        glDrawArrays(GL_TRIANGLES, 0, 3);
    }

    virtual void shutdown()
    {
        glDeleteVertexArrays(1, &vao);
        glDeleteProgram(program);
    }

private:
    GLuint          program;
    GLuint          vao;
};

DECLARE_MAIN(singlepoint_app)

JOGL代码:

import java.awt.Dimension; 
import java.awt.event.WindowAdapter; 
import java.awt.event.WindowEvent; 
import java.nio.IntBuffer; 
import java.nio.ByteBuffer;

import javax.media.opengl.GL; 
import javax.media.opengl.GL2; 
import javax.media.opengl.GLAutoDrawable; 
import javax.media.opengl.GLEventListener; 
import javax.media.opengl.awt.GLCanvas; 
import javax.swing.JFrame; 
import javax.swing.SwingUtilities; 

import com.jogamp.opengl.util.FPSAnimator; 
import com.jogamp.opengl.util.GLBuffers; 


public class TestVerAttributes extends GLCanvas implements GLEventListener 
{ 
   private static final long serialVersionUID = 1L; 

   // Define constants for the top-level container 
   private static String TITLE = "Test glsl"; 
   private static final int CANVAS_WIDTH = 320;  // width of the drawable 
   private static final int CANVAS_HEIGHT = 240; // height of the drawable 
   private static final int FPS = 60; // animator's target frames per second 

   private IntBuffer vaoNameBuff = GLBuffers.newDirectIntBuffer(1); 
   private int shaderProgram; 
   private float green[] = { 0.0f, 0.25f, 0.0f, 1.0f }; 

   //private float temp; 

   public TestVerAttributes() 
   { 
      addGLEventListener(this); 
   } 

   public void init(GLAutoDrawable drawable) 
   { 
      GL2 gl = drawable.getGL().getGL2(); 

      initShaders(gl); 
   } 

   public void initShaders(GL2 gl) 
   { 
      int verShader = gl.glCreateShader(GL2.GL_VERTEX_SHADER); 
      int fraShader = gl.glCreateShader(GL2.GL_FRAGMENT_SHADER); 

      String[] vsrc = 
                    { 
                            "#version 400    \n" + 
                            "void main(void) \n" + 
                            "{        \n" + 
                            " const vec4 vertices[] = vec4[](vec4(0.25, -0.25, 0.5, 1), \n" + 
                            " vec4(-0.25, -0.25, 0.5, 1), \n" + 
                            " vec4( 0.25, 0.25, 0.5, 1)); \n" + 
                            " gl_Position = vertices[gl_VertexID]; \n" + 
                            "}" 
                    }; 

      gl.glShaderSource(verShader, 1, vsrc, null); 
      gl.glCompileShader(verShader); 
      printShaderInfoLog(gl, verShader);

      String[] fsrc = 
                    { 
                            "#version 400     \n" + 
                            "out vec4 color;  \n" + 
                            "void main( void) \n" + 
                            "{ \n" + 
                            " color = vec4(0.0, 0.8, 1.0, 1.0); \n" + 
                            "}" 
                    }; 

      gl.glShaderSource(fraShader, 1, fsrc, null); 
      gl.glCompileShader(fraShader); 
      printShaderInfoLog(gl, fraShader);

      shaderProgram = gl.glCreateProgram(); 
      gl.glAttachShader(shaderProgram, verShader); 
      gl.glAttachShader(shaderProgram, fraShader); 
      gl.glLinkProgram(shaderProgram); 
      gl.glValidateProgram(shaderProgram); 
      printProgramInfoLog(gl, shaderProgram);

      gl.glGenVertexArrays(1, vaoNameBuff); 
      gl.glBindVertexArray(vaoNameBuff.get(0)); 
   } 

   public void printShaderInfoLog(GL2 gl, int shader)
   {
      IntBuffer intBuffer = IntBuffer.allocate(1);
      gl.glGetShaderiv(shader, GL2.GL_INFO_LOG_LENGTH, intBuffer);

      if (intBuffer.get(0) > 12)
      {
          int size = intBuffer.get(0);

          System.err.println("Shader compiling error: ");

          if (size > 0)
          {
              ByteBuffer byteBuffer = ByteBuffer.allocate(size);

              gl.glGetShaderInfoLog(shader, size, intBuffer, byteBuffer);

              for (byte b : byteBuffer.array())
              {
                  System.err.print((char) b);
              }
          }
          else
          {
              System.out.println("Unknown");
          }

          System.exit(1);
       }
   }

   public void printProgramInfoLog(GL2 gl, int program)
   {
      IntBuffer intBuffer = IntBuffer.allocate(1);
      gl.glGetProgramiv(program, GL2.GL_INFO_LOG_LENGTH, intBuffer);

      if (intBuffer.get(0) > 1)
      {
          int size = intBuffer.get(0);

          System.err.println("Shader compiling error: ");

          if (size > 0)
          {
              ByteBuffer byteBuffer = ByteBuffer.allocate(size);

              gl.glGetProgramInfoLog(program, size, intBuffer, byteBuffer);

              for (byte b : byteBuffer.array())
              {
                  System.err.print((char) b);
              }
          }
          else
          {
              System.out.println("Unknown");
          }

          System.exit(1);
      }
   }

   public void dispose(GLAutoDrawable drawable) 
   { 
      GL2 gl = drawable.getGL().getGL2(); 

      gl.glDeleteVertexArrays(1, vaoNameBuff); 
      gl.glDeleteProgram(shaderProgram); 
   } 

   public void display(GLAutoDrawable drawable) 
   { 
      GL2 gl = drawable.getGL().getGL2(); 

      /*float attrib[] = { 
            (float) Math.sin(temp) * 0.5f, 
            (float) Math.cos(temp) * 0.6f, 
            0.0f, 0.0f 
            }; 

      if (temp < 3.14f) 
      { 
         temp += 0.01f; 
      } 
      else 
      { 
         temp = 0f; 
      }*/ 

      gl.glClearBufferfv(GL2.GL_COLOR, 0, green, 0); 
      gl.glUseProgram(shaderProgram); 
      //gl.glVertexAttrib4fv(0, attrib, 0); 
      gl.glDrawArrays(GL.GL_TRIANGLES, 0, 3); 
   } 

   public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) 
   { 

   } 

   /** The entry main() method to setup the top-level container and animator */ 
   public static void main(String[] args) 
   { 
      // Run the GUI codes in the event-dispatching thread for thread safety 
      SwingUtilities.invokeLater(new Runnable() 
      { 
         public void run() 
         { 
            // Create the OpenGL rendering canvas 
               GLCanvas canvas = new TestVerAttributes(); 
               canvas.setPreferredSize(new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT)); 

               // Create a animator that drives canvas' display() at the specified FPS. 
               final FPSAnimator animator = new FPSAnimator(canvas, FPS, true); 

               // Create the top-level container 
               final JFrame frame = new JFrame(); // Swing's JFrame or AWT's Frame 
               frame.getContentPane().add(canvas); 
               frame.addWindowListener(new WindowAdapter() 
               { 
                  public void windowClosing(WindowEvent e) 
                  { 
                     // Use a dedicate thread to run the stop() to ensure that the 
                     // animator stops before program exits. 
                     new Thread() 
                     { 
                        public void run() 
                        { 
                           if (animator.isStarted()) 
                           { 
                              animator.stop(); 
                           } 
                           System.exit(0); 
                        } 
                     }.start(); 
                  } 
               }); 
               frame.setTitle(TITLE); 
               frame.pack(); 
               frame.setVisible(true); 
               animator.start(); // start the animation loop 
         } 
      }); 
   } 
} 

1 个答案:

答案 0 :(得分:0)

事情是我忘了在笔记本电脑上启用Nvidia显卡。这是英特尔集成的图形卡,它不支持继续工作的GL着色语言版本4.0+。

还发现了一些小错误。 “;”错过了这一行:“color = vec4(0.0,0.8,1.0,1.0); \ n”+。

添加了着色器信息日志和着色器程序链接信息日志的跟踪。

现在我上面发布的代码应该可以按预期工作了。