Learning OpenGL for N-body Sims #2 - GLSL Vertex shading and fragmenter

If we keep using OpenGL with everything on CPU with millions of points things are going to slow down really quickly. Fortunately GPUs have optimized these sorts of operations, which we can take advantage of using a shader and fragmenter with the OpenGL Shading Language (GLSL).

Communication between CPU and GPU works through buffers, we set up a buffer that we'll push our vertices through and the GPU will read.

// Give our vertices to OpenGL. glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);

Here is the vertex shader code:


#version 330 core layout(location = 0) in vec3 vertexPosition_modelspace;
void main()
{
gl_Position.xyz = vertexPosition_modelspace;
gl_Position.w = 1.0;
}

All it does is pass on the points passed in one to one. In the fragment shader code we set the color to red.

#version 330 core out vec3 color;
void main()
{
color = vec3(1,0,0);
}

And we now have a red triangle, pretty underwhelming, but this framework will set the stage for our real-time N-body simulation.

red triangle

Source code on Github

Popular posts from this blog

Strawberry DNA extraction and viewing with a cheap USB Microscope

Visualizing TLE Orbital Elements with Python and matplotlib

Relearning web development by writing a clicking game using React, Npm and Browserify