Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (579)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Java Game APIs & Engines / OpenGL Development / Re: VBOs and Shaders in JOGL on: 2011-08-04 22:05:33
Okay thank you, I plan on getting the red book at some point; maybe I should make some point now.
2  Java Game APIs & Engines / OpenGL Development / Re: VBOs and Shaders in JOGL on: 2011-08-04 02:51:11
Ok yeah I'd fixed some of the nonsensical values for drawRangeElements (or so I though I had) in the latest paste of the code, but I really had no idea what to do to convert the coordinates to screen coordinates. And on a side note it works Smiley Thank you very much everyone for your help. And ihkbob do you have any recommendation for future reference for where to learn how to deal with openGL matrices?
3  Java Game APIs & Engines / OpenGL Development / Re: VBOs and Shaders in JOGL on: 2011-08-04 01:08:04
Mm yeah I tried what I think you suggested and it still didn't work, I think I'm going to try looking at the code here: http://www.spacesimulator.net/wiki/index.php/Tutorials:OpenGL_and_Glut_(OpenGL3.3) and porting the matrix related things over.
On a side note, how do you specify the link text for a link on this forum?
4  Java Game APIs & Engines / OpenGL Development / Re: VBOs and Shaders in JOGL on: 2011-08-03 23:39:11
So I fixed my shader compiling/linking/etc code and now glGetAttribLocation returns 0 (the first index) and the program compiles and runs without complaint, however all I'm getting right now is a screen drawn to the clearcolor.
Here's my shader loading code for anyone trying to follow in my footsteps.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.nio.CharBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Scanner;

import javax.media.opengl.DebugGL3;
import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GL3;
import javax.media.opengl.GLAutoDrawable;

public class ShaderProgram {


   int p;
   boolean valid = false;

   File vertf;
   File fragf;
   
   String []vertS = {"#version 150\nin vec3 vertex;\nvoid main()\n{\ngl_Position = vec4(vertex,1.0);\n}"};
   String []fragS = {"#version 150\nout vec4 fragmentColor;\nvoid main()\n{\nfragmentColor = vec4(0.0, 1.0, 0.0, 1.0);\n}"};
   
   public ShaderProgram(GLAutoDrawable drawable, File vertf, File fragf) {
      this.vertf = vertf;
      this.fragf = fragf;        
      DebugGL3 gl = new DebugGL3( drawable.getGL().getGL3());

      p = gl.glCreateProgram();

      int v = gl.glCreateShader(gl.GL_VERTEX_SHADER);      
      int f = gl.glCreateShader(gl.GL_FRAGMENT_SHADER);
      readAndCompileShader(v, vertf, gl);
      readAndCompileShader(f, fragf, gl);

      gl.glLinkProgram(p);      



      // check link status
     int[] query = new int[1];
      gl.glGetProgramiv(p, GL3.GL_LINK_STATUS, query, 0);
      if (query[0] == GL.GL_TRUE){
         System.out.println("P: successful"); // program linked successfully
        valid = true;
      }else{
         // link failed, read the log and return it
        gl.glGetProgramiv(p, GL3.GL_INFO_LOG_LENGTH, query, 0);
         int maxLogLength = query[0];
         if (maxLogLength > 0) {
            byte[] log = new byte[maxLogLength];
            gl.glGetProgramInfoLog(p, maxLogLength, query, 0, log, 0);

            valid = false;
            System.out.println(new String(log, 0, query[0]));
         } else{
            valid = false;
            System.out.println("unknown link error");
         }
      }
   }
   
   void readAndCompileShader(int shader, File sourceF, DebugGL3 gl){
      String [] source = new String[1];
      try {
         source[0] = readFile(sourceF);
      } catch (IOException e) {
         System.out.println("Could not find shader file");
      }
      source[0] = source[0].replaceAll("\r\n", "\n");
      //System.out.print(source[0]);
     
      int[] lengths = new int[]{source[0].length()};

      gl.glShaderSource(shader, 1, source, lengths, 0);        
      gl.glCompileShader(shader);
      gl.glAttachShader(p,shader);

      // query compile status and possibly read log
     int[] status = new int[1];
      gl.glGetShaderiv(shader, GL3.GL_COMPILE_STATUS, status, 0);
      if (status[0] == GL.GL_TRUE){
         System.out.println("shader: succesful");
         // everything compiled successfully, no log
     }else{
         // compile failed, read the log and return it
        gl.glGetShaderiv(shader, GL3.GL_INFO_LOG_LENGTH, status, 0);
         int maxLogLength = status[0];
         if (maxLogLength > 0) {
            byte[] log = new byte[maxLogLength];
            gl.glGetShaderInfoLog(shader, maxLogLength, status, 0, log, 0);

            System.out.println("shader: " + new String(log, 0, status[0]));
         } else
            System.out.println("shader: "+ "unknown compilation error");
      }
   }
   
   private static String readFile(File file) throws IOException {
        FileInputStream stream = new FileInputStream(file);
        try {
          FileChannel fc = stream.getChannel();
          MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
          /* Instead of using default, pass in a decoder. */
          return Charset.forName("UTF-8").decode(bb).toString();
        }
        finally {
          stream.close();
        }
      }
}
5  Java Game APIs & Engines / OpenGL Development / Re: VBOs and Shaders in JOGL on: 2011-08-03 21:13:05
Yeah that doesn't change anything, still getting the same
Vertex shader(s) failed to link, fragment shader(s) failed to link.
ERROR: error(#280) Not all shaders have valid object code
ERROR: error(#280) Not all shaders have valid object code
after the shaders report compiling successfully. *sigh*
I guess I'll try updating my drivers and running it on a different computer.

EDIT:
The problem is definitely in the way I read in my shaders; hardcoding in the shaders as a string array makes everything compile fine (although I still need to figure out how to use them to draw)
6  Java Game APIs & Engines / OpenGL Development / Re: VBOs and Shaders in JOGL on: 2011-08-03 03:42:35
vec3 is the type of the input value vertex, and it's used to set the position of the vertex.
And vec4 is saying that there is an output that has 4 pieces of data.
7  Java Game APIs & Engines / OpenGL Development / Re: VBOs and Shaders in JOGL on: 2011-08-03 03:11:54
I thought they were correct, they look like this:
vertex shader code
1  
2  
3  
4  
5  
6  
7  
#version 150
in vec3 vertex;
void
main()
{
    gl_Position = vec4(vertex,1.0);
}

fragment shader code
1  
2  
3  
4  
5  
6  
7  
#version 150
out vec4 fragmentColor;
void
main()
{
    fragmentColor = vec4(0.0, 1.0, 0.0, 1.0);
}
8  Java Game APIs & Engines / OpenGL Development / Re: VBOs and Shaders in JOGL on: 2011-08-03 02:17:09
Okay so I fixed my shaders by removing the trailing ;, and modified my shader reading code to use nextline and added on \n to the end of each string in the source array and that made the shaders compiled without error, however when I try to attach the shaders and compile the program it says this:

Vertex shader(s) failed to link, fragment shader(s) failed to link.
ERROR: error(#280) Not all shaders have valid object code
ERROR: error(#280) Not all shaders have valid object code

Current shader code look like this
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.Scanner;

import javax.media.opengl.DebugGL3;
import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GL3;
import javax.media.opengl.GLAutoDrawable;

public class ShaderProgram {

   public ShaderProgram(GLAutoDrawable drawable, File vertf, File fragf) {
      this.vertf = vertf;
      this.fragf = fragf;        
      DebugGL3 gl = new DebugGL3( drawable.getGL().getGL3());

      p = gl.glCreateProgram();

      //VERTEX STUFF
     int v = gl.glCreateShader(gl.GL_VERTEX_SHADER);

      ArrayList <String> vSourceA = new ArrayList<String>();
      Scanner vert = null;
      try {
         vert = new Scanner(vertf);
      } catch (FileNotFoundException e) {
         System.out.println("Vertex shader file was not found");
      }

      vert.useDelimiter("\n");
      while(vert.hasNextLine()){
         String temp = vert.nextLine() + "\n";
         vSourceA.add(temp);
         System.out.print(temp);
      }

      String[] vSource = new String[vSourceA.size()];
      vSource = vSourceA.toArray(vSource);
      int [] lengths= new int[vSource.length];
      for(int i = 0; i < vSource.length; i++){
         lengths[i] = vSource[i].length();
      }

      gl.glShaderSource(v, 1, vSource, lengths, 0);        
      gl.glCompileShader(v);
      gl.glAttachShader(p,v);

      // query compile status and possibly read log
     int[] status = new int[1];
      gl.glGetShaderiv(v, GL3.GL_COMPILE_STATUS, status, 0);
      if (status[0] == GL.GL_TRUE){
         System.out.println("v: succesful");
         // everything compiled successfully, no log
     }else{
         // compile failed, read the log and return it
        gl.glGetShaderiv(v, GL3.GL_INFO_LOG_LENGTH, status, 0);
         int maxLogLength = status[0];
         if (maxLogLength > 0) {
            byte[] log = new byte[maxLogLength];
            gl.glGetShaderInfoLog(v, maxLogLength, status, 0, log, 0);

            System.out.println("v: " + new String(log, 0, status[0]));
         } else
            System.out.println("v: "+ "unknown compilation error");
      }




      //FRAGMENT SHADER CODE
     int f = gl.glCreateShader(gl.GL_FRAGMENT_SHADER);

      ArrayList <String> fSourceA = new ArrayList<String>();

      Scanner frag = null;
      try {
         frag = new Scanner(fragf);
      } catch (FileNotFoundException e) {
         System.out.println("Fragment shader file was not found");
      }


      frag.useDelimiter("\n");
      while(frag.hasNextLine()){
         String temp = frag.nextLine() + "\n";
         fSourceA.add(temp);
         System.out.print(temp);
      }

      String[] fSource = new String[fSourceA.size()];
      fSource = fSourceA.toArray(fSource);
      lengths= new int[fSource.length];
      for(int i = 0; i < fSource.length; i++){
         lengths[i] = fSource[i].length();
      }

      gl.glShaderSource(f, 1, fSource, lengths, 0);        
      gl.glCompileShader(f);
      gl.glAttachShader(p,f);

      // query compile status and possibly read log
     status = new int[1];
      gl.glGetShaderiv(f, GL3.GL_COMPILE_STATUS, status, 0);
      if (status[0] == GL.GL_TRUE){
         System.out.println("f: succesful");
         // everything compiled successfully, no log
     }else{
         // compile failed, read the log and return it
        gl.glGetShaderiv(f, GL3.GL_INFO_LOG_LENGTH, status, 0);
         int maxLogLength = status[0];
         if (maxLogLength > 0) {
            byte[] log = new byte[maxLogLength];
            gl.glGetShaderInfoLog(f, maxLogLength, status, 0, log, 0);
            System.out.println("f: " + new String(log, 0, status[0]));
         } else
            System.out.println("f: " + "unknown compilation error");
      }


      //PROGRAM CODE
     gl.glLinkProgram(p);      



      // check link status
     int[] query = new int[1];
      gl.glGetProgramiv(p, GL3.GL_LINK_STATUS, query, 0);
      if (query[0] == GL.GL_TRUE){
         System.out.println("P: successful"); // program linked successfully
        valid = true;
      }else{
         // link failed, read the log and return it
        gl.glGetProgramiv(p, GL3.GL_INFO_LOG_LENGTH, query, 0);
         int maxLogLength = query[0];
         if (maxLogLength > 0) {
            byte[] log = new byte[maxLogLength];
            gl.glGetProgramInfoLog(p, maxLogLength, query, 0, log, 0);

            valid = false;
            System.out.println(new String(log, 0, query[0]));
         } else{
            valid = false;
            System.out.println("unknown link error");
         }
      }
   }

   int p;
   boolean valid = false;

   File vertf;
   File fragf;

}
9  Java Game APIs & Engines / OpenGL Development / Re: VBOs and Shaders in JOGL on: 2011-08-02 06:30:23
So I got the vertex and fragment shaders to compile by taking out the extra newline char I was adding in *oops* and taking out the #version line at the top. But now I have a different problem than before with glVertexAttribPointer
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
Exception in thread "Timer-0" javax.media.opengl.GLException: javax.media.opengl.GLException: Thread[AWT-EventQueue-0,6,main] glGetError() returned the following error codes after a call to glVertexAttribPointer(<int> 0xFFFFFFFF, <int> 0x3, <int> 0x1406, <boolean>, <int> 0x0, <long>): GL_INVALID_VALUE ( 1281 0x501), 
   at jogamp.opengl.awt.AWTThreadingPlugin.invokeOnOpenGLThread(AWTThreadingPlugin.java:98)
   at jogamp.opengl.ThreadingImpl.invokeOnOpenGLThread(ThreadingImpl.java:197)
   at javax.media.opengl.Threading.invokeOnOpenGLThread(Threading.java:164)
   at javax.media.opengl.awt.GLCanvas.maybeDoSingleThreadedWorkaround(GLCanvas.java:767)
   at javax.media.opengl.awt.GLCanvas.display(GLCanvas.java:388)
   at com.jogamp.opengl.util.AWTAnimatorImpl.display(AWTAnimatorImpl.java:74)
   at com.jogamp.opengl.util.AnimatorBase.display(AnimatorBase.java:140)
   at com.jogamp.opengl.util.FPSAnimator$1.run(FPSAnimator.java:125)
   at java.util.TimerThread.mainLoop(Unknown Source)
   at java.util.TimerThread.run(Unknown Source)
Caused by: javax.media.opengl.GLException: Thread[AWT-EventQueue-0,6,main] glGetError() returned the following error codes after a call to glVertexAttribPointer(<int> 0xFFFFFFFF, <int> 0x3, <int> 0x1406, <boolean>, <int> 0x0, <long>): GL_INVALID_VALUE ( 1281 0x501),
   at javax.media.opengl.DebugGL3.checkGLGetError(DebugGL3.java:12595)
   at javax.media.opengl.DebugGL3.glVertexAttribPointer(DebugGL3.java:8895)
   at RealMain$JOGLRenderer.display(RealMain.java:103)
   at jogamp.opengl.GLDrawableHelper.display(GLDrawableHelper.java:191)
   at javax.media.opengl.awt.GLCanvas$DisplayAction.run(GLCanvas.java:873)
   at jogamp.opengl.GLDrawableHelper.invokeGL(GLDrawableHelper.java:363)
   at javax.media.opengl.awt.GLCanvas$DisplayOnEventDispatchThreadAction.run(GLCanvas.java:890)
   at java.awt.event.InvocationEvent.dispatch(Unknown Source)
   at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
   at java.awt.EventQueue.access$000(Unknown Source)
   at java.awt.EventQueue$1.run(Unknown Source)
   at java.awt.EventQueue$1.run(Unknown Source)
   at java.security.AccessController.doPrivileged(Native Method)
   at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
   at java.awt.EventQueue.dispatchEvent(Unknown Source)
   at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
   at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
   at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
   at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
   at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
   at java.awt.EventDispatchThread.run(Unknown Source)

current realmain code is below
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
import javax.media.opengl.DebugGL3;
import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GL3;
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.*;

import com.jogamp.opengl.util.FPSAnimator;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;

public class RealMain{    
   public static void main(String[] args)    {        
      JFrame frame = new JFrame("JOGL test");
      frame.setSize(640, 480);
      GLProfile glProfile = GLProfile.getDefault();
      GLCapabilities glCapabilities = new GLCapabilities(glProfile);
      GLCanvas canvas = new GLCanvas(glCapabilities);        
      frame.add(canvas);      
      canvas.addGLEventListener(new JOGLRenderer());  
      FPSAnimator animator = new FPSAnimator(canvas, 60);  
      animator.add(canvas);
      animator.start();      
      frame.addWindowListener(new WindowAdapter(){          
         public void windowClosing(WindowEvent e)            {  
            System.exit(0);            }        });  
      frame.setVisible(true);  
   }  
   private static class JOGLRenderer implements GLEventListener    {
      ShaderProgram shader;
      public void init(GLAutoDrawable drawable){



         shader = new ShaderProgram(drawable, new File("bin/vert.txt"), new File("bin/frag.txt"));

         //setup opengl        
        DebugGL3 gl = new DebugGL3( drawable.getGL().getGL3());
         gl.glClearColor(0.2f, 0.2f, 0.0f, 0.0f);        
         gl.glClearDepth(1.0f);          
         gl.glEnable(GL3.GL_DEPTH_TEST);        

         // Create vertex data        
        float[] vertexData = new float[]{
               -1f, 1f, -2.0f, //0
              1f, 1f, -2.0f, //1
              -1f, -1f, -2.0f, //2
              1f, -1f, -2.0f, //3
        };
         ByteBuffer vertexByteBuffer = ByteBuffer.allocateDirect(3 * 4 * 4);            
         vertexByteBuffer.order(ByteOrder.nativeOrder());      
         FloatBuffer vertexBuffer = vertexByteBuffer.asFloatBuffer();          
         vertexBuffer.put(vertexData);

         // Create vertex buffer              
        int[] vertexBufferId = new int[1];        
         gl.glGenBuffers(1, vertexBufferId, 0);        
         gl.glBindBuffer(GL3.GL_ARRAY_BUFFER, vertexBufferId[0]);      
         gl.glBufferData(GL3.GL_ARRAY_BUFFER, 48, (ByteBuffer)null, GL3.GL_DYNAMIC_DRAW);
         // Load vertex data into vertex buffer        
        gl.glBufferSubData(GL3.GL_ARRAY_BUFFER, 0, vertexByteBuffer.capacity(), vertexByteBuffer);

         // Create index data          
        int[] indexData = new int[]{0, 1, 2, 3};

         ByteBuffer indexByteBuffer = ByteBuffer.allocateDirect(4 * 4);
         indexByteBuffer.order(ByteOrder.nativeOrder());
         IntBuffer indexBuffer = indexByteBuffer.asIntBuffer();
         indexBuffer.put(indexData);

         // Create index buffer    
        int[] indexBufferId = new int[1];
         gl.glGenBuffers(1, indexBufferId, 0);
         gl.glBindBuffer(GL3.GL_ELEMENT_ARRAY_BUFFER, indexBufferId[0]);          
         gl.glBufferData(GL3.GL_ELEMENT_ARRAY_BUFFER, 16, (ByteBuffer)null, GL3.GL_DYNAMIC_DRAW);      
         // Load index data into index buffer          
        gl.glBufferSubData(GL3.GL_ELEMENT_ARRAY_BUFFER, 0, indexByteBuffer.capacity(), indexByteBuffer);



      }
      public void dispose(GLAutoDrawable drawable){}
      public void display(GLAutoDrawable drawable){        
         DebugGL3 gl = new DebugGL3( drawable.getGL().getGL3());
         gl.glClear(GL3.GL_COLOR_BUFFER_BIT | GL3.GL_DEPTH_BUFFER_BIT);      


         //gl.glMatrixMode(GL2.GL_MODELVIEW);
        //gl.glLoadIdentity();          
        int stride = 3 * 4; //3 floats per vert * 4 bytes per float

         gl.glVertexAttribPointer(gl.glGetAttribLocation(shader.p, "vertex"), 3, GL3.GL_FLOAT, false, 0, 0);

         //shader code
        gl.glUseProgram(shader.p);
         gl.glDrawRangeElements(GL3.GL_TRIANGLE_STRIP, 0, 3, 4, GL3.GL_UNSIGNED_INT, 0); //start render at 0 verts go to 3 for a count of 4
        gl.glUseProgram(0);
      }    


      public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height)        {          
         GL3 gl = drawable.getGL().getGL3();    
         GLU glu = new GLU();
         gl.glViewport(0, 0, width, height);  
         glu.gluPerspective(45.0f, (float) width / (float) height, 0.1f, 100.0f);  
      }
   }
}
10  Java Game APIs & Engines / OpenGL Development / Re: VBOs and Shaders in JOGL on: 2011-08-02 03:20:47
Wow, that was super helpful, thank you very much.
So here's the code that is compiled for each shader followed by the error it produces.
Since they both give the same error I'm thinking there's something wrong with the way I'm reading in and feeding the code to openGL.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
#version 150
in vec3 vertex;
void
main()
{
    gl_Position = vec4(vertex,1.0);
}
";
v: Vertex shader failed to compile with the following errors:
ERROR: 7:2: error(#90) Syntax error ERROR___CPP_EOL_IN_STRING
ERROR: 7:2: error(#132) Syntax error: '<' parse error
ERROR: error(#273) 2 compilation errors.  No code generated

#version 150
out vec4 fragmentColor;
void
main()
{
    fragmentColor = vec4(0.0, 1.0, 0.0, 1.0);
}
"
;
f: Fragment shader failed to compile with the following errors:
ERROR: 7:2: error(#90) Syntax error ERROR___CPP_EOL_IN_STRING
ERROR: 7:2: error(#132) Syntax error: '<' parse error
ERROR: error(#273) 2 compilation errors.  No code generated

for reference here's my code that handles compiling the shaders (I commented out the linking related things for now)
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
public ShaderProgram(GLAutoDrawable drawable, File vertf, File fragf) {
      this.vertf = vertf;
      this.fragf = fragf;        
      DebugGL3 gl = new DebugGL3( drawable.getGL().getGL3());

      p = gl.glCreateProgram();

      //VERTEX STUFF
     int v = gl.glCreateShader(gl.GL_VERTEX_SHADER);

      ArrayList <String> vSourceA = new ArrayList<String>();
      Scanner vert = null;
      try {
         vert = new Scanner(vertf);
      } catch (FileNotFoundException e) {
         System.out.println("Vertex shader file was not found");
      }

      vert.useDelimiter("\n");

      while(vert.hasNext()){
         String temp = vert.next() + "\n";
         vSourceA.add(temp);
         System.out.print(temp);
      }

      String[] vSource = new String[vSourceA.size()];
      vSource = vSourceA.toArray(vSource);
      int [] lengths= new int[vSource.length];
      for(int i = 0; i < vSource.length; i++){
         lengths[i] = vSource[i].length();
      }

      gl.glShaderSource(v, lengths.length, vSource, lengths, 0);        
      gl.glCompileShader(v);

      // query compile status and possibly read log
     int[] status = new int[1];
      gl.glGetShaderiv(v, GL3.GL_COMPILE_STATUS, status, 0);
      if (status[0] == GL.GL_TRUE){
         System.out.println("v: succesful");
         // everything compiled successfully, no log
     }else{
         // compile failed, read the log and return it
        gl.glGetShaderiv(v, GL3.GL_INFO_LOG_LENGTH, status, 0);
         int maxLogLength = status[0];
         if (maxLogLength > 0) {
            byte[] log = new byte[maxLogLength];
            gl.glGetShaderInfoLog(v, maxLogLength, status, 0, log, 0);

            System.out.println("v: " + new String(log, 0, status[0]));
         } else
            System.out.println("v: "+ "unknown compilation error");
      }
      //FRAGMENT SHADER CODE
     int f = gl.glCreateShader(gl.GL_FRAGMENT_SHADER);

      ArrayList <String> fSourceA = new ArrayList<String>();

      Scanner frag = null;
      try {
         frag = new Scanner(fragf);
      } catch (FileNotFoundException e) {
         System.out.println("Fragment shader file was not found");
      }


      frag.useDelimiter("\n");
      while(frag.hasNext()){
         String temp = frag.next() + "\n";
         fSourceA.add(temp);
         System.out.print(temp);
      }

      String[] fSource = new String[fSourceA.size()];
      fSource = fSourceA.toArray(fSource);
      lengths= new int[fSource.length];
      for(int i = 0; i < fSource.length; i++){
         lengths[i] = fSource[i].length();
      }

      gl.glShaderSource(f, lengths.length, fSource, lengths, 0);        
      gl.glCompileShader(f);

      // query compile status and possibly read log
     status = new int[1];
      gl.glGetShaderiv(f, GL3.GL_COMPILE_STATUS, status, 0);
      if (status[0] == GL.GL_TRUE){
         System.out.println("f: succesful");
         // everything compiled successfully, no log
     }else{
         // compile failed, read the log and return it
        gl.glGetShaderiv(f, GL3.GL_INFO_LOG_LENGTH, status, 0);
         int maxLogLength = status[0];
         if (maxLogLength > 0) {
            byte[] log = new byte[maxLogLength];
            gl.glGetShaderInfoLog(f, maxLogLength, status, 0, log, 0);
            System.out.println("f: " + new String(log, 0, status[0]));
         } else
            System.out.println("f: " + "unknown compilation error");
      }
      //PROGRAM CODE
     //gl.glLinkProgram(p);      
     //gl.glValidateProgram(p);


      valid = true;
   }
11  Java Game APIs & Engines / OpenGL Development / Re: VBOs and Shaders in JOGL on: 2011-08-02 01:14:31
So in my ShaderProgram code I was assigning the program handle to a local variable *facepalm* but my program is still not working correctly, new error below
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
Exception in thread "Timer-0" javax.media.opengl.GLException: javax.media.opengl.GLException: Thread[AWT-EventQueue-0,6,main] glGetError() returned the following error codes after a call to glGetAttribLocation(<int> 0x1, <java.lang.String>): GL_INVALID_OPERATION ( 1282 0x502), 
   at jogamp.opengl.awt.AWTThreadingPlugin.invokeOnOpenGLThread(AWTThreadingPlugin.java:98)
   at jogamp.opengl.ThreadingImpl.invokeOnOpenGLThread(ThreadingImpl.java:197)
   at javax.media.opengl.Threading.invokeOnOpenGLThread(Threading.java:164)
   at javax.media.opengl.awt.GLCanvas.maybeDoSingleThreadedWorkaround(GLCanvas.java:767)
   at javax.media.opengl.awt.GLCanvas.display(GLCanvas.java:388)
   at com.jogamp.opengl.util.AWTAnimatorImpl.display(AWTAnimatorImpl.java:74)
   at com.jogamp.opengl.util.AnimatorBase.display(AnimatorBase.java:140)
   at com.jogamp.opengl.util.FPSAnimator$1.run(FPSAnimator.java:125)
   at java.util.TimerThread.mainLoop(Unknown Source)
   at java.util.TimerThread.run(Unknown Source)
Caused by: javax.media.opengl.GLException: Thread[AWT-EventQueue-0,6,main] glGetError() returned the following error codes after a call to glGetAttribLocation(<int> 0x1, <java.lang.String>): GL_INVALID_OPERATION ( 1282 0x502),
   at javax.media.opengl.DebugGL3.checkGLGetError(DebugGL3.java:12595)
   at javax.media.opengl.DebugGL3.glGetAttribLocation(DebugGL3.java:5641)
   at RealMain$JOGLRenderer.display(RealMain.java:105)
   at jogamp.opengl.GLDrawableHelper.display(GLDrawableHelper.java:191)
   at javax.media.opengl.awt.GLCanvas$DisplayAction.run(GLCanvas.java:873)
   at jogamp.opengl.GLDrawableHelper.invokeGL(GLDrawableHelper.java:363)
   at javax.media.opengl.awt.GLCanvas$DisplayOnEventDispatchThreadAction.run(GLCanvas.java:890)
   at java.awt.event.InvocationEvent.dispatch(Unknown Source)
   at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
   at java.awt.EventQueue.access$000(Unknown Source)
   at java.awt.EventQueue$1.run(Unknown Source)
   at java.awt.EventQueue$1.run(Unknown Source)
   at java.security.AccessController.doPrivileged(Native Method)
   at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
   at java.awt.EventQueue.dispatchEvent(Unknown Source)
   at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
   at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
   at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
   at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
   at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
   at java.awt.EventDispatchThread.run(Unknown Source)

This makes me think that my shader program is not compiling successfully,  but I'm not quite sure how to check that.
12  Java Game APIs & Engines / OpenGL Development / Re: VBOs and Shaders in JOGL on: 2011-08-02 00:33:12
Yeah I got that, the error was caused by
            ByteBuffer vertexByteBuffer = ByteBuffer.allocateDirect(7 * 4 * 4);    
which should have been
            ByteBuffer vertexByteBuffer = ByteBuffer.allocateDirect(3 * 4 * 4);    
I'd forgotten to change the buffer size after I removed the color data from the vertices.

But now back to my broken shaders.
new error code is posted below:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
Exception in thread "Timer-0" javax.media.opengl.GLException: javax.media.opengl.GLException: Thread[AWT-EventQueue-0,6,main] glGetError() returned the following error codes after a call to glGetAttribLocation(<int> 0x0, <java.lang.String>): GL_INVALID_VALUE ( 1281 0x501), 
   at jogamp.opengl.awt.AWTThreadingPlugin.invokeOnOpenGLThread(AWTThreadingPlugin.java:98)
   at jogamp.opengl.ThreadingImpl.invokeOnOpenGLThread(ThreadingImpl.java:197)
   at javax.media.opengl.Threading.invokeOnOpenGLThread(Threading.java:164)
   at javax.media.opengl.awt.GLCanvas.maybeDoSingleThreadedWorkaround(GLCanvas.java:767)
   at javax.media.opengl.awt.GLCanvas.display(GLCanvas.java:388)
   at com.jogamp.opengl.util.AWTAnimatorImpl.display(AWTAnimatorImpl.java:74)
   at com.jogamp.opengl.util.AnimatorBase.display(AnimatorBase.java:140)
   at com.jogamp.opengl.util.FPSAnimator$1.run(FPSAnimator.java:125)
   at java.util.TimerThread.mainLoop(Unknown Source)
   at java.util.TimerThread.run(Unknown Source)
Caused by: javax.media.opengl.GLException: Thread[AWT-EventQueue-0,6,main] glGetError() returned the following error codes after a call to glGetAttribLocation(<int> 0x0, <java.lang.String>): GL_INVALID_VALUE ( 1281 0x501),
   at javax.media.opengl.DebugGL3.checkGLGetError(DebugGL3.java:12595)
   at javax.media.opengl.DebugGL3.glGetAttribLocation(DebugGL3.java:5641)
   at RealMain$JOGLRenderer.display(RealMain.java:105)
   at jogamp.opengl.GLDrawableHelper.display(GLDrawableHelper.java:191)
   at javax.media.opengl.awt.GLCanvas$DisplayAction.run(GLCanvas.java:873)
   at jogamp.opengl.GLDrawableHelper.invokeGL(GLDrawableHelper.java:363)
   at javax.media.opengl.awt.GLCanvas$DisplayOnEventDispatchThreadAction.run(GLCanvas.java:890)
   at java.awt.event.InvocationEvent.dispatch(Unknown Source)
   at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
   at java.awt.EventQueue.access$000(Unknown Source)
   at java.awt.EventQueue$1.run(Unknown Source)
   at java.awt.EventQueue$1.run(Unknown Source)
   at java.security.AccessController.doPrivileged(Native Method)
   at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
   at java.awt.EventQueue.dispatchEvent(Unknown Source)
   at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
   at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
   at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
   at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
   at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
   at java.awt.EventDispatchThread.run(Unknown Source)

and updated code for realmain is below
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
import javax.media.opengl.DebugGL3;
import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GL3;
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.*;

import com.jogamp.opengl.util.FPSAnimator;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;

public class RealMain{    
   public static void main(String[] args)    {        
      JFrame frame = new JFrame("JOGL test");
      frame.setSize(640, 480);
      GLProfile glProfile = GLProfile.getDefault();
      GLCapabilities glCapabilities = new GLCapabilities(glProfile);
      GLCanvas canvas = new GLCanvas(glCapabilities);        
      frame.add(canvas);      
      canvas.addGLEventListener(new JOGLRenderer());  
      FPSAnimator animator = new FPSAnimator(canvas, 60);  
      animator.add(canvas);
      animator.start();      
      frame.addWindowListener(new WindowAdapter(){          
         public void windowClosing(WindowEvent e)            {  
            System.exit(0);            }        });  
      frame.setVisible(true);  
   }  
   private static class JOGLRenderer implements GLEventListener    {
      ShaderProgram shader;
      public void init(GLAutoDrawable drawable){



         shader = new ShaderProgram(drawable, new File("bin/vert.txt"), new File("bin/frag.txt"));

         //setup opengl        
        DebugGL3 gl = new DebugGL3( drawable.getGL().getGL3());
         gl.glClearColor(0.2f, 0.2f, 0.0f, 0.0f);        
         gl.glClearDepth(1.0f);          
         gl.glEnable(GL3.GL_DEPTH_TEST);        

         // Create vertex data        
        float[] vertexData = new float[]{
               -1f, 1f, -2.0f, //0
              1f, 1f, -2.0f, //1
              -1f, -1f, -2.0f, //2
              1f, -1f, -2.0f, //3
        };
         ByteBuffer vertexByteBuffer = ByteBuffer.allocateDirect(3 * 4 * 4);            
         vertexByteBuffer.order(ByteOrder.nativeOrder());      
         FloatBuffer vertexBuffer = vertexByteBuffer.asFloatBuffer();          
         vertexBuffer.put(vertexData);

         // Create vertex buffer              
        int[] vertexBufferId = new int[1];        
         gl.glGenBuffers(1, vertexBufferId, 0);        
         gl.glBindBuffer(GL3.GL_ARRAY_BUFFER, vertexBufferId[0]);      
         gl.glBufferData(GL3.GL_ARRAY_BUFFER, 48, (ByteBuffer)null, GL3.GL_DYNAMIC_DRAW);
         // Load vertex data into vertex buffer        
        gl.glBufferSubData(GL3.GL_ARRAY_BUFFER, 0, vertexByteBuffer.capacity(), vertexByteBuffer);

         // Create index data          
        int[] indexData = new int[]{0, 1, 2, 3};

         ByteBuffer indexByteBuffer = ByteBuffer.allocateDirect(4 * 4);
         indexByteBuffer.order(ByteOrder.nativeOrder());
         IntBuffer indexBuffer = indexByteBuffer.asIntBuffer();
         indexBuffer.put(indexData);

         // Create index buffer    
        int[] indexBufferId = new int[1];
         gl.glGenBuffers(1, indexBufferId, 0);
         gl.glBindBuffer(GL3.GL_ELEMENT_ARRAY_BUFFER, indexBufferId[0]);          
         gl.glBufferData(GL3.GL_ELEMENT_ARRAY_BUFFER, 16, (ByteBuffer)null, GL3.GL_DYNAMIC_DRAW);      
         // Load index data into index buffer          
        gl.glBufferSubData(GL3.GL_ELEMENT_ARRAY_BUFFER, 0, indexByteBuffer.capacity(), indexByteBuffer);

         

      }
      public void dispose(GLAutoDrawable drawable){}
      public void display(GLAutoDrawable drawable){        
         DebugGL3 gl = new DebugGL3( drawable.getGL().getGL3());
         gl.glClear(GL3.GL_COLOR_BUFFER_BIT | GL3.GL_DEPTH_BUFFER_BIT);      


         //gl.glMatrixMode(GL2.GL_MODELVIEW);
        //gl.glLoadIdentity();          
        int stride = 3 * 4; //3 floats per vert * 4 bytes per float


         
         gl.glVertexAttribPointer(gl.glGetAttribLocation(shader.p, "vertex"), 3, GL3.GL_FLOAT, false, 20, 0);
         System.out.println(gl.glGetError());
         
         //shader code
        gl.glUseProgram(shader.p);
         gl.glDrawRangeElements(GL3.GL_TRIANGLE_STRIP, 0, 3, 4, GL3.GL_UNSIGNED_INT, 0); //start render at 0 verts go to 3 for a count of 4
        gl.glUseProgram(0);
      }    


      public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height)        {          
         GL3 gl = drawable.getGL().getGL3();    
         GLU glu = new GLU();
         gl.glViewport(0, 0, width, height);  
         glu.gluPerspective(45.0f, (float) width / (float) height, 0.1f, 100.0f);  
      }
   }
}

Right now I'm looking into the fact that my string for glGetAttribLocation is not null teminated
13  Java Game APIs & Engines / OpenGL Development / Re: VBOs and Shaders in JOGL on: 2011-08-02 00:14:35
Hm, I did not know that you could do that that's neat.
So after wrapping gl with DebugGL3 it gives me this error message:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
Exception in thread "Timer-0" javax.media.opengl.GLException: javax.media.opengl.GLException: Thread[AWT-EventQueue-0,6,main] glGetError() returned the following error codes after a call to glBufferSubData(<int> 0x8892, <long>, <long>, <java.nio.Buffer>): GL_INVALID_VALUE ( 1281 0x501), 
   at jogamp.opengl.awt.AWTThreadingPlugin.invokeOnOpenGLThread(AWTThreadingPlugin.java:98)
   at jogamp.opengl.ThreadingImpl.invokeOnOpenGLThread(ThreadingImpl.java:197)
   at javax.media.opengl.Threading.invokeOnOpenGLThread(Threading.java:164)
   at javax.media.opengl.awt.GLCanvas.maybeDoSingleThreadedWorkaround(GLCanvas.java:767)
   at javax.media.opengl.awt.GLCanvas.display(GLCanvas.java:388)
   at com.jogamp.opengl.util.AWTAnimatorImpl.display(AWTAnimatorImpl.java:74)
   at com.jogamp.opengl.util.AnimatorBase.display(AnimatorBase.java:140)
   at com.jogamp.opengl.util.FPSAnimator$1.run(FPSAnimator.java:125)
   at java.util.TimerThread.mainLoop(Unknown Source)
   at java.util.TimerThread.run(Unknown Source)
Caused by: javax.media.opengl.GLException: Thread[AWT-EventQueue-0,6,main] glGetError() returned the following error codes after a call to glBufferSubData(<int> 0x8892, <long>, <long>, <java.nio.Buffer>): GL_INVALID_VALUE ( 1281 0x501),
   at javax.media.opengl.DebugGL3.checkGLGetError(DebugGL3.java:12595)
   at javax.media.opengl.DebugGL3.glBufferSubData(DebugGL3.java:1409)
   at RealMain$JOGLRenderer.init(RealMain.java:72)
   at jogamp.opengl.GLDrawableHelper.init(GLDrawableHelper.java:155)
   at jogamp.opengl.GLDrawableHelper.init(GLDrawableHelper.java:175)
   at javax.media.opengl.awt.GLCanvas$InitAction.run(GLCanvas.java:856)
   at jogamp.opengl.GLDrawableHelper.invokeGL(GLDrawableHelper.java:356)
   at javax.media.opengl.awt.GLCanvas$DisplayOnEventDispatchThreadAction.run(GLCanvas.java:890)
   at java.awt.event.InvocationEvent.dispatch(Unknown Source)
   at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
   at java.awt.EventQueue.access$000(Unknown Source)
   at java.awt.EventQueue$1.run(Unknown Source)
   at java.awt.EventQueue$1.run(Unknown Source)
   at java.security.AccessController.doPrivileged(Native Method)
   at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
   at java.awt.EventQueue.dispatchEvent(Unknown Source)
   at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
   at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
   at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
   at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
   at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
   at java.awt.EventDispatchThread.run(Unknown Source)

So looking at that error it looks like I managed to break my VBOs while trying to get shaders to work...
14  Java Game APIs & Engines / OpenGL Development / VBOs and Shaders in JOGL on: 2011-08-01 13:25:12
So I got the hang of immediate mode drawing and tried doing some basic landscape rendering with it but was not pleased with the speed so I decided to try my hand with VBOs, then having conquered VBOs I decided to try adding shaders to that, and that's where I ran into serious trouble. I've been trying the last few days to get the below code to work and it compiles fine, but opengl gives me a 1281 error

this code goes in the realmain class
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GL3;
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.*;

import com.jogamp.opengl.util.FPSAnimator;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;

public class RealMain{    
   public static void main(String[] args)    {        
      JFrame frame = new JFrame("JOGL test");
      frame.setSize(640, 480);
      GLProfile glProfile = GLProfile.getDefault();
      GLCapabilities glCapabilities = new GLCapabilities(glProfile);
      GLCanvas canvas = new GLCanvas(glCapabilities);        
      frame.add(canvas);      
      canvas.addGLEventListener(new JOGLRenderer());  
      FPSAnimator animator = new FPSAnimator(canvas, 60);  
      animator.add(canvas);
      animator.start();      
      frame.addWindowListener(new WindowAdapter(){          
         public void windowClosing(WindowEvent e)            {  
            System.exit(0);            }        });  
      frame.setVisible(true);  
   }  
   private static class JOGLRenderer implements GLEventListener    {
      ShaderProgram shader;
      public void init(GLAutoDrawable drawable){



         shader = new ShaderProgram(drawable, new File("bin/vert.txt"), new File("bin/frag.txt"));

         //setup opengl
        GL3 gl = drawable.getGL().getGL3();      
         gl.glClearColor(0.2f, 0.2f, 0.0f, 0.0f);        
         gl.glClearDepth(1.0f);          
         gl.glEnable(GL.GL_DEPTH_TEST);        
         gl.glDepthFunc(GL.GL_LEQUAL);          
         gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);  

         // Create vertex data        
        float[] vertexData = new float[]{
               -1f, 1f, -2.0f, //0
              1f, 1f, -2.0f, //1
              -1f, -1f, -2.0f, //2
              1f, -1f, -2.0f, //3
        };
         ByteBuffer vertexByteBuffer = ByteBuffer.allocateDirect(7 * 4 * 4);            
         vertexByteBuffer.order(ByteOrder.nativeOrder());      
         FloatBuffer vertexBuffer = vertexByteBuffer.asFloatBuffer();          
         vertexBuffer.put(vertexData);

         // Create vertex buffer              
        int[] vertexBufferId = new int[1];        
         gl.glGenBuffers(1, vertexBufferId, 0);        
         gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, vertexBufferId[0]);      
         gl.glBufferData(GL2.GL_ARRAY_BUFFER, 48, null, GL2.GL_DYNAMIC_DRAW);
         // Load vertex data into vertex buffer        
        gl.glBufferSubData(GL2.GL_ARRAY_BUFFER, 0, vertexByteBuffer.capacity(), vertexByteBuffer);

         // Create index data          
        int[] indexData = new int[]{0, 1, 2, 3};

         ByteBuffer indexByteBuffer = ByteBuffer.allocateDirect(4 * 4);    
         indexByteBuffer.order(ByteOrder.nativeOrder());
         IntBuffer indexBuffer = indexByteBuffer.asIntBuffer();      
         indexBuffer.put(indexData);

         // Create index buffer    
        int[] indexBufferId = new int[1];      
         gl.glGenBuffers(1, indexBufferId, 0);
         gl.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, indexBufferId[0]);          
         gl.glBufferData(GL2.GL_ELEMENT_ARRAY_BUFFER, 16, null, GL2.GL_DYNAMIC_DRAW);      
         // Load index data into index buffer          
        gl.glBufferSubData(GL2.GL_ELEMENT_ARRAY_BUFFER, 0, indexByteBuffer.capacity(), indexByteBuffer);

         

      }
      public void dispose(GLAutoDrawable drawable){}
      public void display(GLAutoDrawable drawable){        
         GL3 gl = drawable.getGL().getGL3();  
         gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);      


         //gl.glMatrixMode(GL2.GL_MODELVIEW);
        //gl.glLoadIdentity();          
        int stride = 3 * 4; //3 floats per vert * 4 bytes per float


         
         gl.glVertexAttribPointer(gl.glGetAttribLocation(shader.p, "vertex"), 3, gl.GL_FLOAT, false, 20, 0);
         System.out.println(gl.glGetError());
         
         //shader code
        gl.glUseProgram(shader.p);
         gl.glDrawRangeElements(GL2.GL_TRIANGLE_STRIP, 0, 2, 3, GL2.GL_UNSIGNED_INT, 0); //start render at 0 verts go to 3 for a count of 4
        gl.glUseProgram(0);
      }    


      public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height)        {          
         GL3 gl = drawable.getGL().getGL3();    
         GLU glu = new GLU();
         gl.glViewport(0, 0, width, height);  
         glu.gluPerspective(45.0f, (float) width / (float) height, 0.1f, 100.0f);  
      }
   }
}


and this goes in shaderprogram
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.Scanner;

import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GL3;
import javax.media.opengl.GLAutoDrawable;
 
public class ShaderProgram {
   
    public ShaderProgram(GLAutoDrawable drawable, File vertf, File fragf) {
       this.vertf = vertf;
       this.fragf = fragf;
        GL3 gl = drawable.getGL().getGL3();
       
        int p = gl.glCreateProgram();
       
       
        //VERTEX STUFF
       int v = gl.glCreateShader(gl.GL_VERTEX_SHADER);
       
        ArrayList <String> vSourceA = new ArrayList<String>();
        Scanner vert = null;
      try {
         vert = new Scanner(vertf);
      } catch (FileNotFoundException e) {
         System.out.println("Vertex shader file was not found");
      }

      vert.useDelimiter("\n");
     
        while(vert.hasNext()){
           String temp = vert.next() + "\n";
           vSourceA.add(temp);
           System.out.print(temp);
        }
       
        String[] vSource = new String[vSourceA.size()];
        vSource = vSourceA.toArray(vSource);
        int [] lengths= new int[vSource.length];
        for(int i = 0; i < vSource.length; i++){
           lengths[i] = vSource[i].length();
        }
       
        gl.glShaderSource(v, lengths.length, vSource, lengths, 0);        
        gl.glCompileShader(v);
        gl.glAttachShader(p, v);
       
        //FRAGMENT SHADER CODE
       int f = gl.glCreateShader(gl.GL_FRAGMENT_SHADER);
       
        ArrayList <String> fSourceA = new ArrayList<String>();
       
        Scanner frag = null;
      try {
         frag = new Scanner(fragf);
      } catch (FileNotFoundException e) {
         System.out.println("Fragment shader file was not found");
      }
     
     
      frag.useDelimiter("\n");
        while(frag.hasNext()){
           String temp = frag.next() + "\n";
           fSourceA.add(temp);
           System.out.print(temp);
        }
       
        String[] fSource = new String[fSourceA.size()];
        fSource = fSourceA.toArray(fSource);
        lengths= new int[fSource.length];
        for(int i = 0; i < fSource.length; i++){
           lengths[i] = fSource[i].length();
        }
       
        gl.glShaderSource(f, lengths.length, fSource, lengths, 0);        
        gl.glCompileShader(f);
        gl.glAttachShader(p, f);
       
       
        //PROGRAM CODE
       gl.glLinkProgram(p);
        gl.glValidateProgram(p);
        System.out.println(gl.glGetError());
        valid = true;
    }
 
    int p;
    boolean valid = false;
     
    File vertf;
    File fragf;
 
}

frag.txt
1  
2  
3  
4  
5  
6  
7  
8  
#version 150
out vec4 fragmentColor;
void
main()
{
    fragmentColor = vec4(0.0, 1.0, 0.0, 1.0);
}
";

vert.txt
1  
2  
3  
4  
5  
6  
7  
8  
#version 150
in vec3 vertex;
void
main()
{
    gl_Position = vec4(vertex,1.0);
}
";
Pages: [1]
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (38 views)
2014-04-15 18:08:23

BurntPizza (34 views)
2014-04-15 03:46:01

UprightPath (50 views)
2014-04-14 17:39:50

UprightPath (32 views)
2014-04-14 17:35:47

Porlus (49 views)
2014-04-14 15:48:38

tom_mai78101 (71 views)
2014-04-10 04:04:31

BurntPizza (130 views)
2014-04-08 23:06:04

tom_mai78101 (230 views)
2014-04-05 13:34:39

trollwarrior1 (193 views)
2014-04-04 12:06:45

CJLetsGame (201 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!