this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Made 3d-demo use shaders so it demonstrates a better approach.

+124 -49
+103 -49
demos/3d/main.c
··· 1 1 #include "SDL.h" 2 2 #include "SDL_gpu.h" 3 3 #include "SDL_gpu_OpenGL_1.h" 4 + #include "SDL_gpu_GL_matrix.h" 4 5 #include "common.h" 5 - 6 + 7 + 8 + 9 + 10 + GLuint VAO; 11 + GLuint VBO; 12 + GLuint modelViewProjection_loc; 13 + GLuint vertex_loc; 14 + GLuint color_loc; 15 + Uint32 v, f, p; 6 16 7 17 void begin_3d(GPU_Target* screen) 8 18 { 9 19 GPU_FlushBlitBuffer(); 10 20 11 - glMatrixMode( GL_PROJECTION ); 12 - glPushMatrix(); 13 - glLoadIdentity(); 14 - 15 - glMatrixMode( GL_MODELVIEW ); 16 - glPushMatrix(); 17 - glLoadIdentity(); 21 + GPU_MatrixMode(GPU_MODELVIEW); 22 + GPU_LoadIdentity(); 23 + GPU_MatrixMode(GPU_PROJECTION); 24 + GPU_LoadIdentity(); 18 25 } 19 26 20 27 void end_3d(GPU_Target* screen) 21 - { 22 - glPopMatrix(); 23 - glMatrixMode( GL_PROJECTION ); 24 - glPopMatrix(); 25 - glMatrixMode( GL_MODELVIEW ); 26 - 27 - //GPU_SetCamera(screen, NULL); 28 + { 29 + GPU_ResetRendererState(); 28 30 } 29 31 30 32 void draw_spinning_triangle() 31 33 { 32 - GLfloat glverts[9]; 33 - GLfloat glcolors[9]; 34 + GLfloat gldata[21]; 35 + float mvp[16]; 34 36 float t = SDL_GetTicks()/1000.0f; 35 37 36 - glRotatef(100*t, 0, 0.707, 0.707); 37 - glRotatef(20*t, 0.707, 0.707, 0); 38 + GPU_Rotate(100*t, 0, 0.707, 0.707); 39 + GPU_Rotate(20*t, 0.707, 0.707, 0); 38 40 39 41 40 - glverts[0] = 0; 41 - glverts[1] = 0.2f; 42 - glverts[2] = 0; 43 - glcolors[0] = 1.0f; 44 - glcolors[1] = 0.0f; 45 - glcolors[2] = 0.0f; 42 + gldata[0] = 0; 43 + gldata[1] = 0.2f; 44 + gldata[2] = 0; 46 45 47 - glverts[3] = -0.2f; 48 - glverts[4] = -0.2f; 49 - glverts[5] = 0; 50 - glcolors[3] = 0.0f; 51 - glcolors[4] = 1.0f; 52 - glcolors[5] = 0.0f; 46 + gldata[3] = 1.0f; 47 + gldata[4] = 0.0f; 48 + gldata[5] = 0.0f; 49 + gldata[6] = 1.0f; 53 50 54 - glverts[6] = 0.2f; 55 - glverts[7] = -0.2f; 56 - glverts[8] = 0; 57 - glcolors[6] = 0.0f; 58 - glcolors[7] = 0.0f; 59 - glcolors[8] = 1.0f; 51 + 52 + gldata[7] = -0.2f; 53 + gldata[8] = -0.2f; 54 + gldata[9] = 0; 60 55 61 - glVertexPointer(3, GL_FLOAT, 0, glverts); 62 - glColorPointer(3, GL_FLOAT, 0, glcolors); 63 - glEnableClientState(GL_VERTEX_ARRAY); 64 - glEnableClientState(GL_COLOR_ARRAY); 65 - glDrawArrays(GL_TRIANGLE_STRIP, 0, 3); 66 - glDisableClientState(GL_COLOR_ARRAY); 67 - glDisableClientState(GL_VERTEX_ARRAY); 56 + gldata[10] = 0.0f; 57 + gldata[11] = 1.0f; 58 + gldata[12] = 0.0f; 59 + gldata[13] = 1.0f; 60 + 61 + gldata[14] = 0.2f; 62 + gldata[15] = -0.2f; 63 + gldata[16] = 0; 64 + gldata[17] = 0.0f; 65 + gldata[18] = 0.0f; 66 + gldata[19] = 1.0f; 67 + gldata[20] = 1.0f; 68 + 69 + 70 + glUseProgram(p); 71 + 72 + glBindVertexArray(VAO); 73 + 74 + GPU_GetModelViewProjection(mvp); 75 + glUniformMatrix4fv(modelViewProjection_loc, 1, 0, mvp); 76 + 77 + 78 + glEnableVertexAttribArray(vertex_loc); 79 + glEnableVertexAttribArray(color_loc); 80 + 81 + glBindBuffer(GL_ARRAY_BUFFER, VBO); 82 + glBufferData(GL_ARRAY_BUFFER, sizeof(gldata), gldata, GL_STREAM_DRAW); 83 + 84 + glVertexAttribPointer( 85 + vertex_loc, 86 + 3, // size 87 + GL_FLOAT, // type 88 + GL_FALSE, // normalize 89 + sizeof(float)*7, // stride 90 + (void*)0 // offset 91 + ); 92 + 93 + glVertexAttribPointer( 94 + color_loc, 95 + 4, // size 96 + GL_FLOAT, // type 97 + GL_FALSE, // normalize 98 + sizeof(float)*7, // stride 99 + (void*)(sizeof(float)*3) // offset 100 + ); 101 + 102 + glDrawArrays(GL_TRIANGLES, 0, 3); 103 + 104 + glDisableVertexAttribArray(color_loc); 105 + glDisableVertexAttribArray(vertex_loc); 106 + 107 + glBindVertexArray(0); 68 108 } 69 109 70 110 void draw_3d_stuff(GPU_Target* screen) 71 111 { 72 - begin_3d(screen); 112 + begin_3d(screen); 73 113 74 114 draw_spinning_triangle(); 75 115 ··· 82 122 begin_3d(screen); 83 123 84 124 t = SDL_GetTicks()/1000.0f; 85 - glRotatef(t*60, 0, 0, 1); 86 - glTranslatef(0.4f, 0.4f, 0); 125 + GPU_Rotate(t*60, 0, 0, 1); 126 + GPU_Translate(0.4f, 0.4f, 0); 87 127 draw_spinning_triangle(); 88 128 89 129 end_3d(screen); ··· 118 158 119 159 image = GPU_LoadImage("data/test.bmp"); 120 160 if (image == NULL) 121 - return -1; 122 - 161 + return -1; 162 + 163 + v = GPU_LoadShader(GPU_VERTEX_SHADER, "3d/untextured.vert"); 164 + f = GPU_LoadShader(GPU_FRAGMENT_SHADER, "3d/untextured.frag"); 165 + p = GPU_LinkShaders(v, f); 166 + 167 + glUseProgram(p); 168 + vertex_loc = GPU_GetAttributeLocation(p, "gpu_Vertex"); 169 + color_loc = GPU_GetAttributeLocation(p, "gpu_Color"); 170 + modelViewProjection_loc = GPU_GetUniformLocation(p, "gpu_ModelViewProjectionMatrix"); 171 + 172 + glGenVertexArrays(1, &VAO); 173 + glBindVertexArray(VAO); 174 + 175 + glGenBuffers(1, &VBO); 176 + glBindBuffer(GL_ARRAY_BUFFER, VBO); 123 177 124 178 dt = 0.010f; 125 179
+8
demos/3d/untextured.frag
··· 1 + #version 110 2 + 3 + varying vec4 color; 4 + 5 + void main(void) 6 + { 7 + gl_FragColor = color; 8 + }
+13
demos/3d/untextured.vert
··· 1 + #version 110 2 + 3 + attribute vec3 gpu_Vertex; 4 + attribute vec4 gpu_Color; 5 + uniform mat4 gpu_ModelViewProjectionMatrix; 6 + 7 + varying vec4 color; 8 + 9 + void main(void) 10 + { 11 + color = gpu_Color; 12 + gl_Position = gpu_ModelViewProjectionMatrix * vec4(gpu_Vertex, 1.0); 13 + }