1
#define GL_GLEXT_PROTOTYPES
2
#define EGL_EGLEXT_PROTOTYPES
10
static const char vertex_shader[] =
12
"precision highp float;\n"
14
"attribute float indices;\n"
15
"uniform sampler2D nodeInfo;\n"
20
" float s = (indices + 0.5) / 512.; \n"
21
" vec4 v = texture2D(nodeInfo, vec2( s, 0.5));\n"
22
" gl_Position = vec4(v.x, v.y, 0.5, 1.);\n"
23
" gl_PointSize = v.z;\n"
24
" color = vec4(0.5 + v.w/2., 0.5 + 0.5 * v.w/2., 0.5, 1);\n"
26
static const char fragment_shader[] =
28
"precision highp float;\n"
31
"varying vec4 color;\n"
34
" float dst = distance(vec2(0.5, 0.5), gl_PointCoord); \n"
35
" gl_FragColor = color;\n"
37
" gl_FragColor = vec4(0., 0., 0.5, 0.2);\n"
39
"if ( dst > 0.5) discard;\n"
41
struct NodeInfo { //structure that we want to transmit to our shaders
47
GLuint nodeTexture; //texture id used to bind
48
GLuint nodeSamplerLocation; //shader sampler address
49
GLuint indicesAttributeLocation; //shader attribute address
50
GLuint indicesVBO; //Vertex Buffer Object Id;
51
const int nbNodes = 512;
52
NodeInfo * data = new NodeInfo[nbNodes]; //our data that will be transmitted using float texture.
53
double alpha = 0; //use to make a simple funny effect;
54
static void updateFloatTexture() {
56
for (float x=0; x < nbNodes; ++x ) {
57
data[count].x = 0.2*pow(cos(alpha), 3) + (sin(alpha)*3. + 3.5) * x/nbNodes * cos(alpha + x/nbNodes * 16. * M_PI);
58
data[count].y = 0.2*pow(sin(alpha), 3) + (sin(alpha)*3. + 3.5) * x/nbNodes * sin(alpha + x/nbNodes * 16. * M_PI);
59
data[count].s = (16. + 16. * cos(alpha + x/nbNodes * 32. * M_PI)) + 8.;// * fmod(x/nbNodes + alpha, 1.) + 5.;
60
data[count].c = 0.5 + 0.5 * sin(alpha + x/nbNodes * 32. * M_PI);
63
glBindTexture(GL_TEXTURE_2D, nodeTexture);
64
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, nbNodes, 1, 0, GL_RGBA, GL_FLOAT, data);
65
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
66
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
67
glBindTexture(GL_TEXTURE_2D, NULL);
70
static void glut_draw_callback(void) {
71
glDisable(GL_CULL_FACE);
72
glDisable(GL_DEPTH_TEST);
74
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
75
glClearColor(1., 1., 1., 0.);
76
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
77
glActiveTexture(GL_TEXTURE0);
78
updateFloatTexture(); //we change the texture each time to create the effect (it is just for the test)
79
glBindTexture(GL_TEXTURE_2D, nodeTexture);
80
glUniform1i(nodeSamplerLocation, GL_TEXTURE0);
81
glEnableVertexAttribArray(0);
82
glBindBuffer(GL_ARRAY_BUFFER, indicesVBO);
83
glVertexAttribPointer(0, 1, GL_FLOAT, GL_FALSE, 0, NULL);
84
glDrawArrays(GL_POINTS, 0, nbNodes);
87
GLuint createShader(const char source[], int type) {
89
GLuint shader = glCreateShader(type);
90
glShaderSource(shader, 1, (const GLchar**)(&source), NULL);
91
glCompileShader(shader);
92
glGetShaderInfoLog(shader, sizeof msg, NULL, msg);
93
std::cout << "Shader info: " << msg << std::endl;
96
static void gl_init(void) {
97
GLuint program = glCreateProgram();
98
glAttachShader(program, createShader(vertex_shader , GL_VERTEX_SHADER));
99
glAttachShader(program, createShader(fragment_shader, GL_FRAGMENT_SHADER));
100
glLinkProgram(program);
102
glGetProgramInfoLog(program, sizeof msg, NULL, msg);
103
std::cout << "info: " << msg << std::endl;
104
glUseProgram(program);
105
std::vector<float> elements(nbNodes);
107
for (float x=0; x < nbNodes; ++x ) {
108
elements[count] = count;
111
/*Create one texture to store all the needed information */
112
glGenTextures(1, &nodeTexture);
113
/* Store the vertices in a vertex buffer object (VBO) */
114
glGenBuffers(1, &indicesVBO);
115
glBindBuffer(GL_ARRAY_BUFFER, indicesVBO);
116
glBufferData(GL_ARRAY_BUFFER, elements.size() * sizeof(uint), &elements[0], GL_STATIC_DRAW);
117
/* Get the locations of the uniforms so we can access them */
118
nodeSamplerLocation = glGetUniformLocation(program, "nodeInfo");
119
glBindAttribLocation(program, 0, "indices");
120
//Enable glPoint size in shader, always enable in Open Gl ES 2.
121
glEnable(GL_VERTEX_PROGRAM_POINT_SIZE);
122
glEnable(GL_POINT_SPRITE);
124
int main(int argc, char *argv[]) {
125
glutInit(&argc, argv);
126
glutInitWindowSize(640, 480);
127
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
128
glutCreateWindow("Simple FLOAT Texture Test");
129
/* Set up glut callback functions */
130
glutDisplayFunc(glut_draw_callback );