diff --git a/chapter08/Makefile b/chapter08/Makefile new file mode 100644 index 0000000..38191a5 --- /dev/null +++ b/chapter08/Makefile @@ -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 "$<" diff --git a/chapter08/main.cpp b/chapter08/main.cpp index c7397c1..72cd482 100644 --- a/chapter08/main.cpp +++ b/chapter08/main.cpp @@ -1,6 +1,5 @@ #include "cookbookogl.h" -#include -#include +#include #include "glutils.h" #include "scenedecay.h" @@ -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 ) { @@ -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"); +} diff --git a/chapter08/noisetex.cpp b/chapter08/noisetex.cpp index e1479ce..ab374aa 100644 --- a/chapter08/noisetex.cpp +++ b/chapter08/noisetex.cpp @@ -5,14 +5,11 @@ #include #include -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 ]; @@ -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); diff --git a/chapter08/noisetex.h b/chapter08/noisetex.h index 67e61b0..577f1c6 100644 --- a/chapter08/noisetex.h +++ b/chapter08/noisetex.h @@ -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 diff --git a/chapter08/scenenightvision.cpp b/chapter08/scenenightvision.cpp index a217641..a2897de 100644 --- a/chapter08/scenenightvision.cpp +++ b/chapter08/scenenightvision.cpp @@ -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); diff --git a/chapter08/scenesky.cpp b/chapter08/scenesky.cpp index 3f03617..34fb33b 100644 --- a/chapter08/scenesky.cpp +++ b/chapter08/scenesky.cpp @@ -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); } diff --git a/chapter08/scenewood.cpp b/chapter08/scenewood.cpp index b54def9..37bb171 100644 --- a/chapter08/scenewood.cpp +++ b/chapter08/scenewood.cpp @@ -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); } diff --git a/chapter08/shader/nightvision.fs b/chapter08/shader/nightvision.fs index 7164612..36ece69 100644 --- a/chapter08/shader/nightvision.fs +++ b/chapter08/shader/nightvision.fs @@ -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() diff --git a/chapter08/shader/sky.fs b/chapter08/shader/sky.fs index d121db8..7b87b6f 100644 --- a/chapter08/shader/sky.fs +++ b/chapter08/shader/sky.fs @@ -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 ); } diff --git a/chapter09/Makefile b/chapter09/Makefile new file mode 100644 index 0000000..e9da3a2 --- /dev/null +++ b/chapter09/Makefile @@ -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 "$<" diff --git a/chapter09/animtimer.cpp b/chapter09/animtimer.cpp deleted file mode 100644 index b963d17..0000000 --- a/chapter09/animtimer.cpp +++ /dev/null @@ -1,28 +0,0 @@ -#include "animtimer.h" - -AnimTimer::AnimTimer() : timeLastStop(0.0), clockLastStart(0.0) -{ - -} - -void AnimTimer::start() -{ - clockLastStart = getClock(); -} - -void AnimTimer::stop() -{ - timeLastStop = curTime(); -} - -double AnimTimer::curTime() -{ - double t = getClock(); - return timeLastStop + (t - clockLastStart); -} - -double AnimTimer::getClock() -{ - static double clockFac = 1.0 / CLOCKS_PER_SEC; - return clock() * clockFac; -} diff --git a/chapter09/animtimer.h b/chapter09/animtimer.h deleted file mode 100644 index 0461cb5..0000000 --- a/chapter09/animtimer.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef ANIMTIMER_H -#define ANIMTIMER_H - -#include - -class AnimTimer -{ -private: - double timeLastStop; - double clockLastStart; - - double getClock(); - -public: - AnimTimer(); - - void start(); - void stop(); - double curTime(); -}; - -#endif // ANIMTIMER_H diff --git a/chapter09/main.cpp b/chapter09/main.cpp index 389d9ba..42df772 100644 --- a/chapter09/main.cpp +++ b/chapter09/main.cpp @@ -1,9 +1,7 @@ #include "cookbookogl.h" -#include -#include +#include #include "glutils.h" -#include "animtimer.h" #include "scenefire.h" #include "sceneparticles.h" #include "sceneparticlesfeedback.h" @@ -11,56 +9,113 @@ #include "scenesmoke.h" #include "scenewave.h" -Scene *scene; -AnimTimer animTimer; - -void initializeGL() { +#define WIN_WIDTH 800 +#define WIN_HEIGHT 600 - //////////////// PLUG IN SCENE HERE ///////////////// - scene = new SceneWave(); - //////////////////////////////////////////////////// +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 ) { scene->resize(w,h); } -void updateGL() { - scene->update(animTimer.curTime()); - glutPostRedisplay(); -} - 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); - 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]); + // Open the window + if( !glfwOpenWindow( WIN_WIDTH, WIN_HEIGHT, 8,8,8,8,24,0, GLFW_WINDOW ) ) { + glfwTerminate(); + exit( EXIT_FAILURE ); + } + string title = "Chapter 9 -- " + recipe; + glfwSetWindowTitle(title.c_str()); - glload::LoadFunctions(); + // Load the OpenGL functions. + if( ogl_LoadFunctions() == ogl_LOAD_FAILED ) { + glfwTerminate(); + exit(EXIT_FAILURE); + } + GLUtils::dumpGLInfo(); + + // Initialization initializeGL(); - glutDisplayFunc(paintGL); - glutReshapeFunc(resizeGL); - glutIdleFunc(updateGL); + resizeGL(WIN_WIDTH,WIN_HEIGHT); + + // Enter the main loop + mainLoop(); - glutMainLoop(); - return 0; + // 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 == "fire" ) { + scene = new SceneFire(); + } else if( recipe == "particles") { + scene = new SceneParticles(); + } else if( recipe == "particles-feedback") { + scene = new SceneParticlesFeedback(); + } else if( recipe == "particles-instanced" ) { + scene = new SceneParticlesInstanced(); + } else if( recipe == "smoke" ) { + scene = new SceneSmoke(); + } else if( recipe == "wave" ) { + scene = new SceneWave(); + } 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(" fire : description...\n"); + printf(" particles : description...\n"); + printf(" particles-feedback : description...\n"); + printf(" particles-instanced : description...\n"); + printf(" smoke : description...\n"); + printf(" wave : description...\n"); +} diff --git a/chapter09/scenefire.cpp b/chapter09/scenefire.cpp index ce157da..6979338 100644 --- a/chapter09/scenefire.cpp +++ b/chapter09/scenefire.cpp @@ -3,7 +3,7 @@ #include #include -#include +#include "bmpreader.h" #include "glutils.h" #include "defines.h" @@ -48,32 +48,9 @@ void SceneFire::initScene() glGenQueries(1, &query); - const char * texName = "../media/texture/fire.png"; - try { - glimg::ImageSet * imgSet; - imgSet = glimg::loaders::stb::LoadFromFile(texName); - const glimg::SingleImage &img = imgSet->GetImage(0); - glimg::OpenGLPixelTransferParams params = glimg::GetUploadFormatType(img.GetFormat(), 0); - glimg::Dimensions dims = img.GetDimensions(); - - glPixelStorei(GL_UNPACK_ALIGNMENT, img.GetFormat().LineAlign()); - - // Copy file to OpenGL - glActiveTexture(GL_TEXTURE0); - GLuint tid; - glGenTextures(1, &tid); - glBindTexture(GL_TEXTURE_2D, tid); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, dims.width, dims.height, 0, - params.format, params.type, img.GetImageData()); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - - delete imgSet; - - } catch( glimg::loaders::stb::StbLoaderException &e ) { - fprintf(stderr, "Unable to load texture %s: %s\n", texName, e.what()); - exit(1); - } + const char * texName = "../media/texture/fire.bmp"; + glActiveTexture(GL_TEXTURE0); + BMPReader::loadTex(texName); prog.setUniform("ParticleTex", 0); prog.setUniform("ParticleLifetime", 4.0f); diff --git a/chapter09/sceneparticles.cpp b/chapter09/sceneparticles.cpp index 9c50e8f..0ceb0c9 100644 --- a/chapter09/sceneparticles.cpp +++ b/chapter09/sceneparticles.cpp @@ -3,7 +3,7 @@ #include #include -#include +#include "bmpreader.h" #include "glutils.h" #include "defines.h" @@ -26,8 +26,6 @@ void SceneParticles::initScene() glClearColor(0.1f,0.1f,0.1f,1.0f); -// glEnable(GL_DEPTH_TEST); - // Enable blending glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); @@ -38,40 +36,15 @@ void SceneParticles::initScene() plane = new VBOPlane(13.0f, 10.0f, 200, 2); projection = mat4(1.0f); - //prog.setUniform("Light.Intensity", vec3(1.0f,1.0f,1.0f) ); - angle = (float)(PI / 2.0f); // Generate our vertex buffers initBuffers(); // The particle texture - const char * texName = "../media/texture/bluewater.png"; - try { - glimg::ImageSet * imgSet; - imgSet = glimg::loaders::stb::LoadFromFile(texName); - const glimg::SingleImage &img = imgSet->GetImage(0); - glimg::OpenGLPixelTransferParams params = glimg::GetUploadFormatType(img.GetFormat(), 0); - glimg::Dimensions dims = img.GetDimensions(); - - glPixelStorei(GL_UNPACK_ALIGNMENT, img.GetFormat().LineAlign()); - - // Copy file to OpenGL - glActiveTexture(GL_TEXTURE0); - GLuint tid; - glGenTextures(1, &tid); - glBindTexture(GL_TEXTURE_2D, tid); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, dims.width, dims.height, 0, - params.format, params.type, img.GetImageData()); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - - delete imgSet; - - } catch( glimg::loaders::stb::StbLoaderException &e ) { - fprintf(stderr, "Unable to load texture %s: %s\n", texName, e.what()); - exit(1); - } + const char * texName = "../media/texture/bluewater.bmp"; + glActiveTexture(GL_TEXTURE0); + BMPReader::loadTex(texName); prog.setUniform("ParticleTex", 0); prog.setUniform("ParticleLifetime", 3.5f); diff --git a/chapter09/sceneparticlesfeedback.cpp b/chapter09/sceneparticlesfeedback.cpp index daec2e0..b54162e 100644 --- a/chapter09/sceneparticlesfeedback.cpp +++ b/chapter09/sceneparticlesfeedback.cpp @@ -3,7 +3,7 @@ #include #include -#include +#include "bmpreader.h" #include "glutils.h" #include "defines.h" @@ -48,32 +48,9 @@ void SceneParticlesFeedback::initScene() glGenQueries(1, &query); - const char * texName = "../media/texture/bluewater.png"; - try { - glimg::ImageSet * imgSet; - imgSet = glimg::loaders::stb::LoadFromFile(texName); - const glimg::SingleImage &img = imgSet->GetImage(0); - glimg::OpenGLPixelTransferParams params = glimg::GetUploadFormatType(img.GetFormat(), 0); - glimg::Dimensions dims = img.GetDimensions(); - - glPixelStorei(GL_UNPACK_ALIGNMENT, img.GetFormat().LineAlign()); - - // Copy file to OpenGL - glActiveTexture(GL_TEXTURE0); - GLuint tid; - glGenTextures(1, &tid); - glBindTexture(GL_TEXTURE_2D, tid); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, dims.width, dims.height, 0, - params.format, params.type, img.GetImageData()); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - - delete imgSet; - - } catch( glimg::loaders::stb::StbLoaderException &e ) { - fprintf(stderr, "Unable to load texture %s: %s\n", texName, e.what()); - exit(1); - } + const char * texName = "../media/texture/bluewater.bmp"; + glActiveTexture(GL_TEXTURE0); + BMPReader::loadTex(texName); prog.setUniform("ParticleTex", 0); prog.setUniform("ParticleLifetime", 3.5f); diff --git a/chapter09/sceneparticlesinstanced.cpp b/chapter09/sceneparticlesinstanced.cpp index 4479074..3448a08 100644 --- a/chapter09/sceneparticlesinstanced.cpp +++ b/chapter09/sceneparticlesinstanced.cpp @@ -105,9 +105,8 @@ void SceneParticlesInstanced::initBuffers() glVertexAttribPointer(4, 1, GL_FLOAT, GL_FALSE, 0, NULL); glEnableVertexAttribArray(4); - // Need to use the ARB version of these due to a bug in glload - glVertexAttribDivisorARB(3, 1); - glVertexAttribDivisorARB(4, 1); + glVertexAttribDivisor(3, 1); + glVertexAttribDivisor(4, 1); glBindVertexArray(0); } diff --git a/chapter09/scenesmoke.cpp b/chapter09/scenesmoke.cpp index 2e6a1cf..85d0655 100644 --- a/chapter09/scenesmoke.cpp +++ b/chapter09/scenesmoke.cpp @@ -3,7 +3,7 @@ #include #include -#include +#include "bmpreader.h" #include "glutils.h" #include "defines.h" @@ -48,32 +48,9 @@ void SceneSmoke::initScene() glGenQueries(1, &query); - const char * texName = "../media/texture/smoke.png"; - try { - glimg::ImageSet * imgSet; - imgSet = glimg::loaders::stb::LoadFromFile(texName); - const glimg::SingleImage &img = imgSet->GetImage(0); - glimg::OpenGLPixelTransferParams params = glimg::GetUploadFormatType(img.GetFormat(), 0); - glimg::Dimensions dims = img.GetDimensions(); - - glPixelStorei(GL_UNPACK_ALIGNMENT, img.GetFormat().LineAlign()); - - // Copy file to OpenGL - glActiveTexture(GL_TEXTURE0); - GLuint tid; - glGenTextures(1, &tid); - glBindTexture(GL_TEXTURE_2D, tid); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, dims.width, dims.height, 0, - params.format, params.type, img.GetImageData()); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - - delete imgSet; - - } catch( glimg::loaders::stb::StbLoaderException &e ) { - fprintf(stderr, "Unable to load texture %s: %s\n", texName, e.what()); - exit(1); - } + const char * texName = "../media/texture/smoke.bmp"; + glActiveTexture(GL_TEXTURE0); + BMPReader::loadTex(texName); prog.setUniform("ParticleTex", 0); prog.setUniform("ParticleLifetime", 6.0f); diff --git a/media/texture/bluewater.bmp b/media/texture/bluewater.bmp new file mode 100644 index 0000000..d6d80e4 Binary files /dev/null and b/media/texture/bluewater.bmp differ diff --git a/media/texture/fire.bmp b/media/texture/fire.bmp new file mode 100644 index 0000000..a414da3 Binary files /dev/null and b/media/texture/fire.bmp differ diff --git a/media/texture/smoke.bmp b/media/texture/smoke.bmp new file mode 100644 index 0000000..705d5d5 Binary files /dev/null and b/media/texture/smoke.bmp differ