SHOW:
|
|
- or go back to the newest paste.
1 | // gl_loop.c | |
2 | ||
3 | #include "gl_draw.h" | |
4 | #include "gl_loop.h" | |
5 | #include "gl_screen.h" | |
6 | #include "i_err.h" | |
7 | #include "i_salloc.h" | |
8 | #include "i_types.h" | |
9 | ||
10 | // gl_screen variables, extern'd thru gl-screen to rest of program | |
11 | unsigned short globsperscreenW; | |
12 | unsigned short globsperscreenH; | |
13 | ||
14 | void initglscreen() { | |
15 | globsperscreenW = GLOBS_PER_TILE_WIDTH * TILES_PER_SCREEN_WIDTH; | |
16 | globsperscreenH = GLOBS_PER_TILE_HEIGHT * TILES_PER_SCREEN_HEIGHT; | |
17 | ||
18 | // make sure the total size isn't over USHRT_MAX | |
19 | int totalsize = globsperscreenW * globsperscreenH; | |
20 | if (totalsize > USHRT_MAX) error("Total size of glscreen is greater than the maximum USHRT value",false); | |
21 | ||
22 | // also sets up some variables that aren't important | |
23 | } | |
24 | ||
25 | #include "g_tick.h" | |
26 | #include "gl_shaders.h" | |
27 | ||
28 | // opengl headers | |
29 | #include <stdio.h> | |
30 | #include <stdlib.h> | |
31 | ||
32 | #include <GL/glew.h> | |
33 | #include <GLFW/glfw3.h> | |
34 | ||
35 | int beginGLLoop() { | |
36 | // firstly, create the window | |
37 | puts("Creating window..."); | |
38 | ||
39 | // initialize GLFW | |
40 | glewExperimental = true; | |
41 | if (!glfwInit()) error("Failed to initialize GLFW",true); | |
42 | ||
43 | // set window hints | |
44 | glfwWindowHint(GLFW_SAMPLES, 4); | |
45 | glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); | |
46 | glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); | |
47 | glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); | |
48 | glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); | |
49 | ||
50 | // actually create the window | |
51 | GLFWwindow *window; | |
52 | window = glfwCreateWindow(720,720,"Test Program",NULL,NULL); | |
53 | if (window == NULL) error("Unable to initialize window",true); | |
54 | glfwMakeContextCurrent(window); | |
55 | glewExperimental = true; | |
56 | if (glewInit() != GLEW_OK) error("Failed to initialize GLEW",true); | |
57 | ||
58 | puts("Window successfully initialized, initializing screen buffer..."); | |
59 | ||
60 | // while we're here, initialize the screen | |
61 | initglscreen(); // sets up some (currently useless) variables | |
62 | ||
63 | puts("Screen buffer initialized, initializing vertex diagram..."); | |
64 | ||
65 | // secondly, calculate the vertices that we need to render | |
66 | // (more complex calculations are run by the vertex shader) | |
67 | size_t verticesSize; | |
68 | GLuint vertexbuffer; | |
69 | vertexbuffer = initvertexdiagram(&verticesSize); | |
70 | GLuint colorbuffer = initcolordiagram(); | |
71 | ||
72 | puts("Vertex diagram initialized, loading shaders..."); | |
73 | ||
74 | // thirdly, load and compile the shaders | |
75 | GLuint program = loadshaders("./src/shaders/vertex.glsl","./src/shaders/fragment.glsl"); // TODO: find a more reliable way of storing the shader paths | |
76 | ||
77 | puts("Shaders are loaded, running final initialization procedures..."); | |
78 | ||
79 | // fourthly, initialize some things that have to be done before rendering | |
80 | initdraw(); | |
81 | ||
82 | puts("Initialization completed, begininng rendering loop..."); | |
83 | ||
84 | // fifthly, begin rendering loop | |
85 | bool continueRendering = true; // TODO: create a more sophisticated way of doing this | |
86 | glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE); | |
87 | int windowwidth, windowheight; | |
88 | //glUseProgram(program); | |
89 | do { | |
90 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
91 | glUseProgram(program); | |
92 | ||
93 | // TODO: poll game to update screen variables | |
94 | ||
95 | glfwGetWindowSize(window,&windowwidth,&windowheight); | |
96 | drawscreen(window,windowwidth,windowheight,vertexbuffer,colorbuffer); | |
97 | ||
98 | // swap buffers | |
99 | glfwSwapBuffers(window); | |
100 | glfwPollEvents(); | |
101 | ||
102 | // event polling | |
103 | continueRendering = (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS) && gametick(); | |
104 | } while (glfwWindowShouldClose(window) == 0 && continueRendering); | |
105 | ||
106 | ||
107 | puts("Freeing memory before exit..."); | |
108 | // finally, release any memory held by the program | |
109 | glDeleteBuffers(1,&vertexbuffer); | |
110 | glDeleteBuffers(1,&colorbuffer); | |
111 | puts("Memory freed, exiting program."); | |
112 | ||
113 | // return | |
114 | return 0; | |
115 | } | |
116 | ||
117 | // gl_draw.c | |
118 | #include "i_salloc.h" | |
119 | #include "i_types.h" | |
120 | ||
121 | #include "gl_draw.h" | |
122 | #include "gl_screen.h" | |
123 | ||
124 | #include <stdio.h> | |
125 | #include <stdlib.h> | |
126 | ||
127 | // initialize collection of vertices used to draw stuff | |
128 | GLuint initvertexdiagram(size_t *size) { | |
129 | // initialize memory | |
130 | const byte numsPerVertice = 3; | |
131 | *size = sizeof(GLfloat) * (globsperscreenW * globsperscreenH * ((numsPerVertice * 4))); | |
132 | GLfloat *verticies = checkalloc(*size); | |
133 | ||
134 | // initialize recurring variables | |
135 | const GLfloat zVal = 0.0f; | |
136 | GLfloat xVal = -1.0f; | |
137 | GLfloat yVal = -1.0f; | |
138 | ||
139 | GLfloat xIncrement = 2.0f / globsperscreenW; | |
140 | GLfloat yIncrement = 2.0f / globsperscreenH; | |
141 | ||
142 | // begin loop to fill diagram | |
143 | unsigned short currentIndex = 0; | |
144 | for (unsigned short i = 0; i < globsperscreenH; i++) { | |
145 | for (unsigned short j = 0; j < globsperscreenH; j++) { | |
146 | // assign x y and z values | |
147 | verticies[currentIndex] = xVal; | |
148 | verticies[currentIndex + 1] = yVal + yIncrement; | |
149 | verticies[currentIndex + 2] = zVal; | |
150 | ||
151 | verticies[currentIndex + 3] = xVal; | |
152 | verticies[currentIndex + 4] = yVal; | |
153 | verticies[currentIndex + 5] = zVal; | |
154 | ||
155 | verticies[currentIndex + 6] = xVal + xIncrement; | |
156 | verticies[currentIndex + 7] = yVal; | |
157 | verticies[currentIndex + 8] = zVal; | |
158 | ||
159 | verticies[currentIndex + 9] = xVal + xIncrement; | |
160 | verticies[currentIndex + 10] = yVal + yIncrement; | |
161 | verticies[currentIndex + 11] = zVal; | |
162 | ||
163 | ||
164 | // increment index | |
165 | currentIndex += 12; | |
166 | ||
167 | // change values | |
168 | xVal += xIncrement; | |
169 | } | |
170 | yVal += yIncrement; | |
171 | } | |
172 | ||
173 | // setup opengl-style buffer | |
174 | GLuint buffer; | |
175 | glGenBuffers(1,&buffer); | |
176 | glBindBuffer(GL_ARRAY_BUFFER,buffer); | |
177 | glBufferData(GL_ARRAY_BUFFER,*size,verticies,GL_STATIC_DRAW); | |
178 | ||
179 | free(verticies); | |
180 | ||
181 | // return the pointer | |
182 | return buffer; | |
183 | } | |
184 | ||
185 | // initialize a buffer of colors | |
186 | GLuint initcolordiagram() { | |
187 | size_t size = sizeof(GLfloat) * globsperscreenW * globsperscreenH * 3 * 4; | |
188 | GLfloat *colors = checkalloc(size); | |
189 | ||
190 | // loop through it | |
191 | int currentIndex = 0; | |
192 | for (int i = 0; i < globsperscreenW * globsperscreenH * 4; i++) { | |
193 | colors[currentIndex] = 0.0f; | |
194 | colors[currentIndex + 1] = 0.0f; | |
195 | colors[currentIndex + 2] = 1.0f; | |
196 | currentIndex += 3; | |
197 | } | |
198 | ||
199 | ||
200 | // bind buffers | |
201 | GLuint buffer; | |
202 | glGenBuffers(1,&buffer); | |
203 | glBindBuffer(GL_ARRAY_BUFFER,buffer); | |
204 | glBufferData(GL_ARRAY_BUFFER,size,colors,GL_STATIC_DRAW); | |
205 | ||
206 | free(colors); | |
207 | ||
208 | return buffer; | |
209 | } | |
210 | ||
211 | void initdraw() { | |
212 | glClearColor(0.0f, 0.4f, 0.4f, 0.0f); | |
213 | } | |
214 | ||
215 | void drawscreen(GLFWwindow *window, int width, int height, GLuint vertexbuffer, GLuint colorbuffer) { | |
216 | glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); | |
217 | glEnableVertexAttribArray(0); | |
218 | glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,(void *)0); | |
219 | glBindBuffer(GL_ARRAY_BUFFER, colorbuffer); | |
220 | glEnableVertexAttribArray(1); | |
221 | glVertexAttribPointer(1,3,GL_FLOAT,GL_FALSE,0,(void *)0); | |
222 | ||
223 | // draw the squares | |
224 | static const GLubyte indicies[] = {0,1,2,0,2,3}; | |
225 | ||
226 | glDrawElements(GL_TRIANGLES,6,GL_UNSIGNED_BYTE,indicies); | |
227 | ||
228 | glDisableVertexAttribArray(1); | |
229 | glDisableVertexAttribArray(0); | |
230 | } | |
231 | ||
232 | // vertex shader | |
233 | #version 330 core | |
234 | ||
235 | layout (location = 0) in vec3 vertexPos; | |
236 | layout (location = 1) in vec3 fColor; | |
237 | ||
238 | out vec3 vertexColor; | |
239 | ||
240 | void main() { | |
241 | gl_Position.xyz = vertexPos; | |
242 | gl_Position.w = 1.0; | |
243 | ||
244 | vertexColor = fColor; | |
245 | } | |
246 | ||
247 | // fragment shader | |
248 | #version 330 core | |
249 | ||
250 | - | // this file is a part of Avaritia, made by John Nunley and licensed under GPL 3.0 |
250 | + | |
251 | ||
252 | in vec3 vertexColor; | |
253 | ||
254 | void main() { | |
255 | fragmentColor.xyz = vertexColor; | |
256 | fragmentColor.a = 1.0f; | |
257 | } |