Path: blob/main/test/browser/gl_ps_strides.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#ifndef __EMSCRIPTEN__25#define USE_GLEW 126#endif2728#if USE_GLEW29#include "GL/glew.h"30#endif3132#include "SDL/SDL.h"33#include "SDL/SDL_image.h"34#if !USE_GLEW35#include "SDL/SDL_opengl.h"36#endif3738#include <stdio.h>39#include <string.h>40#include <assert.h>4142void shaders() {43#if USE_GLEW44glewInit();45#endif4647GLint ok;4849const char *vertexShader = "void main(void) \n"50"{ \n"51" gl_Position = ftransform(); \n"52" gl_TexCoord[0] = gl_MultiTexCoord0; \n"53" gl_FrontColor = gl_Color; \n"54"} \n";55const char *fragmentShader = "uniform sampler2D tex0; \n"56"void main(void) \n"57"{ \n"58" gl_FragColor = gl_Color * texture2D(tex0, gl_TexCoord[0].xy); \n"59"} \n";6061GLuint vs = glCreateShader(GL_VERTEX_SHADER);62glShaderSource(vs, 1, &vertexShader, NULL);63glCompileShader(vs);64glGetShaderiv(vs, GL_COMPILE_STATUS, &ok);65assert(ok);6667GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);68glShaderSource(fs, 1, &fragmentShader, NULL);69glCompileShader(fs);70glGetShaderiv(fs, GL_COMPILE_STATUS, &ok);71assert(ok);7273GLuint program = glCreateProgram();7475glAttachShader(program, vs);76glAttachShader(program, fs);77glLinkProgram(program);78glGetProgramiv(program, GL_LINK_STATUS, &ok);79assert(ok);80assert(glIsProgram(program));81assert(!glIsProgram(0));82assert(!glIsProgram(program+1)); // a number that can't be a real shader8384glUseProgram(program);8586{87// Also, check getting the error log88const char *fakeVertexShader = "atbute ve4 blarg; ### AAA\n";89GLuint vs = glCreateShader(GL_VERTEX_SHADER);90glShaderSource(vs, 1, &fakeVertexShader, NULL);91glCompileShader(vs);92glGetShaderiv(vs, GL_COMPILE_STATUS, &ok);93assert(!ok);94GLint infoLen = 0;95glGetShaderiv(vs, GL_INFO_LOG_LENGTH, &infoLen);96assert(infoLen > 1);97}98}99100int main(int argc, char *argv[])101{102SDL_Surface *screen;103104// Slightly different SDL initialization105if ( SDL_Init(SDL_INIT_VIDEO) != 0 ) {106printf("Unable to initialize SDL: %s\n", SDL_GetError());107return 1;108}109110SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); // *new*111112screen = SDL_SetVideoMode( 640, 480, 16, SDL_OPENGL ); // *changed*113if ( !screen ) {114printf("Unable to set video mode: %s\n", SDL_GetError());115return 1;116}117118// Set the OpenGL state after creating the context with SDL_SetVideoMode119120glClearColor( 0, 0, 0, 0 );121122#ifndef __EMSCRIPTEN__123glEnable( GL_TEXTURE_2D ); // Need this to display a texture XXX unnecessary in OpenGL ES 2.0/WebGL124#endif125126glViewport( 0, 0, 640, 480 );127128glMatrixMode( GL_PROJECTION );129GLfloat matrixData[] = { 2.0/640, 0, 0, 0,1300, -2.0/480, 0, 0,1310, 0, -1, 0,132-1, 1, 0, 1 };133glLoadMatrixf(matrixData); // test loadmatrix134135glMatrixMode( GL_MODELVIEW );136glLoadIdentity();137138// Load the OpenGL texture139140GLuint texture; // Texture object handle141SDL_Surface *surface; // Gives us the information to make the texture142143if ( (surface = IMG_Load("screenshot.png")) ) {144145// Check that the image's width is a power of 2146if ( (surface->w & (surface->w - 1)) != 0 ) {147printf("warning: image.bmp's width is not a power of 2\n");148}149150// Also check if the height is a power of 2151if ( (surface->h & (surface->h - 1)) != 0 ) {152printf("warning: image.bmp's height is not a power of 2\n");153}154155// Have OpenGL generate a texture object handle for us156glGenTextures( 1, &texture );157158// Bind the texture object159glBindTexture( GL_TEXTURE_2D, texture );160161// Set the texture's stretching properties162glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );163glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );164165//SDL_LockSurface(surface);166167// Add some greyness168memset(surface->pixels, 0x66, surface->w*surface->h);169170// Edit the texture object's image data using the information SDL_Surface gives us171glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, surface->w, surface->h, 0,172GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels );173174//SDL_UnlockSurface(surface);175}176else {177printf("SDL could not load image.bmp: %s\n", SDL_GetError());178SDL_Quit();179return 1;180}181182// Free the SDL_Surface only if it was successfully created183if ( surface ) {184SDL_FreeSurface( surface );185}186187// Clear the screen before drawing188glClear( GL_COLOR_BUFFER_BIT );189190shaders();191192// Bind the texture to which subsequent calls refer to193glBindTexture( GL_TEXTURE_2D, texture );194195// Use clientside vertex pointers to render two items196GLfloat vertexData[] = { 0, 0, 10, 10, // texture2, position21971, 0, 300, 10,1981, 1, 300, 128,1990, 1, 10, 128,2000, 0.5, 410, 10,2011, 0.5, 600, 10,2021, 1, 630, 200,2030.5, 1, 310, 250,2040, 0, 100, 300,2051, 0, 300, 300,2061, 1, 300, 400,2070, 1, 100, 400 };208209glEnableClientState(GL_TEXTURE_COORD_ARRAY);210glTexCoordPointer(2, GL_FLOAT, 4*4, &vertexData[0]);211glEnableClientState(GL_VERTEX_ARRAY);212glVertexPointer(2, GL_FLOAT, 4*4, &vertexData[2]);213214glDrawArrays(GL_QUADS, 0, 4);215216glTexCoordPointer(2, GL_FLOAT, 4*4*2, &vertexData[0]); // and now with a different stride217glVertexPointer(2, GL_FLOAT, 4*4*2, &vertexData[2]);218glDrawArrays(GL_QUADS, 4, 4);219220glTexCoordPointer(2, GL_FLOAT, 4*4, &vertexData[0]); // and back221glVertexPointer(2, GL_FLOAT, 4*4, &vertexData[2]);222glDrawArrays(GL_QUADS, 8, 4);223224glDisableClientState(GL_TEXTURE_COORD_ARRAY);225glDisableClientState(GL_VERTEX_ARRAY);226227SDL_GL_SwapBuffers();228229#ifndef __EMSCRIPTEN__230// Wait for 3 seconds to give us a chance to see the image231SDL_Delay(3000);232#endif233234// Now we can delete the OpenGL texture and close down SDL235glDeleteTextures( 1, &texture );236237SDL_Quit();238239return 0;240}241242243