Untitled
raw download clone
TEXT
views 18
,
size 6605 b
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <chrono>
#include "extern/stb_image.h"

//author : laennocode
#define ASSERT(x) if(!(x)) __debugbreak();
#define GLCall(x) GLClearError();x;ASSERT(!GLShowErrors());
static void GLClearError() {
	while (glGetError() != GL_NO_ERROR) {
	}
}
static bool GLShowErrors() {
	bool errors = false;
	while (GLenum error = glGetError()) {
		std::cout << "[opengl error] {" << error << ")" << std::endl;
		errors = true;
	}
	return errors;
}
void error_callback(int error, const char* description)
{
	fprintf(stderr, "Error: %s\n", description);
}
static unsigned int compileShader(unsigned int shaderType, const std::string& source) {
	unsigned int id = glCreateShader(shaderType);
	const char* src = source.c_str();
	glShaderSource(id, 1, &src, nullptr);
	glCompileShader(id);
	int result;
	glGetShaderiv(id, GL_COMPILE_STATUS, &result);
	if (!result) {
		int length;
		glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);
		char* message = (char*)alloca(length * sizeof(char));
		glGetShaderInfoLog(id, length, &length, message);
		std::cout << "error while compiling the shader : " << message << std::endl;
		glDeleteShader(id);
		return 0;
	}
	return id;
}

static unsigned int createShader(const std::string& vertexShader, const std::string& fragmentShader) {
	unsigned int program = glCreateProgram();
	unsigned int verShad = compileShader(GL_VERTEX_SHADER, vertexShader);
	unsigned int fraShad = compileShader(GL_FRAGMENT_SHADER, fragmentShader);
	glAttachShader(program, verShad);
	glAttachShader(program, fraShad);
	glLinkProgram(program);
	glValidateProgram(program);
	glDeleteShader(verShad);
	glDeleteShader(fraShad);
	return program;
}

int main(void)
{
	stbi_set_flip_vertically_on_load(1);
	int mWidth, mHeight;
	int channelsInFile;
	unsigned int texture_id;
	unsigned char* imageData = stbi_load("res/texture/avatar.png", &mWidth, &mHeight,&channelsInFile, 4);
	std::cout << channelsInFile << "," << mWidth << "," << mHeight << "," << sizeof(imageData)<< std::endl;
	unsigned int shader;

	std::string vertexShader =
		"#version 330\n"
		"\n"
		"layout(location = 0)in vec4 position;\n"
		"layout(location = 1)in vec4 texCoord;\n"
		"out vec2 v_texCoord;\n"
		"void main()\n"
		"{\n"
		"   v_texCoord = texCoord.xy;\n"
		"   gl_Position= position;\n"
		"}\n";
	std::string fragmentShader =
		"#version 330\n"
		"\n"
		"uniform float time;\n"
		"uniform sampler2D u_texture;\n"
		"in vec2 v_texCoord;\n"
		"layout(location = 0) out vec4 color;\n"
		"void main()\n"
		"{\n"
		"vec4 texColor = texture(u_texture, v_texCoord);\n"
		"float squared =  (gl_FragCoord.xy[0] - 320) * (gl_FragCoord.xy[0] - 320) + (gl_FragCoord.xy[1] - 240) * (gl_FragCoord.xy[1] - 240); \n"
		"float sqr =sqrt(  squared*10  ) -  time/10 ;\n"
		"   vec4 colorO = vec4(sin(gl_FragCoord.xy[0]/300.0 + time/300.0),cos(gl_FragCoord.xy[1]/300.0 + time/200.0), 1.0, 1.0);\n"
		"color = texColor + 0.1*colorO;\n"
		"}\n";
	GLFWwindow* window;
	glfwSetErrorCallback(error_callback);
	unsigned int vertexBuffer;
	unsigned int indexBuffer;
	unsigned int indexes[] = {
		0,1,2,
		2,3,0
	};
	float vertices[] = {
		-1.0,-1.0, 0.0, 0.0,
		-1.0, 1.0, 0.0, 1.0,
		 1.0, 1.0, 1.0, 1.0,
		 1.0,-1.0, 1.0, 0.0

	};
	/* Initialize the library */
	if (!glfwInit())
		return -1;
	/* Create a windowed mode window and its OpenGL context */
	window = glfwCreateWindow(640, 560, "Hello World", NULL, NULL);

	if (!window)
	{
		std::cout << glfwGetError(NULL) << std::endl;
		glfwTerminate();
		return -1;
	}
	std::cout << "window is successfully created" << std::endl;

	/* Make the window's context current */
	glfwMakeContextCurrent(window);
	if (glewInit() != GLEW_OK)
		std::cout << "glew not ok" << std::endl;
	std::cout << glGetString(GL_VERSION) << std::endl;

	GLCall(glGenBuffers(1, &vertexBuffer));
	std::cout << "generated buffer n°" << vertexBuffer << std::endl;
	GLCall(glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer));
	GLCall(glBufferData(GL_ARRAY_BUFFER, 16 * sizeof(float), vertices, GL_STATIC_DRAW));

	GLCall(glGenBuffers(1, &indexBuffer));
	std::cout << "generated buffer n°" << indexBuffer << std::endl;
	GLCall(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer));
	GLCall(glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6 * sizeof(unsigned int), indexes, GL_STATIC_DRAW));

	GLCall(glEnableVertexAttribArray(0));
	GLCall(glEnableVertexAttribArray(1));
	GLCall(glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 4, 0));
	GLCall(glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 4, (GLvoid*) (2*sizeof(float))));
	GLCall(glGenTextures(1, &texture_id));
	GLCall(glBindTexture(GL_TEXTURE_2D, texture_id));
	GLCall(glActiveTexture(GL_TEXTURE0));
	GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST));
	GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST));
	GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT));
	GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT));
	GLCall(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, mWidth, mHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData));
	GLCall(glGenerateMipmap(GL_TEXTURE_2D));
	
	
	
	int timeLocation;
	int texLocation;
	GLCall(shader = createShader(vertexShader, fragmentShader));
	GLCall(glUseProgram(shader));
	GLCall(timeLocation = glGetUniformLocation(shader, "time"));
	GLCall(texLocation = glGetUniformLocation(shader, "u_texture"));
	std::cout << timeLocation <<" "<< texLocation << std::endl;
	/* Loop until the user closes the window */
	while (!glfwWindowShouldClose(window))
	{
		auto time = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
		float actuaTime = time%300000000;
		//std::cout << actuaTime << std::endl;
		GLCall(glBindTexture(GL_TEXTURE_2D, texture_id));
		GLCall(glActiveTexture(GL_TEXTURE0));
		GLCall(glUniform1f(timeLocation, actuaTime));
		GLCall(glUniform1i(texLocation, 0));
		//GLCall(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
		/* Render here */
		GLCall(glClear(GL_COLOR_BUFFER_BIT));
		GLCall(glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr));
		/* Swap front and back buffers */
		glfwSwapBuffers(window);

		/* Poll for and process events */
		glfwPollEvents();
	}

	glDeleteBuffers(1, &vertexBuffer);
	glDeleteBuffers(1, &indexBuffer);
	glDeleteProgram(shader);
	stbi_image_free(imageData);
	glfwTerminate();
	return 0;
}
close fullscreen
Login or Register to edit or fork this paste. It's free.