OpenGL圆形纹理填充

#include <glad/glad.h>
#include <glfw3.h>

#include <iostream>
#include "myOpenGL.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
	glViewport(0, 0, width, height);
}
float firstTriangle[] = {
	-0.5f,-0.5f,0.0f,
	0.5f,-0.5f,0.0f,
	0.0f,0.5f,0.0f
};
const int n = 200;
GLfloat v[n] = { 0.0f };
GLfloat R = 0.5f;
const GLfloat Pi = 3.1415926f;

GLfloat angle = 2 * Pi / n;
void initVertex() {
	for (int i = 0; i < n; ++i) {
		v[i] = R * cos(2 * Pi / n * i);
		++i;
		v[i] = R * sin(2 * Pi / n * i);
		++i;
		v[i] = 0.0f;
		++i;
		v[i] = 1.0 - (GLfloat)((R * cos(angle * i)) + 1.0)*0.5;
		++i;
		v[i] = (GLfloat)((R * sin(angle * i)) + 1.0)*0.5;
	}
}
void printVertex() {
	for (int i = 0; i < n; i++) {
		if (i % 3 == 0) {
			std::cout << std::endl;
		}
		std::cout << v[i] << ",";
	}
}
const char * vertexShaderSource = "#version 330 core\n"
"layout(location = 0) in vec3 aPos;\n"
"layout(location=1) in vec3 aColor;\n"
"layout(location=2) in vec2 aTexCoord;\n"
"out vec3 ourColor;\n"
"out vec2 TexCoord;\n"
"void main()\n"
"{\n"
"	gl_Position = vec4(aPos,1.0);\n"
"ourColor = aColor;\n"
"TexCoord = aTexCoord;\n"
"}\0";

const char * fragmentShader1Source = "#version 330 core\n"
"out vec4 FragColor;\n"

"in vec3 ourColor;\n"
"in vec2 TexCoord;\n"

"uniform sampler2D ourTexture;\n"

"void main()\n"
"{\n"
"	FragColor = texture(ourTexture, TexCoord);\n"
"}\0";
int main() {
	glfwInit();
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

	GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "open gl", NULL, NULL);
	
	glfwMakeContextCurrent(window);

	gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);

	unsigned int vs = glCreateShader(GL_VERTEX_SHADER);
	glShaderSource(vs, 1, &vertexShaderSource, NULL);
	glCompileShader(vs);

	unsigned int fs = glCreateShader(GL_FRAGMENT_SHADER);
	glShaderSource(fs, 1, &fragmentShader1Source,NULL);
	glCompileShader(fs);

	unsigned int sProgram = glCreateProgram();
	glAttachShader(sProgram, vs);
	glAttachShader(sProgram, fs);
	glLinkProgram(sProgram);
	initVertex();
	printVertex();
	unsigned int vbo, vao;
	glGenVertexArrays(1, &vao);
	glGenBuffers(1, &vbo);
	glBindVertexArray(vao);
	glBindBuffer(GL_ARRAY_BUFFER, vbo);
	glBufferData(GL_ARRAY_BUFFER, sizeof(v), v, GL_STATIC_DRAW);
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void *)0);
	glEnableVertexAttribArray(0);
	glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
	glEnableVertexAttribArray(2);

	int width, height, nrChannels;
	unsigned int texture;
	glGenTextures(1, &texture);
	glBindTexture(GL_TEXTURE_2D, texture);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);	// set texture wrapping to GL_REPEAT (default wrapping method)
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
	// set texture filtering parameters
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

	unsigned char *data = stbi_load("2019-2-25.jpg", &width, &height, &nrChannels, 0);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
	glGenerateMipmap(GL_TEXTURE_2D);
	stbi_image_free(data);

	glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
	while (!glfwWindowShouldClose(window))
	{
		glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
		glClear(GL_COLOR_BUFFER_BIT);

		glUseProgram(sProgram);
		glBindVertexArray(vao);
		glDrawArrays(GL_TRIANGLE_FAN, 0, 40);

		glfwSwapBuffers(window);
		glfwPollEvents();
	}
}

 

void initVertex() {
	for (int i = 0; i < n; ++i) {
		v[i] = R * cos(2 * Pi / n * i);
		++i;
		v[i] = R * sin(2 * Pi / n * i);
		++i;
		v[i] = 0.0f;
		++i;
		v[i] = 1.0 - (GLfloat)((R * cos(angle * i)) + 1.0)*0.5;
		++i;
		v[i] = (GLfloat)((R * sin(angle * i)) + 1.0)*0.5;
	}
}

这段代码是整个程序的核心,它定义了纹理坐标,和顶点位置,xyzst 。

整个程序难就难在,纹理坐标和顶点坐标的转换,上面代码就完成了这一点,然后把数据传给顶点着色器(Vertex Shader)进行解析,然后再传给片段着色器(Fragment shader)。

这里有opengl的教程https://learnopengl-cn.github.io/01%20Getting%20started/04%20Hello%20Triangle/#_3

 

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值