Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/test/browser/glbegin_points.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
#include "SDL/SDL.h"
26
#include "SDL/SDL_image.h"
27
#include "SDL/SDL_opengl.h"
28
29
#include <stdio.h>
30
#include <string.h>
31
32
int main(int argc, char *argv[])
33
{
34
SDL_Surface *screen;
35
36
// Slightly different SDL initialization
37
if ( SDL_Init(SDL_INIT_VIDEO) != 0 ) {
38
printf("Unable to initialize SDL: %s\n", SDL_GetError());
39
return 1;
40
}
41
42
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); // *new*
43
44
screen = SDL_SetVideoMode( 640, 480, 16, SDL_OPENGL ); // *changed*
45
if ( !screen ) {
46
printf("Unable to set video mode: %s\n", SDL_GetError());
47
return 1;
48
}
49
50
// Set the OpenGL state after creating the context with SDL_SetVideoMode
51
52
glClearColor( 0, 0, 0, 0 );
53
54
glEnable( GL_TEXTURE_2D ); // Needed when we're using the fixed-function pipeline.
55
56
glViewport( 0, 0, 640, 480 );
57
58
glMatrixMode( GL_PROJECTION );
59
GLfloat matrixData[] = { 2.0/640, 0, 0, 0,
60
0, -2.0/480, 0, 0,
61
0, 0, -1, 0,
62
-1, 1, 0, 1 };
63
glLoadMatrixf(matrixData); // test loadmatrix
64
65
glMatrixMode( GL_MODELVIEW );
66
glLoadIdentity();
67
68
// Load the OpenGL texture
69
70
GLuint texture; // Texture object handle
71
SDL_Surface *surface; // Gives us the information to make the texture
72
73
if ( (surface = IMG_Load("screenshot.png")) ) {
74
75
// Check that the image's width is a power of 2
76
if ( (surface->w & (surface->w - 1)) != 0 ) {
77
printf("warning: image.bmp's width is not a power of 2\n");
78
}
79
80
// Also check if the height is a power of 2
81
if ( (surface->h & (surface->h - 1)) != 0 ) {
82
printf("warning: image.bmp's height is not a power of 2\n");
83
}
84
85
// Have OpenGL generate a texture object handle for us
86
glGenTextures( 1, &texture );
87
88
// Bind the texture object
89
glBindTexture( GL_TEXTURE_2D, texture );
90
91
// Set the texture's stretching properties
92
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
93
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
94
95
//SDL_LockSurface(surface);
96
97
// Add some greyness
98
memset(surface->pixels, 0x66, surface->w*surface->h);
99
100
// Edit the texture object's image data using the information SDL_Surface gives us
101
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, surface->w, surface->h, 0,
102
GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels );
103
104
//SDL_UnlockSurface(surface);
105
}
106
else {
107
printf("SDL could not load image.bmp: %s\n", SDL_GetError());
108
SDL_Quit();
109
return 1;
110
}
111
112
// Free the SDL_Surface only if it was successfully created
113
if ( surface ) {
114
SDL_FreeSurface( surface );
115
}
116
117
// Clear the screen before drawing
118
glClear( GL_COLOR_BUFFER_BIT );
119
120
// Bind the texture to which subsequent calls refer to
121
glBindTexture( GL_TEXTURE_2D, texture );
122
123
// Use clientside vertex pointers to render two items
124
GLfloat vertexData[] = { 0, 0, 10, 10, // texture2, position2
125
1, 0, 300, 10,
126
1, 1, 300, 128,
127
0, 1, 10, 128,
128
0, 0.5, 410, 10,
129
1, 0.5, 600, 10,
130
1, 1, 630, 200,
131
0.5, 1, 310, 250 };
132
133
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
134
glTexCoordPointer(2, GL_FLOAT, 4*4, &vertexData[0]);
135
glEnableClientState(GL_VERTEX_ARRAY);
136
glVertexPointer(2, GL_FLOAT, 4*4, &vertexData[2]);
137
138
glDrawArrays(GL_POINTS, 0, 8);
139
140
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
141
glDisableClientState(GL_VERTEX_ARRAY);
142
143
// Render the last item using oldschool glBegin etc
144
glBegin( GL_POINTS );
145
glTexCoord2i( 0, 0 ); glVertex3f( 100, 300, 0 );
146
glTexCoord2i( 1, 0 ); glVertex2f( 300, 300 );
147
glTexCoord2i( 1, 1 ); { float vals[3] = { 300, 400, 0 }; glVertex3fv(vals); }
148
glTexCoord2i( 0, 1 ); { float vals[2] = { 500, 410 }; glVertex2fv(vals); }
149
glEnd();
150
151
SDL_GL_SwapBuffers();
152
153
#if !defined(__EMSCRIPTEN__)
154
// Wait for 3 seconds to give us a chance to see the image
155
SDL_Delay(3000);
156
#endif
157
158
// Now we can delete the OpenGL texture and close down SDL
159
glDeleteTextures( 1, &texture );
160
161
SDL_Quit();
162
163
return 0;
164
}
165
166