💭

頂点シェーダーとフラグメントシェーダーを利用し、テクスチャをマッピングする方法

2024/11/12に公開
#include <iostream>
#include <sstream>
#define GLEW_STATIC
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include"stb_image/stb_image.h"

int Widht = 800,Height = 600;

const GLchar* vertexShaderSrc =

"#version 330 core \n"
"layout (location = 0) in vec3 pos;"
"layout (location = 1) in vec2 texCoord;"
"out vec2 TexCoord;"
"uniform vec2 posOffset;"
"void main()"
"{"
"    gl_Position = vec4(pos.x + posOffset.x, pos.y + posOffset.y, pos.z, 1.0f);"
"    TexCoord = texCoord;                                                      "
"}";

const GLchar* fragmentShaderSrc =
"#version 330 core \n"
"in vec2 TexCoord;"
"uniform vec4 vertColor;"
"out vec4 frag_color;"
"uniform sampler2D texSampler1;"
"void main()"
"{"
"    frag_color = texture(texSampler1, TexCoord);"
"}";
//"    //frag_color = vertColor;"

void glfw_onKey(GLFWwindow* window, int key, int scancode, int action, int mode);

int main()
{
	glfwInit();
	GLFWwindow* gWindow = glfwCreateWindow(Widht, Height, "Window", NULL, NULL);
	glfwMakeContextCurrent(gWindow);
	glewExperimental = GL_TRUE;
	glewInit();

	glfwSetKeyCallback(gWindow, glfw_onKey);

	GLfloat vertices[] = {
		// position			 // tex coords
		-0.5f,  0.5f, 0.0f,	 0.0f, 1.0f,		// Top left
		 0.5f,  0.5f, 0.0f,	 1.0f, 1.0f,		// Top right
		 0.5f, -0.5f, 0.0f,	 1.0f, 0.0f,		// Bottom right
		-0.5f, -0.5f, 0.0f,	 0.0f, 0.0f			// Bottom left 
	};

	GLuint indices[] = {
		0, 1, 2,  // First Triangle
		0, 2, 3   // Second Triangle
	};

	GLuint vbo, ibo, vao;

	glGenBuffers(1, &vbo);
	glBindBuffer(GL_ARRAY_BUFFER, vbo);
	glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

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

	// Position attribute
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5* sizeof(GLfloat), (GLvoid*)(0));
	glEnableVertexAttribArray(0);
	// Texture Coord attribute
	glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
	glEnableVertexAttribArray(1);

	glGenBuffers(1, &ibo);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

	glBindVertexArray(0);

	//////////////////////////////////////////////////////////////////
	GLuint vs = glCreateShader(GL_VERTEX_SHADER);
	glShaderSource(vs, 1, &vertexShaderSrc, NULL);
	glCompileShader(vs);

	GLint fs = glCreateShader(GL_FRAGMENT_SHADER);
	glShaderSource(fs, 1, &fragmentShaderSrc, NULL);
	glCompileShader(fs);
	
	GLint shaderProgram = glCreateProgram();
	glAttachShader(shaderProgram, vs);
	glAttachShader(shaderProgram, fs);
	glLinkProgram(shaderProgram);
	GLchar infoLog[512];
	glGetProgramInfoLog(shaderProgram, sizeof(infoLog), NULL, infoLog);
	std::cout << infoLog << std::endl;
	glDeleteShader(vs);
	glDeleteShader(fs);
	///////////////////////////////////////////
	bool generateMipMaps = true;
	GLuint mTextrue;
	std::string fileName = "textures/airplane.png";
	int width, height, components;
	unsigned char* imageData = stbi_load(fileName.c_str(),
		&width, &height, &components, STBI_rgb_alpha);

	glGenTextures(1, &mTextrue);
	glBindTexture(GL_TEXTURE_2D, mTextrue);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA,
		GL_UNSIGNED_BYTE, imageData);
	glGenerateMipmap(GL_TEXTURE_2D);
	stbi_image_free(imageData);
	glBindTexture(GL_TEXTURE_2D, 0);

	while (!glfwWindowShouldClose(gWindow))
	{
		glfwPollEvents();
		glClearColor(0.f, 0.5f, 0.5f, 1.0f);
		glClear(GL_COLOR_BUFFER_BIT);

		glUseProgram(shaderProgram);

		glActiveTexture(GL_TEXTURE0);
		glBindTexture(GL_TEXTURE_2D, mTextrue);
		GLint TexLoc = glGetUniformLocation(shaderProgram, "texSampler1");
		glUniform1i(TexLoc, 0);


		GLfloat time = (GLfloat)glfwGetTime();
		GLfloat blueColor = (sin(time) / 2) + 0.5f;
		glm::vec2 posoffset;
		posoffset.x = sin(time) / 2;
		posoffset.y = cos(time) / 2;
		GLint posOffsetLoc = glGetUniformLocation(shaderProgram, "posOffset");
		glUniform2f(posOffsetLoc, posoffset.x, posoffset.y);

		//GLint vertColorLoc = glGetUniformLocation(shaderProgram, "vertColor");
		//glUniform4f(vertColorLoc, 0.0f, 0.0f, blueColor, 1.0f);

		glBindVertexArray(vao);
		glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
		glBindVertexArray(0);

		glfwSwapBuffers(gWindow);
	}
	glDeleteTextures(1, &mTextrue);
	glDeleteProgram(shaderProgram);
	glDeleteVertexArrays(1, &vao);
	glDeleteBuffers(1, &vbo);
	glDeleteBuffers(1, &ibo);

	glfwTerminate();

	return 0;
}

void glfw_onKey(GLFWwindow* window, int key, int scancode, int action, int mode)
{
	if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
	{
		glfwSetWindowShouldClose(window, GL_TRUE);
	}
}

Discussion