summaryrefslogtreecommitdiff
path: root/glgfx.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'glgfx.cpp')
-rw-r--r--glgfx.cpp256
1 files changed, 162 insertions, 94 deletions
diff --git a/glgfx.cpp b/glgfx.cpp
index f3cdec9..2983391 100644
--- a/glgfx.cpp
+++ b/glgfx.cpp
@@ -25,6 +25,10 @@ GLint lightattribhandler;
GLuint vertexshaderhandle;
GLuint fragmentshaderhandle;
GLuint shaderprogramhandle;
+GLuint vertexshadowshaderhandle;
+GLuint fragmentshadowshaderhandle;
+GLuint shadowshaderprogramhandle;
+GLuint geomshadowshaderhandle;
int initglgfx() {
//glew is the library that links all of opengl's fnctions
//for you
@@ -39,24 +43,7 @@ int initglgfx() {
glCullFace(GL_BACK);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
- GLuint glnum;
- glGenBuffers(1, &glnum);
- printf("%u\n",glnum);
- //vertex data load of the box model
-/* float vertexdata[] = {
--0.500000,0.500000,-0.500000, 0.000000,0.000000,-1.000000, 0.500000,0.500000,-0.500000, 0.000000,0.000000,-1.000000, -0.500000,-0.500000,-0.500000, 0.000000,0.000000,-1.000000,
--0.500000,-0.500000,-0.500000, 0.000000,0.000000,-1.000000, 0.500000,0.500000,-0.500000, 0.000000,0.000000,-1.000000, 0.500000,-0.500000,-0.500000, 0.000000,0.000000,-1.000000,
-0.500000,0.500000,-0.500000, 1.000000,0.000000,0.000000, 0.500000,0.500000,0.500000, 1.000000,0.000000,0.000000, 0.500000,-0.500000,-0.500000, 1.000000,0.000000,0.000000,
-0.500000,-0.500000,-0.500000, 1.000000,0.000000,0.000000, 0.500000,0.500000,0.500000, 1.000000,0.000000,0.000000, 0.500000,-0.500000,0.500000, 1.000000,0.000000,0.000000,
-0.500000,0.500000,0.500000, 0.000000,0.000000,1.000000, -0.500000,0.500000,0.500000, 0.000000,0.000000,1.000000, 0.500000,-0.500000,0.500000, 0.000000,0.000000,1.000000,
--0.500000,0.500000,0.500000, 0.000000,0.000000,1.000000, -0.500000,-0.500000,0.500000, 0.000000,0.000000,1.000000, 0.500000,-0.500000,0.500000, 0.000000,0.000000,1.000000,
--0.500000,0.500000,0.500000, -1.000000,0.000000,0.000000, -0.500000,0.500000,-0.500000, -1.000000,0.000000,0.000000, -0.500000,-0.500000,0.500000, -1.000000,0.000000,0.000000,
--0.500000,0.500000,-0.500000, -1.000000,0.000000,0.000000, -0.500000,-0.500000,-0.500000, -1.000000,0.000000,0.000000, -0.500000,-0.500000,0.500000, -1.000000,0.000000,0.000000,
--0.500000,-0.500000,-0.500000, 0.000000,-1.000000,0.000000, 0.500000,-0.500000,0.500000, 0.000000,-1.000000,0.000000, -0.500000,-0.500000,0.500000, 0.000000,-1.000000,0.000000,
-0.500000,-0.500000,-0.500000, 0.000000,-1.000000,0.000000, 0.500000,-0.500000,0.500000, 0.000000,-1.000000,0.000000, -0.500000,-0.500000,-0.500000, 0.000000,-1.000000,0.000000,
--0.500000,0.500000,-0.500000, 0.000000,1.000000,0.000000, -0.500000,0.500000,0.500000, 0.000000,1.000000,0.000000, 0.500000,0.500000,0.500000, 0.000000,1.000000,0.000000,
-0.500000,0.500000,0.500000, 0.000000,1.000000,0.000000, 0.500000,0.500000,-0.500000, 0.000000,1.000000,0.000000, -0.500000,0.500000,-0.500000, 0.000000,1.000000,0.000000,
- };*/
+
float vertexdata[] = {
-0.500000,0.500000,-0.500000, 0.000000,0.000000,-1.000000, 0.000000,1.000000,0.000000, 0.500000,0.500000,-0.500000, 0.000000,0.000000,-1.000000, 1.000000,1.000000,0.000000, -0.500000,-0.500000,-0.500000, 0.000000,0.000000,-1.000000, 0.000000,0.000000,0.000000,
-0.500000,-0.500000,-0.500000, 0.000000,0.000000,-1.000000, 0.000000,0.000000,0.000000, 0.500000,0.500000,-0.500000, 0.000000,0.000000,-1.000000, 1.000000,1.000000,0.000000, 0.500000,-0.500000,-0.500000, 0.000000,0.000000,-1.000000, 1.000000,0.000000,0.000000,
@@ -71,47 +58,7 @@ int initglgfx() {
-0.500000,0.500000,-0.500000, 0.000000,1.000000,0.000000, 0.000000,0.000000,0.000000, -0.500000,0.500000,0.500000, 0.000000,1.000000,0.000000, 1.000000,0.000000,0.000000, 0.500000,0.500000,0.500000, 0.000000,1.000000,0.000000, 1.000000,1.000000,0.000000,
0.500000,0.500000,0.500000, 0.000000,1.000000,0.000000, 1.000000,1.000000,0.000000, 0.500000,0.500000,-0.500000, 0.000000,1.000000,0.000000, 0.000000,1.000000,0.000000, -0.500000,0.500000,-0.500000, 0.000000,1.000000,0.000000, 0.000000,0.000000,0.000000,
};
-/* 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,
- //normal positions
- 1.0, 0.0, 0.0,
- -1.0, 0.0, 0.0,
- 0.0, 1.0, 0.0,
- 0.0, -1.0, 0.0,
- 0.0, 0.0, 1.0,
- 0.0, 0.0, -1.0,
- };
-*/
- //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,12, 1,12, 3,12,
- 3,12, 1,12, 2,12,
-
- 1,8, 5,8, 2,8,
- 2,8, 5,8, 6,8,
-
- 5,13, 4,13, 6,13,
- 4,13, 7,13, 6,13,
-
- 4,9, 0,9, 7,9,
- 0,9, 3,9, 7,9,
-
- 3,11, 6,11, 7,11,
- 2,11, 6,11, 3,11,
- 0,10, 4,10, 5,10,
- 5,10, 1,10, 0,10,
- };
-*/
GLuint elementdata[] = {
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35
};
@@ -132,10 +79,26 @@ int initglgfx() {
glgfxloadtexture("textures/elev.bmp");
//loads the shaders
genshaders();
- glUniform1i(glGetUniformLocation(shaderprogramhandle, "tex"), 0);
- glUniform1i(glGetUniformLocation(shaderprogramhandle, "elev"), 1);
+ genshadowshaders();
}
//loads shaders and models
+GLuint loadshaderscript(const char * file, GLenum type) {
+ std::string source = readfile(file).c_str();
+ const char * shaderstr = source.c_str();
+ GLuint handle = glCreateShader(type);
+ int len = source.length();
+ glShaderSource(handle, 1, (&shaderstr), &len);
+ glCompileShader(handle);
+ char buffer[512];
+ GLint err;
+ glGetShaderiv(handle, GL_COMPILE_STATUS, &err);
+ if (err == false) {
+ glGetShaderInfoLog(handle, 512, NULL, buffer);
+ printf("error compiling shader in file : %s\n", file);
+ printf(buffer);
+ }
+ return handle;
+}
int genshaders() {
//flowchart
//vertex shader receives raw vertex data, outputs that data transformed in 3d space(camera), that data is passed to
@@ -145,40 +108,13 @@ int genshaders() {
//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
+ //---=[make the shader program]=---
+ //| |
+ //| |
+ //---------------------------------
+ GLuint vertexshaderhandle = loadshaderscript("vertexshader.glsl", GL_VERTEX_SHADER);
+ GLuint fragmentshaderhandle = loadshaderscript("fragmentshader.glsl", GL_FRAGMENT_SHADER);
+ //GLuint vertexshaderhandle = loadshaderscript("vertexshader.glsl", GL_VERTEX_SHADER);
shaderprogramhandle = glCreateProgram();
glAttachShader(shaderprogramhandle,vertexshaderhandle);
glAttachShader(shaderprogramhandle,fragmentshaderhandle);
@@ -186,6 +122,10 @@ int genshaders() {
glBindFragDataLocation(shaderprogramhandle,0,"outpix");
glLinkProgram(shaderprogramhandle);
glUseProgram(shaderprogramhandle);
+ //---=[vertex data format declaration]=---
+ //| |
+ //| |
+ //----------------------------------------
//vertex data
GLint pos;
pos = glGetAttribLocation(shaderprogramhandle, "position");
@@ -205,7 +145,105 @@ int genshaders() {
//number of bytes between values, offset from beginning in bytes
glVertexAttribPointer(texcoord, 3, GL_FLOAT, GL_FALSE, 9 * sizeof(float), (void *) (6 * sizeof(float)) );
glEnableVertexAttribArray(texcoord);
+ //---=[uniform handler loading]=---
+ //| |
+ //| |
+ //---------------------------------
+ //getting uniform handler
+ glUniform1i(glGetUniformLocation(shaderprogramhandle, "tex"), 0);
+ glUniform1i(glGetUniformLocation(shaderprogramhandle, "elev"), 1);
+ /*projhandler = glGetUniformLocation(shaderprogramhandle, "proj");
+ camhandler = glGetUniformLocation(shaderprogramhandle, "cam");
+ tranhandler = glGetUniformLocation(shaderprogramhandle, "tran");
+ rothandler = glGetUniformLocation(shaderprogramhandle, "rot");
+ lightposhandler = glGetUniformLocation(shaderprogramhandle, "lightpos");
+ camposhandler = glGetUniformLocation(shaderprogramhandle, "campos");
+ lightattribhandler = glGetUniformLocation(shaderprogramhandle, "lightattrib");*/
+}
+
+int genshadowshaders() {
+//flowchart
+//vertex shadowshader receives raw vertex data, outputs that data transformed in 3d space(camera), that data is passed to
+//geometry shadowshader, 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 shadowshader
+//which is where we do the light calculations
+//tests and blending(multiple buffers?)
+ //---=[make the shadowshader program]=---
+ //| |
+ //| |
+ //---------------------------------
+ GLuint vertexshadowshaderhandle = loadshaderscript("vertexshadowshader.glsl", GL_VERTEX_SHADER);
+ GLuint fragmentshadowshaderhandle = loadshaderscript("fragmentshadowshader.glsl", GL_FRAGMENT_SHADER);
+ //GLuint vertexshadowshaderhandle = loadshaderscript("vertexshader.glsl", GL_VERTEX_SHADER);
+ shadowshaderprogramhandle = glCreateProgram();
+ glAttachShader(shadowshaderprogramhandle,vertexshadowshaderhandle);
+ glAttachShader(shadowshaderprogramhandle,fragmentshadowshaderhandle);
+ //the 0 is which buffer, 0 is the default, 1 will be fore volumetric shading
+ glBindFragDataLocation(shadowshaderprogramhandle,0,"outpix");
+ glLinkProgram(shadowshaderprogramhandle);
+ glUseProgram(shadowshaderprogramhandle);
+ //---=[vertex data format declaration]=---
+ //| |
+ //| |
+ //----------------------------------------
+ //vertex data
+ GLint pos;
+ pos = glGetAttribLocation(shadowshaderprogramhandle, "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, 9 * sizeof(float), 0);
+ glEnableVertexAttribArray(pos);
+ GLint vnorm;
+ vnorm = glGetAttribLocation(shadowshaderprogramhandle, "vnormal");
+ //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(vnorm, 3, GL_FLOAT, GL_FALSE, 9 * sizeof(float), (void *) (3 * sizeof(float)) );
+ glEnableVertexAttribArray(vnorm);
+ GLint texcoord;
+ texcoord = glGetAttribLocation(shadowshaderprogramhandle, "texcoord");
+ //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(texcoord, 3, GL_FLOAT, GL_FALSE, 9 * sizeof(float), (void *) (6 * sizeof(float)) );
+ glEnableVertexAttribArray(texcoord);
+ //---=[uniform handler loading]=---
+ //| |
+ //| |
+ //---------------------------------
//getting uniform handler
+ glUniform1i(glGetUniformLocation(shadowshaderprogramhandle, "tex"), 0);
+ glUniform1i(glGetUniformLocation(shadowshaderprogramhandle, "elev"), 1);
+ /*projhandler = glGetUniformLocation(shadowshaderprogramhandle, "proj");
+ camhandler = glGetUniformLocation(shadowshaderprogramhandle, "cam");
+ tranhandler = glGetUniformLocation(shadowshaderprogramhandle, "tran");
+ rothandler = glGetUniformLocation(shadowshaderprogramhandle, "rot");
+ lightposhandler = glGetUniformLocation(shadowshaderprogramhandle, "lightpos");
+ camposhandler = glGetUniformLocation(shadowshaderprogramhandle, "campos");
+ lightattribhandler = glGetUniformLocation(shadowshaderprogramhandle, "lightattrib");*/
+}
+int useregshaders () {
+ glUseProgram(shaderprogramhandle);
+ /*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, 9 * sizeof(float), 0);
+ glEnableVertexAttribArray(pos);
+ GLint vnorm;
+ vnorm = glGetAttribLocation(shaderprogramhandle, "vnormal");
+ //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(vnorm, 3, GL_FLOAT, GL_FALSE, 9 * sizeof(float), (void *) (3 * sizeof(float)) );
+ glEnableVertexAttribArray(vnorm);
+ GLint texcoord;
+ texcoord = glGetAttribLocation(shaderprogramhandle, "texcoord");
+ //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(texcoord, 3, GL_FLOAT, GL_FALSE, 9 * sizeof(float), (void *) (6 * sizeof(float)) );
+ glEnableVertexAttribArray(texcoord); */
+ //glUniform1i(glGetUniformLocation(shaderprogramhandle, "tex"), 0);
+ //glUniform1i(glGetUniformLocation(shaderprogramhandle, "elev"), 1);
projhandler = glGetUniformLocation(shaderprogramhandle, "proj");
camhandler = glGetUniformLocation(shaderprogramhandle, "cam");
tranhandler = glGetUniformLocation(shaderprogramhandle, "tran");
@@ -214,6 +252,36 @@ int genshaders() {
camposhandler = glGetUniformLocation(shaderprogramhandle, "campos");
lightattribhandler = glGetUniformLocation(shaderprogramhandle, "lightattrib");
}
+int useshadowshaders() {
+ glUseProgram(shadowshaderprogramhandle);
+ /*GLint pos;
+ pos = glGetAttribLocation(shadowshaderprogramhandle, "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, 9 * sizeof(float), 0);
+ glEnableVertexAttribArray(pos);
+ GLint vnorm;
+ vnorm = glGetAttribLocation(shadowshaderprogramhandle, "vnormal");
+ //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(vnorm, 3, GL_FLOAT, GL_FALSE, 9 * sizeof(float), (void *) (3 * sizeof(float)) );
+ glEnableVertexAttribArray(vnorm);
+ GLint texcoord;
+ texcoord = glGetAttribLocation(shadowshaderprogramhandle, "texcoord");
+ //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(texcoord, 3, GL_FLOAT, GL_FALSE, 9 * sizeof(float), (void *) (6 * sizeof(float)) );
+ glEnableVertexAttribArray(texcoord);*/
+ //glUniform1i(glGetUniformLocation(shadowshaderprogramhandle, "tex"), 0);
+ //glUniform1i(glGetUniformLocation(shadowshaderprogramhandle, "elev"), 1);
+ projhandler = glGetUniformLocation(shadowshaderprogramhandle, "proj");
+ camhandler = glGetUniformLocation(shadowshaderprogramhandle, "cam");
+ tranhandler = glGetUniformLocation(shadowshaderprogramhandle, "tran");
+ rothandler = glGetUniformLocation(shadowshaderprogramhandle, "rot");
+ lightposhandler = glGetUniformLocation(shadowshaderprogramhandle, "lightpos");
+ camposhandler = glGetUniformLocation(shadowshaderprogramhandle, "campos");
+ lightattribhandler = glGetUniformLocation(shadowshaderprogramhandle, "lightattrib");
+}
//opens a filebuffer stream and reads every char
std::string readfile(char const * filename) {
std::ifstream filebuffer;