1
/*******************************************************************
3
* Using SDL With OpenGL *
5
* Tutorial by Kyle Foley (sdw) *
7
* http://gpwiki.org/index.php/SDL:Tutorials:Using_SDL_with_OpenGL *
9
*******************************************************************/
12
THIS WORK, INCLUDING THE SOURCE CODE, DOCUMENTATION
13
AND RELATED MEDIA AND DATA, IS PLACED INTO THE PUBLIC DOMAIN.
15
THE ORIGINAL AUTHOR IS KYLE FOLEY.
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.
36
#include "SDL/SDL_opengl.h"
43
int main(int argc, char *argv[])
47
// Slightly different SDL initialization
48
if ( SDL_Init(SDL_INIT_VIDEO) != 0 ) {
49
printf("Unable to initialize SDL: %s\n", SDL_GetError());
53
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); // *new*
55
screen = SDL_SetVideoMode( 640, 480, 16, SDL_OPENGL ); // *changed*
57
printf("Unable to set video mode: %s\n", SDL_GetError());
61
// Set the OpenGL state after creating the context with SDL_SetVideoMode
63
glClearColor( 0, 0, 0, 0 );
66
glEnable( GL_TEXTURE_2D ); // Need this to display a texture XXX unnecessary in OpenGL ES 2.0/WebGL
69
glViewport( 0, 0, 640, 480 );
71
glMatrixMode( GL_MODELVIEW );
74
// Clear the screen before drawing
75
glClear( GL_COLOR_BUFFER_BIT );
77
typedef struct Color {
84
typedef struct Vertex {
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}}
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}}
132
// make a vertex buffer for the second set of vertices
134
glGenBuffers(1, &vbo);
138
glBindBuffer(GL_ARRAY_BUFFER, vbo);
140
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices2), vertices2, GL_STATIC_READ); // GL_STATIC_READ is not in WebGL!
143
glBindBuffer(GL_ARRAY_BUFFER, 0);
148
// Clear the screen before drawing
149
glClear( GL_COLOR_BUFFER_BIT );
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
// The first test shows implicit striding (the zero indicates tightly packed)
154
// The second test shows explicit striding where the stride is passed in
155
// even though it also is tightly packed
157
glEnableClientState(GL_VERTEX_ARRAY);
158
glEnableClientState(GL_COLOR_ARRAY);
160
// TEST 1 - clientside data
162
glVertexPointer(2, GL_FLOAT, sizeof(Vertex), vertices);
163
glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Vertex), &vertices[0].color);
164
glDrawArrays(GL_TRIANGLE_STRIP, 0, 6);
165
glDrawArrays(GL_TRIANGLE_STRIP, 10, 3);
169
// TEST 2 - bind to array buffer, gl*Pointer calls are offsets into the buffer, which was previously uploaded to
171
glBindBuffer(GL_ARRAY_BUFFER, vbo);
173
glVertexPointer(2, GL_FLOAT, sizeof(Vertex), 0);
174
glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Vertex), (GLvoid*)((GLvoid*)&vertices2[0].color - (GLvoid*)&vertices2[0]));
176
// gldrawarrays first with a low number of vertices, then with a high number
177
glDrawArrays(GL_TRIANGLE_STRIP, 0, 6);
178
glDrawArrays(GL_TRIANGLE_STRIP, 10, 3);
180
glBindBuffer(GL_ARRAY_BUFFER, 0);
182
glDisableClientState(GL_COLOR_ARRAY);
183
glDisableClientState(GL_VERTEX_ARRAY);
185
SDL_GL_SwapBuffers();
188
// Wait for 3 seconds to give us a chance to see the image