jeudi 18 août 2016

Randomized particles are collapsed to Right-Top of the window

Firstly, i am a beginner in OpenGL. Be mercyful. And i do not have a good English. If you see something wrong, just edit.

Hello, i am trying to generate 200000 particles with different coordinates and colors. So i declare a random number generator for this:

std::random_device TrueRandomNumberGenerator;
std::uniform_real_distribution<float> Distribution(0.0, 1.0);

This is will generate coordinates between 1 and 0. Visible OpenGL coordinates are between 1 and 0. And RGBA values are between 1 and 0.

After that i create array with size 200000 * 3 for particle coordinates and fill the array with random values between 1 and 0:

float Points[600000];

for(size_t I = 0; I < 600000; I++)
    Points[I] = Distribution(TrueRandomNumberGenerator);

After that i generate 2 VBO and i pass the Points to first VBO:

unsigned int VBO[2];
glGenBuffers (2, VBO);

glBindBuffer (GL_ARRAY_BUFFER, VBO[0]);
glBufferData (GL_ARRAY_BUFFER, sizeof(Points), Points, GL_STATIC_DRAW);

And i do same things for Colors:

float Colors[600000];

for(size_t I = 0; I < 600000; I++)
    Colors[I] = Distribution2(TrueRandomNumberGenerator);

glBindBuffer(GL_ARRAY_BUFFER, VBO[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(Colors), Colors, GL_STATIC_DRAW);

And i generate a VAO and bind it for provide the communication between shader program and the VBO:

unsigned int VAO = 0;
glGenVertexArrays (1, &VAO);

glBindVertexArray (VAO);

And after that i enabled the vertex attributes and loaded the shaders:

glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);

glBindBuffer (GL_ARRAY_BUFFER, VBO[0]);
glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, 0, NULL);

glBindBuffer (GL_ARRAY_BUFFER, VBO[1]);
glVertexAttribPointer (1, 3, GL_FLOAT, GL_FALSE, 0, NULL);

Shader shader; //Shader is a class for loading shaders dynamically
shader.Create("Shaders/Test.VS", GL_VERTEX_SHADER); 
shader.Create("Shaders/Test.FS", GL_FRAGMENT_SHADER);
shader.Program(); //Creates the program and saves it.

glUseProgram(shader.getProgram()); //Returns the program ID

And in my main loop, i make the draw call:

glDrawArrays (GL_POINTS, 0, 600000); 

I compile and run the code, there is no problem with colors but i notice the particles are 'collapsed' (I do not sure about it is the true word) to right-top of the window.

Image: Output

Test.VS:

#version 330

in vec3 VPosition;
in vec3 VColor;
out vec4 Color;

void main(){

    gl_Position = vec4(VPosition, 1.0);
    Color = vec4(VColor, 1.0);
}

Test.FS:

#version 330

out vec4 FragmentColor;
in vec4 Color;

void main()
{
    FragmentColor = Color;
}

What is the problem?




Aucun commentaire:

Enregistrer un commentaire