紋理渲染

#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include<iostream>
#include"shader.h"
#include"stb_image.h"
#include<string>
using namespace std;

void framebuffer_size_callback(GLFWwindow* window, int width, int height){
	glViewport(0, 0, width, height);
}

void processInput(GLFWwindow *window)
{
	if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
		glfwSetWindowShouldClose(window, true);
}

const char *vertexShaderSource = "#version 450 core\n"
"layout (location = 0) in vec3 aPos;\n"
"layout (location = 1) in vec3 aColor;\n"
"out vec3 ourColor;\n"
"void main()\n"
"{\n"
"   gl_Position = vec4(aPos, 1.0);\n"
"   ourColor = aColor;\n"
"}\0";

const char *fragmentShaderSource = "#version 450 core\n"
"out vec4 FragColor;\n"
"in vec3 ourColor;\n"
"void main()\n"
"{\n"
"   FragColor = vec4(ourColor, 1.0f);\n"
"}\n\0";

//片段着色器

const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;

int main(){
	glfwInit();
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
	//glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#ifdef __APPLE__
	glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // uncomment this statement to fix compilation on OS X
#endif

	GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
	//寬 高 標題
	if (window == NULL)
	{
		cout << "Failed to create GLFW window" <<endl;
		glfwTerminate();
		return -1;
	}
	glfwMakeContextCurrent(window);

	if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))//初始化GLAD
	{
		cout << "Failed to initialize GLAD" <<endl;
		return -1;
	}

	glViewport(0, 0, 800, 600);

	Shader ourShader("4.5.1.vs", "4.5.1.fs");

	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,   // 左下
			-0.5f,  0.5f, 0.0f,   1.0f, 1.0f, 0.0f,   0.0f, 1.0f    // 左上
	};
	unsigned int indices[] = {
	   0, 1, 3, // first triangle
	   1, 2, 3  // second triangle
	};


	unsigned int VBO, VAO, EBO;
	//VBO頂點緩衝對象,VAO頂點數組對象,EBO索引緩衝對象
	glGenVertexArrays(1, &VAO);
	glGenBuffers(1, &VBO);
	glGenBuffers(1, &EBO);
	//用glGenBuffers函數和一個緩衝ID生成一個VBO對象和一個EBO

	glBindVertexArray(VAO);
	//用glBindVertexArray綁定VAO

	glBindBuffer(GL_ARRAY_BUFFER, VBO);

	glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
	//用glBindBuffer函數把新創建的緩衝對象綁定到GL_ARRAY_BUFFER目標上

	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
	//用glBindBuffer函數把新創建的EBO綁定到GL_ELEMENT_ARRAY_BUFFER目標上

	glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
	//用glBufferData函數,它會把之前定義的頂點數據複製到緩衝的內存中
	/*
		GL_STATIC_DRAW :數據不會或幾乎不會改變。
		GL_DYNAMIC_DRAW:數據會被改變很多。
		GL_STREAM_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);
	/*
	第一個參數指定要配置的頂點屬性
	第二個參數指定頂點屬性的大小。頂點屬性是一個vec3,它由3個值組成,所以大小是3。
	第三個參數指定數據的類型,這裏是GL_FLOAT
	第四個參數定義我們是否希望數據被標準化(Normalize)。如果我們設置爲GL_TRUE,
	所有數據都會被映射到0(對於有符號型signed數據是-1)到1之間。我們把它設置爲GL_FALSE。
	第五個參數叫做步長(Stride),它告訴我們在連續的頂點屬性組之間的間隔
	從這個屬性第二次出現的地方到整個數組0位置之間有多少字節
	最後一個參數的類型是void*,所以需要我們進行這個奇怪的強制類型轉換。
	它表示位置數據在緩衝中起始位置的偏移量(Offset)。
	*/

	glBindVertexArray(0);

	//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);啓用線框模式
	//glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);恢復默認模式


	//紋理處理
	unsigned int texture1, texture2;
	glGenTextures(1, &texture1);
	glBindTexture(GL_TEXTURE_2D, texture1); // all upcoming GL_TEXTURE_2D operations now have effect on this texture object
	// set the texture wrapping parameters
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);	
	// set texture wrapping to GL_REPEAT (default wrapping method)
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	// set texture filtering parameters
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	// load image, create texture and generate mipmaps
	stbi_set_flip_vertically_on_load(true);
	int width, height, nrChannels;
	//string path = "C:/Users/Administrator/Desktop/OPENGL/image/container.jpg";
	unsigned char *data = stbi_load("C:/Users/Administrator/Desktop/OPENGL/image/container.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);
		if (nrChannels == 3)//rgb 適用於jpg圖像
			glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);//後面一個是RGBA
		else if (nrChannels == 4)//rgba 適用於png圖像
			glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);//注意,兩個都是RGBA
		//std::cout<<nrChannels<<std::endl;
		glGenerateMipmap(GL_TEXTURE_2D);

		/*
		第一個參數指定了紋理目標(Target)。設置爲GL_TEXTURE_2D意味着會生成與
		當前綁定的紋理對象在同一個目標上的紋理(任何綁定到GL_TEXTURE_1D
		和GL_TEXTURE_3D的紋理不會受到影響)。
		第二個參數爲紋理指定多級漸遠紋理的級別,如果你希望單獨手動設置每個多級漸遠紋理的級別的話。
		這裏我們填0,也就是基本級別。
		第三個參數告訴OpenGL我們希望把紋理儲存爲何種格式。我們的圖像只有RGB值,
		因此我們也把紋理儲存爲RGB值。
		第四個和第五個參數設置最終的紋理的寬度和高度。我們之前加載圖像的時候儲存了它們,
		所以我們使用對應的變量。
		下個參數應該總是被設爲0(歷史遺留的問題)。
		第七第八個參數定義了源圖的格式和數據類型。我們使用RGB值加載這個圖像,
		並把它們儲存爲char(byte)數組,我們將會傳入對應值。
		最後一個參數是真正的圖像數據。
		*/
	}
	else
	{
		cout << "Failed to load texture" << endl;
	}
	stbi_image_free(data);

	glGenTextures(1, &texture2);
	glBindTexture(GL_TEXTURE_2D, texture2); // all upcoming GL_TEXTURE_2D operations now have effect on this texture object
	// set the texture wrapping parameters
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	// set texture wrapping to GL_REPEAT (default wrapping method)
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	// set texture filtering parameters
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	// load image, create texture and generate mipmaps
	//path = "C:/Users/Administrator/Desktop/OPENGL/image/awesomeface.png";
	data = stbi_load("C:/Users/Administrator/Desktop/OPENGL/image/awesomeface.png", &width, &height, &nrChannels, 0);
	if (data)
	{
		cout << "132\n";
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
		glGenerateMipmap(GL_TEXTURE_2D);
	}
	else
	{
		cout << "Failed to load texture" << endl;
	}
	stbi_image_free(data);

	ourShader.use(); // don't forget to activate/use the shader before setting uniforms!
	// either set it manually like so:
	glUniform1i(glGetUniformLocation(ourShader.ID, "texture1"), 0);
	// or set it via the texture class
	ourShader.setInt("texture2", 1);


	while (!glfwWindowShouldClose(window))
	{
		processInput(window);

		// 渲染
		// 清除顏色緩衝
		glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
		glClear(GL_COLOR_BUFFER_BIT);
		
		glActiveTexture(GL_TEXTURE0);
		glBindTexture(GL_TEXTURE_2D, texture1);
		glActiveTexture(GL_TEXTURE1);
		glBindTexture(GL_TEXTURE_2D, texture2);

		ourShader.use();
		glBindVertexArray(VAO); 
		glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
		//第一個參數,渲染基元類型
		//第二個參數,頂點數量
		//第三個,參數值類型
		//第四個,偏移量

		glfwSwapBuffers(window);
		//函數會交換顏色緩衝
		glfwPollEvents();
		//檢查有沒有觸發什麼事件(比如鍵盤輸入、鼠標移動等)、更新窗口狀態,
		//並調用對應的回調函數(可以通過回調方法手動設置)。
	}
	glDeleteVertexArrays(1, &VAO);
	glDeleteBuffers(1, &VBO);
	glDeleteBuffers(1, &EBO);
	//解除綁定
	glfwTerminate();

	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章