OpenGL实现平移缩放旋转

#define GLEW_STATIC
#include  <GL\glew.h>
#include <GLFW/glfw3.h>

#include <iostream>

#include "Shader.h"

#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"


#include <glm.hpp>
#include <gtc/matrix_transform.hpp>
#include <gtc/type_ptr.hpp>

using namespace std;
//QQ技术交流群:386476712

//terminate	[ˈtɜ:mɪneɪt]  结束 终结
//hint [hɪnt] 提示 注意事项


void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
	cout << "width :  " << width << endl;
	glViewport(0, 0, width, height);
}

int main(){
	//glfw初始化
	//告诉glfw当前所用的OpenGL的版本号是3.3
	//告诉glfw当前使用核心模式,意味着我们只能使用OpenGL功能的一个子集(没有我们已不再需要的向后兼容特性)
	glfwInit();
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

	//使用glfw创建一个窗口
	GLFWwindow* window = glfwCreateWindow(800, 600, "Hunk Xu  OpenGL", NULL, NULL);
	if (window == NULL)
	{
		std::cout << "Failed to create GLFW window" << std::endl;
		glfwTerminate();
		return -1;
	}
	//通知GLFW将window的上下文设置为当前线程的主上下文,设置主活动窗口
	glfwMakeContextCurrent(window);

    
	//glew初始化
	if (glewInit() != GLEW_OK){
		printf("glew init failed");
		glfwTerminate();
		return -1;
	}


	unsigned int texture;
	glGenTextures(1, &texture);
	glBindTexture(GL_TEXTURE_2D, texture);
	// 为当前绑定的纹理对象设置环绕、过滤方式
	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);
	// 加载并生成纹理
	int width, height, nrChannels;
	unsigned char *data = stbi_load("wall.jpg", &width, &height, &nrChannels, 0);
	if (data)
	{
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
		glGenerateMipmap(GL_TEXTURE_2D);
	}
	else
	{
		std::cout << "Failed to load texture" << std::endl;
	}
	stbi_image_free(data);



	//告诉OpenGL视口(Viewport)大小
	//前两个参数为窗口左下角位置
	//后两个参数渲染窗口的宽和高(像素)
	glViewport(0, 0, 800, 600);
	//每当窗口调整大小时候,就调用这个函数
	glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);

	float vertices[] = {
		//     ---- 位置 ----       ---- 颜色 ----     - 纹理坐标 -
		0.5f, 0.5f, 0.0f,         1.0f, 0.0f, 0.0f,     1.0f, 1.0f,   // 右上
		0.5f, -0.5f, 0.0f,        0.0f, 1.0f, 0.0f,     1.0f, 0.0f,   // 右下
		-0.5f, -0.5f, 0.0f,       0.0f, 0.0f, 1.0f,     0.0f, 0.0f,   // 左下
	};
	unsigned int indices[] = {  // note that we start from 0!
		0, 1, 2,  // first Triangle
	};
	unsigned int VBO, VAO, EBO;
	glGenVertexArrays(1, &VAO);
	glGenBuffers(1, &VBO);
	glGenBuffers(1, &EBO);

	glBindVertexArray(VAO);
	glBindBuffer(GL_ARRAY_BUFFER, VBO);//第二个参数要设置成你想操作内存的标识,然后接下来的操作都是针对这一块内存进行的
	glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

	// 位置属性
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
	glEnableVertexAttribArray(0);
	// 颜色属性
	glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
	glEnableVertexAttribArray(1);

	glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
	glEnableVertexAttribArray(2);


	//将顶点数据buffer和之前的绑定关系进行解绑 用于打破之前的顶点数据buffer的绑定关系
	//使OpenGL的顶点数据buffer绑定状态恢复到默认状态。
	glBindBuffer(GL_ARRAY_BUFFER, 0); 



	//将VAO绑定到默认的VAO处,一般用于打破之前的VAO绑定关系
	//使OpenGL的VAO绑定状态恢复到默认状态
	glBindVertexArray(0);


	Shader* myShader = new Shader("vertexSource.txt", "fragementSource.txt");

	glm::mat4 trans;
	//trans = glm::translate(trans, glm::vec3(-0.5f, 0.0f, 0.0f));   //平移图像
	//trans = glm::rotate(trans, glm::radians(45.0f), glm::vec3(0.0, 0.0, 1.0));  //旋转图像
	trans = glm::scale(trans, glm::vec3(1.5f, 1.5f, 1.5f));  //缩放图像
	//渲染循环
	while (!glfwWindowShouldClose(window)){

		myShader->use();


		glBindTexture(GL_TEXTURE_2D, texture);
		glBindVertexArray(VAO); 

		unsigned int transformLoc = glGetUniformLocation(myShader->ID, "transform");
		glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(trans));

		glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, 0); //3表示3个顶点索引




		//双缓冲(Double Buffer)
		//前缓冲保存着最终输出的图像,它会在屏幕上显示;
		//而所有的的渲染指令都会在后缓冲上绘制。
		//当所有的渲染指令执行完毕后,我们交换(Swap)前缓冲和后缓冲,这样图像就立即呈显出来
		//不会出现图像闪烁的问题
		glfwSwapBuffers(window);  
		glfwPollEvents(); //轮询用户的输入(键盘移动,鼠标输入)
	}
	//终止
	glfwTerminate();
	return 0;
}


#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;
layout (location = 2) in vec2 aTexCoord;

out vec3 ourColor;
out vec2 TexCoord;
uniform  mat4 transform;

void main()
{
    gl_Position = transform*vec4(aPos, 1.0);
    ourColor = aColor;
    TexCoord = aTexCoord;
}

 下面是改动地方



 


下面就是放大后的图形 

 


FR:海涛高软(Hunk Xu)
QQ技术群:386476712

猜你喜欢

转载自blog.csdn.net/qq_15267341/article/details/83479613