summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorknolax <1339802534.kk@gmail.com>2017-02-16 10:46:14 -0500
committerknolax <1339802534.kk@gmail.com>2017-02-16 10:46:14 -0500
commitd137bf2485050b54bb0ac65ef543ba24f18fabbe (patch)
treef66009aa6db3c88c715b5e23d874f22b1c530a4e
initial commit
-rw-r--r--example/Terminus.ttfbin0 -> 423992 bytes
-rw-r--r--example/fragmentshader.glsl6
-rwxr-xr-xexample/mainbin0 -> 49192 bytes
-rw-r--r--example/main.cpp57
-rw-r--r--example/main.h4
-rw-r--r--example/main.obin0 -> 4424 bytes
-rw-r--r--example/makefile6
-rw-r--r--example/vertexshader.glsl10
-rw-r--r--glgfx.cpp191
-rw-r--r--glgfx.h16
-rw-r--r--glgfx.obin0 -> 54880 bytes
-rw-r--r--libglgfx.abin0 -> 58890 bytes
-rw-r--r--makefile8
-rw-r--r--readme7
14 files changed, 305 insertions, 0 deletions
diff --git a/example/Terminus.ttf b/example/Terminus.ttf
new file mode 100644
index 0000000..3eb75d2
--- /dev/null
+++ b/example/Terminus.ttf
Binary files differ
diff --git a/example/fragmentshader.glsl b/example/fragmentshader.glsl
new file mode 100644
index 0000000..d15860a
--- /dev/null
+++ b/example/fragmentshader.glsl
@@ -0,0 +1,6 @@
+#version 130
+out vec4 outpix;
+void main()
+{
+ outpix = vec4(1.0, 0.0, 0.0, 0.5);
+}
diff --git a/example/main b/example/main
new file mode 100755
index 0000000..5d3327b
--- /dev/null
+++ b/example/main
Binary files differ
diff --git a/example/main.cpp b/example/main.cpp
new file mode 100644
index 0000000..0748ace
--- /dev/null
+++ b/example/main.cpp
@@ -0,0 +1,57 @@
+#include<stdio.h>
+#include<time.h>
+#include "glgfx.h"
+#include "gfx.h"
+#include "main.h"
+#include "gui.h"
+int loop;
+int rot = 0;
+float dir = 0.0f;
+int main (int argc, char * argv[]) {
+ //inits program
+ initgfx(400,600, "rotating cube");
+ initglgfx();
+ //main loop, this is defined in main.h and global
+ loop = 1;
+ while(loop) {
+ cleargfx();
+ //polls events, which are in a buffer
+ //returns 1 if there is event in buffer
+ //returns 0 if buffer is empty
+ SDL_Event event;
+ while (SDL_PollEvent(&event)) {
+ switch (event.type) {
+ //loop is a global defined in main.h
+ case SDL_QUIT:
+ printf("loop = 0;\n");
+ loop = 0;
+ break;
+
+ }
+ }
+ dir += 0.05;
+ if (dir > 360.0f) {
+ rot++;
+ rot = rot % 3;
+ dir = 0.0f;
+ }
+ switch (rot) {
+ case 0:
+ drawmodel(0.0f, 0.0f, -2.0f, dir, 0.0f, 0.0f);
+ drawmodel(0.5f, 3.0f, -10.0f, dir, 0.0f, 0.0f);
+ break;
+ case 1:
+ drawmodel(0.0f, 0.0f, -2.0f, 0.0f, dir, 0.0f);
+ drawmodel(0.5f, 3.0f, -10.0f, 0.0f, dir, 0.0f);
+ break;
+ case 2:
+ drawmodel(0.0f, 0.0f, -2.0f, 0.0f, 0.0f, dir);
+ drawmodel(0.5f, 3.0f, -10.0f, 0.0f, 0.0f, dir);
+ break;
+ }
+ updategfx();
+ }
+ quitgfx();
+ return 0;
+}
+
diff --git a/example/main.h b/example/main.h
new file mode 100644
index 0000000..e02ea4b
--- /dev/null
+++ b/example/main.h
@@ -0,0 +1,4 @@
+#ifndef MAIN_H
+#define MAIN_H
+extern int loop;
+#endif
diff --git a/example/main.o b/example/main.o
new file mode 100644
index 0000000..5b75d16
--- /dev/null
+++ b/example/main.o
Binary files differ
diff --git a/example/makefile b/example/makefile
new file mode 100644
index 0000000..578059f
--- /dev/null
+++ b/example/makefile
@@ -0,0 +1,6 @@
+main : main.o
+ g++ main.o -o main -L/home/knolax/code/cpp/gfx -lgui -lgfx -lSDL2 -lSDL2_ttf -L/home/knolax/code/cpp/glgfx -lglgfx -lGLEW -lGL
+main.o : main.cpp
+ g++ -c main.cpp -I/home/knolax/code/cpp/gfx -I/home/knolax/code/cpp/glgfx
+clean :
+ rm main.o
diff --git a/example/vertexshader.glsl b/example/vertexshader.glsl
new file mode 100644
index 0000000..4187969
--- /dev/null
+++ b/example/vertexshader.glsl
@@ -0,0 +1,10 @@
+#version 130
+uniform mat4 proj;
+uniform mat4 tran;
+uniform mat4 cam;
+in vec3 position;
+
+void main()
+{
+ gl_Position = proj * cam * tran * vec4(position, 1.0f);
+}
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() {
+}
diff --git a/glgfx.h b/glgfx.h
new file mode 100644
index 0000000..b2e9997
--- /dev/null
+++ b/glgfx.h
@@ -0,0 +1,16 @@
+#ifndef GLGFX_H
+#define GLGFX_H
+#include "GL/glew.h"
+#include "GL/gl.h"
+#include "gfx.h"
+#include <iostream>
+#include <fstream>
+#include <glm/glm.hpp>
+#include <glm/gtc/matrix_transform.hpp>
+#include <glm/gtc/type_ptr.hpp>
+int initglgfx();
+int genshaders();
+int drawmodel(float x, float y, float z, float xr, float yr, float zr);
+int quitglgfx();
+std::string readfile(const char *);
+#endif
diff --git a/glgfx.o b/glgfx.o
new file mode 100644
index 0000000..0cc6ffc
--- /dev/null
+++ b/glgfx.o
Binary files differ
diff --git a/libglgfx.a b/libglgfx.a
new file mode 100644
index 0000000..1e0dddb
--- /dev/null
+++ b/libglgfx.a
Binary files differ
diff --git a/makefile b/makefile
new file mode 100644
index 0000000..13e9f38
--- /dev/null
+++ b/makefile
@@ -0,0 +1,8 @@
+main : libglgfx.a
+libglgfx.a : glgfx.o
+ ar ru libglgfx.a glgfx.o
+ ranlib libglgfx.a
+glgfx.o : glgfx.cpp
+ g++ -c glgfx.cpp -I/home/knolax/code/cpp/gfx
+clean :
+ rm glgfx.a libglgfx.a glgfx.o
diff --git a/readme b/readme
new file mode 100644
index 0000000..0bc43e3
--- /dev/null
+++ b/readme
@@ -0,0 +1,7 @@
+in this directory are the glgfx cpp source file and headerfile.
+Use -I$thisdirectory to include the header and -L$thisdirectory for the library file along with -lglgfx.
+the makefile makes the libraries.
+The example folder contains an example program with a rotating cube.
+Both the glgfx library and the example program requires the gl/gui library.
+replace all references to /home/knolax/code/cpp/gfx with whereever the gfx library is located
+and /home/knolax/code/cpp/glgfx with the location of this directory.