summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorknolax <1339802534.kk@gmail.com>2017-02-22 20:43:47 -0500
committerknolax <1339802534.kk@gmail.com>2017-02-22 20:43:47 -0500
commitc7bdd6fcae58e09597071405c1449df64e084c51 (patch)
treec9ca26278a7a86725aa2e5af7e1a330c89265137
parentd137bf2485050b54bb0ac65ef543ba24f18fabbe (diff)
implemented phoung shading and surrounding infrastructure, gen.cpp compiles a simple program that creates raw vertex data from vertexes in a way that surppassess the limitations of opengl's native elements[] protocol. next step is to allow for z distance based override and multi draw;
-rw-r--r--example/fragmentshader.glsl21
-rwxr-xr-xexample/mainbin49192 -> 49488 bytes
-rw-r--r--example/main.cpp2
-rw-r--r--example/main.obin4424 -> 4752 bytes
-rw-r--r--example/vertexshader.glsl10
-rw-r--r--gen.cpp55
-rwxr-xr-xgenvertexbin0 -> 8496 bytes
-rw-r--r--glgfx.cpp114
-rw-r--r--glgfx.h2
-rw-r--r--glgfx.obin54880 -> 57920 bytes
-rw-r--r--libglgfx.abin58890 -> 62084 bytes
11 files changed, 175 insertions, 29 deletions
diff --git a/example/fragmentshader.glsl b/example/fragmentshader.glsl
index d15860a..70c55b5 100644
--- a/example/fragmentshader.glsl
+++ b/example/fragmentshader.glsl
@@ -1,6 +1,25 @@
#version 130
out vec4 outpix;
+uniform vec3 campos;
+uniform vec3 lightpos;
+//varing blends them it is transformed to world space
+varying vec4 fragpos;
+varying vec4 fragnormal;
void main()
{
- outpix = vec4(1.0, 0.0, 0.0, 0.5);
+ vec3 lightnormal = normalize((lightpos - vec3(fragpos.x, fragpos.y, fragpos.z)));
+ float diffuse = dot(lightnormal, vec3(fragnormal.x, fragnormal.y, fragnormal.z));
+ if (diffuse < 0.0) {
+ diffuse = 0.0;
+ }
+ vec3 camnormal = normalize(campos - vec3(fragpos.x, fragpos.y, fragpos.z));
+ float reflect = dot(camnormal, fragnormal.xyz);
+ diffuse = pow(diffuse, 2.0);
+ reflect = pow(reflect, 2.0);
+ float light = .4 * reflect + .6 * diffuse;
+ //this should be the vertex in camera space without the w value
+ //outpix = gl_FragCoord;
+ outpix = vec4(light * 1.0,light * 1.0,light * 1.0, 1.0);
+ //outpix = vec4(fragpos.x ,fragpos.y ,fragpos.z, 0.5);
+ //outpix = vec4(fragnormal.x, fragnormal.y, fragnormal.z, 0.5);
}
diff --git a/example/main b/example/main
index 5d3327b..e4895a1 100755
--- a/example/main
+++ b/example/main
Binary files differ
diff --git a/example/main.cpp b/example/main.cpp
index 0748ace..05777a4 100644
--- a/example/main.cpp
+++ b/example/main.cpp
@@ -11,6 +11,8 @@ int main (int argc, char * argv[]) {
//inits program
initgfx(400,600, "rotating cube");
initglgfx();
+ setlight(0,10,10);
+ setcam(0.0,0.0,1.0, 0.0,0.0,0.0, 0.0,1.0,0.0);
//main loop, this is defined in main.h and global
loop = 1;
while(loop) {
diff --git a/example/main.o b/example/main.o
index 5b75d16..37c8220 100644
--- a/example/main.o
+++ b/example/main.o
Binary files differ
diff --git a/example/vertexshader.glsl b/example/vertexshader.glsl
index 4187969..f60ec9f 100644
--- a/example/vertexshader.glsl
+++ b/example/vertexshader.glsl
@@ -2,9 +2,15 @@
uniform mat4 proj;
uniform mat4 tran;
uniform mat4 cam;
+uniform mat4 rot;
in vec3 position;
-
+in vec3 vnormal;
+varying vec4 fragpos;
+varying vec4 fragnormal; // if this needs to not be blended jsut have all vertexes in a poly be the same
void main()
{
- gl_Position = proj * cam * tran * vec4(position, 1.0f);
+ //this is the actual position
+ gl_Position = proj * cam * tran * vec4(position, 1.0f);// - vec4(gl_Normal, 1.0f);
+ fragpos = tran * vec4(position, 1.0f);
+ fragnormal = (tran * vec4(vnormal, 1.0f)) - (tran * vec4(0.0, 0.0, 0.0, 1.0));
}
diff --git a/gen.cpp b/gen.cpp
new file mode 100644
index 0000000..167d9e7
--- /dev/null
+++ b/gen.cpp
@@ -0,0 +1,55 @@
+#include <stdio.h>
+int main () {
+ 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
+ int elementdata[] = {
+ 0,13, 1,13, 3,13,
+ 3,13, 1,13, 2,13,
+
+ 1,8, 5,8, 2,8,
+ 2,8, 5,8, 6,8,
+
+ 5,12, 4,12, 6,12,
+ 4,12, 7,12, 6,12,
+
+ 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,
+ };
+ int i = 0;
+ printf("float vertexdata[] = {\n");
+ while (i < 72) {
+ printf("%f,%f,%f, ",vertexdata[(elementdata[i] * 3)],vertexdata[(elementdata[i] * 3) + 1],vertexdata[(elementdata[i] * 3 )+ 2]);
+ if (((i + 1) % 6) == 0) {
+ printf("\n");
+ } else if (((i + 1) % 2) == 0) {
+ printf("\t");
+ }
+ i++;
+ }
+ printf("};");
+ return 0;
+}
diff --git a/genvertex b/genvertex
new file mode 100755
index 0000000..d34e017
--- /dev/null
+++ b/genvertex
Binary files differ
diff --git a/glgfx.cpp b/glgfx.cpp
index 6700601..4dd25ec 100644
--- a/glgfx.cpp
+++ b/glgfx.cpp
@@ -6,8 +6,10 @@ GLuint ebhandler;
//handlers for the perspective transforms
GLint projhandler;
GLint tranhandler;
+GLint rothandler;
GLint camhandler;
-glm::mat4 tran;
+GLint camposhandler;
+GLint lightposhandler;
//device coordinates
// ^
// (y+)
@@ -38,6 +40,20 @@ int initglgfx() {
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.5, 0.5, -.5, // x y z
0.5, 0.5, -.5,
0.5, -0.5, -.5,
@@ -45,24 +61,41 @@ int initglgfx() {
-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
+ -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,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,
+ 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
};
//binds to the element buffer
glGenBuffers(1,&vbhandler);
@@ -131,9 +164,21 @@ int genshaders() {
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);
+ glVertexAttribPointer(pos, 3, GL_FLOAT, GL_FALSE, 6 * 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, 6 * sizeof(float), (void *) (3 * sizeof(float)) );
+ glEnableVertexAttribArray(vnorm);
+ //getting uniform handler
+ projhandler = glGetUniformLocation(shaderprogramhandle, "proj");
+ camhandler = glGetUniformLocation(shaderprogramhandle, "cam");
+ tranhandler = glGetUniformLocation(shaderprogramhandle, "tran");
+ rothandler = glGetUniformLocation(shaderprogramhandle, "rot");
+ lightposhandler = glGetUniformLocation(shaderprogramhandle, "lightpos");
+ camposhandler = glGetUniformLocation(shaderprogramhandle, "campos");
}
//opens a filebuffer stream and reads every char
std::string readfile(char const * filename) {
@@ -152,6 +197,18 @@ std::string readfile(char const * filename) {
filebuffer.close();
return rstr;
}
+int setcam (float cpx, float cpy, float cpz, float clx, float cly, float clz, float cux, float cuy, float cuz) {
+ //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));
+ glm::mat4 cam = glm::lookAt(glm::vec3(cpx, cpy, cpz), glm::vec3(clx, cly, clz), glm::vec3(cux, cuy, cuz));
+ glUniformMatrix4fv(camhandler, 1, GL_FALSE, glm::value_ptr(cam));
+ glUniform3fv(camposhandler, 1, glm::value_ptr(glm::vec3(cpx,cpy,cpz)));
+ return 0;
+}
+int setlight (float x, float y, float z) {
+ glUniform3fv(camposhandler, 1, glm::value_ptr(glm::vec3(x,y,z)));
+ return 0;
+}
//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) {
@@ -159,12 +216,8 @@ int drawmodel(float x, float y, float z, float xr, float yr, float zr) {
//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
@@ -178,14 +231,23 @@ int drawmodel(float x, float y, float z, float xr, float yr, float zr) {
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));
+ glm::mat4 rot;
+ tran = glm::translate(tran, glm::vec3(0,0,0));
+ rot = glm::rotate(rot,glm::radians(xr),glm::vec3(1,0,0)); // axis of rotation x y z
+ rot = glm::rotate(rot,glm::radians(yr),glm::vec3(0,1,0));
+ rot = glm::rotate(rot,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);
+ //printf("translated vector x%f\n",(tran * glm::vec4(1.0f,0.0f,0.0f,1.0f)).x);
+ //printf("translated vector y%f\n",(tran * glm::vec4(0.0f,1.0f,0.0f,1.0f)).y);
+ //printf("translated vector z%f\n",(tran * glm::vec4(0.0f,0.0f,1.0f,1.0f)).z);
+ //printf("translated vector x%f\n",(rot * glm::vec4(1.0f,0.0f,0.0f,1.0f)).x);
+ //printf("translated vector y%f\n",(rot * glm::vec4(0.0f,1.0f,0.0f,1.0f)).y);
+ //printf("translated vector z%f\n",(rot * glm::vec4(0.0f,0.0f,1.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);
+ glUniformMatrix4fv(rothandler, 1, GL_FALSE, glm::value_ptr(rot));
+ //glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);
+ glDrawArrays(GL_TRIANGLES,0,36);
}
int quitglgfx() {
}
diff --git a/glgfx.h b/glgfx.h
index b2e9997..0cb1ff9 100644
--- a/glgfx.h
+++ b/glgfx.h
@@ -11,6 +11,8 @@
int initglgfx();
int genshaders();
int drawmodel(float x, float y, float z, float xr, float yr, float zr);
+int setcam (float cpx, float cpy, float cpz, float clx, float cly, float clz, float cux, float cuy, float cuz);
+int setlight (float x, float y, float z);
int quitglgfx();
std::string readfile(const char *);
#endif
diff --git a/glgfx.o b/glgfx.o
index 0cc6ffc..292e2d8 100644
--- a/glgfx.o
+++ b/glgfx.o
Binary files differ
diff --git a/libglgfx.a b/libglgfx.a
index 1e0dddb..94ff8f8 100644
--- a/libglgfx.a
+++ b/libglgfx.a
Binary files differ