Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/test/browser/gl_matrix_identity.c
7085 views
1
/*
2
* Copyright 2012 The Emscripten Authors. All rights reserved.
3
* Emscripten is available under two separate licenses, the MIT license and the
4
* University of Illinois/NCSA Open Source License. Both these licenses can be
5
* found in the LICENSE file.
6
*/
7
8
/*
9
THIS WORK, INCLUDING THE SOURCE CODE, DOCUMENTATION
10
AND RELATED MEDIA AND DATA, IS PLACED INTO THE PUBLIC DOMAIN.
11
12
THE ORIGINAL AUTHOR IS KYLE FOLEY.
13
14
THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY
15
OF ANY KIND, NOT EVEN THE IMPLIED WARRANTY OF
16
MERCHANTABILITY. THE AUTHOR OF THIS SOFTWARE,
17
ASSUMES _NO_ RESPONSIBILITY FOR ANY CONSEQUENCE
18
RESULTING FROM THE USE, MODIFICATION, OR
19
REDISTRIBUTION OF THIS SOFTWARE.
20
*/
21
22
#ifndef __EMSCRIPTEN__
23
#define USE_GLEW 1
24
#endif
25
26
#if USE_GLEW
27
#include "GL/glew.h"
28
#endif
29
30
#include "SDL/SDL.h"
31
#if !USE_GLEW
32
#include "SDL/SDL_opengl.h"
33
#endif
34
35
#include <stdio.h>
36
#include <stdlib.h>
37
#include <string.h>
38
#include <assert.h>
39
40
void verify() {
41
int width = 640, height = 480;
42
unsigned char *data = (unsigned char*)malloc(width*height*4);
43
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data);
44
int sum = 0;
45
for (int x = 0; x < width*height*4; x++) {
46
if (x % 4 != 3) sum += x * data[x];
47
}
48
#ifdef __EMSCRIPTEN__
49
REPORT_RESULT(sum);
50
#endif
51
}
52
53
int main(int argc, char *argv[]) {
54
SDL_Surface *screen;
55
if ( SDL_Init(SDL_INIT_VIDEO) != 0 ) {
56
printf("Unable to initialize SDL: %s\n", SDL_GetError());
57
return 1;
58
}
59
60
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
61
screen = SDL_SetVideoMode( 640, 480, 24, SDL_OPENGL );
62
if ( !screen ) {
63
printf("Unable to set video mode: %s\n", SDL_GetError());
64
return 1;
65
}
66
67
// Create a texture
68
69
GLuint texture;
70
glGenTextures( 1, &texture );
71
glBindTexture( GL_TEXTURE_2D, texture );
72
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
73
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
74
GLubyte textureData[] = { 0x7f, 0, 0,
75
0, 0xff, 0,
76
0x7f, 0, 0,
77
0, 0xff, 0};
78
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
79
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,
80
GL_RGB, GL_UNSIGNED_BYTE, textureData );
81
82
83
// BEGIN
84
85
#if USE_GLEW
86
glewInit();
87
#endif
88
89
glClearColor( 0, 0, 0.5, 1.0 );
90
glClear( GL_COLOR_BUFFER_BIT );
91
92
glColor4f(0.8, 0.8, 0.8, 1);
93
94
glDisable(GL_DEPTH_TEST);
95
glDisable(GL_CULL_FACE);
96
glEnable(GL_TEXTURE_2D);
97
glDisable(GL_BLEND);
98
99
const int kRowSize = 20;
100
GLuint buffer;
101
glGenBuffers(1, &buffer);
102
glBindBuffer(GL_ARRAY_BUFFER, buffer);
103
float fbuf[] = {0, 1, 0, 0, 1,
104
1, 1, 0, 1, 1,
105
1, 0, 0, 1, 0,
106
0, 1, 0, 0, 1,
107
1, 0, 0, 1, 0,
108
0, 0, 0, 0, 0};
109
glBufferData(GL_ARRAY_BUFFER, sizeof(fbuf) * sizeof(float), fbuf, GL_STATIC_DRAW);
110
111
glTexCoordPointer(2, GL_FLOAT, kRowSize, (GLvoid*)(3*4));
112
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
113
114
glVertexPointer(3, GL_FLOAT, kRowSize, 0);
115
glEnableClientState(GL_VERTEX_ARRAY);
116
117
glDrawArrays(GL_TRIANGLES, 0, 6);
118
119
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
120
glDisableClientState(GL_VERTEX_ARRAY);
121
122
// END
123
124
SDL_GL_SwapBuffers();
125
126
verify();
127
128
#ifndef __EMSCRIPTEN__
129
SDL_Delay(1500);
130
#endif
131
132
SDL_Quit();
133
134
return 0;
135
}
136
137