Path: blob/main/test/browser/gl_vertex_buffer_pre.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 026#endif2728#if USE_GLEW29#include "GL/glew.h"30#endif3132#include <SDL/SDL.h>3334#if !USE_GLEW35#include "SDL/SDL_opengl.h"36#endif3738#include <stdio.h>39#include <string.h>40#include <assert.h>4142int main(int argc, char *argv[])43{44SDL_Surface *screen;4546// Slightly different SDL initialization47if ( SDL_Init(SDL_INIT_VIDEO) != 0 ) {48printf("Unable to initialize SDL: %s\n", SDL_GetError());49return 1;50}5152SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); // *new*5354screen = SDL_SetVideoMode( 640, 480, 16, SDL_OPENGL ); // *changed*55if ( !screen ) {56printf("Unable to set video mode: %s\n", SDL_GetError());57return 1;58}5960// Set the OpenGL state after creating the context with SDL_SetVideoMode6162glClearColor( 0, 0, 0, 0 );6364#ifndef __EMSCRIPTEN__65glEnable( GL_TEXTURE_2D ); // Need this to display a texture XXX unnecessary in OpenGL ES 2.0/WebGL66#endif6768glViewport( 0, 0, 640, 480 );6970glMatrixMode( GL_MODELVIEW );71glLoadIdentity();7273// Clear the screen before drawing74glClear( GL_COLOR_BUFFER_BIT );7576typedef struct Color {77GLubyte r;78GLubyte g;79GLubyte b;80GLubyte a;81} Color;8283typedef struct Vertex {84GLfloat x;85GLfloat y;86Color color;87} Vertex;8889Vertex vertices[18] = {90{-1.00, 0.0, {0xFF, 0x00, 0xFF, 0xFF}},91{-1.00, 1.0, {0xFF, 0xFF, 0x00, 0xFF}},92{-0.75, 0.0, {0xFF, 0x00, 0x00, 0xFF}},93{-0.75, 1.0, {0xFF, 0xFF, 0xFF, 0xFF}},94{-0.50, 0.0, {0xFF, 0x00, 0x00, 0xFF}},95{-0.50, 1.0, {0xFF, 0xFF, 0x00, 0xFF}},96{-0.25, 0.0, {0xFF, 0x00, 0xFF, 0xFF}},97{-0.25, 1.0, {0xFF, 0xFF, 0x00, 0xFF}},98{-0.00, 0.0, {0xFF, 0x00, 0x00, 0xFF}},99{-0.00, 1.0, {0xFF, 0xFF, 0xFF, 0xFF}},100{ 0.25, 0.0, {0xFF, 0x00, 0x00, 0xFF}},101{ 0.25, 1.0, {0xFF, 0xFF, 0x00, 0xFF}},102{ 0.50, 0.0, {0xFF, 0x00, 0xFF, 0xFF}},103{ 0.50, 1.0, {0xFF, 0xFF, 0x00, 0xFF}},104{ 0.75, 0.0, {0xFF, 0x00, 0x00, 0xFF}},105{ 0.75, 1.0, {0xFF, 0xFF, 0xFF, 0xFF}},106{ 1.00, 0.0, {0xFF, 0x00, 0x00, 0xFF}},107{ 1.00, 1.0, {0xFF, 0xFF, 0x00, 0xFF}}108};109110Vertex vertices2[18] = {111{-1.00, -1.0, {0xFF, 0x00, 0xFF, 0xFF}},112{-1.00, 0.0, {0xFF, 0xFF, 0x00, 0xFF}},113{-0.75, -1.0, {0xFF, 0x00, 0x00, 0xFF}},114{-0.75, 0.0, {0xFF, 0xFF, 0xFF, 0xFF}},115{-0.50, -1.0, {0xFF, 0x00, 0x00, 0xFF}},116{-0.50, 0.0, {0xFF, 0xFF, 0x00, 0xFF}},117{-0.25, -1.0, {0xFF, 0x00, 0xFF, 0xFF}},118{-0.25, 0.0, {0xFF, 0xFF, 0x00, 0xFF}},119{-0.00, -1.0, {0xFF, 0x00, 0x00, 0xFF}},120{-0.00, 0.0, {0xFF, 0xFF, 0xFF, 0xFF}},121{ 0.25, -1.0, {0xFF, 0x00, 0x00, 0xFF}},122{ 0.25, 0.0, {0xFF, 0xFF, 0x00, 0xFF}},123{ 0.50, -1.0, {0xFF, 0x00, 0xFF, 0xFF}},124{ 0.50, 0.0, {0xFF, 0xFF, 0x00, 0xFF}},125{ 0.75, -1.0, {0xFF, 0x00, 0x00, 0xFF}},126{ 0.75, 0.0, {0xFF, 0xFF, 0xFF, 0xFF}},127{ 1.00, -1.0, {0xFF, 0x00, 0x00, 0xFF}},128{ 1.00, 0.0, {0xFF, 0xFF, 0x00, 0xFF}}129};130131// make a vertex buffer for the second set of vertices132GLuint vbo = 0;133glGenBuffers(1, &vbo);134135136// bind to it137glBindBuffer(GL_ARRAY_BUFFER, vbo);138// send it to gl139glBufferData(GL_ARRAY_BUFFER, sizeof(vertices2), vertices2, GL_STATIC_DRAW);140141// unbind from it142glBindBuffer(GL_ARRAY_BUFFER, 0);143144145// DRAW146147// Clear the screen before drawing148glClear( GL_COLOR_BUFFER_BIT );149150// This test ensures that we can use two separate arrays in memory for different151// attributes, and that they each can have different stride.152153glEnableClientState(GL_VERTEX_ARRAY);154glEnableClientState(GL_COLOR_ARRAY);155156glVertexPointer(2, GL_FLOAT, sizeof(Vertex), vertices);157glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Vertex), &vertices[0].color);158glDrawArrays(GL_TRIANGLE_STRIP, 10, 3);159glDrawArrays(GL_TRIANGLE_STRIP, 0, 6);160161glBindBuffer(GL_ARRAY_BUFFER, 0);162163glDisableClientState(GL_COLOR_ARRAY);164glDisableClientState(GL_VERTEX_ARRAY);165166SDL_GL_SwapBuffers();167168#ifndef __EMSCRIPTEN__169// Wait for 3 seconds to give us a chance to see the image170SDL_Delay(3000);171#endif172173SDL_Quit();174175return 0;176}177178179