Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/test/browser/gl_vertex_buffer_pre.c
7085 views
1
/*******************************************************************
2
* *
3
* Using SDL With OpenGL *
4
* *
5
* Tutorial by Kyle Foley (sdw) *
6
* *
7
* http://gpwiki.org/index.php/SDL:Tutorials:Using_SDL_with_OpenGL *
8
* *
9
*******************************************************************/
10
11
/*
12
THIS WORK, INCLUDING THE SOURCE CODE, DOCUMENTATION
13
AND RELATED MEDIA AND DATA, IS PLACED INTO THE PUBLIC DOMAIN.
14
15
THE ORIGINAL AUTHOR IS KYLE FOLEY.
16
17
THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY
18
OF ANY KIND, NOT EVEN THE IMPLIED WARRANTY OF
19
MERCHANTABILITY. THE AUTHOR OF THIS SOFTWARE,
20
ASSUMES _NO_ RESPONSIBILITY FOR ANY CONSEQUENCE
21
RESULTING FROM THE USE, MODIFICATION, OR
22
REDISTRIBUTION OF THIS SOFTWARE.
23
*/
24
25
#ifndef __EMSCRIPTEN__
26
#define USE_GLEW 0
27
#endif
28
29
#if USE_GLEW
30
#include "GL/glew.h"
31
#endif
32
33
#include <SDL/SDL.h>
34
35
#if !USE_GLEW
36
#include "SDL/SDL_opengl.h"
37
#endif
38
39
#include <stdio.h>
40
#include <string.h>
41
#include <assert.h>
42
43
int main(int argc, char *argv[])
44
{
45
SDL_Surface *screen;
46
47
// Slightly different SDL initialization
48
if ( SDL_Init(SDL_INIT_VIDEO) != 0 ) {
49
printf("Unable to initialize SDL: %s\n", SDL_GetError());
50
return 1;
51
}
52
53
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); // *new*
54
55
screen = SDL_SetVideoMode( 640, 480, 16, SDL_OPENGL ); // *changed*
56
if ( !screen ) {
57
printf("Unable to set video mode: %s\n", SDL_GetError());
58
return 1;
59
}
60
61
// Set the OpenGL state after creating the context with SDL_SetVideoMode
62
63
glClearColor( 0, 0, 0, 0 );
64
65
#ifndef __EMSCRIPTEN__
66
glEnable( GL_TEXTURE_2D ); // Need this to display a texture XXX unnecessary in OpenGL ES 2.0/WebGL
67
#endif
68
69
glViewport( 0, 0, 640, 480 );
70
71
glMatrixMode( GL_MODELVIEW );
72
glLoadIdentity();
73
74
// Clear the screen before drawing
75
glClear( GL_COLOR_BUFFER_BIT );
76
77
typedef struct Color {
78
GLubyte r;
79
GLubyte g;
80
GLubyte b;
81
GLubyte a;
82
} Color;
83
84
typedef struct Vertex {
85
GLfloat x;
86
GLfloat y;
87
Color color;
88
} Vertex;
89
90
Vertex vertices[18] = {
91
{-1.00, 0.0, {0xFF, 0x00, 0xFF, 0xFF}},
92
{-1.00, 1.0, {0xFF, 0xFF, 0x00, 0xFF}},
93
{-0.75, 0.0, {0xFF, 0x00, 0x00, 0xFF}},
94
{-0.75, 1.0, {0xFF, 0xFF, 0xFF, 0xFF}},
95
{-0.50, 0.0, {0xFF, 0x00, 0x00, 0xFF}},
96
{-0.50, 1.0, {0xFF, 0xFF, 0x00, 0xFF}},
97
{-0.25, 0.0, {0xFF, 0x00, 0xFF, 0xFF}},
98
{-0.25, 1.0, {0xFF, 0xFF, 0x00, 0xFF}},
99
{-0.00, 0.0, {0xFF, 0x00, 0x00, 0xFF}},
100
{-0.00, 1.0, {0xFF, 0xFF, 0xFF, 0xFF}},
101
{ 0.25, 0.0, {0xFF, 0x00, 0x00, 0xFF}},
102
{ 0.25, 1.0, {0xFF, 0xFF, 0x00, 0xFF}},
103
{ 0.50, 0.0, {0xFF, 0x00, 0xFF, 0xFF}},
104
{ 0.50, 1.0, {0xFF, 0xFF, 0x00, 0xFF}},
105
{ 0.75, 0.0, {0xFF, 0x00, 0x00, 0xFF}},
106
{ 0.75, 1.0, {0xFF, 0xFF, 0xFF, 0xFF}},
107
{ 1.00, 0.0, {0xFF, 0x00, 0x00, 0xFF}},
108
{ 1.00, 1.0, {0xFF, 0xFF, 0x00, 0xFF}}
109
};
110
111
Vertex vertices2[18] = {
112
{-1.00, -1.0, {0xFF, 0x00, 0xFF, 0xFF}},
113
{-1.00, 0.0, {0xFF, 0xFF, 0x00, 0xFF}},
114
{-0.75, -1.0, {0xFF, 0x00, 0x00, 0xFF}},
115
{-0.75, 0.0, {0xFF, 0xFF, 0xFF, 0xFF}},
116
{-0.50, -1.0, {0xFF, 0x00, 0x00, 0xFF}},
117
{-0.50, 0.0, {0xFF, 0xFF, 0x00, 0xFF}},
118
{-0.25, -1.0, {0xFF, 0x00, 0xFF, 0xFF}},
119
{-0.25, 0.0, {0xFF, 0xFF, 0x00, 0xFF}},
120
{-0.00, -1.0, {0xFF, 0x00, 0x00, 0xFF}},
121
{-0.00, 0.0, {0xFF, 0xFF, 0xFF, 0xFF}},
122
{ 0.25, -1.0, {0xFF, 0x00, 0x00, 0xFF}},
123
{ 0.25, 0.0, {0xFF, 0xFF, 0x00, 0xFF}},
124
{ 0.50, -1.0, {0xFF, 0x00, 0xFF, 0xFF}},
125
{ 0.50, 0.0, {0xFF, 0xFF, 0x00, 0xFF}},
126
{ 0.75, -1.0, {0xFF, 0x00, 0x00, 0xFF}},
127
{ 0.75, 0.0, {0xFF, 0xFF, 0xFF, 0xFF}},
128
{ 1.00, -1.0, {0xFF, 0x00, 0x00, 0xFF}},
129
{ 1.00, 0.0, {0xFF, 0xFF, 0x00, 0xFF}}
130
};
131
132
// make a vertex buffer for the second set of vertices
133
GLuint vbo = 0;
134
glGenBuffers(1, &vbo);
135
136
137
// bind to it
138
glBindBuffer(GL_ARRAY_BUFFER, vbo);
139
// send it to gl
140
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices2), vertices2, GL_STATIC_DRAW);
141
142
// unbind from it
143
glBindBuffer(GL_ARRAY_BUFFER, 0);
144
145
146
// DRAW
147
148
// Clear the screen before drawing
149
glClear( GL_COLOR_BUFFER_BIT );
150
151
// This test ensures that we can use two separate arrays in memory for different
152
// attributes, and that they each can have different stride.
153
154
glEnableClientState(GL_VERTEX_ARRAY);
155
glEnableClientState(GL_COLOR_ARRAY);
156
157
glVertexPointer(2, GL_FLOAT, sizeof(Vertex), vertices);
158
glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Vertex), &vertices[0].color);
159
glDrawArrays(GL_TRIANGLE_STRIP, 10, 3);
160
glDrawArrays(GL_TRIANGLE_STRIP, 0, 6);
161
162
glBindBuffer(GL_ARRAY_BUFFER, 0);
163
164
glDisableClientState(GL_COLOR_ARRAY);
165
glDisableClientState(GL_VERTEX_ARRAY);
166
167
SDL_GL_SwapBuffers();
168
169
#ifndef __EMSCRIPTEN__
170
// Wait for 3 seconds to give us a chance to see the image
171
SDL_Delay(3000);
172
#endif
173
174
SDL_Quit();
175
176
return 0;
177
}
178
179