Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (769)
Games in Android Showcase (230)
games submitted by our members
Games in WIP (856)
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  
  Incorrect VBOs  (Read 1230 times)
0 Members and 1 Guest are viewing this topic.
Offline Spacebeans
« Posted 2014-06-24 17:32:58 »

I'm slowly learning VBOs and GLSL shaders, and I'm trying to get the basics down until I get into things like uniform matrices, and non-depreciated VBOs. But I have this as my 'block' code. Its ran during the game loop, and called correctly in the main 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  
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  
183  
184  
185  
186  
187  
188  
189  
package passage.games;

import java.nio.FloatBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.util.vector.Vector3f;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;

public class Block {
   public Vector3f location;
   public Vector3f rotation;
   
   private int vertexPointer;
   private int colorPointer;
   
   private FloatBuffer vertexBuffer;
   private FloatBuffer colorBuffer;
   
   private Shader boxShader;
     
   private float[] vertexData = {
       -1.0f,-1.0f,-1.0f,
       -1.0f,-1.0f, 1.0f,
       -1.0f, 1.0f, 1.0f,
       
       1.0f, 1.0f,-1.0f,
       -1.0f,-1.0f,-1.0f,
       -1.0f, 1.0f,-1.0f,
       
       1.0f,-1.0f, 1.0f,
       -1.0f,-1.0f,-1.0f,
       1.0f,-1.0f,-1.0f,
       
       1.0f, 1.0f,-1.0f,
       1.0f,-1.0f,-1.0f,
       -1.0f,-1.0f,-1.0f,
       
       -1.0f,-1.0f,-1.0f,
       -1.0f, 1.0f, 1.0f,
       -1.0f, 1.0f,-1.0f,
       
       1.0f,-1.0f, 1.0f,
       -1.0f,-1.0f, 1.0f,
       -1.0f,-1.0f,-1.0f,
       
       -1.0f, 1.0f, 1.0f,
       -1.0f,-1.0f, 1.0f,
       1.0f,-1.0f, 1.0f,
       
       1.0f, 1.0f, 1.0f,
       1.0f,-1.0f,-1.0f,
       1.0f, 1.0f,-1.0f,
       
       1.0f,-1.0f,-1.0f,
       1.0f, 1.0f, 1.0f,
       1.0f,-1.0f, 1.0f,
       
       1.0f, 1.0f, 1.0f,
       1.0f, 1.0f,-1.0f,
       -1.0f, 1.0f,-1.0f,
       
       1.0f, 1.0f, 1.0f,
       -1.0f, 1.0f,-1.0f,
       -1.0f, 1.0f, 1.0f,
       
       1.0f, 1.0f, 1.0f,
       -1.0f, 1.0f, 1.0f,
       1.0f,-1.0f, 1.0f
   };
   
   public float[] colorData = {
      0.583f,  0.771f,  0.014f,
       0.609f,  0.115f,  0.436f,
       0.327f,  0.483f,  0.844f,
       
       0.822f,  0.569f,  0.201f,
       0.435f,  0.602f,  0.223f,
       0.310f,  0.747f,  0.185f,
       
       0.597f,  0.770f,  0.761f,
       0.559f,  0.436f,  0.730f,
       0.359f,  0.583f,  0.152f,
       
       0.483f,  0.596f,  0.789f,
       0.559f,  0.861f,  0.639f,
       0.195f,  0.548f,  0.859f,
       
       0.014f,  0.184f,  0.576f,
       0.771f,  0.328f,  0.970f,
       0.406f,  0.615f,  0.116f,
       
       0.676f,  0.977f,  0.133f,
       0.971f,  0.572f,  0.833f,
       0.140f,  0.616f,  0.489f,
       
       0.997f,  0.513f,  0.064f,
       0.945f,  0.719f,  0.592f,
       0.543f,  0.021f,  0.978f,
       
       0.279f,  0.317f,  0.505f,
       0.167f,  0.620f,  0.077f,
       0.347f,  0.857f,  0.137f,
       
       0.055f,  0.953f,  0.042f,
       0.714f,  0.505f,  0.345f,
       0.783f,  0.290f,  0.734f,
       
       0.722f,  0.645f,  0.174f,
       0.302f,  0.455f,  0.848f,
       0.225f,  0.587f,  0.040f,
       
       0.517f,  0.713f,  0.338f,
       0.053f,  0.959f,  0.120f,
       0.393f,  0.621f,  0.362f,
       
       0.673f,  0.211f,  0.457f,
       0.820f,  0.883f,  0.371f,
       0.982f,  0.099f,  0.879f,
   };
   
   public Block(Vector3f location, Vector3f rotation){
      this.location = location;
      this.rotation = rotation;
   }
   
   public Block(Vector3f location){
      this.location = location;
      this.rotation = new Vector3f(0, 0, 0);
   }
   
   public Block(){
      this.location = new Vector3f(0, 0, 0);
      this.rotation = new Vector3f(0, 0, 0);
   }
   
   public void init(){
      vertexBuffer = BufferUtils.createFloatBuffer(108);
      vertexBuffer.put(vertexData);
      vertexBuffer.flip();
     
      colorBuffer = BufferUtils.createFloatBuffer(108);
      colorBuffer.put(colorData);
      colorBuffer.flip();
     
      vertexPointer = glGenBuffers();
      glBindBuffer(GL_ARRAY_BUFFER, vertexPointer);
      glBufferData(GL_ARRAY_BUFFER, vertexBuffer, GL_STATIC_DRAW);
      glBindBuffer(GL_ARRAY_BUFFER, 0);

      colorPointer = glGenBuffers();
      glBindBuffer(GL_ARRAY_BUFFER, colorPointer);
      glBufferData(GL_ARRAY_BUFFER, colorBuffer, GL_STATIC_DRAW);
      glBindBuffer(GL_ARRAY_BUFFER, 0);
     
      boxShader = new Shader();
      boxShader.attachVertexShader("./assets/box.vert");
      boxShader.attachFragmentShader("./assets/box.frag");
      boxShader.link();
   }
   
   public void render(){
      boxShader.bind();
         glEnableClientState(GL_VERTEX_ARRAY);
         glEnableClientState(GL_COLOR_ARRAY);
         
         glBindBuffer(GL_ARRAY_BUFFER, vertexPointer);
         glVertexPointer(3, GL_FLOAT, 0, 0);
         
         glBindBuffer(GL_ARRAY_BUFFER, colorPointer);
         glColorPointer(3, GL_FLOAT, 0, 0);
         
         glDrawArrays(GL_TRIANGLES, 0, 12);
         
         glDisableClientState(GL_VERTEX_ARRAY);
         glDisableClientState(GL_COLOR_ARRAY);
      boxShader.unbind();
   }
   
   public void update(){
     
   }
   
   public void destroy(){
      glDeleteBuffers(vertexPointer);
      glDeleteBuffers(colorPointer);
   }
}


I haven't used uniform matrices yet, or layouts and in/out. I've got the basics of the the built-in matrices

Fragment shader:
1  
2  
3  
void main(){
   gl_FragColor = gl_Color;
}


Vertex shader:
1  
2  
3  
4  
void main(){
    gl_FrontColor = gl_Color;
   gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex;
}


Result:
Offline Longarmx
« Reply #1 - Posted 2014-06-24 17:36:24 »

Have you verified that those triangles aren't being subjected to face culling? I would try switching the order of some of those triangles vertices.

Offline Spacebeans
« Reply #2 - Posted 2014-06-24 17:37:43 »

No, there isn't face culling enabled. (I don't think its enabled by default...)
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Longarmx
« Reply #3 - Posted 2014-06-24 17:39:51 »

Here is the culprit I believe:

1  
glDrawArrays(GL_TRIANGLES, 0, 12);


This says that you are going to draw 12 indices, not 12 triangles. It should be

1  
glDrawArrays(GL_TRIANGLES, 0, 12 * 3);


Edit: Actually, you don't even supply enough vertices to draw an entire cube with glDrawArrays(). You are going to have to have 36 vertices to draw a full cube.

Offline Spacebeans
« Reply #4 - Posted 2014-06-24 23:44:50 »

Thank you so much! It worked.
Pages: [1]
  ignore  |  Print  
 
 

 
EgonOlsen (1674 views)
2018-06-10 19:43:48

EgonOlsen (1722 views)
2018-06-10 19:43:44

EgonOlsen (1161 views)
2018-06-10 19:43:20

DesertCoockie (1588 views)
2018-05-13 18:23:11

nelsongames (1188 views)
2018-04-24 18:15:36

nelsongames (1717 views)
2018-04-24 18:14:32

ivj94 (2542 views)
2018-03-24 14:47:39

ivj94 (1767 views)
2018-03-24 14:46:31

ivj94 (2847 views)
2018-03-24 14:43:53

Solater (972 views)
2018-03-17 05:04:08
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46
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!