summaryrefslogtreecommitdiff
path: root/glgfx.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'glgfx.cpp')
-rw-r--r--glgfx.cpp191
1 files changed, 191 insertions, 0 deletions
diff --git a/glgfx.cpp b/glgfx.cpp
new file mode 100644
index 0000000..6700601
--- /dev/null
+++ b/glgfx.cpp
@@ -0,0 +1,191 @@
+#include "glgfx.h"
+//buffer handles are the ints that gl returns
+//buffer data is the data you bind to opengl
+GLuint vbhandler; //vertex buffer handle
+GLuint ebhandler;
+//handlers for the perspective transforms
+GLint projhandler;
+GLint tranhandler;
+GLint camhandler;
+glm::mat4 tran;
+//device coordinates
+// ^
+// (y+)
+// |
+//<(x-)-+-(x+)>
+// |
+// (y-)
+// v
+//shaders
+// z+ is close to screen z- is farther away
+GLuint vertexshaderhandle;
+GLuint fragmentshaderhandle;
+GLuint shaderprogramhandle;
+int initglgfx() {
+ //glew is the library that links all of opengl's fnctions
+ //for you
+ glewExperimental = GL_TRUE;
+ glewInit();
+ glEnable (GL_BLEND);
+ // so we can have transparent polygons
+ glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+ //culls back face triagnles, which is determined by whether vertexes are clockwise
+ // or counterclockwise
+ glEnable(GL_CULL_FACE);
+ glCullFace(GL_BACK);
+ GLuint glnum;
+ glGenBuffers(1, &glnum);
+ printf("%u\n",glnum);
+ //vertex data load of the box model
+ float vertexdata[] = {
+ -0.5, 0.5, -.5, // x y z
+ 0.5, 0.5, -.5,
+ 0.5, -0.5, -.5,
+ -0.5, -0.5, -.5,
+ -0.5, 0.5, 0.5, // x y z
+ 0.5, 0.5, 0.5,
+ 0.5, -0.5, 0.5,
+ -0.5, -0.5, 0.5
+ };
+ //polygons of the box mode based on the vertex data.
+ //they must be clockwise from the front so that they are culled when not
+ //seen
+ GLuint elementdata[] = {
+ 0,1,3,
+ 3,1,2,
+ 1,5,2,
+ 2,5,6,
+ 5,4,6,
+ 4,7,6,
+ 4,0,7,
+ 0,3,7,
+ 3,6,7,
+ 2,6,3,
+ 0,4,5,
+ 5,1,0,
+ };
+ //binds to the element buffer
+ glGenBuffers(1,&vbhandler);
+ glBindBuffer(GL_ARRAY_BUFFER,vbhandler);
+ glBufferData(GL_ARRAY_BUFFER, sizeof(vertexdata), vertexdata, GL_DYNAMIC_DRAW);
+ glGenBuffers(1,&ebhandler);
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,ebhandler);
+ glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elementdata), elementdata, GL_DYNAMIC_DRAW);
+ //loads the shaders
+ genshaders();
+
+}
+//loads shaders and models
+int genshaders() {
+//flowchart
+//vertex shader receives raw vertex data, outputs that data transformed in 3d space(camera), that data is passed to
+//geometry shader, which is where we will add normal vector for
+//light calculations // also where we make the volumetric information
+//all these are rasterized, and blended for every pixel value
+//then passed to fragment shader
+//which is where we do the light calculations
+//tests and blending(multiple buffers?)
+ //reads from files
+ //debug printout
+ //printf(readfile("vertexshader.glsl").c_str());
+ std::string vertexshadersource = readfile("vertexshader.glsl").c_str();
+ std::string fragmentshadersource = readfile("fragmentshader.glsl").c_str();
+ const char * vertexshaderstr = vertexshadersource.c_str();
+ const char * fragmentshaderstr = fragmentshadersource.c_str();
+ GLuint vertexshaderhandle = glCreateShader(GL_VERTEX_SHADER);
+ int vertexshaderlen = vertexshadersource.length();
+ //length of the source must be specified
+ glShaderSource(vertexshaderhandle, 1, (&vertexshaderstr), &vertexshaderlen);
+ glCompileShader(vertexshaderhandle);
+ //error buffer
+ char buffer[512];
+ GLint err;
+ glGetShaderiv(vertexshaderhandle, GL_COMPILE_STATUS, &err);
+ if (err == GL_FALSE) {
+ glGetShaderInfoLog(vertexshaderhandle, 512, NULL, buffer);
+ printf("error compiling vertex shhader\n");
+ printf(buffer);
+ }
+
+ GLuint fragmentshaderhandle = glCreateShader(GL_FRAGMENT_SHADER);
+ int fragmentshaderlen = fragmentshadersource.length();
+ //the length of the source must be specified
+ glShaderSource(fragmentshaderhandle, 1, (&fragmentshaderstr), &fragmentshaderlen);
+ glCompileShader(fragmentshaderhandle);
+ glGetShaderiv(fragmentshaderhandle, GL_COMPILE_STATUS, &err);
+ if (err == GL_FALSE) {
+ glGetShaderInfoLog(fragmentshaderhandle, 512, NULL, buffer);
+ printf("error compiling fragment shader\n");
+ printf(buffer);
+ }
+ //make the shader program
+ shaderprogramhandle = glCreateProgram();
+ glAttachShader(shaderprogramhandle,vertexshaderhandle);
+ glAttachShader(shaderprogramhandle,fragmentshaderhandle);
+ //the 0 is which buffer, 0 is the default, 1 will be fore volumetric shading
+ glBindFragDataLocation(shaderprogramhandle,0,"outpix");
+ glLinkProgram(shaderprogramhandle);
+ glUseProgram(shaderprogramhandle);
+ //vertex data
+ GLint pos;
+ pos = glGetAttribLocation(shaderprogramhandle, "position");
+ //the actual atribute, number of values, type, whether they need to be normalied to -1 1,
+ //number of bytes between values, offset from beginning in bytes
+ glVertexAttribPointer(pos, 3, GL_FLOAT, GL_FALSE, 0, 0);
+ glEnableVertexAttribArray(pos);
+
+}
+//opens a filebuffer stream and reads every char
+std::string readfile(char const * filename) {
+ std::ifstream filebuffer;
+ std::string line;
+ std::string rstr;
+ rstr = "";
+ char c;
+ filebuffer.open(filename);
+ if (!filebuffer.is_open()) {
+ printf("error reading file");
+ }
+ while (filebuffer.get(c)) {
+ rstr = rstr + c;
+ }
+ filebuffer.close();
+ return rstr;
+}
+//draws the model, setting perspective transform and position transform
+//done with magic numbers for now for testing purposes
+int drawmodel(float x, float y, float z, float xr, float yr, float zr) {
+// the transformation matrix = rotation matrix of object * scaling matrix * translaton matrix representing difference in camera and object location * -rotation matrix of the camera * perspective transform
+//order of application ir right to left
+ //fov, aspect ratio, size of near and far planes distance
+ glm::mat4 proj = glm::perspective(glm::radians(45.0f), (float)maxx/(float)maxy, 0.1f, 100.0f);
+ projhandler = glGetUniformLocation(shaderprogramhandle, "proj");
+ glUniformMatrix4fv(projhandler, 1, GL_FALSE, glm::value_ptr(proj));
+ //lookat eye position of camera, center center of screen, up up direction
+ glm::mat4 cam = glm::lookAt(glm::vec3(0,0,1), glm::vec3(0,0,0),glm::vec3(0,1,0));
+ camhandler = glGetUniformLocation(shaderprogramhandle, "cam");
+ glUniformMatrix4fv(camhandler, 1, GL_FALSE, glm::value_ptr(cam));
+ //a valid tranlation matrix showing the colun first conventions of glm::mat4
+ //tran = glm::mat4(
+ // glm::vec4(1.0f,0.0f,0.0f,0.0f), //x transform is the one on the last column
+ // glm::vec4(0.0f,1.0f,0.0f,0.0f), //y
+ // glm::vec4(0.0f,0.0f,1.0f,0.0f), // z
+ // glm::vec4(0.0f,0.0f,0.5f,1.0f) //w just make it 1
+ //);
+ //the translation matrix, matrixes added last are applied first
+ glm::mat4 tran;
+ tran = glm::translate(tran, glm::vec3(x,y,z));
+ tran = glm::rotate(tran,glm::radians(xr),glm::vec3(1,0,0)); // axis of rotation x y z
+ tran = glm::rotate(tran,glm::radians(yr),glm::vec3(0,1,0));
+ tran = glm::rotate(tran,glm::radians(zr),glm::vec3(0,0,1));
+ //deprecated debug stuff
+ //printf("translated vector x%f\n",(tran * glm::vec4(0.0f,0.0f,0.0f,1.0f)).x);
+ //printf("translated vector y%f\n",(tran * glm::vec4(0.0f,0.0f,0.0f,1.0f)).y);
+ //printf("translated vector z%f\n",(tran * glm::vec4(0.0f,0.0f,0.0f,1.0f)).z);
+ //loads the transaltion stuff
+ tranhandler = glGetUniformLocation(shaderprogramhandle, "tran");
+ glUniformMatrix4fv(tranhandler, 1, GL_FALSE, glm::value_ptr(tran));
+ glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);
+}
+int quitglgfx() {
+}