Commits

Jason McKesson  committed a01f1a8

Tutorial 3 code finished. Added document file for it.

  • Participants
  • Parent commits 33930b7

Comments (0)

Files changed (13)

File Tut 03 Playing with Colors/FragPosition.cpp

+
+#include <string>
+#include <vector>
+#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 theProgram;
+GLuint positionAttrib;
+GLuint elapsedTimeUniform;
+GLuint wndDimensions;
+
+void InitializeProgram()
+{
+	std::vector<GLuint> shaderList;
+
+	shaderList.push_back(Framework::LoadShader(GL_VERTEX_SHADER, "FragPosition.vert"));
+	shaderList.push_back(Framework::LoadShader(GL_FRAGMENT_SHADER, "FragPosition.frag"));
+
+	theProgram = Framework::CreateProgram(shaderList);
+
+	positionAttrib = glGetAttribLocation(theProgram, "position");
+	elapsedTimeUniform = glGetUniformLocation(theProgram, "time");
+	wndDimensions = glGetUniformLocation(theProgram, "wndDimensions");
+
+	GLuint loopDurationUnf = glGetUniformLocation(theProgram, "loopDuration");
+
+	glUseProgram(theProgram);
+	glUniform1f(loopDurationUnf, 5.0f);
+	glUseProgram(0);
+}
+
+const float vertexData[] = {
+	0.25f, 0.25f, 0.0f, 1.0f,
+	0.25f, -0.25f, 0.0f, 1.0f,
+	-0.25f, -0.25f, 0.0f, 1.0f,
+};
+
+GLuint vertexBufferObject;
+GLuint vao;
+
+
+void InitializeVertexBuffer()
+{
+	glGenBuffers(1, &vertexBufferObject);
+
+	glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject);
+	glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData), vertexData, GL_STREAM_DRAW);
+	glBindBuffer(GL_ARRAY_BUFFER, 0);
+}
+
+//Called after the window and OpenGL are initialized. Called exactly once, before the main loop.
+void init()
+{
+	InitializeProgram();
+	InitializeVertexBuffer();
+
+	glGenVertexArrays(1, &vao);
+	glBindVertexArray(vao);
+}
+
+//Called to update the display.
+//You should call glutSwapBuffers after all of your rendering to display what you rendered.
+//If you need continuous updates of the screen, call glutPostRedisplay() at the end of the function.
+void display()
+{
+	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+	glClear(GL_COLOR_BUFFER_BIT);
+
+	glUseProgram(theProgram);
+
+	glUniform1f(elapsedTimeUniform, glutGet(GLUT_ELAPSED_TIME) / 1000.0f);
+
+	glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject);
+	glEnableVertexAttribArray(positionAttrib);
+	glVertexAttribPointer(positionAttrib, 4, GL_FLOAT, GL_FALSE, 0, 0);
+
+	glDrawArrays(GL_TRIANGLES, 0, 3);
+
+	glDisableVertexAttribArray(positionAttrib);
+	glUseProgram(0);
+
+	glutSwapBuffers();
+	glutPostRedisplay();
+}
+
+//Called whenever the window is resized. The new window size is given, in pixels.
+//This is an opportunity to call glViewport or glScissor to keep up with the change in size.
+void reshape (int w, int h)
+{
+	glViewport(0, 0, (GLsizei) w, (GLsizei) h);
+
+	glUseProgram(theProgram);
+	glUniform2f(wndDimensions, float(w), float(h));
+	glUseProgram(0);
+}
+
+//Called whenever a key on the keyboard was pressed.
+//The key is given by the ''key'' parameter, which is in ASCII.
+//It's often a good idea to have the escape key (ASCII value 27) call glutLeaveMainLoop() to 
+//exit the program.
+void keyboard(unsigned char key, int x, int y)
+{
+	switch (key)
+	{
+	case 27:
+		glutLeaveMainLoop();
+		break;
+	}
+}
+
+

File Tut 03 Playing with Colors/VertexColors.cpp

+
+#include <string>
+#include <vector>
+#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 theProgram;
+GLuint positionAttrib;
+GLuint colorAttrib;
+
+void InitializeProgram()
+{
+	std::vector<GLuint> shaderList;
+
+	shaderList.push_back(Framework::LoadShader(GL_VERTEX_SHADER, "VertexColors.vert"));
+	shaderList.push_back(Framework::LoadShader(GL_FRAGMENT_SHADER, "VertexColors.frag"));
+
+	theProgram = Framework::CreateProgram(shaderList);
+
+	positionAttrib = glGetAttribLocation(theProgram, "position");
+	colorAttrib = glGetAttribLocation(theProgram, "color");
+}
+
+const float vertexData[] = {
+	 0.0f,    0.5f, 0.0f, 1.0f,
+	 0.5f, -0.366f, 0.0f, 1.0f,
+	-0.5f, -0.366f, 0.0f, 1.0f,
+	 1.0f,    0.0f, 0.0f, 1.0f,
+	 0.0f,    1.0f, 0.0f, 1.0f,
+	 0.0f,    0.0f, 1.0f, 1.0f,
+};
+
+GLuint vertexBufferObject;
+GLuint vao;
+
+
+void InitializeVertexBuffer()
+{
+	glGenBuffers(1, &vertexBufferObject);
+
+	glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject);
+	glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData), vertexData, GL_STREAM_DRAW);
+	glBindBuffer(GL_ARRAY_BUFFER, 0);
+}
+
+//Called after the window and OpenGL are initialized. Called exactly once, before the main loop.
+void init()
+{
+	InitializeProgram();
+	InitializeVertexBuffer();
+
+	glGenVertexArrays(1, &vao);
+	glBindVertexArray(vao);
+}
+
+//Called to update the display.
+//You should call glutSwapBuffers after all of your rendering to display what you rendered.
+//If you need continuous updates of the screen, call glutPostRedisplay() at the end of the function.
+void display()
+{
+	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+	glClear(GL_COLOR_BUFFER_BIT);
+
+	glUseProgram(theProgram);
+
+	glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject);
+	glEnableVertexAttribArray(positionAttrib);
+	glVertexAttribPointer(positionAttrib, 4, GL_FLOAT, GL_FALSE, 0, 0);
+	glEnableVertexAttribArray(colorAttrib);
+	glVertexAttribPointer(colorAttrib, 4, GL_FLOAT, GL_FALSE, 0, (void*)48);
+
+	glDrawArrays(GL_TRIANGLES, 0, 3);
+
+	glDisableVertexAttribArray(positionAttrib);
+	glUseProgram(0);
+
+	glutSwapBuffers();
+	glutPostRedisplay();
+}
+
+//Called whenever the window is resized. The new window size is given, in pixels.
+//This is an opportunity to call glViewport or glScissor to keep up with the change in size.
+void reshape (int w, int h)
+{
+	glViewport(0, 0, (GLsizei) w, (GLsizei) h);
+}
+
+//Called whenever a key on the keyboard was pressed.
+//The key is given by the ''key'' parameter, which is in ASCII.
+//It's often a good idea to have the escape key (ASCII value 27) call glutLeaveMainLoop() to 
+//exit the program.
+void keyboard(unsigned char key, int x, int y)
+{
+	switch (key)
+	{
+	case 27:
+		glutLeaveMainLoop();
+		break;
+	}
+}
+
+

File Tut 03 Playing with Colors/data/FragPosition.frag

+#version 150
+
+out vec4 outputColor;
+
+uniform vec2 wndDimensions;
+
+const vec4 firstColor = vec4(1.0f, 1.0f, 1.0f, 1.0f);
+const vec4 secondColor = vec4(0.2f, 0.2f, 0.2f, 1.0f);
+
+void main()
+{
+	float lerpValue = gl_FragCoord.y / wndDimensions.y;
+		
+	outputColor = mix(firstColor, secondColor, lerpValue);
+}

File Tut 03 Playing with Colors/data/FragPosition.vert

+#version 150
+
+in vec4 position;
+uniform float loopDuration;
+uniform float time;
+
+void main()
+{
+	float timeScale = 3.14159f * 2.0f / loopDuration;
+	
+	float currTime = mod(time, loopDuration);
+	vec4 totalOffset = vec4(
+		cos(currTime * timeScale) * 0.5f,
+		sin(currTime * timeScale) * 0.5f,
+		0.0f,
+		0.0f);
+
+	gl_Position = position + totalOffset;
+}

File Tut 03 Playing with Colors/data/VertexColors.frag

+#version 150
+
+smooth in vec4 theColor;
+
+out vec4 outputColor;
+
+void main()
+{
+	outputColor = theColor;
+}

File Tut 03 Playing with Colors/data/VertexColors.vert

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

File Tut 03 Playing with Colors/premake4.lua

+
+dofile("../framework/framework.lua")
+
+SetupSolution("Tutorial3")
+SetupProject("Fragment Position", "FragPosition.cpp")
+SetupProject("Vertex Colors", "VertexColors.cpp")

File Tutorial Documents/Outline.xml

         <orderedlist>
             <listitem>
                 <para>Use gl_FragCoord to calculate fragment colors based on the position of the
-                    fragments.</para>
+                    fragments. Use the moving triangle as a base.</para>
             </listitem>
             <listitem>
                 <para>Use multiple vertex arrays to send position and color data. Use a vertex

File Tutorial Documents/Tutorial 03/Tutorial 03.xml

+<?xml version="1.0" encoding="UTF-8"?>
+<?oxygen RNGSchema="http://docbook.org/xml/5.0/rng/docbookxi.rng" type="xml"?>
+<?oxygen SCHSchema="http://docbook.org/xml/5.0/rng/docbookxi.rng"?>
+<chapter xmlns="http://docbook.org/ns/docbook" xmlns:xi="http://www.w3.org/2001/XInclude"
+    xmlns:xlink="http://www.w3.org/1999/xlink" version="5.0">
+    <title>Playing with Colors</title>
+    <para>G</para>
+</chapter>

File Tutorial Documents/Tutorial Documents.xpr

         <folder name="Tutorial 02">
             <file name="Tutorial%2002/Tutorial%2002.xml"/>
         </folder>
+        <folder name="Tutorial 03">
+            <file name="Tutorial%2003/Tutorial%2003.xml"/>
+        </folder>
         <file name="Building%20the%20Tutorials.xml"/>
         <file name="Outline.xml"/>
         <file name="Tutorials.xml"/>

File framework/framework.cpp

-/*
-* smooth_opengl3.c, based on smooth.c, which is (c) by SGI, see below.
-* This program demonstrates smooth shading in a way which is fully
-* OpenGL-3.1-compliant.
-* A smooth shaded polygon is drawn in a 2-D projection.
-*/
 
-/*
-* Original copyright notice from smooth.c:
-*
-* License Applicability. Except to the extent portions of this file are
-* made subject to an alternative license as permitted in the SGI Free
-* Software License B, Version 1.1 (the "License"), the contents of this
-* file are subject only to the provisions of the License. You may not use
-* this file except in compliance with the License. You may obtain a copy
-* of the License at Silicon Graphics, Inc., attn: Legal Services, 1600
-* Amphitheatre Parkway, Mountain View, CA 94043-1351, or at:
-* 
-* http://oss.sgi.com/projects/FreeB
-* 
-* Note that, as provided in the License, the Software is distributed on an
-* "AS IS" basis, with ALL EXPRESS AND IMPLIED WARRANTIES AND CONDITIONS
-* DISCLAIMED, INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTIES AND
-* CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, FITNESS FOR A
-* PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
-* 
-* Original Code. The Original Code is: OpenGL Sample Implementation,
-* Version 1.2.1, released January 26, 2000, developed by Silicon Graphics,
-* Inc. The Original Code is Copyright (c) 1991-2000 Silicon Graphics, Inc.
-* Copyright in any portions created by third parties is as indicated
-* elsewhere herein. All Rights Reserved.
-* 
-* Additional Notice Provisions: The application programming interfaces
-* established by SGI in conjunction with the Original Code are The
-* OpenGL(R) Graphics System: A Specification (Version 1.2.1), released
-* April 1, 1999; The OpenGL(R) Graphics System Utility Library (Version
-* 1.3), released November 4, 1998; and OpenGL(R) Graphics with the X
-* Window System(R) (Version 1.3), released October 19, 1998. This software
-* was created using the OpenGL(R) version 1.2.1 Sample Implementation
-* published by SGI, but has not been independently verified as being
-* compliant with the OpenGL(R) version 1.2.1 Specification.
-*
-*/
-
+#include <string>
+#include <vector>
+#include <fstream>
+#include <sstream>
 #include <glloader/gl_3_2_comp.h>
 #include <glloader/wgl_exts.h>
 #include <glloader/gle.h>
 #include <GL/freeglut.h>
 
 
+namespace Framework
+{
+	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;
+	}
+}
+
+
 void init();
 void display();
 void reshape(int w, int h);

File framework/framework.h

+#ifndef FRAMEWORK_H
+#define FRAMEWORK_H
+
+namespace Framework
+{
+	GLuint CreateShader(GLenum eShaderType, const std::string &strShaderFile);
+	GLuint LoadShader(GLenum eShaderType, const std::string &strShaderFilename);
+	GLuint CreateProgram(const std::vector<GLuint> &shaderList);
+}
+
+#endif //FRAMEWORK_H

File framework/framework.lua

 		language "c++"
 		
 		files { "../framework/framework.cpp" }
+		files { "../framework/framework.h" }
 		files {...}
 		targetdir "bin"