Permalink
Browse files

Some work on nonrealistic rendering, shows a tree outline

  • Loading branch information...
1 parent b2f420a commit 26d8c37dba5b9d38ad12f697cd86eda4652a3e37 @kiirala committed Oct 19, 2011
Showing with 514 additions and 12 deletions.
  1. +1 −0 .gitignore
  2. +1 −1 Makefile
  3. +60 −0 common/mesh.cpp
  4. +6 −0 common/mesh.h
  5. +1 −1 common/phongmodel.cpp
  6. +12 −9 common/texturepreview.cpp
  7. +25 −0 nonrealistic/Makefile
  8. +72 −0 nonrealistic/edges.cpp
  9. +25 −0 nonrealistic/edges.h
  10. +310 −0 nonrealistic/renderer.cpp
  11. +1 −1 tool/sdlmain.cpp
View
@@ -6,3 +6,4 @@ common/common.a
*/renderer.so
gospace/gospace
depthpeeling/depthpeeling
+nonrealistic/nonrealistic
View
@@ -1,7 +1,7 @@
export GENERAL_FLAGS=-Os -g -march=native -Wall -Wextra -std=c++0x -MMD -MP
PROGRAMS=main sdlmain.o
-PROJECTS=gospace depthpeeling
+PROJECTS=gospace depthpeeling nonrealistic
COMMON=common/common.a
all: demotool $(PROGRAMS) $(PROJECTS) $(COMMON)
View
@@ -19,6 +19,14 @@ Mesh::Mesh()
colour.set(0.0, 0.0, 0.0);
}
+Mesh::Mesh(Mesh const &other)
+ : type(other.type), colour(other.colour),
+ vertices(other.vertices), texcoords(other.texcoords),
+ colours(other.colours),
+ v_array(0), col_array(0), n_array(0), texcoord_array(0)
+{
+}
+
Mesh::~Mesh() {
delete [] v_array;
delete [] n_array;
@@ -570,3 +578,55 @@ Mesh Mesh::createBall(double const radius, int const subdivisions) {
return m;
}
+
+GLuint Mesh::meshType() {
+ switch (this->type) {
+ case TRIANGLES:
+ return GL_TRIANGLES;
+ case TRIANGLE_STRIP:
+ return GL_TRIANGLE_STRIP;
+ case TRIANGLE_FAN:
+ return GL_TRIANGLE_FAN;
+ }
+ return GL_TRIANGLES;
+}
+
+void Mesh::append(Mesh const &other) {
+ assert(this->type == other.type);
+
+ if (this->type != TRIANGLES) {
+ if (this->vertices.size() > 0 && other.vertices.size() > 0) {
+ Vertex v1 = this->vertices.back();
+ Vertex v2 = other.vertices.front();
+ this->vertices.push_back(v1);
+ this->vertices.push_back(v2);
+ }
+ if (this->texcoords.size() > 0 && other.texcoords.size() > 0) {
+ Point v1 = this->texcoords.back();
+ Point v2 = other.texcoords.front();
+ this->texcoords.push_back(v1);
+ this->texcoords.push_back(v2);
+ }
+ if (this->colours.size() > 0 && other.colours.size() > 0) {
+ Colour v1 = this->colours.back();
+ Colour v2 = other.colours.front();
+ this->colours.push_back(v1);
+ this->colours.push_back(v2);
+ }
+ }
+
+ for (std::vector<Vertex>::const_iterator i = other.vertices.begin() ;
+ i != other.vertices.end() ; ++i) {
+ this->vertices.push_back(*i);
+ }
+
+ for (std::vector<Point>::const_iterator i = other.texcoords.begin() ;
+ i != other.texcoords.end() ; ++i) {
+ this->texcoords.push_back(*i);
+ }
+
+ for (std::vector<Colour>::const_iterator i = other.colours.begin() ;
+ i != other.colours.end() ; ++i) {
+ this->colours.push_back(*i);
+ }
+}
View
@@ -13,6 +13,10 @@ class Mesh {
CW, CCW
};
+ Mesh(Mesh const &other);
+
+ void append(Mesh const &other);
+
void setColour(double r, double g, double b) {
colour.set(r, g, b);
}
@@ -25,6 +29,8 @@ class Mesh {
GLfloat *normalArray();
GLfloat *texcoordArray();
+ GLuint meshType();
+
void addVertex(Point pos, Point normal);
void addColour(Colour colour);
View
@@ -68,7 +68,7 @@ void PhongModel::render(double /*time*/) {
uniform("shininess", shininess);
uniform("colour", colour);
- glDrawArrays(GL_TRIANGLES, 0, mesh.vertexCount());
+ glDrawArrays(mesh.meshType(), 0, mesh.vertexCount());
logErrors();
}
View
@@ -6,32 +6,34 @@
#include "mesh.h"
const char *vertex_shader =
-"#version 150\n"
+"#version 120\n"
"const float PI = 3.14159265;\n"
-"in vec3 in_Position;\n"
+"in vec3 in_position;\n"
"out vec2 texcoord;\n"
"uniform float size;\n"
"void main(void) {\n"
" texcoord = in_Position.xy;\n"
-" gl_Position = vec4((in_Position.xy - vec2(1.0, 0.0)) * vec2(9.0 / 16.0 * size, size) + vec2(1.0, -1.0), gl_DepthRange.near, 1.0);\n"
+" gl_Position = vec4((in_position.xy - vec2(1.0, 0.0)) * vec2(9.0 / 16.0 * size, size) + vec2(1.0, -1.0),"
+" (gl_DepthRange.near + gl_DepthRange.far) / 2.0,"
+" 1.0);\n"
"}\n"
;
const char *fragment_shader =
-"#version 150\n"
+"#version 120\n"
"const float PI = 3.14159265;\n"
"// It was expressed that some drivers required this next line to function properly\n"
-"precision highp float;\n"
+"//precision highp float;\n"
"in vec2 texcoord;\n"
-"out vec4 FragColor;\n"
+ //"out vec4 FragColor;\n"
"uniform sampler2D texture;\n"
"uniform float scale;\n"
"void main(void) {\n"
-" FragColor = vec4(texture2D(texture, texcoord).rgb * scale, 1.0);\n"
+" gl_FragColor = vec4(texture2D(texture, texcoord).rgb * scale, 1.0);\n"
"}\n"
;
@@ -48,9 +50,10 @@ void TexturePreview::render(double /*time*/) {
logErrors();
glShadeModel(GL_SMOOTH);
glDisable(GL_DEPTH_TEST);
- glBindVertexArray(vao);
- glUseProgram(shader_prog);
+ glDisable(GL_CULL_FACE);
+ render_init();
uniform_i("texture", 0);
+ glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
uniform("size", size);
uniform("scale", scale);
View
@@ -0,0 +1,25 @@
+CXXFLAGS=`pkg-config --cflags sdl` -I. -I../common $(GENERAL_FLAGS) -MMD -MP
+LDFLAGS=-lGLU -lGLEW -lSDL_sound $(GENERAL_FLAGS)
+
+STATIC=nonrealistic
+DYNOBJ=renderer.so
+DYNREQS=renderer.o edges.o
+
+all: $(STATIC) $(DYNOBJ)
+
+$(STATIC): $(DYNREQS) ../tool/sdlmain.o ../common/common.a
+ $(CXX) `pkg-config --libs sdl` $(LDFLAGS) $^ -o $@
+
+$(DYNOBJ): $(DYNREQS) ../common/common.a
+ $(CXX) -shared $(LDFLAGS) $^ -o $@
+
+$(DYNREQS): %.o: %.cpp $(IMAGES)
+ $(CXX) -fPIC -c $(CXXFLAGS) $< -o $@
+
+$(IMAGES): %.h: %.svg
+ $(MAKE) -C img
+
+-include $(DYNREQS:.o=.d)
+
+clean:
+ rm -f $(STATIC) $(DYNOBJ) $(DYNREQS) $(DYNREQS:.o=.d) *~
View
@@ -0,0 +1,72 @@
+#include <cstdio>
+#include <GL/glew.h>
+
+#include "edges.h"
+#include "globject.h"
+#include "mesh.h"
+
+const char *vertex_shader =
+"#version 120\n"
+"const float PI = 3.14159265;\n"
+"in vec3 in_Position;\n"
+"out vec2 texcoord;\n"
+
+"void main(void) {\n"
+" texcoord = in_Position.xy;\n"
+" gl_Position = vec4((in_Position.xy - vec2(1.0, 0.0)) * vec2(9.0 / 16.0, 1.0) + vec2(1.0, -1.0),"
+" (gl_DepthRange.near + gl_DepthRange.far) / 2.0,"
+" 1.0);\n"
+" gl_Position = vec4(in_Position.xy * 2.0 - vec2(1.0, 1.0), (gl_DepthRange.near + gl_DepthRange.far) / 2.0, 1.0);\n"
+"}\n"
+ ;
+
+const char *fragment_shader =
+"#version 120\n"
+"const float PI = 3.14159265;\n"
+
+"// It was expressed that some drivers required this next line to function properly\n"
+"//precision highp float;\n"
+
+"in vec2 texcoord;\n"
+"uniform sampler2D texture;\n"
+
+"const float distance = 1.0;\n"
+
+"void main(void) {\n"
+" vec2 dx = vec2(1.0 / 1280.0, 0.0) * distance;\n"
+" vec2 dy = vec2(0.0, 1.0 / 720.0) * distance;\n"
+" vec2 dd = vec2(1.0 / 1280.0, 1.0 / 720.0) * distance;\n"
+" vec4 sum = 8.0 * texture2D(texture, texcoord)\n"
+" - texture2D(texture, texcoord - dx)\n"
+" - texture2D(texture, texcoord + dx)\n"
+" - texture2D(texture, texcoord - dy)\n"
+" - texture2D(texture, texcoord + dy)\n"
+" - texture2D(texture, texcoord - dx - dy)\n"
+" - texture2D(texture, texcoord - dx + dy)\n"
+" - texture2D(texture, texcoord + dx - dy)\n"
+" - texture2D(texture, texcoord + dx + dy);\n"
+" gl_FragColor = vec4(vec3(1.0) - sum.rgb * 1.0, 1.0);\n"
+"}\n"
+;
+
+Edges::Edges()
+ : GLObject(Mesh::createSquare(),
+ vertex_shader, fragment_shader,
+ (GLObject::ShaderParams)(GLObject::POSITION | GLObject::TEXCOORD)),
+ texture(0)
+{
+
+}
+
+void Edges::render(double /*time*/) {
+ logErrors();
+ glShadeModel(GL_SMOOTH);
+ glDisable(GL_DEPTH_TEST);
+ glDisable(GL_CULL_FACE);
+ render_init();
+ uniform_i("texture", 0);
+ glActiveTexture(GL_TEXTURE0);
+ glBindTexture(GL_TEXTURE_2D, texture);
+ glDrawArrays(GL_TRIANGLES, 0, mesh.vertexCount());
+ logErrors();
+}
View
@@ -0,0 +1,25 @@
+#ifndef DEMOTOOL_EDGES_H
+#define DEMOTOOL_EDGES_H
+
+#include "mesh.h"
+#include "globject.h"
+
+class Edges : public GLObject {
+public:
+ Edges();
+
+ void render(double time);
+
+ GLuint texture;
+
+private:
+
+};
+
+
+/*
+Local Variables:
+mode:c++
+End:
+*/
+#endif /* DEMOTOOL_EDGES_H */
Oops, something went wrong.

0 comments on commit 26d8c37

Please sign in to comment.