Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (532)
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  
  glDrawArrays - problem with normals  (Read 2347 times)
0 Members and 1 Guest are viewing this topic.
Offline Darth Revan

Senior Newbie





« Posted 2012-09-19 18:13:22 »

Hi, i try to make a Voxel Engine.

I have a VAO that have a VBO. In this are the vertex and the normals of the Cubes.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
vaoId = GL30.glGenVertexArrays();
GL30.glBindVertexArray(vaoId);

ByteBuffer verticesBuffer = BufferUtils.createByteBuffer(numberOfVerices * 6);
verticesBuffer.put(verticesAndNormals, 0, numberOfVerices);
verticesBuffer.flip();

vboId = GL15.glGenBuffers();
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboId);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, verticesBuffer, GL15.GL_DYNAMIC_DRAW);
     
GL20.glVertexAttribPointer(0, 3, GL11.GL_BYTE, false, 6, 0); // vertex
GL20.glVertexAttribPointer(1, 3, GL11.GL_BYTE, false, 6, 3); // normals

GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
GL30.glBindVertexArray(0);


I can render the cubes like this:
1  
2  
3  
4  
5  
6  
7  
GL30.glBindVertexArray(vaoId);
GL20.glEnableVertexAttribArray(0);

GL11.glDrawArrays(GL11.GL_QUADS, 0, vertexCount);

GL20.glDisableVertexAttribArray(0);
GL30.glBindVertexArray(0);


But how can I say OpenGL that, the second VBO includes the normals?

And is this the right way to render my Cubes? Or is there a better?

PS: Sorry for my bad English.
Offline StumpyStrust
« Reply #1 - Posted 2012-09-19 18:47:02 »

Make sure your offsets are right as it looks like you are interleaving your VBO.

1 float is 4 bytes.

So a 3 float vertex is 4*3 bytes

I am still learning this stuff too.

Offline Darth Revan

Senior Newbie





« Reply #2 - Posted 2012-09-19 19:04:23 »

Make sure your offsets are right as it looks like you are interleaving your VBO.

1 float is 4 bytes.

So a 3 float vertex is 4*3 bytes

I am still learning this stuff too.
Yes I interleaving the VBO and I am sure that it is not because of that. (3 bytes for coordinate, 3 bytes for normals)

Could it be that I have to use shaders to tell OpenGL that the second attribute are the normals?

PS: Sorry for my bad English.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline theagentd
« Reply #3 - Posted 2012-09-19 19:16:10 »

Post your shader? Attributes are treated the same no matter what your shader uses them for...

Myomyomyo.
Offline Darth Revan

Senior Newbie





« Reply #4 - Posted 2012-09-19 19:34:39 »

I don't use a shader at the moment.

PS: Sorry for my bad English.
Offline Darth Revan

Senior Newbie





« Reply #5 - Posted 2012-09-20 08:40:34 »

I have tried the following vertex shader.

1  
2  
3  
4  
5  
6  
7  
8  
9  
#version 150 core

in vec3 in_Position;
in vec3 in_Normal;

void main(){
   gl_Position = in_Position;
   gl_Normal = in_Normal;
}


But it just brought me the following error message: Invalid operation

PS: Sorry for my bad English.
Offline theagentd
« Reply #6 - Posted 2012-09-20 10:26:55 »

Then you should use glVertexPointer and glNormalPointer instead and "enable" those attributes with glEnableClientState. The GL20 functions you use now are meant for custom shader attributes.

Myomyomyo.
Offline Darth Revan

Senior Newbie





« Reply #7 - Posted 2012-09-20 10:44:26 »

I used this command because I was trying to work with this tutorial to make my code ready for future versions of OpenGL (http://lwjgl.org/wiki/index.php?title=The_Quad_with_DrawArrays).

Do you mean so? With this code I get the error: Invalid enum

Here I'd use no VAO. Is this right? And is this a good way to render my cubes? Or are there better?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
ByteBuffer verticesBuffer = BufferUtils.createByteBuffer(numberOfVerices * 6);
verticesBuffer.put(verticesAndNormals, 0, numberOfVerices * 6);
verticesBuffer.flip();
     
vboId = GL15.glGenBuffers();
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboId);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, verticesBuffer, GL15.GL_DYNAMIC_DRAW);
     
GL11.glVertexPointer(3, GL11.GL_BYTE, 6, 0);
GL11.glNormalPointer(GL11.GL_BYTE, 6, 3);

GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
GL11.glEnableClientState(GL11.GL_NORMAL_ARRAY);

GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboId);

GL11.glDrawArrays(GL11.GL_QUADS, 0, vertexCount);

GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);

GL11.glDisableClientState(GL11.GL_NORMAL_ARRAY);
GL11.glDisableClientState(GL11.GL_VERTEX_ARRAY);

PS: Sorry for my bad English.
Offline Darth Revan

Senior Newbie





« Reply #8 - Posted 2012-09-20 16:56:29 »

I've found out that glVertexPointer(...) assumes no GL_BYTE as data type.

Maybe it would be better if I do it in a different way, because otherwise I will have to send twice as many bytes for the coordinates to the graphics card.

It is important for me that the frame rate is not going down even if I render many cubes and some of them change each frame.

PS: Sorry for my bad English.
Offline theagentd
« Reply #9 - Posted 2012-09-20 23:06:24 »

You're right, glVertexPointer and glNormalPointer do not accept byte data, only float data. glVertexAttribPointer accept any kind of data since it's made for general attributes. I'd recommend you to learn how to use shaders, since that'll allow you to do what you want to do.

Myomyomyo.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Darth Revan

Senior Newbie





« Reply #10 - Posted 2012-09-21 12:09:48 »

Ok, I have read some article about shader.

Now I want to make a vertex shader that simple set the attribute 0 as position of the vertex and the attribute 1 as normal. And a fragment shader that do the same that it does normal (because I have read that I can not use only a vertex shader without fragment shader).

I have found that code in a tutorial. But there are some things I don't understand.

1. Does that mean, that I must set the matrices projModelViewMatrix and normalMatrix each time before I render?
2. How does OpenGL know, that VertexOut.normal is the normal for that vertex? / How I can change the normal in the vertex shader if gl_Normal is readable only?
3. How I can make a "default" fragment shader?
4. Does it makes a difference in performance if I use old GLSL Methodes like ftransform()?
5. Will it be later more complicated to use shadow mapping, if I use me own shader?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
#version 150

layout (std140) uniform Matrices {
   mat4 projModelViewMatrix;
   mat3 normalMatrix;
};

in vec3 position;
in vec3 normal;

out VertexData {
   vec3 normal;
} VertexOut;

void main() {
   VertexOut.normal = normalize(normalMatrix * normal);
   gl_Position = projModelViewMatrix * vec4(position, 1.0);
}



PS: Sorry for my bad English.
Offline theagentd
« Reply #11 - Posted 2012-09-22 03:03:20 »

1 and 2. That's an GLSL 1.50 shader = OpenGL 3.0 shader, so you're making this more complicated than it has to be, so point 1 and 2 are solved by switching to an older version.

3) With older versions you can actually have a vertex shader without a fragment shader, but I recommend that you do use a fragment shader anyway.
4) No, it does the same thing under the hood for all but the most ancient hardware out there. You might win some FPS on old Intel hardware or so, assuming your program doesn't crash immediately due to their drivers. It won't work in your case though, since ftransform() can only use gl_Position, and you can't use that because it only accepts floats...
5) It's easier with shaders since you don't have to bother with a shitload of texture settings. Just a few more extra shader lines, obviously assuming you know how to generate a shadow map already.

1  
2  
3  
4  
5  
6  
7  
attribute vec3 position;
attribute vec3 normal;

void main() {
   gl_Position = gl_ModelViewProjectionMatrix * vec4(position, 1);
   gl_Normal = normalize(gl_NormalMatrix * normal);
}

This code uses the built-in matrices instead, so no need for a matrix library. One step at a time.

Now I want to make a vertex shader that simple set the attribute 0 as position of the vertex and the attribute 1 as normal.
That's not how it works, at least in older versions of GLSL. The driver specific GLSL compiler decides the attribute locations, and the OpenGL program queries the attribute locations with GL20.glGetAttribLocation(program, attribName);. In practice pretty much every driver would set position to 0 and normal to 1 in the above shader, but there's no guarantee and you could get problems on some platforms. Another important thing is that if for example position isn't used anywhere, it will be optimized away, meaning that normal would get the location 0 instead of 1. BOOOOoooooom...


I hope this gets you rolling, but feel free to ask if there's something that's unclear.

Myomyomyo.
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.

pw (15 views)
2014-07-24 01:59:36

Riven (14 views)
2014-07-23 21:16:32

Riven (13 views)
2014-07-23 21:07:15

Riven (15 views)
2014-07-23 20:56:16

ctomni231 (43 views)
2014-07-18 06:55:21

Zero Volt (40 views)
2014-07-17 23:47:54

danieldean (32 views)
2014-07-17 23:41:23

MustardPeter (36 views)
2014-07-16 23:30:00

Cero (51 views)
2014-07-16 00:42:17

Riven (50 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!