~ubuntu-branches/ubuntu/vivid/emscripten/vivid

« back to all changes in this revision

Viewing changes to tests/float_tex.cpp

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-05-02 13:11:51 UTC
  • Revision ID: package-import@ubuntu.com-20130502131151-q8dvteqr1ef2x7xz
Tags: upstream-1.4.1~20130504~adb56cb
ImportĀ upstreamĀ versionĀ 1.4.1~20130504~adb56cb

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#define GL_GLEXT_PROTOTYPES
 
2
#define EGL_EGLEXT_PROTOTYPES
 
3
#include <cmath>
 
4
#include <iostream>
 
5
#include <vector>
 
6
extern "C" {
 
7
#include <GL/gl.h>
 
8
#include <GL/glut.h>
 
9
}
 
10
static const char vertex_shader[] =
 
11
        "#ifdef GL_ES\n"
 
12
        "precision highp float;\n"
 
13
        "#endif\n"
 
14
        "attribute float indices;\n"
 
15
        "uniform sampler2D nodeInfo;\n"
 
16
        "varying vec4 color;"
 
17
        "\n"
 
18
        "void main(void)\n"
 
19
        "{\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"
 
25
        "}\n";
 
26
static const char fragment_shader[] =
 
27
        "#ifdef GL_ES\n"
 
28
        "precision highp float;\n"
 
29
        "#endif\n"
 
30
        "\n"
 
31
        "varying vec4 color;\n"
 
32
        "void main(void)\n"
 
33
        "{\n"
 
34
        "  float dst = distance(vec2(0.5, 0.5), gl_PointCoord); \n"
 
35
        "  gl_FragColor = color;\n"
 
36
        "  if ( dst > 0.3) {"
 
37
        "    gl_FragColor = vec4(0., 0., 0.5, 0.2);\n"
 
38
        "}\n"
 
39
        "if ( dst > 0.5) discard;\n"
 
40
        "}";
 
41
struct NodeInfo { //structure that we want to transmit to our shaders
 
42
    float x;
 
43
    float y;
 
44
    float s;
 
45
    float c;
 
46
};
 
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() {
 
55
    int count = 0;
 
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);
 
61
        ++count;
 
62
    }
 
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);
 
68
    alpha -= 0.001;
 
69
}
 
70
static void glut_draw_callback(void) {
 
71
    glDisable(GL_CULL_FACE);
 
72
    glDisable(GL_DEPTH_TEST);
 
73
    glEnable(GL_BLEND);
 
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);
 
85
    glutSwapBuffers();
 
86
}
 
87
GLuint createShader(const char source[], int type) {
 
88
    char msg[512];
 
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;
 
94
    return shader;
 
95
}
 
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);
 
101
    char msg[512];
 
102
    glGetProgramInfoLog(program, sizeof msg, NULL, msg);
 
103
    std::cout << "info: " <<  msg << std::endl;
 
104
    glUseProgram(program);
 
105
    std::vector<float> elements(nbNodes);
 
106
    int count = 0;
 
107
    for (float x=0; x < nbNodes; ++x ) {
 
108
        elements[count] = count;
 
109
        ++count;
 
110
    }
 
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);
 
123
}
 
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      );
 
131
    gl_init();
 
132
    glutMainLoop();
 
133
    return 0;
 
134
}
 
135
 
 
136