33// University of Illinois/NCSA Open Source License. Both these licenses can be
44// found in the LICENSE file.
55
6+ #include <math.h>
7+ #include <stdio.h>
8+ #include <string.h>
9+
610#define GL_GLEXT_PROTOTYPES
711#define EGL_EGLEXT_PROTOTYPES
8- #include < cmath>
9- #include < iostream>
10- #include < vector>
11- extern " C" {
1212#include <GL/gl.h>
1313#include <GL/glut.h>
14- }
14+
1515static const char vertex_shader [] =
1616 "#ifdef GL_ES\n"
1717 "precision lowp float;\n"
@@ -28,6 +28,7 @@ static const char vertex_shader[] =
2828 " gl_PointSize = v.z;\n"
2929 " color = vec4(0.5 + v.w/2., 0.5 + 0.5 * v.w/2., 0.5, 1);\n"
3030 "}\n" ;
31+
3132static const char fragment_shader [] =
3233 "#ifdef GL_ES\n"
3334 "precision lowp float;\n"
@@ -43,26 +44,29 @@ static const char fragment_shader[] =
4344 "}\n"
4445 "if ( dst > 0.5) discard;\n"
4546 "}" ;
46- struct NodeInfo { // structure that we want to transmit to our shaders
47+
48+ typedef struct NodeInfo { //structure that we want to transmit to our shaders
4749 float x ;
4850 float y ;
4951 float s ;
5052 float c ;
51- };
53+ } NodeInfo ;
54+
5255GLuint nodeTexture ; //texture id used to bind
5356GLuint nodeSamplerLocation ; //shader sampler address
5457GLuint indicesAttributeLocation ; //shader attribute address
5558GLuint indicesVBO ; //Vertex Buffer Object Id;
56- const int nbNodes = 512 ;
57- NodeInfo data[nbNodes ]; // our data that will be transmitted using float texture.
59+ #define NUM_NODES 512
60+ NodeInfo data [NUM_NODES ]; //our data that will be transmitted using float texture.
5861double alpha = 0 ; //use to make a simple funny effect;
62+
5963static void updateFloatTexture () {
6064 int count = 0 ;
61- for (float x=0 ; x < nbNodes ; ++x ) {
62- data[count].x = 0.2 *pow (cos (alpha), 3 ) + (sin (alpha)*3 . + 3.5 ) * x/nbNodes * cos (alpha + x/nbNodes * 16 . * M_PI);
63- data[count].y = 0.2 *pow (sin (alpha), 3 ) + (sin (alpha)*3 . + 3.5 ) * x/nbNodes * sin (alpha + x/nbNodes * 16 . * M_PI);
64- data[count].s = (16 . + 16 . * cos (alpha + x/nbNodes * 32 . * M_PI)) + 8 .;// * fmod(x/nbNodes + alpha, 1.) + 5.;
65- data[count].c = 0.5 + 0.5 * sin (alpha + x/nbNodes * 32 . * M_PI);
65+ for (float x = 0 ; x < NUM_NODES ; ++ x ) {
66+ data [count ].x = 0.2 * pow (cos (alpha ), 3 ) + (sin (alpha )* 3. + 3.5 ) * x /NUM_NODES * cos (alpha + x /NUM_NODES * 16. * M_PI );
67+ data [count ].y = 0.2 * pow (sin (alpha ), 3 ) + (sin (alpha )* 3. + 3.5 ) * x /NUM_NODES * sin (alpha + x /NUM_NODES * 16. * M_PI );
68+ data [count ].s = (16. + 16. * cos (alpha + x /NUM_NODES * 32. * M_PI )) + 8. ;// * fmod(x/NUM_NODES + alpha, 1.) + 5.;
69+ data [count ].c = 0.5 + 0.5 * sin (alpha + x /NUM_NODES * 32. * M_PI );
6670 ++ count ;
6771 }
6872 glBindTexture (GL_TEXTURE_2D , nodeTexture );
@@ -72,12 +76,13 @@ static void updateFloatTexture() {
7276 // In desktop GL, we can also use sized internal formats.
7377 const GLenum internalFormat = GL_RGBA32F ;
7478#endif
75- glTexImage2D (GL_TEXTURE_2D, 0 , internalFormat, nbNodes , 1 , 0 , GL_RGBA, GL_FLOAT, data);
79+ glTexImage2D (GL_TEXTURE_2D , 0 , internalFormat , NUM_NODES , 1 , 0 , GL_RGBA , GL_FLOAT , data );
7680 glTexParameteri (GL_TEXTURE_2D , GL_TEXTURE_MAG_FILTER , GL_NEAREST );
7781 glTexParameteri (GL_TEXTURE_2D , GL_TEXTURE_MIN_FILTER , GL_NEAREST );
7882 glBindTexture (GL_TEXTURE_2D , 0 );
7983 alpha -= 0.001 ;
8084}
85+
8186static void glut_draw_callback (void ) {
8287 glDisable (GL_CULL_FACE );
8388 glDisable (GL_DEPTH_TEST );
@@ -92,30 +97,32 @@ static void glut_draw_callback(void) {
9297 glEnableVertexAttribArray (0 );
9398 glBindBuffer (GL_ARRAY_BUFFER , indicesVBO );
9499 glVertexAttribPointer (0 , 1 , GL_FLOAT , GL_FALSE , 0 , NULL );
95- glDrawArrays (GL_POINTS, 0 , nbNodes );
100+ glDrawArrays (GL_POINTS , 0 , NUM_NODES );
96101 glutSwapBuffers ();
97102}
98- GLuint createShader (const char source[], int type) {
103+
104+ GLuint createShader (const char * source , int type ) {
99105 char msg [512 ];
100106 GLuint shader = glCreateShader (type );
101- glShaderSource (shader, 1 , ( const GLchar**)( &source) , NULL );
107+ glShaderSource (shader , 1 , & source , NULL );
102108 glCompileShader (shader );
103109 glGetShaderInfoLog (shader , sizeof msg , NULL , msg );
104- std::cout << " Shader info: " << msg << std::endl ;
110+ printf ( "Shader info: %s\n" , msg ) ;
105111 return shader ;
106112}
113+
107114static void gl_init (void ) {
108115 GLuint program = glCreateProgram ();
109116 glAttachShader (program , createShader (vertex_shader , GL_VERTEX_SHADER ));
110117 glAttachShader (program , createShader (fragment_shader , GL_FRAGMENT_SHADER ));
111118 glLinkProgram (program );
112119 char msg [512 ];
113120 glGetProgramInfoLog (program , sizeof msg , NULL , msg );
114- std::cout << " info: " << msg << std::endl ;
121+ printf ( "info: %s\n" , msg ) ;
115122 glUseProgram (program );
116- std::vector< float > elements (nbNodes) ;
123+ float elements [ NUM_NODES ] ;
117124 int count = 0 ;
118- for (float x=0 ; x < nbNodes ; ++x ) {
125+ for (float x = 0 ; x < NUM_NODES ; ++ x ) {
119126 elements [count ] = count ;
120127 ++ count ;
121128 }
@@ -124,28 +131,29 @@ static void gl_init(void) {
124131 /* Store the vertices in a vertex buffer object (VBO) */
125132 glGenBuffers (1 , & indicesVBO );
126133 glBindBuffer (GL_ARRAY_BUFFER , indicesVBO );
127- float zeroes[nbNodes ];
134+ float zeroes [NUM_NODES ];
128135 memset (zeroes , 0 , sizeof (zeroes ));
129- glBufferData (GL_ARRAY_BUFFER, elements. size () * sizeof (float ), zeroes, GL_STATIC_DRAW);
130- for (int x = 0 ; x < nbNodes ; x++) {
136+ glBufferData (GL_ARRAY_BUFFER , NUM_NODES * sizeof (float ), zeroes , GL_STATIC_DRAW );
137+ for (int x = 0 ; x < NUM_NODES ; x ++ ) {
131138 glBufferSubData (GL_ARRAY_BUFFER , x * sizeof (float ), sizeof (float ), & elements [x ]);
132139 }
133140 /* Get the locations of the uniforms so we can access them */
134- nodeSamplerLocation = glGetUniformLocation (program, " nodeInfo" );
141+ nodeSamplerLocation = glGetUniformLocation (program , "nodeInfo" );
135142 glBindAttribLocation (program , 0 , "indices" );
136143#ifndef __EMSCRIPTEN__ // GLES2 & WebGL do not have these, only pre 3.0 desktop GL and compatibility mode GL3.0+ GL do.
137144 //Enable glPoint size in shader, always enable in Open Gl ES 2.
138145 glEnable (GL_VERTEX_PROGRAM_POINT_SIZE );
139146 glEnable (GL_POINT_SPRITE );
140147#endif
141148}
149+
142150int main (int argc , char * argv []) {
143151 glutInit (& argc , argv );
144152 glutInitWindowSize (640 , 480 );
145153 glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
146- glutCreateWindow (" Simple FLOAT Texture Test" );
154+ int w = glutCreateWindow ("Simple FLOAT Texture Test" );
147155 /* Set up glut callback functions */
148- glutDisplayFunc (glut_draw_callback );
156+ glutDisplayFunc (glut_draw_callback );
149157 gl_init ();
150158 glutMainLoop ();
151159 return 0 ;
0 commit comments