OLD | NEW |
| (Empty) |
1 // | |
2 // Book: OpenGL(R) ES 2.0 Programming Guide | |
3 // Authors: Aaftab Munshi, Dan Ginsburg, Dave Shreiner | |
4 // ISBN-10: 0321502795 | |
5 // ISBN-13: 9780321502797 | |
6 // Publisher: Addison-Wesley Professional | |
7 // URLs: http://safari.informit.com/9780321563835 | |
8 // http://www.opengles-book.com | |
9 // | |
10 | |
11 // Hello_Triangle.c | |
12 // | |
13 // This is a simple example that draws a single triangle with | |
14 // a minimal vertex/fragment shader. The purpose of this | |
15 // example is to demonstrate the basic concepts of | |
16 // OpenGL ES 2.0 rendering. | |
17 | |
18 #include "Hello_Triangle.h" | |
19 | |
20 #include <stdlib.h> | |
21 | |
22 /// | |
23 // Initialize the shader and program object | |
24 // | |
25 int htInit ( ESContext *esContext ) | |
26 { | |
27 HTUserData *userData = esContext->userData; | |
28 | |
29 GLbyte vShaderStr[] = | |
30 "attribute vec4 vPosition; \n" | |
31 "void main() \n" | |
32 "{ \n" | |
33 " gl_Position = vPosition; \n" | |
34 "} \n"; | |
35 | |
36 // TODO(alokp): Shaders containing "precision" do not compile. | |
37 GLbyte fShaderStr[] = | |
38 "//precision mediump float; \n" | |
39 "void main() \n" | |
40 "{ \n" | |
41 " gl_FragColor = vec4 ( 1.0, 0.0, 0.0, 1.0 );\n" | |
42 "} \n"; | |
43 | |
44 GLfloat vVertices[] = { 0.0f, 0.5f, 0.0f, | |
45 -0.5f, -0.5f, 0.0f, | |
46 0.5f, -0.5f, 0.0f }; | |
47 | |
48 userData->programObject = esLoadProgram ( vShaderStr, fShaderStr ); | |
49 if ( userData->programObject == 0 ) return FALSE; | |
50 | |
51 // Bind vPosition to attribute 0 | |
52 glBindAttribLocation ( userData->programObject, 0, "vPosition" ); | |
53 | |
54 glGenBuffers ( 1, &userData->vbo ); | |
55 glBindBuffer ( GL_ARRAY_BUFFER, userData->vbo ); | |
56 glBufferData ( GL_ARRAY_BUFFER, sizeof(vVertices), NULL, GL_STATIC_DRAW ); | |
57 glBufferSubData ( GL_ARRAY_BUFFER, 0, sizeof(vVertices), vVertices ); | |
58 | |
59 glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f ); | |
60 return TRUE; | |
61 } | |
62 | |
63 /// | |
64 // Draw a triangle using the shader pair created in Init() | |
65 // | |
66 void htDraw ( ESContext *esContext ) | |
67 { | |
68 HTUserData *userData = esContext->userData; | |
69 | |
70 // Set the viewport | |
71 glViewport ( 0, 0, esContext->width, esContext->height ); | |
72 | |
73 // Clear the color buffer | |
74 glClear ( GL_COLOR_BUFFER_BIT ); | |
75 | |
76 // Use the program object | |
77 glUseProgram ( userData->programObject ); | |
78 | |
79 // Load the vertex data | |
80 glBindBuffer ( GL_ARRAY_BUFFER, userData->vbo ); | |
81 glEnableVertexAttribArray ( 0 ); | |
82 glVertexAttribPointer ( 0, 3, GL_FLOAT, GL_FALSE, 0, 0 ); | |
83 | |
84 glDrawArrays ( GL_TRIANGLES, 0, 3 ); | |
85 | |
86 // Nothing is drawn or application crashes without glFlush. | |
87 // TODO(alokp): glFlush should not be necessary with SwapBuffers(). | |
88 glFlush(); | |
89 } | |
90 | |
91 /// | |
92 // Cleanup | |
93 // | |
94 void htShutDown ( ESContext *esContext ) | |
95 { | |
96 HTUserData *userData = esContext->userData; | |
97 | |
98 // Delete program object | |
99 if ( userData->programObject != 0 ) | |
100 { | |
101 glDeleteProgram ( userData->programObject ); | |
102 userData->programObject = 0; | |
103 } | |
104 if ( userData->vbo != 0 ) | |
105 { | |
106 glDeleteBuffers ( 1, &userData->vbo ); | |
107 userData->vbo = 0; | |
108 } | |
109 } | |
OLD | NEW |