3
3
// University of Illinois/NCSA Open Source License. Both these licenses can be
4
4
// found in the LICENSE file.
5
5
6
+ #include <math.h>
7
+ #include <stdio.h>
8
+ #include <string.h>
9
+
6
10
#define GL_GLEXT_PROTOTYPES
7
11
#define EGL_EGLEXT_PROTOTYPES
8
- #include < cmath>
9
- #include < iostream>
10
- #include < vector>
11
- extern " C" {
12
12
#include <GL/gl.h>
13
13
#include <GL/glut.h>
14
- }
14
+
15
15
static const char vertex_shader [] =
16
16
"#ifdef GL_ES\n"
17
17
"precision lowp float;\n"
@@ -28,6 +28,7 @@ static const char vertex_shader[] =
28
28
" gl_PointSize = v.z;\n"
29
29
" color = vec4(0.5 + v.w/2., 0.5 + 0.5 * v.w/2., 0.5, 1);\n"
30
30
"}\n" ;
31
+
31
32
static const char fragment_shader [] =
32
33
"#ifdef GL_ES\n"
33
34
"precision lowp float;\n"
@@ -43,42 +44,46 @@ static const char fragment_shader[] =
43
44
"}\n"
44
45
"if ( dst > 0.5) discard;\n"
45
46
"}" ;
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
47
49
float x ;
48
50
float y ;
49
51
float s ;
50
52
float c ;
51
- };
53
+ } NodeInfo ;
54
+
52
55
GLuint nodeTexture ; //texture id used to bind
53
56
GLuint nodeSamplerLocation ; //shader sampler address
54
57
GLuint indicesAttributeLocation ; //shader attribute address
55
58
GLuint 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.
58
61
double alpha = 0 ; //use to make a simple funny effect;
62
+
59
63
static void updateFloatTexture () {
60
64
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 );
66
70
++ count ;
67
71
}
68
72
glBindTexture (GL_TEXTURE_2D , nodeTexture );
69
- glTexImage2D (GL_TEXTURE_2D, 0 , GL_RGBA32F, nbNodes , 1 , 0 , GL_RGBA, GL_FLOAT, data);
73
+ glTexImage2D (GL_TEXTURE_2D , 0 , GL_RGBA32F , NUM_NODES , 1 , 0 , GL_RGBA , GL_FLOAT , data );
70
74
#ifdef __EMSCRIPTEN__ // In GLES2 and WebGL1, we must use unsized texture internal formats.
71
75
const GLenum internalFormat = GL_RGBA ;
72
76
#else
73
77
// In desktop GL, we can also use sized internal formats.
74
78
const GLenum internalFormat = GL_RGBA32F ;
75
79
#endif
76
- glTexImage2D (GL_TEXTURE_2D, 0 , internalFormat, nbNodes , 1 , 0 , GL_RGBA, GL_FLOAT, data);
80
+ glTexImage2D (GL_TEXTURE_2D , 0 , internalFormat , NUM_NODES , 1 , 0 , GL_RGBA , GL_FLOAT , data );
77
81
glTexParameteri (GL_TEXTURE_2D , GL_TEXTURE_MAG_FILTER , GL_NEAREST );
78
82
glTexParameteri (GL_TEXTURE_2D , GL_TEXTURE_MIN_FILTER , GL_NEAREST );
79
83
glBindTexture (GL_TEXTURE_2D , 0 );
80
84
alpha -= 0.001 ;
81
85
}
86
+
82
87
static void glut_draw_callback (void ) {
83
88
glDisable (GL_CULL_FACE );
84
89
glDisable (GL_DEPTH_TEST );
@@ -93,30 +98,32 @@ static void glut_draw_callback(void) {
93
98
glEnableVertexAttribArray (0 );
94
99
glBindBuffer (GL_ARRAY_BUFFER , indicesVBO );
95
100
glVertexAttribPointer (0 , 1 , GL_FLOAT , GL_FALSE , 0 , NULL );
96
- glDrawArrays (GL_POINTS, 0 , nbNodes );
101
+ glDrawArrays (GL_POINTS , 0 , NUM_NODES );
97
102
glutSwapBuffers ();
98
103
}
99
- GLuint createShader (const char source[], int type) {
104
+
105
+ GLuint createShader (const char * source , int type ) {
100
106
char msg [512 ];
101
107
GLuint shader = glCreateShader (type );
102
- glShaderSource (shader, 1 , ( const GLchar**)( &source) , NULL );
108
+ glShaderSource (shader , 1 , & source , NULL );
103
109
glCompileShader (shader );
104
110
glGetShaderInfoLog (shader , sizeof msg , NULL , msg );
105
- std::cout << " Shader info: " << msg << std::endl ;
111
+ printf ( "Shader info: %s\n" , msg ) ;
106
112
return shader ;
107
113
}
114
+
108
115
static void gl_init (void ) {
109
116
GLuint program = glCreateProgram ();
110
117
glAttachShader (program , createShader (vertex_shader , GL_VERTEX_SHADER ));
111
118
glAttachShader (program , createShader (fragment_shader , GL_FRAGMENT_SHADER ));
112
119
glLinkProgram (program );
113
120
char msg [512 ];
114
121
glGetProgramInfoLog (program , sizeof msg , NULL , msg );
115
- std::cout << " info: " << msg << std::endl ;
122
+ printf ( "info: %s\n" , msg ) ;
116
123
glUseProgram (program );
117
- std::vector< float > elements (nbNodes) ;
124
+ float elements [ NUM_NODES ] ;
118
125
int count = 0 ;
119
- for (float x=0 ; x < nbNodes ; ++x ) {
126
+ for (float x = 0 ; x < NUM_NODES ; ++ x ) {
120
127
elements [count ] = count ;
121
128
++ count ;
122
129
}
@@ -125,10 +132,10 @@ static void gl_init(void) {
125
132
/* Store the vertices in a vertex buffer object (VBO) */
126
133
glGenBuffers (1 , & indicesVBO );
127
134
glBindBuffer (GL_ARRAY_BUFFER , indicesVBO );
128
- float zeroes[nbNodes ];
135
+ float zeroes [NUM_NODES ];
129
136
memset (zeroes , 0 , sizeof (zeroes ));
130
- glBufferData (GL_ARRAY_BUFFER, elements. size () * sizeof (float ), zeroes, GL_STATIC_DRAW);
131
- for (int x = 0 ; x < nbNodes ; x++) {
137
+ glBufferData (GL_ARRAY_BUFFER , NUM_NODES * sizeof (float ), zeroes , GL_STATIC_DRAW );
138
+ for (int x = 0 ; x < NUM_NODES ; x ++ ) {
132
139
glBufferSubData (GL_ARRAY_BUFFER , x * sizeof (float ), sizeof (float ), & elements [x ]);
133
140
}
134
141
/* Get the locations of the uniforms so we can access them */
@@ -140,13 +147,14 @@ static void gl_init(void) {
140
147
glEnable (GL_POINT_SPRITE );
141
148
#endif
142
149
}
150
+
143
151
int main (int argc , char * argv []) {
144
152
glutInit (& argc , argv );
145
153
glutInitWindowSize (640 , 480 );
146
154
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
147
- glutCreateWindow (" Simple FLOAT Texture Test" );
155
+ int w = glutCreateWindow ("Simple FLOAT Texture Test" );
148
156
/* Set up glut callback functions */
149
- glutDisplayFunc (glut_draw_callback );
157
+ glutDisplayFunc (glut_draw_callback );
150
158
gl_init ();
151
159
glutMainLoop ();
152
160
return 0 ;
0 commit comments