Path: blob/main/test/browser/glbegin_points.c
7085 views
/*******************************************************************1* *2* Using SDL With OpenGL *3* *4* Tutorial by Kyle Foley (sdw) *5* *6* http://gpwiki.org/index.php/SDL:Tutorials:Using_SDL_with_OpenGL *7* *8*******************************************************************/910/*11THIS WORK, INCLUDING THE SOURCE CODE, DOCUMENTATION12AND RELATED MEDIA AND DATA, IS PLACED INTO THE PUBLIC DOMAIN.1314THE ORIGINAL AUTHOR IS KYLE FOLEY.1516THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY17OF ANY KIND, NOT EVEN THE IMPLIED WARRANTY OF18MERCHANTABILITY. THE AUTHOR OF THIS SOFTWARE,19ASSUMES _NO_ RESPONSIBILITY FOR ANY CONSEQUENCE20RESULTING FROM THE USE, MODIFICATION, OR21REDISTRIBUTION OF THIS SOFTWARE.22*/2324#include "SDL/SDL.h"25#include "SDL/SDL_image.h"26#include "SDL/SDL_opengl.h"2728#include <stdio.h>29#include <string.h>3031int main(int argc, char *argv[])32{33SDL_Surface *screen;3435// Slightly different SDL initialization36if ( SDL_Init(SDL_INIT_VIDEO) != 0 ) {37printf("Unable to initialize SDL: %s\n", SDL_GetError());38return 1;39}4041SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); // *new*4243screen = SDL_SetVideoMode( 640, 480, 16, SDL_OPENGL ); // *changed*44if ( !screen ) {45printf("Unable to set video mode: %s\n", SDL_GetError());46return 1;47}4849// Set the OpenGL state after creating the context with SDL_SetVideoMode5051glClearColor( 0, 0, 0, 0 );5253glEnable( GL_TEXTURE_2D ); // Needed when we're using the fixed-function pipeline.5455glViewport( 0, 0, 640, 480 );5657glMatrixMode( GL_PROJECTION );58GLfloat matrixData[] = { 2.0/640, 0, 0, 0,590, -2.0/480, 0, 0,600, 0, -1, 0,61-1, 1, 0, 1 };62glLoadMatrixf(matrixData); // test loadmatrix6364glMatrixMode( GL_MODELVIEW );65glLoadIdentity();6667// Load the OpenGL texture6869GLuint texture; // Texture object handle70SDL_Surface *surface; // Gives us the information to make the texture7172if ( (surface = IMG_Load("screenshot.png")) ) {7374// Check that the image's width is a power of 275if ( (surface->w & (surface->w - 1)) != 0 ) {76printf("warning: image.bmp's width is not a power of 2\n");77}7879// Also check if the height is a power of 280if ( (surface->h & (surface->h - 1)) != 0 ) {81printf("warning: image.bmp's height is not a power of 2\n");82}8384// Have OpenGL generate a texture object handle for us85glGenTextures( 1, &texture );8687// Bind the texture object88glBindTexture( GL_TEXTURE_2D, texture );8990// Set the texture's stretching properties91glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );92glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );9394//SDL_LockSurface(surface);9596// Add some greyness97memset(surface->pixels, 0x66, surface->w*surface->h);9899// Edit the texture object's image data using the information SDL_Surface gives us100glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, surface->w, surface->h, 0,101GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels );102103//SDL_UnlockSurface(surface);104}105else {106printf("SDL could not load image.bmp: %s\n", SDL_GetError());107SDL_Quit();108return 1;109}110111// Free the SDL_Surface only if it was successfully created112if ( surface ) {113SDL_FreeSurface( surface );114}115116// Clear the screen before drawing117glClear( GL_COLOR_BUFFER_BIT );118119// Bind the texture to which subsequent calls refer to120glBindTexture( GL_TEXTURE_2D, texture );121122// Use clientside vertex pointers to render two items123GLfloat vertexData[] = { 0, 0, 10, 10, // texture2, position21241, 0, 300, 10,1251, 1, 300, 128,1260, 1, 10, 128,1270, 0.5, 410, 10,1281, 0.5, 600, 10,1291, 1, 630, 200,1300.5, 1, 310, 250 };131132glEnableClientState(GL_TEXTURE_COORD_ARRAY);133glTexCoordPointer(2, GL_FLOAT, 4*4, &vertexData[0]);134glEnableClientState(GL_VERTEX_ARRAY);135glVertexPointer(2, GL_FLOAT, 4*4, &vertexData[2]);136137glDrawArrays(GL_POINTS, 0, 8);138139glDisableClientState(GL_TEXTURE_COORD_ARRAY);140glDisableClientState(GL_VERTEX_ARRAY);141142// Render the last item using oldschool glBegin etc143glBegin( GL_POINTS );144glTexCoord2i( 0, 0 ); glVertex3f( 100, 300, 0 );145glTexCoord2i( 1, 0 ); glVertex2f( 300, 300 );146glTexCoord2i( 1, 1 ); { float vals[3] = { 300, 400, 0 }; glVertex3fv(vals); }147glTexCoord2i( 0, 1 ); { float vals[2] = { 500, 410 }; glVertex2fv(vals); }148glEnd();149150SDL_GL_SwapBuffers();151152#if !defined(__EMSCRIPTEN__)153// Wait for 3 seconds to give us a chance to see the image154SDL_Delay(3000);155#endif156157// Now we can delete the OpenGL texture and close down SDL158glDeleteTextures( 1, &texture );159160SDL_Quit();161162return 0;163}164165166