Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // gl_loop.c
- #include "gl_draw.h"
- #include "gl_loop.h"
- #include "gl_screen.h"
- #include "i_err.h"
- #include "i_salloc.h"
- #include "i_types.h"
- // gl_screen variables, extern'd thru gl-screen to rest of program
- unsigned short globsperscreenW;
- unsigned short globsperscreenH;
- void initglscreen() {
- globsperscreenW = GLOBS_PER_TILE_WIDTH * TILES_PER_SCREEN_WIDTH;
- globsperscreenH = GLOBS_PER_TILE_HEIGHT * TILES_PER_SCREEN_HEIGHT;
- // make sure the total size isn't over USHRT_MAX
- int totalsize = globsperscreenW * globsperscreenH;
- if (totalsize > USHRT_MAX) error("Total size of glscreen is greater than the maximum USHRT value",false);
- // also sets up some variables that aren't important
- }
- #include "g_tick.h"
- #include "gl_shaders.h"
- // opengl headers
- #include <stdio.h>
- #include <stdlib.h>
- #include <GL/glew.h>
- #include <GLFW/glfw3.h>
- int beginGLLoop() {
- // firstly, create the window
- puts("Creating window...");
- // initialize GLFW
- glewExperimental = true;
- if (!glfwInit()) error("Failed to initialize GLFW",true);
- // set window hints
- glfwWindowHint(GLFW_SAMPLES, 4);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
- glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
- glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
- // actually create the window
- GLFWwindow *window;
- window = glfwCreateWindow(720,720,"Test Program",NULL,NULL);
- if (window == NULL) error("Unable to initialize window",true);
- glfwMakeContextCurrent(window);
- glewExperimental = true;
- if (glewInit() != GLEW_OK) error("Failed to initialize GLEW",true);
- puts("Window successfully initialized, initializing screen buffer...");
- // while we're here, initialize the screen
- initglscreen(); // sets up some (currently useless) variables
- puts("Screen buffer initialized, initializing vertex diagram...");
- // secondly, calculate the vertices that we need to render
- // (more complex calculations are run by the vertex shader)
- size_t verticesSize;
- GLuint vertexbuffer;
- vertexbuffer = initvertexdiagram(&verticesSize);
- GLuint colorbuffer = initcolordiagram();
- puts("Vertex diagram initialized, loading shaders...");
- // thirdly, load and compile the shaders
- GLuint program = loadshaders("./src/shaders/vertex.glsl","./src/shaders/fragment.glsl"); // TODO: find a more reliable way of storing the shader paths
- puts("Shaders are loaded, running final initialization procedures...");
- // fourthly, initialize some things that have to be done before rendering
- initdraw();
- puts("Initialization completed, begininng rendering loop...");
- // fifthly, begin rendering loop
- bool continueRendering = true; // TODO: create a more sophisticated way of doing this
- glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
- int windowwidth, windowheight;
- //glUseProgram(program);
- do {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glUseProgram(program);
- // TODO: poll game to update screen variables
- glfwGetWindowSize(window,&windowwidth,&windowheight);
- drawscreen(window,windowwidth,windowheight,vertexbuffer,colorbuffer);
- // swap buffers
- glfwSwapBuffers(window);
- glfwPollEvents();
- // event polling
- continueRendering = (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS) && gametick();
- } while (glfwWindowShouldClose(window) == 0 && continueRendering);
- puts("Freeing memory before exit...");
- // finally, release any memory held by the program
- glDeleteBuffers(1,&vertexbuffer);
- glDeleteBuffers(1,&colorbuffer);
- puts("Memory freed, exiting program.");
- // return
- return 0;
- }
- // gl_draw.c
- #include "i_salloc.h"
- #include "i_types.h"
- #include "gl_draw.h"
- #include "gl_screen.h"
- #include <stdio.h>
- #include <stdlib.h>
- // initialize collection of vertices used to draw stuff
- GLuint initvertexdiagram(size_t *size) {
- // initialize memory
- const byte numsPerVertice = 3;
- *size = sizeof(GLfloat) * (globsperscreenW * globsperscreenH * ((numsPerVertice * 4)));
- GLfloat *verticies = checkalloc(*size);
- // initialize recurring variables
- const GLfloat zVal = 0.0f;
- GLfloat xVal = -1.0f;
- GLfloat yVal = -1.0f;
- GLfloat xIncrement = 2.0f / globsperscreenW;
- GLfloat yIncrement = 2.0f / globsperscreenH;
- // begin loop to fill diagram
- unsigned short currentIndex = 0;
- for (unsigned short i = 0; i < globsperscreenH; i++) {
- for (unsigned short j = 0; j < globsperscreenH; j++) {
- // assign x y and z values
- verticies[currentIndex] = xVal;
- verticies[currentIndex + 1] = yVal + yIncrement;
- verticies[currentIndex + 2] = zVal;
- verticies[currentIndex + 3] = xVal;
- verticies[currentIndex + 4] = yVal;
- verticies[currentIndex + 5] = zVal;
- verticies[currentIndex + 6] = xVal + xIncrement;
- verticies[currentIndex + 7] = yVal;
- verticies[currentIndex + 8] = zVal;
- verticies[currentIndex + 9] = xVal + xIncrement;
- verticies[currentIndex + 10] = yVal + yIncrement;
- verticies[currentIndex + 11] = zVal;
- // increment index
- currentIndex += 12;
- // change values
- xVal += xIncrement;
- }
- yVal += yIncrement;
- }
- // setup opengl-style buffer
- GLuint buffer;
- glGenBuffers(1,&buffer);
- glBindBuffer(GL_ARRAY_BUFFER,buffer);
- glBufferData(GL_ARRAY_BUFFER,*size,verticies,GL_STATIC_DRAW);
- free(verticies);
- // return the pointer
- return buffer;
- }
- // initialize a buffer of colors
- GLuint initcolordiagram() {
- size_t size = sizeof(GLfloat) * globsperscreenW * globsperscreenH * 3 * 4;
- GLfloat *colors = checkalloc(size);
- // loop through it
- int currentIndex = 0;
- for (int i = 0; i < globsperscreenW * globsperscreenH * 4; i++) {
- colors[currentIndex] = 0.0f;
- colors[currentIndex + 1] = 0.0f;
- colors[currentIndex + 2] = 1.0f;
- currentIndex += 3;
- }
- // bind buffers
- GLuint buffer;
- glGenBuffers(1,&buffer);
- glBindBuffer(GL_ARRAY_BUFFER,buffer);
- glBufferData(GL_ARRAY_BUFFER,size,colors,GL_STATIC_DRAW);
- free(colors);
- return buffer;
- }
- void initdraw() {
- glClearColor(0.0f, 0.4f, 0.4f, 0.0f);
- }
- void drawscreen(GLFWwindow *window, int width, int height, GLuint vertexbuffer, GLuint colorbuffer) {
- glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
- glEnableVertexAttribArray(0);
- glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,(void *)0);
- glBindBuffer(GL_ARRAY_BUFFER, colorbuffer);
- glEnableVertexAttribArray(1);
- glVertexAttribPointer(1,3,GL_FLOAT,GL_FALSE,0,(void *)0);
- // draw the squares
- static const GLubyte indicies[] = {0,1,2,0,2,3};
- glDrawElements(GL_TRIANGLES,6,GL_UNSIGNED_BYTE,indicies);
- glDisableVertexAttribArray(1);
- glDisableVertexAttribArray(0);
- }
- // vertex shader
- #version 330 core
- layout (location = 0) in vec3 vertexPos;
- layout (location = 1) in vec3 fColor;
- out vec3 vertexColor;
- void main() {
- gl_Position.xyz = vertexPos;
- gl_Position.w = 1.0;
- vertexColor = fColor;
- }
- // fragment shader
- #version 330 core
- // this file is a part of Avaritia, made by John Nunley and licensed under GPL 3.0
- out vec4 fragmentColor;
- in vec3 vertexColor;
- void main() {
- fragmentColor.xyz = vertexColor;
- fragmentColor.a = 1.0f;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement