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,26 +44,29 @@ 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 );
@@ -72,12 +76,13 @@ static void updateFloatTexture() {
72
76
// In desktop GL, we can also use sized internal formats.
73
77
const GLenum internalFormat = GL_RGBA32F ;
74
78
#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 );
76
80
glTexParameteri (GL_TEXTURE_2D , GL_TEXTURE_MAG_FILTER , GL_NEAREST );
77
81
glTexParameteri (GL_TEXTURE_2D , GL_TEXTURE_MIN_FILTER , GL_NEAREST );
78
82
glBindTexture (GL_TEXTURE_2D , 0 );
79
83
alpha -= 0.001 ;
80
84
}
85
+
81
86
static void glut_draw_callback (void ) {
82
87
glDisable (GL_CULL_FACE );
83
88
glDisable (GL_DEPTH_TEST );
@@ -92,30 +97,32 @@ static void glut_draw_callback(void) {
92
97
glEnableVertexAttribArray (0 );
93
98
glBindBuffer (GL_ARRAY_BUFFER , indicesVBO );
94
99
glVertexAttribPointer (0 , 1 , GL_FLOAT , GL_FALSE , 0 , NULL );
95
- glDrawArrays (GL_POINTS, 0 , nbNodes );
100
+ glDrawArrays (GL_POINTS , 0 , NUM_NODES );
96
101
glutSwapBuffers ();
97
102
}
98
- GLuint createShader (const char source[], int type) {
103
+
104
+ GLuint createShader (const char * source , int type ) {
99
105
char msg [512 ];
100
106
GLuint shader = glCreateShader (type );
101
- glShaderSource (shader, 1 , ( const GLchar**)( &source) , NULL );
107
+ glShaderSource (shader , 1 , & source , NULL );
102
108
glCompileShader (shader );
103
109
glGetShaderInfoLog (shader , sizeof msg , NULL , msg );
104
- std::cout << " Shader info: " << msg << std::endl ;
110
+ printf ( "Shader info: %s\n" , msg ) ;
105
111
return shader ;
106
112
}
113
+
107
114
static void gl_init (void ) {
108
115
GLuint program = glCreateProgram ();
109
116
glAttachShader (program , createShader (vertex_shader , GL_VERTEX_SHADER ));
110
117
glAttachShader (program , createShader (fragment_shader , GL_FRAGMENT_SHADER ));
111
118
glLinkProgram (program );
112
119
char msg [512 ];
113
120
glGetProgramInfoLog (program , sizeof msg , NULL , msg );
114
- std::cout << " info: " << msg << std::endl ;
121
+ printf ( "info: %s\n" , msg ) ;
115
122
glUseProgram (program );
116
- std::vector< float > elements (nbNodes) ;
123
+ float elements [ NUM_NODES ] ;
117
124
int count = 0 ;
118
- for (float x=0 ; x < nbNodes ; ++x ) {
125
+ for (float x = 0 ; x < NUM_NODES ; ++ x ) {
119
126
elements [count ] = count ;
120
127
++ count ;
121
128
}
@@ -124,28 +131,29 @@ static void gl_init(void) {
124
131
/* Store the vertices in a vertex buffer object (VBO) */
125
132
glGenBuffers (1 , & indicesVBO );
126
133
glBindBuffer (GL_ARRAY_BUFFER , indicesVBO );
127
- float zeroes[nbNodes ];
134
+ float zeroes [NUM_NODES ];
128
135
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 ++ ) {
131
138
glBufferSubData (GL_ARRAY_BUFFER , x * sizeof (float ), sizeof (float ), & elements [x ]);
132
139
}
133
140
/* Get the locations of the uniforms so we can access them */
134
- nodeSamplerLocation = glGetUniformLocation (program, " nodeInfo" );
141
+ nodeSamplerLocation = glGetUniformLocation (program , "nodeInfo" );
135
142
glBindAttribLocation (program , 0 , "indices" );
136
143
#ifndef __EMSCRIPTEN__ // GLES2 & WebGL do not have these, only pre 3.0 desktop GL and compatibility mode GL3.0+ GL do.
137
144
//Enable glPoint size in shader, always enable in Open Gl ES 2.
138
145
glEnable (GL_VERTEX_PROGRAM_POINT_SIZE );
139
146
glEnable (GL_POINT_SPRITE );
140
147
#endif
141
148
}
149
+
142
150
int main (int argc , char * argv []) {
143
151
glutInit (& argc , argv );
144
152
glutInitWindowSize (640 , 480 );
145
153
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
146
- glutCreateWindow (" Simple FLOAT Texture Test" );
154
+ int w = glutCreateWindow ("Simple FLOAT Texture Test" );
147
155
/* Set up glut callback functions */
148
- glutDisplayFunc (glut_draw_callback );
156
+ glutDisplayFunc (glut_draw_callback );
149
157
gl_init ();
150
158
glutMainLoop ();
151
159
return 0 ;
0 commit comments