Path: blob/main/test/browser/gl_vertex_buffer.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_READ); // GL_STATIC_READ is not in WebGL!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.152// The first test shows implicit striding (the zero indicates tightly packed)153// The second test shows explicit striding where the stride is passed in154// even though it also is tightly packed155156glEnableClientState(GL_VERTEX_ARRAY);157glEnableClientState(GL_COLOR_ARRAY);158159// TEST 1 - clientside data160161glVertexPointer(2, GL_FLOAT, sizeof(Vertex), vertices);162glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Vertex), &vertices[0].color);163glDrawArrays(GL_TRIANGLE_STRIP, 0, 6);164glDrawArrays(GL_TRIANGLE_STRIP, 10, 3);165166167168// TEST 2 - bind to array buffer, gl*Pointer calls are offsets into the buffer, which was previously uploaded to169170glBindBuffer(GL_ARRAY_BUFFER, vbo);171172glVertexPointer(2, GL_FLOAT, sizeof(Vertex), 0);173glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Vertex), (GLvoid*)((GLvoid*)&vertices2[0].color - (GLvoid*)&vertices2[0]));174175// gldrawarrays first with a low number of vertices, then with a high number176glDrawArrays(GL_TRIANGLE_STRIP, 0, 6);177glDrawArrays(GL_TRIANGLE_STRIP, 10, 3);178179glBindBuffer(GL_ARRAY_BUFFER, 0);180181glDisableClientState(GL_COLOR_ARRAY);182glDisableClientState(GL_VERTEX_ARRAY);183184SDL_GL_SwapBuffers();185186#ifndef __EMSCRIPTEN__187// Wait for 3 seconds to give us a chance to see the image188SDL_Delay(3000);189#endif190191SDL_Quit();192193return 0;194}195196197