summaryrefslogtreecommitdiff
path: root/glbigshader.c
blob: 92297a1ca2a7ca7d8a2e1f322b4445eb6f5583c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
 * Copyright (C) 1999-2001  Brian Paul   All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

/*
 * This is a port of the infamous "glxgears" demo to straight EGL
 * Port by Dane Rushton 10 July 2005
 *
 * No command line options.
 * Program runs for 5 seconds then exits, outputing framerate to console
 */
#define MAXFRAME    5
#define PFINISH     1

#include <string.h>
#include <stdio.h>
#include <math.h>
#define GL_GLEXT_PROTOTYPES
#include <GL/glew.h>
#include <GL/glut.h>

static GLuint win_width = 1024;
static GLuint win_height = 512;
static GLuint frames = 0;

static GLuint shader_prog;
static GLuint vs_shader;
static GLuint ps_shader;

static GLuint uni_index;
static GLuint uni_col;
static GLuint uni_row;
static GLuint uni_expect;

const char *vs_shader_src =
    "#version 120                                                   \n"
    "void main()                                                    \n"
    "{                                                              \n"
    "    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;    \n"
    "}                                                              \n";

const char *ps_shader_src =
    "#version 120                                                   \n"
    "uniform int index;                                             \n"
    "uniform int col;                                               \n"
    "uniform int row;                                               \n"
    "uniform float expect;                                          \n"
    "                                                               \n"
    "void main()                                                    \n"
    "{                                                              \n"
    "    mat4x4[3] m = mat4x4[3](                                   \n"
    "        mat4x4(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0),\n"
    "        mat4x4(17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0, 25.0, 26.0, 27.0, 28.0, 29.0, 30.0, 31.0, 32.0),\n"
    "        mat4x4(33.0, 34.0, 35.0, 36.0, 37.0, 38.0, 39.0, 40.0, 41.0, 42.0, 43.0, 44.0, 45.0, 46.0, 47.0, 48.0));\n"
    "                                                               \n"
    "    gl_FragColor = (m[index][col][row] == expect)              \n"
    "        ? vec4(0.0, 1.0, 0.0, 1.0) : vec4(1.0, 0.0, 0.0, 1.0); \n"
    "}                                                              \n";

static void draw_rect(float x, float y, float w, float h)
{
    float verts[4][4];

    verts[0][0] = x;
    verts[0][1] = y;
    verts[0][2] = 0.0;
    verts[0][3] = 1.0;
    verts[1][0] = x + w;
    verts[1][1] = y;
    verts[1][2] = 0.0;
    verts[1][3] = 1.0;
    verts[2][0] = x + w;
    verts[2][1] = y + h;
    verts[2][2] = 0.0;
    verts[2][3] = 1.0;
    verts[3][0] = x;
    verts[3][1] = y + h;
    verts[3][2] = 0.0;
    verts[3][3] = 1.0;

    glVertexPointer(4, GL_FLOAT, 0, verts);
    glEnableClientState(GL_VERTEX_ARRAY);
    glDrawArrays(GL_QUADS, 0, 4);
    glDisableClientState(GL_VERTEX_ARRAY);
}

static void draw(void)
{
#if PFINISH
    fprintf(stderr, "EE FRAME -----------------------------------------------------\n");
#endif
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glUseProgram(0);
    glColor3f(0.0f, 0.0f, 1.0f);
    draw_rect(15, 15, 20, 20);

#if 0
    glUseProgram(shader_prog);
    glUniform1i(uni_index, 0);
    glUniform1i(uni_col, 0);
    glUniform1i(uni_row, 0);
    glUniform1f(uni_expect, 1.0);
    draw_rect(5, 5, 10, 10);
#endif

#if PFINISH
    glFinish();
    fprintf(stderr, "EE FRAME _____________________________________________________\n");
#endif
    glutSwapBuffers();

    frames++;
    if (frames >= MAXFRAME) {
        glFinish();
        exit(0);
    }
}

static void idle(void)
{
    glutPostRedisplay();
}

/* new window size or exposure */
static void reshape(int width, int height)
{
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, width, 0, height, -1, 1);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}


static void init(void)
{
    const GLchar *p[1];
    GLint len[1];
    GLint status;

    shader_prog = glCreateProgram();

    vs_shader = glCreateShader(GL_VERTEX_SHADER);
    p[0] = vs_shader_src;
    len[0] = strlen(vs_shader_src);
    glShaderSource(vs_shader, 1, p, len);
    glCompileShader(vs_shader);
    glGetShaderiv(vs_shader, GL_COMPILE_STATUS, &status);
    if (!status) {
        GLchar log[1024];

        glGetShaderInfoLog(vs_shader, 1024, NULL, log);
        fprintf(stderr, "Error compiling vs shader %s: \n", log);
        exit(1);
    }
    glAttachShader(shader_prog, vs_shader);

    ps_shader = glCreateShader(GL_FRAGMENT_SHADER);
    p[0] = ps_shader_src;
    len[0] = strlen(ps_shader_src);
    glShaderSource(ps_shader, 1, p, len);
    glCompileShader(ps_shader);
    glGetShaderiv(ps_shader, GL_COMPILE_STATUS, &status);
    if (!status) {
        GLchar log[1024];

        glGetShaderInfoLog(ps_shader, 1024, NULL, log);
        fprintf(stderr, "Error compiling ps shader %s: \n", log);
        exit(1);
    }
    glAttachShader(shader_prog, ps_shader);

    glLinkProgram(shader_prog);
    glGetProgramiv(shader_prog, GL_LINK_STATUS, &status);
    if (!status) {
        GLchar log[1024];

        glGetProgramInfoLog(shader_prog, 1024, NULL, log);
        fprintf(stderr, "Error linking shader program failed %s: \n", log);
        exit(1);
    }
    glValidateProgram(shader_prog);
    glGetProgramiv(shader_prog, GL_VALIDATE_STATUS, &status);
    if (!status) {
        GLchar log[1024];

        glGetProgramInfoLog(shader_prog, 1024, NULL, log);
        fprintf(stderr, "Invalid shader program %s: \n", log);
        exit(1);
    }

    uni_index = glGetUniformLocation(shader_prog, "index");
    uni_col = glGetUniformLocation(shader_prog, "col");
    uni_row = glGetUniformLocation(shader_prog, "row");
    uni_expect = glGetUniformLocation(shader_prog, "expect");
}

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
    glutInitWindowSize(win_width, win_height);
    glutCreateWindow("glgears");

    GLenum err = glewInit();
    if (GLEW_OK != err) {
        fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
        return -1;
    }

    glutIdleFunc(idle);
    glutReshapeFunc(reshape);
    glutDisplayFunc(draw);

    init();
    reshape(win_width, win_height);
    glutMainLoop();

    return 0;
}