Untitled
raw download clone
TEXT
views 39
,
size 5083 b
#include <sb6.h>
#include <math.h>
#include <string>

float mod(float a, float n)
{
	return a - n * floor(a / n);
}

void hsvToRgb(GLfloat h, GLfloat s, GLfloat v, GLfloat* rgb)
{
	h = mod(h, 360.0f);
	GLfloat c = v * s;
	GLfloat x = c * (1 - abs(mod(h / 60, 2.0f) - 1.0f));
	GLfloat m = v - c;

	c += m;
	x += m;

	if (h <  60) { rgb[0] = c; rgb[1] = x; rgb[2] = m; }
	else if (h < 120) { rgb[0] = x; rgb[1] = c; rgb[2] = m; }
	else if (h < 180) { rgb[0] = m; rgb[1] = c; rgb[2] = x; }
	else if (h < 240) { rgb[0] = m; rgb[1] = x; rgb[2] = c; }
	else if (h < 300) { rgb[0] = x; rgb[1] = m; rgb[2] = c; }
	else { rgb[0] = c; rgb[1] = m; rgb[2] = x; }
}

static GLfloat color[] = { 0.0f, 0.0f, 0.0f, 0.0f };

class simpletexture_app : public sb6::application
{
	void init()
	{
		static const char title[] = "Open GL Simple Texture";

		sb6::application::init();
		memcpy(info.title, title, sizeof(title));
	}

	static void printShaderLog(GLuint shader)
	{
		std::string str;
		GLint len;

		glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &len);

		if (len != 0)
		{
			str.resize(len);
			glGetShaderInfoLog(shader, len, NULL, &str[0]);
		}

#ifdef _WIN32
		OutputDebugStringA(str.c_str());
#endif
	}

	void generateTexture(float* data, int width, int height)
	{
		int x, y;

		for (y = 0; y < height; y++)
		{
			for (x = 0; x < width; x++)
			{
				data[(y * width + x) * 4 + 0] = (float)	((x & y) & 0xFF) / 255.0f;
				data[(y * width + x) * 4 + 1] = (float)	(sin((x / 2) / width) + 1.0f) / 2.0f;
				data[(y * width + x) * 4 + 2] = (float)	((x ^ y) & 0xFF) / 255.0f;
				data[(y * width + x) * 4 + 3] = (float) 1.0f;
			}
		}
	}

	virtual void startup()
	{
		static const char* vsSource[] = {
			"#version 420 core                                      \n"
			"                                                       \n"
			"layout(location=0) in vec4 offset;                     \n"
			"layout(location=1) in vec3 axes;                       \n"
			"                                                       \n"
			"const vec4 vertices[] = vec4[](                        \n"
			"    vec4(0.5, -0.5, 0.5, 1.0),                         \n"
			"    vec4(-0.5, -0.5, 0.5, 1.0),                        \n"
			"    vec4(0.0, 0.5, 0.5, 1.0)                           \n"
			");                                                     \n"
			"                                                       \n"
			"void main()                                            \n"
			"{                                                      \n"
			"    gl_Position = vertices[gl_VertexID]                \n"
			"         + axes[gl_VertexID]                           \n"
			"         + offset;                                     \n"
			"}                                                      \n"
			"                                                       \n"
		};

		static const char* fsSource[] = {
			"#version 420 core																				\n"
			"																								\n"
			"uniform sampler2D sampler;																		           \n"
			"out vec4 color;																				\n"
			"																								\n"
			"void main()																					\n"
			"{																								\n"
			"    color = texture(sampler, gl_FragCoord.xy / textureSize(sampler, 0));                                      \n"
			"																						\n"
			"																		\n"
			"}																		  \n"
			"																		  \n"
		};

		// Procedural texturing
		glGenTextures(1, &texture);
		glBindTexture(GL_TEXTURE_2D, texture);
		glTexStorage2D(GL_TEXTURE_2D, 8, GL_RGBA32F, 256, 256);
		float* data = new float[256 * 256 * 4];
		generateTexture(data, 256, 256);
		glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 256, 256, GL_RGBA, GL_FLOAT, data);

		if (program)
		{
			glDeleteProgram(program);
		}

		// Initialize Vertex and Fragment Shader
		program = glCreateProgram();

		GLuint vs = glCreateShader(GL_VERTEX_SHADER);
		glShaderSource(vs, 1, vsSource, NULL);
		glCompileShader(vs);

		GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
		glShaderSource(fs, 1, fsSource, NULL);
		glCompileShader(fs);

		glAttachShader(program, vs);
		glAttachShader(program, fs);

		glLinkProgram(program);

		glGenVertexArrays(1, &vao);
		glBindVertexArray(vao);
	}

	virtual void render(double currentTime)
	{
		hsvToRgb(currentTime * 20.0, 1.0f, 1.0f, color);

		glClearBufferfv(GL_COLOR, 0, color);

		glUseProgram(program);

		GLfloat offset[] = {
			sin(currentTime) * 0.1f,
			cos(currentTime) * 0.1f,
			0.0f,
			0.0f
		};

		GLfloat axes[] = {
			sin(currentTime) * 0.1f,
			cos(currentTime) * 0.1f,
			sin(currentTime) * cos(currentTime) * 0.1f
		};

		glVertexAttrib4fv(0, offset);
		glVertexAttrib3fv(1, axes);

		glDrawArrays(GL_TRIANGLES, 0, 3);
	}

	virtual void shutdown()
	{
		glDeleteVertexArrays(1, &vao);
		glDeleteProgram(program);
	}

private:
	GLuint program = 0;
	GLuint vao = 0;
	GLuint texture;
};

DECLARE_MAIN(simpletexture_app);
close fullscreen
Login or Register to edit or fork this paste. It's free.