Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (120)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  glGenVertexArrays();  (Read 2536 times)
0 Members and 1 Guest are viewing this topic.
Offline DavidW
« Posted 2012-04-20 22:50:03 »

So I am working through the tutorials here: http://arcsynthesis.org/gltut/Basics/Tut01%20Making%20Shaders.html, and I don't fully understand something.  Here is the lwjgl port which someone posted in another thread someplace.

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  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
package rosick.mckesson.I.tut01;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;
import static org.lwjgl.opengl.GL20.*;
import static org.lwjgl.opengl.GL30.*;
import static org.lwjgl.opengl.GL32.*;

import java.nio.FloatBuffer;
import java.util.ArrayList;

import org.lwjgl.BufferUtils;

import rosick.LWJGLWindow;


/**
 * Visit https://github.com/rosickteam/OpenGL for project info, updates and license terms.
 *
 * I. The Basics
 * Chapter 1. Hello, Triangle!
 * http://www.arcsynthesis.org/gltut/Basics/Tutorial%2001.html
 * @author integeruser
 */

public class HelloTriangle01 extends LWJGLWindow {
   
   public static void main(String[] args) {
      new HelloTriangle01().start();
   }

   
   
   /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

   
   private final float vertexPositions[] = {
         0.75f,  0.75f, 0.0f, 1.0f,
         0.75f, -0.75f, 0.0f, 1.0f,
         -0.75f, -0.75f, 0.0f, 1.0f
   };

   private final String strVertexShader =
         "#version 330 \n" +
               "\n" +
               "layout(location = 0) in vec4 position;\n" +
               "void main()\n" +
               "{\n" +
               "    gl_Position = position;\n" +
               "}";

   private final String strFragmentShader =
         "#version 330\n" +
               "\n" +
               "out vec4 outputCol;\n" +
               "void main()\n" +
               "{\n" +
               "   outputCol = vec4(1.0f, 1.0f, 1.0f, 1.0f);\n" +
               "}";
     
   private int theProgram;
   private int positionBufferObject;
   private int vao;

   

   /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

   
   private void initializeProgram() {  
      ArrayList<Integer> shaderList = new ArrayList<>();
      shaderList.add(createShader(GL_VERTEX_SHADER,   strVertexShader));
      shaderList.add(createShader(GL_FRAGMENT_SHADER,   strFragmentShader));

      theProgram = createProgram(shaderList);
     
       for (Integer shader : shaderList) {
          glDeleteShader(shader);
      }
   }
   
   private void initializeVertexBuffer() {
      FloatBuffer vertexPositionsBuffer = BufferUtils.createFloatBuffer(vertexPositions.length);
      vertexPositionsBuffer.put(vertexPositions);
      vertexPositionsBuffer.flip();
       
      positionBufferObject = glGenBuffers();      
      glBindBuffer(GL_ARRAY_BUFFER, positionBufferObject);
       glBufferData(GL_ARRAY_BUFFER, vertexPositionsBuffer, GL_STATIC_DRAW);
      glBindBuffer(GL_ARRAY_BUFFER, 0);
   }

   
   @Override
   protected void init() {
      initializeProgram();
      initializeVertexBuffer();

      vao = glGenVertexArrays();
      glBindVertexArray(vao);
   }
   
     
   @Override
   protected void display() {  
      glClearColor(0f, 0f, 0f, 1.0f);
      glClear(GL_COLOR_BUFFER_BIT);

      glUseProgram(theProgram);

      glBindBuffer(GL_ARRAY_BUFFER, positionBufferObject);
      glEnableVertexAttribArray(0);
      glVertexAttribPointer(0, 4, GL_FLOAT, false, 0, 0);

      glDrawArrays(GL_TRIANGLES, 0, 3);

      glDisableVertexAttribArray(0);
      glUseProgram(0);
   }
   
   
   
   /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

   
   private int createShader(int eShaderType, String strShaderFile) {
        int shader = glCreateShader(eShaderType);
        glShaderSource(shader, strShaderFile);

        glCompileShader(shader);

        int status = glGetShader(shader, GL_COMPILE_STATUS);
        if (status == 0) {
          int infoLogLength = glGetShader(shader, GL_INFO_LOG_LENGTH);

          String strInfoLog = glGetShaderInfoLog(shader, infoLogLength);

          String strShaderType = null;
         switch (eShaderType) {
         case GL_VERTEX_SHADER:
            strShaderType = "vertex";
            break;
         case GL_GEOMETRY_SHADER:
            strShaderType = "geometry";
            break;
         case GL_FRAGMENT_SHADER:
            strShaderType = "fragment";
            break;
         }

          System.err.printf("Compile failure in %s shader:\n%s\n", strShaderType, strInfoLog);
        }
       
      return shader;
   }
   
   
   private int createProgram(ArrayList<Integer> shaderList) {      
      int program = glCreateProgram();

      for (Integer shader : shaderList) {
         glAttachShader(program, shader);
      }

      glLinkProgram(program);
     
      int status = glGetProgram(program, GL_LINK_STATUS);
      if (status == 0) {
         int infoLogLength = glGetProgram(program, GL_INFO_LOG_LENGTH);

         String strInfoLog = null;
         strInfoLog = glGetProgramInfoLog(program, infoLogLength);
         
         System.err.printf("Linker failure: %s\n", strInfoLog);
      }
     
      for (Integer shader : shaderList) {
         glDetachShader(program, shader);
      }

      return program;
   }
}


I understand what everything does except for this one bit of code:

1  
2  
      vao = glGenVertexArrays();
      glBindVertexArray(vao);

I don't see "vao" referenced anywhere else in the program and when I comment these lines out the program still runs perfectly fine.  Can anyone either help me understand this or point me in the right direction.  I have been looking on google and whatnot for quite some time and still don't really see what's going on.

Thanks!

Hello!
Offline theagentd
« Reply #1 - Posted 2012-04-21 04:45:06 »

A Vertex Array Object stores the three method calls before your glDrawArrays call. The proper use is to call these once after creating the VAO, then bind it just before glDrawArrays. You're currently using a hack to make vertex attributes work like in OGL 2. It's not possible to render without a VAO with OGL 3 (well, it is with the combatility profile...). By creating a VAO and binding it at program start, you're hacking around this restriction.

Myomyomyo.
Offline DavidW
« Reply #2 - Posted 2012-04-21 17:35:56 »

Thank you for your reply.  I think I understand now what I am supposed to do.  When I change the code to 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  
@Override
   protected void init() {
      initializeProgram();
      initializeVertexBuffer();

      vao = glGenVertexArrays();
      glBindVertexArray(vao);

      glBindBuffer(GL_ARRAY_BUFFER, positionBufferObject);
      glEnableVertexAttribArray(0);
      glVertexAttribPointer(0, 4, GL_FLOAT, false, 0, 0);
      glDisableVertexAttribArray(0);
      glBindVertexArray(0);

      glClearColor(0f, 0f, 0f, 1.0f);
   }
   
     
   @Override
   protected void display() {  
      glClear(GL_COLOR_BUFFER_BIT);

      glUseProgram(theProgram);

      glBindVertexArray(vao);

      glDrawArrays(GL_TRIANGLES, 0, 3);

      glBindVertexArray(0);

      glUseProgram(0);
   }


Now I get nothing but a black screen.  Is this the right idea or am I still off?  Thanks again!

Hello!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline theagentd
« Reply #3 - Posted 2012-04-22 04:31:58 »

Remove the glDisableVertexAttribArray(0); and it'll work. You might want to query the attribute location in the shader though.

Congratulations on being reasonable and using OpenGL 3!  Grin

Myomyomyo.
Offline DavidW
« Reply #4 - Posted 2012-04-22 08:04:54 »

Hooray!  It works like a charm.  Grin  Thanks again for your help.

Hello!
Offline theagentd
« Reply #5 - Posted 2012-04-22 08:12:14 »

No problem. If you need any more help with OpenGL 3 I´d be glad to help out.  Wink

Myomyomyo.
Offline DavidW
« Reply #6 - Posted 2012-04-22 22:42:53 »

Thank you very much.  I'm sure I will have more questions at some point.  Emo  Tongue

Hello!
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

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

The first screenshot will be displayed as a thumbnail.

Longarmx (52 views)
2014-10-17 03:59:02

Norakomi (43 views)
2014-10-16 15:22:06

Norakomi (33 views)
2014-10-16 15:20:20

lcass (37 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (66 views)
2014-10-14 00:35:47

TehJavaDev (59 views)
2014-10-14 00:32:37

BurntPizza (73 views)
2014-10-11 23:24:42

BurntPizza (45 views)
2014-10-11 23:10:45

BurntPizza (86 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!