Commits

Jason McKesson  committed e9e0b8c

Modified tutorial 3 files and docs to always load shaders from files.

  • Participants
  • Parent commits 47866cc

Comments (0)

Files changed (8)

File Documents/Tutorial 03/Tutorial 03.xml

 }</programlisting>
         </example>
         <para>This shader takes two uniforms: the duration of the loop and the elapsed time.</para>
-        <para>In this shader, we use a number of built-in functions. Think of these as standard
-            library functions. <function>mod</function>, <function>cos</function>, and
-                <function>sin</function> are all standard GLSL functions that you can use as needed.
-            There are a <emphasis>lot</emphasis> of standard GLSL functions available.</para>
+        <para>In this shader, we use a number of standard GLSL functions, like
+                <function>mod</function>, <function>cos</function>, and <function>sin</function>. We
+            saw <function>mix</function> in the last tutorial. And these are just the tip of the
+            iceberg; there are a <emphasis>lot</emphasis> of standard GLSL functions
+            available.</para>
         <para>The rendering code looks quite similar to the previous rendering code:</para>
         <example>
             <title>Rendering with Time</title>
     glUseProgram(0);
 }</programlisting>
         </example>
-        <para>Notice the call to <function>LoadShader</function> for the vertex shader, rather than
-                <function>CreateShader</function>. This is a simple routine that loads the file and
-            passes it in its entirety to the <function>CreateShader</function> function.</para>
         <para>We get the time uniform as normal with <function>glGetUniformLocation</function>. For
             the loop duration, we get that in a local variable. Then we immediately set the current
             program object, set the uniform to a value, and then unset the current program

File Tut 03 OpenGLs Moving Triangle/data/positionOffset.vert

+#version 150
+
+in vec4 position;
+uniform vec2 offset;
+
+void main()
+{
+	vec4 totalOffset = vec4(offset.x, offset.y, 0.0, 0.0);
+	gl_Position = position + totalOffset;
+}

File Tut 03 OpenGLs Moving Triangle/data/standard.frag

+#version 150
+
+out vec4 outputColor;
+
+void main()
+{
+	outputColor = vec4(1.0f, 1.0f, 1.0f, 1.0f);
+}

File Tut 03 OpenGLs Moving Triangle/data/standard.vert

+#version 150
+
+in vec4 position;
+
+void main()
+{
+	gl_Position = position;
+}

File Tut 03 OpenGLs Moving Triangle/tut2a.cpp

 #include <math.h>
 #include <glloader/gl_3_2_comp.h>
 #include <GL/freeglut.h>
+#include "../framework/framework.h"
 
 #define ARRAY_COUNT( array ) (sizeof( array ) / (sizeof( array[0] ) * (sizeof( array ) != sizeof(void*) || sizeof( array[0] ) <= sizeof(void*))))
 
-GLuint CreateShader(GLenum eShaderType, const std::string &strShaderFile)
-{
-	GLuint shader = glCreateShader(eShaderType);
-	const char *strFileData = strShaderFile.c_str();
-	glShaderSource(shader, 1, &strFileData, NULL);
-
-	glCompileShader(shader);
-
-	GLint status;
-	glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
-	if (status == GL_FALSE)
-	{
-		GLint infoLogLength;
-		glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLogLength);
-
-		GLchar *strInfoLog = new GLchar[infoLogLength + 1];
-		glGetShaderInfoLog(shader, infoLogLength, NULL, strInfoLog);
-
-		const char *strShaderType = NULL;
-		switch(eShaderType)
-		{
-		case GL_VERTEX_SHADER: strShaderType = "vertex"; break;
-		case GL_GEOMETRY_SHADER: strShaderType = "geometry"; break;
-		case GL_FRAGMENT_SHADER: strShaderType = "fragment"; break;
-		}
-
-		fprintf(stderr, "Compile failure in %s shader:\n%s\n", strShaderType, strInfoLog);
-		delete[] strInfoLog;
-	}
-
-	return shader;
-}
-
-GLuint CreateProgram(const std::vector<GLuint> &shaderList)
-{
-	GLuint program = glCreateProgram();
-
-	for(size_t iLoop = 0; iLoop < shaderList.size(); iLoop++)
-		glAttachShader(program, shaderList[iLoop]);
-
-	glLinkProgram(program);
-
-	GLint status;
-	glGetProgramiv (program, GL_LINK_STATUS, &status);
-	if (status == GL_FALSE)
-	{
-		GLint infoLogLength;
-		glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLogLength);
-
-		GLchar *strInfoLog = new GLchar[infoLogLength + 1];
-		glGetProgramInfoLog(program, infoLogLength, NULL, strInfoLog);
-		fprintf(stderr, "Linker failure: %s\n", strInfoLog);
-		delete[] strInfoLog;
-	}
-
-	return program;
-}
-
 GLuint theProgram;
 GLuint positionAttrib;
 
-const std::string strVertexShader(
-	"#version 150\n"
-	"in vec4 position;\n"
-	"void main()\n"
-	"{\n"
-	"   gl_Position = position;\n"
-	"}\n"
-);
-
-const std::string strFragmentShader(
-	"#version 150\n"
-	"out vec4 outputColor;\n"
-	"void main()\n"
-	"{\n"
-	"   outputColor = vec4(1.0f, 1.0f, 1.0f, 1.0f);\n"
-	"}\n"
-);
-
 void InitializeProgram()
 {
 	std::vector<GLuint> shaderList;
 
-	shaderList.push_back(CreateShader(GL_VERTEX_SHADER, strVertexShader));
-	shaderList.push_back(CreateShader(GL_FRAGMENT_SHADER, strFragmentShader));
+	shaderList.push_back(Framework::LoadShader(GL_VERTEX_SHADER, "standard.vert"));
+	shaderList.push_back(Framework::LoadShader(GL_FRAGMENT_SHADER, "standard.frag"));
 
-	theProgram = CreateProgram(shaderList);
+	theProgram = Framework::CreateProgram(shaderList);
 
 	positionAttrib = glGetAttribLocation(theProgram, "position");
 }

File Tut 03 OpenGLs Moving Triangle/tut2b.cpp

 #include <math.h>
 #include <glloader/gl_3_2_comp.h>
 #include <GL/freeglut.h>
-
-#define ARRAY_COUNT( array ) (sizeof( array ) / (sizeof( array[0] ) * (sizeof( array ) != sizeof(void*) || sizeof( array[0] ) <= sizeof(void*))))
-
-GLuint CreateShader(GLenum eShaderType, const std::string &strShaderFile)
-{
-	GLuint shader = glCreateShader(eShaderType);
-	const char *strFileData = strShaderFile.c_str();
-	glShaderSource(shader, 1, &strFileData, NULL);
-
-	glCompileShader(shader);
-
-	GLint status;
-	glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
-	if (status == GL_FALSE)
-	{
-		GLint infoLogLength;
-		glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLogLength);
-
-		GLchar *strInfoLog = new GLchar[infoLogLength + 1];
-		glGetShaderInfoLog(shader, infoLogLength, NULL, strInfoLog);
-
-		const char *strShaderType = NULL;
-		switch(eShaderType)
-		{
-		case GL_VERTEX_SHADER: strShaderType = "vertex"; break;
-		case GL_GEOMETRY_SHADER: strShaderType = "geometry"; break;
-		case GL_FRAGMENT_SHADER: strShaderType = "fragment"; break;
-		}
-
-		fprintf(stderr, "Compile failure in %s shader:\n%s\n", strShaderType, strInfoLog);
-		delete[] strInfoLog;
-	}
-
-	return shader;
-}
-
-GLuint CreateProgram(const std::vector<GLuint> &shaderList)
-{
-	GLuint program = glCreateProgram();
-
-	for(size_t iLoop = 0; iLoop < shaderList.size(); iLoop++)
-		glAttachShader(program, shaderList[iLoop]);
-
-	glLinkProgram(program);
-
-	GLint status;
-	glGetProgramiv (program, GL_LINK_STATUS, &status);
-	if (status == GL_FALSE)
-	{
-		GLint infoLogLength;
-		glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLogLength);
-
-		GLchar *strInfoLog = new GLchar[infoLogLength + 1];
-		glGetProgramInfoLog(program, infoLogLength, NULL, strInfoLog);
-		fprintf(stderr, "Linker failure: %s\n", strInfoLog);
-		delete[] strInfoLog;
-	}
-
-	return program;
-}
+#include "../framework/framework.h"
 
 GLuint theProgram;
 GLuint positionAttrib;
 GLuint offsetLocation;
 
-const std::string strVertexShader(
-"#version 150\n"
-"in vec4 position;\n"
-"uniform vec2 offset;"
-"void main()\n"
-"{\n"
-"	vec4 totalOffset = vec4(offset.x, offset.y, 0.0, 0.0);\n"
-"   gl_Position = position + totalOffset;\n"
-"}\n"
-);
-
-const std::string strFragmentShader(
-"#version 150\n"
-"out vec4 outputColor;\n"
-"void main()\n"
-"{\n"
-"   outputColor = vec4(1.0f, 1.0f, 1.0f, 1.0f);\n"
-"}\n"
-);
-
 void InitializeProgram()
 {
 	std::vector<GLuint> shaderList;
 
-	shaderList.push_back(CreateShader(GL_VERTEX_SHADER, strVertexShader));
-	shaderList.push_back(CreateShader(GL_FRAGMENT_SHADER, strFragmentShader));
+	shaderList.push_back(Framework::LoadShader(GL_VERTEX_SHADER, "positionOffset.vert"));
+	shaderList.push_back(Framework::LoadShader(GL_FRAGMENT_SHADER, "standard.frag"));
 
-	theProgram = CreateProgram(shaderList);
+	theProgram = Framework::CreateProgram(shaderList);
 
 	positionAttrib = glGetAttribLocation(theProgram, "position");
 	offsetLocation = glGetUniformLocation(theProgram, "offset");

File Tut 03 OpenGLs Moving Triangle/tut2c.cpp

 
 #include <string>
 #include <vector>
-#include <fstream>
-#include <sstream>
 #include <math.h>
 #include <glloader/gl_3_2_comp.h>
 #include <GL/freeglut.h>
-
-#define ARRAY_COUNT( array ) (sizeof( array ) / (sizeof( array[0] ) * (sizeof( array ) != sizeof(void*) || sizeof( array[0] ) <= sizeof(void*))))
-
-GLuint CreateShader(GLenum eShaderType, const std::string &strShaderFile)
-{
-	GLuint shader = glCreateShader(eShaderType);
-	const char *strFileData = strShaderFile.c_str();
-	glShaderSource(shader, 1, &strFileData, NULL);
-
-	glCompileShader(shader);
-
-	GLint status;
-	glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
-	if (status == GL_FALSE)
-	{
-		GLint infoLogLength;
-		glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLogLength);
-
-		GLchar *strInfoLog = new GLchar[infoLogLength + 1];
-		glGetShaderInfoLog(shader, infoLogLength, NULL, strInfoLog);
-
-		const char *strShaderType = NULL;
-		switch(eShaderType)
-		{
-		case GL_VERTEX_SHADER: strShaderType = "vertex"; break;
-		case GL_GEOMETRY_SHADER: strShaderType = "geometry"; break;
-		case GL_FRAGMENT_SHADER: strShaderType = "fragment"; break;
-		}
-
-		fprintf(stderr, "Compile failure in %s shader:\n%s\n", strShaderType, strInfoLog);
-		delete[] strInfoLog;
-	}
-
-	return shader;
-}
-
-GLuint LoadShader(GLenum eShaderType, const std::string &strShaderFilename)
-{
-	std::string strFilename = "data\\" + strShaderFilename;
-	std::ifstream shaderFile(strFilename.c_str());
-	std::stringstream shaderData;
-	shaderData << shaderFile.rdbuf();
-	shaderFile.close();
-
-	return CreateShader(eShaderType, shaderData.str());
-}
-
-GLuint CreateProgram(const std::vector<GLuint> &shaderList)
-{
-	GLuint program = glCreateProgram();
-
-	for(size_t iLoop = 0; iLoop < shaderList.size(); iLoop++)
-		glAttachShader(program, shaderList[iLoop]);
-
-	glLinkProgram(program);
-
-	GLint status;
-	glGetProgramiv (program, GL_LINK_STATUS, &status);
-	if (status == GL_FALSE)
-	{
-		GLint infoLogLength;
-		glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLogLength);
-
-		GLchar *strInfoLog = new GLchar[infoLogLength + 1];
-		glGetProgramInfoLog(program, infoLogLength, NULL, strInfoLog);
-		fprintf(stderr, "Linker failure: %s\n", strInfoLog);
-		delete[] strInfoLog;
-	}
-
-	return program;
-}
+#include "../framework/framework.h"
 
 GLuint theProgram;
 GLuint positionAttrib;
 GLuint elapsedTimeUniform;
 
-const std::string strFragmentShader(
-"#version 150\n"
-"out vec4 outputColor;\n"
-"void main()\n"
-"{\n"
-"   outputColor = vec4(1.0f, 1.0f, 1.0f, 1.0f);\n"
-"}\n"
-);
-
 void InitializeProgram()
 {
 	std::vector<GLuint> shaderList;
 
-	shaderList.push_back(LoadShader(GL_VERTEX_SHADER, "tut2c.vert"));
-	shaderList.push_back(CreateShader(GL_FRAGMENT_SHADER, strFragmentShader));
+	shaderList.push_back(Framework::LoadShader(GL_VERTEX_SHADER, "tut2c.vert"));
+	shaderList.push_back(Framework::LoadShader(GL_FRAGMENT_SHADER, "standard.frag"));
 
-	theProgram = CreateProgram(shaderList);
+	theProgram = Framework::CreateProgram(shaderList);
 
 	positionAttrib = glGetAttribLocation(theProgram, "position");
 	elapsedTimeUniform = glGetUniformLocation(theProgram, "time");

File Tut 03 OpenGLs Moving Triangle/tut2d.cpp

 
 #include <string>
 #include <vector>
-#include <fstream>
-#include <sstream>
 #include <math.h>
 #include <glloader/gl_3_2_comp.h>
 #include <GL/freeglut.h>
-
-#define ARRAY_COUNT( array ) (sizeof( array ) / (sizeof( array[0] ) * (sizeof( array ) != sizeof(void*) || sizeof( array[0] ) <= sizeof(void*))))
-
-GLuint CreateShader(GLenum eShaderType, const std::string &strShaderFile)
-{
-	GLuint shader = glCreateShader(eShaderType);
-	const char *strFileData = strShaderFile.c_str();
-	glShaderSource(shader, 1, &strFileData, NULL);
-
-	glCompileShader(shader);
-
-	GLint status;
-	glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
-	if (status == GL_FALSE)
-	{
-		GLint infoLogLength;
-		glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLogLength);
-
-		GLchar *strInfoLog = new GLchar[infoLogLength + 1];
-		glGetShaderInfoLog(shader, infoLogLength, NULL, strInfoLog);
-
-		const char *strShaderType = NULL;
-		switch(eShaderType)
-		{
-		case GL_VERTEX_SHADER: strShaderType = "vertex"; break;
-		case GL_GEOMETRY_SHADER: strShaderType = "geometry"; break;
-		case GL_FRAGMENT_SHADER: strShaderType = "fragment"; break;
-		}
-
-		fprintf(stderr, "Compile failure in %s shader:\n%s\n", strShaderType, strInfoLog);
-		delete[] strInfoLog;
-	}
-
-	return shader;
-}
-
-GLuint LoadShader(GLenum eShaderType, const std::string &strShaderFilename)
-{
-	std::string strFilename = "data\\" + strShaderFilename;
-	std::ifstream shaderFile(strFilename.c_str());
-	std::stringstream shaderData;
-	shaderData << shaderFile.rdbuf();
-	shaderFile.close();
-
-	return CreateShader(eShaderType, shaderData.str());
-}
-
-GLuint CreateProgram(const std::vector<GLuint> &shaderList)
-{
-	GLuint program = glCreateProgram();
-
-	for(size_t iLoop = 0; iLoop < shaderList.size(); iLoop++)
-		glAttachShader(program, shaderList[iLoop]);
-
-	glLinkProgram(program);
-
-	GLint status;
-	glGetProgramiv (program, GL_LINK_STATUS, &status);
-	if (status == GL_FALSE)
-	{
-		GLint infoLogLength;
-		glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLogLength);
-
-		GLchar *strInfoLog = new GLchar[infoLogLength + 1];
-		glGetProgramInfoLog(program, infoLogLength, NULL, strInfoLog);
-		fprintf(stderr, "Linker failure: %s\n", strInfoLog);
-		delete[] strInfoLog;
-	}
-
-	return program;
-}
+#include "../framework/framework.h"
 
 GLuint theProgram;
 GLuint positionAttrib;
 GLuint elapsedTimeUniform;
 
-const std::string strFragmentShader(
-"#version 150\n"
-"out vec4 outputColor;\n"
-"void main()\n"
-"{\n"
-"   outputColor = vec4(1.0f, 1.0f, 1.0f, 1.0f);\n"
-"}\n"
-);
-
 void InitializeProgram()
 {
 	std::vector<GLuint> shaderList;
 
-	shaderList.push_back(LoadShader(GL_VERTEX_SHADER, "tut2d.vert"));
-	shaderList.push_back(LoadShader(GL_FRAGMENT_SHADER, "tut2d.frag"));
+	shaderList.push_back(Framework::LoadShader(GL_VERTEX_SHADER, "tut2d.vert"));
+	shaderList.push_back(Framework::LoadShader(GL_FRAGMENT_SHADER, "tut2d.frag"));
 
-	theProgram = CreateProgram(shaderList);
+	theProgram = Framework::CreateProgram(shaderList);
 
 	positionAttrib = glGetAttribLocation(theProgram, "position");
 	elapsedTimeUniform = glGetUniformLocation(theProgram, "time");