Skip to content

Commit

Permalink
Update chapters 8 and 9.
Browse files Browse the repository at this point in the history
  • Loading branch information
David Wolff committed May 20, 2013
1 parent f364ad0 commit 97bbacf
Show file tree
Hide file tree
Showing 21 changed files with 331 additions and 267 deletions.
52 changes: 52 additions & 0 deletions chapter08/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
OBJDIR = obj
TARGETDIR = .
BASEDIR = ..
TARGET = $(TARGETDIR)/chapter08
DEFINES += -D_CRT_SECURE_NO_WARNINGS -D_SCL_SECURE_NO_WARNINGS -D_DEBUG
INCLUDES += -I$(BASEDIR)/ingredients -I$(GLFW_INCLUDE) -I$(GLM_DIR)
CPPFLAGS += -MMD -MP $(DEFINES) $(INCLUDES)
CFLAGS += $(CPPFLAGS) $(ARCH) -g
CXXFLAGS += $(CFLAGS)
LDFLAGS += -L$(BASEDIR)/ingredients -L$(GLFW_DIR)/lib
LIBS += -pthread -lglfw -lrt -lingredients -lGL
RESFLAGS += $(DEFINES) $(INCLUDES)
LDDEPS +=
LINKCMD = $(CXX) -o $(TARGET) $(OBJS) $(LDFLAGS) $(ARCH) $(LIBS)

OBJECTS := \
noisetex.o \
main.o \
scenesky.o \
scenedecay.o \
scenenightvision.o \
scenepaint.o \
scenewood.o

OBJS := $(addprefix $(OBJDIR)/, $(OBJECTS))

.PHONY: clean all

all: $(TARGET)
@:

$(TARGET): $(OBJS) $(LDDEPS) | $(TARGETDIR)
@echo Linking chapter08
$(SILENT) $(LINKCMD)
$(POSTBUILDCMDS)

$(TARGETDIR):
@echo Creating $(TARGETDIR)
$(SILENT) mkdir -p $(TARGETDIR)

$(OBJDIR):
@echo Creating $(OBJDIR)
$(SILENT) mkdir -p $(OBJDIR)

clean:
@echo Cleaning chapter08
$(SILENT) rm -f $(TARGET)
$(SILENT) rm -rf $(OBJDIR)

$(OBJS) : $(OBJDIR)/%.o : %.cpp | $(OBJDIR)
@echo Compiling $(notdir $<)
$(SILENT) $(CXX) $(CXXFLAGS) -o "$@" -c "$<"
112 changes: 86 additions & 26 deletions chapter08/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "cookbookogl.h"
#include <glload/gll.hpp>
#include <GL/freeglut.h>
#include <GL/glfw.h>

#include "glutils.h"
#include "scenedecay.h"
Expand All @@ -9,24 +8,26 @@
#include "scenesky.h"
#include "scenewood.h"

Scene *scene;

void initializeGL() {
#define WIN_WIDTH 800
#define WIN_HEIGHT 600

//////////////// PLUG IN SCENE HERE /////////////////
scene = new SceneWood();
////////////////////////////////////////////////////
Scene *scene;

GLUtils::dumpGLInfo();
string parseCLArgs(int argc, char ** argv);
void printHelpInfo(const char *);

void initializeGL() {
glClearColor(0.5f,0.5f,0.5f,1.0f);
scene->initScene();
}

void paintGL() {
GLUtils::checkForOpenGLError(__FILE__,__LINE__);
scene->render();
glutSwapBuffers();
void mainLoop() {
while( glfwGetWindowParam(GLFW_OPENED) && !glfwGetKey(GLFW_KEY_ESC) ) {
GLUtils::checkForOpenGLError(__FILE__,__LINE__);
scene->update(glfwGetTime());
scene->render();
glfwSwapBuffers();
}
}

void resizeGL(int w, int h ) {
Expand All @@ -35,23 +36,82 @@ void resizeGL(int w, int h ) {

int main(int argc, char *argv[])
{
glutInit(&argc, argv);
string recipe = parseCLArgs(argc, argv);

// Initialize GLFW
if( !glfwInit() ) exit( EXIT_FAILURE );

// Select OpenGL 3.2 with a forward compatible core profile.
glfwOpenWindowHint( GLFW_OPENGL_VERSION_MAJOR, 4 );
glfwOpenWindowHint( GLFW_OPENGL_VERSION_MINOR, 3 );
glfwOpenWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwOpenWindowHint(GLFW_WINDOW_NO_RESIZE, GL_TRUE);
glfwOpenWindowHint(GLFW_FSAA_SAMPLES, 8);

// Open the window
if( !glfwOpenWindow( WIN_WIDTH, WIN_HEIGHT, 8,8,8,8,24,0, GLFW_WINDOW ) ) {
glfwTerminate();
exit( EXIT_FAILURE );
}
string title = "Chapter 8 -- " + recipe;
glfwSetWindowTitle(title.c_str());

glutInitDisplayMode(GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH | GLUT_STENCIL );
glutInitContextVersion (4, 0);
glutInitContextProfile(GLUT_CORE_PROFILE);

glutInitWindowSize (800, 600);
glutInitWindowPosition (300, 200);
glutCreateWindow (argv[0]);
// Load the OpenGL functions.
if( ogl_LoadFunctions() == ogl_LOAD_FAILED ) {
glfwTerminate();
exit(EXIT_FAILURE);
}

glload::LoadFunctions();
GLUtils::dumpGLInfo();

// Initialization
initializeGL();
glutDisplayFunc(paintGL);
glutReshapeFunc(resizeGL);
resizeGL(WIN_WIDTH,WIN_HEIGHT);

glutMainLoop();
return 0;
// Enter the main loop
mainLoop();

// Close window and terminate GLFW
glfwTerminate();
// Exit program
exit( EXIT_SUCCESS );
}

string parseCLArgs(int argc, char ** argv) {

if( argc < 2 ) {
printHelpInfo(argv[0]);
exit(EXIT_FAILURE);
}

string recipe = argv[1];

if( recipe == "decay" ) {
scene = new SceneDecay();
} else if( recipe == "night-vision") {
scene = new SceneNightVision();
} else if( recipe == "paint") {
scene = new ScenePaint();
} else if( recipe == "sky" ) {
scene = new SceneSky();
} else if( recipe == "wood" ) {
scene = new SceneWood();
} else {
printf("Unknown recipe: %s\n", recipe.c_str());
printHelpInfo(argv[0]);
exit(EXIT_FAILURE);
}

return recipe;
}

void printHelpInfo(const char * exeFile) {
printf("Usage: %s recipe-name\n\n", exeFile);
printf("Recipe names: \n");
printf(" decay : description...\n");
printf(" night-vision : description...\n");
printf(" paint : description...\n");
printf(" sky : description...\n");
printf(" wood : description...\n");
}
61 changes: 29 additions & 32 deletions chapter08/noisetex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@
#include <glm/glm.hpp>
#include <glm/gtc/noise.hpp>

int NoiseTex::generate2DTex(bool seamless, float baseFreq, int w, int h) {
int NoiseTex::generate2DTex(bool seamless, float baseFreq, float persistence, int w, int h) {

int width = w;
int height = h;

// Base frequency for octave 1.
//perlinNoise.SetFrequency(baseFreq);

printf("Generating noise texture...");

GLubyte *data = new GLubyte[ width * height * 4 ];
Expand All @@ -21,34 +18,34 @@ int NoiseTex::generate2DTex(bool seamless, float baseFreq, int w, int h) {
double yRange = 1.0;
double xFactor = xRange / width;
double yFactor = yRange / height;
for( int oct = 0; oct < 4; oct++ ) {
// Do something about octaves here

for( int i = 0; i < width; i++ ) {
for( int j = 0 ; j < height; j++ ) {

glm::vec2 p(xFactor * i * 16.0f, yFactor * j * 16.0f);

float val = 0.0f;
if( !seamless ) {
val = glm::perlin(p);
} else {
val = glm::perlin(p, glm::vec2(16.0f));
}

// Scale to roughly between 0 and 1
//val = (val + 1.0f) * 0.5f;

// Clamp strictly between 0 and 1
//val = val > 1.0f ? 1.0f : val;
//val = val < 0.0f ? 0.0f : val;

// Store in texture
data[((j * width + i) * 4) + oct] = (GLubyte) ( val * 255.0f );
}
}
}

for( int i = 0; i < width; i++ ) {
for( int j = 0 ; j < height; j++ ) {
float x = xFactor * i;
float y = yFactor * j;
float sum = 0.0f;
float freq = baseFreq;
float persist = persistence;
for( int oct = 0; oct < 4; oct++ ) {
glm::vec2 p(x * freq, y * freq);

float val = glm::perlin(p) * persist;

sum += val;

float result = sum + 0.5;

// Clamp strictly between 0 and 1
result = result > 1.0f ? 1.0f : result;
result = result < 0.0f ? 0.0f : result;

// Store in texture
data[((j * width + i) * 4) + oct] = (GLubyte) ( result * 255.0f );
freq *= 2.0f;
persist *= persistence;
}
}
}

GLuint texID;
glGenTextures(1, &texID);
Expand Down
2 changes: 1 addition & 1 deletion chapter08/noisetex.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace NoiseTex
{
int generate2DTex(bool seamless = false, float baseFreq = 4.0f, int w = 128, int h = 128);
int generate2DTex(bool seamless = false, float baseFreq = 4.0f, float persistence = 0.5f, int w = 128, int h = 128);
};

#endif // NOISETEX_H
2 changes: 1 addition & 1 deletion chapter08/scenenightvision.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ void SceneNightVision::initScene()
prog.setUniform("RenderTex", 0);
prog.setUniform("Light.Intensity", vec3(1.0f,1.0f,1.0f) );

noiseTex = NoiseTex::generate2DTex(true, 100.0f, 512, 512);
noiseTex = NoiseTex::generate2DTex(true, 200.0f, 0.5f, 512, 512);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, noiseTex);
prog.setUniform("NoiseTex", 1);
Expand Down
3 changes: 0 additions & 3 deletions chapter08/scenesky.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,6 @@ void SceneSky::drawScene()
void SceneSky::setMatrices()
{
mat4 mv = view * model;
//prog.setUniform("ModelViewMatrix", mv);
//prog.setUniform("NormalMatrix",
// mat3( vec3(mv[0]), vec3(mv[1]), vec3(mv[2]) ));
prog.setUniform("MVP", projection * mv);
}

Expand Down
3 changes: 0 additions & 3 deletions chapter08/scenewood.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,6 @@ void SceneWood::drawScene()
void SceneWood::setMatrices()
{
mat4 mv = view * model;
//prog.setUniform("ModelViewMatrix", mv);
//prog.setUniform("NormalMatrix",
// mat3( vec3(mv[0]), vec3(mv[1]), vec3(mv[2]) ));
prog.setUniform("MVP", projection * mv);
}

Expand Down
2 changes: 1 addition & 1 deletion chapter08/shader/nightvision.fs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ vec4 pass2()
float dist2 = length(gl_FragCoord.xy - vec2(3.0 * Width/4.0, Height/2.0));
if( dist1 > Radius && dist2 > Radius ) green = 0.0;

return vec4(0.0, green * clamp( noise.a + 0.25, 0.0, 1.0) , 0.0 ,1.0);
return vec4(0.0, green * clamp( noise.a, 0.0, 1.0) , 0.0 ,1.0);
}

void main()
Expand Down
3 changes: 2 additions & 1 deletion chapter08/shader/sky.fs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ void main()
{
vec4 noise = texture(NoiseTex, TexCoord);

float t = (cos( noise.g * PI ) + 1.0) / 2.0;
float t = (cos( noise.a * PI ) + 1.0) / 2.0;
// t = clamp(t, 0.0, 1.0);

vec4 color = mix( SkyColor, CloudColor, t );

FragColor = vec4( color.rgb , 1.0 );
//FragColor = vec4( noise.a, noise.a, noise.a, 1 );
}
52 changes: 52 additions & 0 deletions chapter09/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
OBJDIR = obj
TARGETDIR = .
BASEDIR = ..
TARGET = $(TARGETDIR)/chapter09
DEFINES += -D_CRT_SECURE_NO_WARNINGS -D_SCL_SECURE_NO_WARNINGS -D_DEBUG
INCLUDES += -I$(BASEDIR)/ingredients -I$(GLFW_INCLUDE) -I$(GLM_DIR)
CPPFLAGS += -MMD -MP $(DEFINES) $(INCLUDES)
CFLAGS += $(CPPFLAGS) $(ARCH) -g
CXXFLAGS += $(CFLAGS)
LDFLAGS += -L$(BASEDIR)/ingredients -L$(GLFW_DIR)/lib
LIBS += -pthread -lglfw -lrt -lingredients -lGL
RESFLAGS += $(DEFINES) $(INCLUDES)
LDDEPS +=
LINKCMD = $(CXX) -o $(TARGET) $(OBJS) $(LDFLAGS) $(ARCH) $(LIBS)

OBJECTS := \
main.o \
scenefire.o \
sceneparticles.o \
sceneparticlesfeedback.o \
sceneparticlesinstanced.o \
scenesmoke.o \
scenewave.o

OBJS := $(addprefix $(OBJDIR)/, $(OBJECTS))

.PHONY: clean all

all: $(TARGET)
@:

$(TARGET): $(OBJS) $(LDDEPS) | $(TARGETDIR)
@echo Linking chapter09
$(SILENT) $(LINKCMD)
$(POSTBUILDCMDS)

$(TARGETDIR):
@echo Creating $(TARGETDIR)
$(SILENT) mkdir -p $(TARGETDIR)

$(OBJDIR):
@echo Creating $(OBJDIR)
$(SILENT) mkdir -p $(OBJDIR)

clean:
@echo Cleaning chapter09
$(SILENT) rm -f $(TARGET)
$(SILENT) rm -rf $(OBJDIR)

$(OBJS) : $(OBJDIR)/%.o : %.cpp | $(OBJDIR)
@echo Compiling $(notdir $<)
$(SILENT) $(CXX) $(CXXFLAGS) -o "$@" -c "$<"
Loading

0 comments on commit 97bbacf

Please sign in to comment.