aboutsummaryrefslogtreecommitdiff
path: root/tests/gl_matrix_identity.c
blob: 98b1c21f7aab9aa070a66fd347d8f5f7773428d3 (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
/*
THIS WORK, INCLUDING THE SOURCE CODE, DOCUMENTATION
AND RELATED MEDIA AND DATA, IS PLACED INTO THE PUBLIC DOMAIN.

THE ORIGINAL AUTHOR IS KYLE FOLEY.

THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY
OF ANY KIND, NOT EVEN THE IMPLIED WARRANTY OF
MERCHANTABILITY. THE AUTHOR OF THIS SOFTWARE,
ASSUMES _NO_ RESPONSIBILITY FOR ANY CONSEQUENCE
RESULTING FROM THE USE, MODIFICATION, OR
REDISTRIBUTION OF THIS SOFTWARE.
*/

#if !EMSCRIPTEN
#define USE_GLEW 1
#endif

#if USE_GLEW
#include "GL/glew.h"
#endif

#include "SDL/SDL.h"
#if !USE_GLEW
#include "SDL/SDL_opengl.h"
#endif

#include <stdio.h>
#include <string.h>
#include <assert.h>

void verify() {
  int width = 640, height = 480;
  unsigned char *data = (unsigned char*)malloc(width*height*4);
  glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data);
  int sum = 0;
  for (int x = 0; x < width*height*4; x++) {
    if (x % 4 != 3) sum += x * data[x];
  }
#if EMSCRIPTEN
  int result = sum;
  REPORT_RESULT();
#endif
}

int main(int argc, char *argv[])
{
    SDL_Surface *screen;
    if ( SDL_Init(SDL_INIT_VIDEO) != 0 ) {
        printf("Unable to initialize SDL: %s\n", SDL_GetError());
        return 1;
    }

    SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
    screen = SDL_SetVideoMode( 640, 480, 24, SDL_OPENGL );
    if ( !screen ) {
        printf("Unable to set video mode: %s\n", SDL_GetError());
        return 1;
    }

    // Create a texture

    GLuint texture;
    glGenTextures( 1, &texture );
    glBindTexture( GL_TEXTURE_2D, texture );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
    GLubyte textureData[]  = { 0x7f,    0,    0,
                                  0, 0xff,    0,
                               0x7f,    0,    0,
                                  0, 0xff,    0};
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
                  GL_RGB, GL_UNSIGNED_BYTE, textureData );


    // BEGIN

#if USE_GLEW
    glewInit();
#endif

    glClearColor( 0, 0, 0.5, 1.0 );
    glClear( GL_COLOR_BUFFER_BIT );

    glColor4f(0.8, 0.8, 0.8, 1);

    glDisable(GL_DEPTH_TEST);
    glDisable(GL_CULL_FACE);
    glEnable(GL_TEXTURE_2D);
    glDisable(GL_BLEND);

    const int kRowSize = 20;
    GLuint buffer;
    glGenBuffers(1, &buffer);
    glBindBuffer(GL_ARRAY_BUFFER, buffer);
    float fbuf[] = {0, 1, 0, 0, 1,
                    1, 1, 0, 1, 1,
                    1, 0, 0, 1, 0,
                    0, 1, 0, 0, 1,
                    1, 0, 0, 1, 0,
                    0, 0, 0, 0, 0};
    glBufferData(GL_ARRAY_BUFFER, sizeof(fbuf) * sizeof(float), fbuf, GL_STATIC_DRAW);

    glTexCoordPointer(2, GL_FLOAT, kRowSize, (GLvoid*)(3*4));
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);

    glVertexPointer(3, GL_FLOAT, kRowSize, 0);
    glEnableClientState(GL_VERTEX_ARRAY);

    glDrawArrays(GL_TRIANGLES, 0, 6);

    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    glDisableClientState(GL_VERTEX_ARRAY);

    // END

    SDL_GL_SwapBuffers();

    verify();

#if !EMSCRIPTEN
    SDL_Delay(1500);
#endif

    SDL_Quit();

    return 0;
}