1. 程式人生 > >OpenGL 入門基礎教程 —— 在第一個視窗繪製一個三角形

OpenGL 入門基礎教程 —— 在第一個視窗繪製一個三角形

首先了解緩衝區物件相關:

1:緩衝區物件的定義

GLuint vertexbuffer;  //定義了一個unsigned int型別的正整形緩衝區物件

2:建立緩衝區物件—建立
void glGenBuffers(GLsizei n, GLuint *buffers);
//在buffers陣列中返回當前n個未使用的名稱,表示緩衝區物件

3:啟用緩衝區物件—繫結
void glBindBuffer(GLenum target, GLuint buffer); //指定當前活動緩衝區的物件

4:用資料分配和初始化緩衝區物件—填充
void glBufferData(GLenum target, GLsizeiptr size, const GLvoid *data, GLenum usage);
//target:可以是GL_ARRAY_BUFFER()(頂點資料)或GL_ELEMENT_ARRAY_BUFFER(索引資料)
//size:儲存相關資料所需的記憶體容量
//data:用於初始化緩衝區物件,可以是一個指向客戶區記憶體的指標,也可以是NULL
//usage:資料在分配之後如何進行讀寫,如GL_STREAM_READ,GL_STREAM_DRAW,GL_STREAM_COPY
畫形狀的流程:

1:啟用頂點陣列/關閉Disable

glEnableVertexAttribArray(0); //引數為頂點陣列的索引值
索引值:index必須是一個介於0到GL_MAX_VERTEX_ATTRIBS-1之間的值。

2:指定了渲染時索引值為 index 的頂點屬性陣列的資料格式和位置—配置

glVertexAttribPointer(
			0,                  // attribute 0. No particular reason for 0, but must match the layout in the shader.
			3,                  // size
			GL_FLOAT,           // type
			GL_FALSE,           // normalized?
			0,                  // stride
			(void*)0            // array buffer offset
		);

void glVertexAttribPointer( GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride,const GLvoid * pointer);

引數:
index:指定要修改的頂點屬性的索引值
size:指定每個頂點屬性的元件數量。必須為1、2、3或者4。初始值為4。(如position是由3個(x,y,z)組成,而顏色是4個(r,g,b,a))
type:指定陣列中每個元件的資料型別。可用的符號常量有GL_BYTE, GL_UNSIGNED_BYTE, GL_SHORT,GL_UNSIGNED_SHORT, GL_FIXED, 和 GL_FLOAT,初始值為GL_FLOAT。
normalized:指定當被訪問時,固定點資料值是否應該被歸一化(GL_TRUE)或者直接轉換為固定點值(GL_FALSE)。
stride:指定連續頂點屬性之間的偏移量。如果為0,那麼頂點屬性會被理解為:它們是緊密排列在一起的。初始值為0。
pointer:指定第一個元件在陣列的第一個頂點屬性中的偏移量。該陣列與GL_ARRAY_BUFFER繫結,儲存於緩衝區中。初始值為0;

3:根據頂點陣列中的座標資料和指定的模式,進行繪製

glDrawArrays(GL_TRIANGLES, 0, 3);

 void glDrawArrays (GLenum mode, GLint first, GLsizei count);
引數說明:
mode,繪製方式,OpenGL2.0以後提供以下引數:GL_POINTS、GL_LINES、GL_LINE_LOOP、GL_LINE_STRIP、GL_TRIANGLES、GL_TRIANGLE_STRIP、GL_TRIANGLE_FAN。
first,從陣列快取中的哪一位開始繪製,一般為0。
count,陣列中頂點的數量。

GL_TRIANGLES - 這個引數意味著OpenGL使用三個頂點來組成圖形。所以,在開始的三個頂點,將用頂點1,頂點2,頂點3來組成一個三角形。完成後,在用下一組的三個頂點(頂點4,5,6)來組成三角形,直到陣列結束,注意多個三角形之間不連線。且頂點在設定的過程中一定要注意需要成三角形,別再一條直線上了~
GL_TRIANGLE_STRIP - OpenGL的使用將最開始的兩個頂點出發,然後遍歷每個頂點,這些頂點將使用前2個頂點一起組成一個三角形。

GL_TRIANGLE_FAN - 在跳過開始的2個頂點,然後遍歷每個頂點,讓OpenGL將這些頂點於它們前一個,以及陣列的第一個頂點一起組成一個三角形

// Include standard headers
#include <stdio.h>
#include <stdlib.h>

// Include GLEW
#include <GL/glew.h>

// Include GLFW
#include <glfw3.h>
GLFWwindow* window;

// Include GLM
#include <glm/glm.hpp>
using namespace glm;

#include <common/shader.hpp>

int main( void )
{
	// Initialise GLFW
	if( !glfwInit() )
	{
		fprintf( stderr, "Failed to initialize GLFW\n" );
		getchar();
		return -1;
	}

	glfwWindowHint(GLFW_SAMPLES, 4);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
	glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

	// Open a window and create its OpenGL context
	window = glfwCreateWindow( 1024, 768, "Tutorial 02 - Red triangle", NULL, NULL);
	if( window == NULL ){
		fprintf( stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n" );
		getchar();
		glfwTerminate();
		return -1;
	}
	glfwMakeContextCurrent(window);

	// Initialize GLEW
	glewExperimental = true; // Needed for core profile
	if (glewInit() != GLEW_OK) {
		fprintf(stderr, "Failed to initialize GLEW\n");
		getchar();
		glfwTerminate();
		return -1;
	}

	// Ensure we can capture the escape key being pressed below
	glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);

	// Dark blue background
	glClearColor(0.0f, 0.0f, 0.4f, 0.0f);

	//建立VAO陣列,並將其設定為當前物件
	GLuint VertexArrayID;
	glGenVertexArrays(1, &VertexArrayID);
	glBindVertexArray(VertexArrayID);

	// Create and compile our GLSL program from the shaders
	GLuint programID = LoadShaders( "SimpleVertexShader.vertexshader", "SimpleFragmentShader.fragmentshader" );
	

	//三角形的頂點陣列
	static const GLfloat g_vertex_buffer_data[] = { 
		-1.0f, -1.0f, 0.0f,
		 1.0f, -1.0f, 0.0f,
		 0.0f,  0.0f, 0.0f,
		 1.0f, 1.0f, 0.0f,
		 -1.0f, 1.0f, 0.0f,
		 0.0f,  0.0f, 0.0f
	};

	//STRIP形式的頂點陣列
	static const GLfloat g_vertex_buffer_data_square[] = {
		0.0f,1.0f, 0.0f,
		0.0f,-1.0f, 0.0f,
		1.0f,0.0f, 0.0f,
		-1.0f,0.0f, 0.0f,
		
	};

	//通過緩衝(buffer)將三角形頂點資料傳給OpenGL
	GLuint vertexbuffer;              //首先定義一個GLunit型別的頂點緩衝區物件
	glGenBuffers(1, &vertexbuffer);   //將該頂點緩衝置為當前物件
	glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); //啟用緩衝區物件
	glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW); 	//用資料初始化緩衝區物件

	do{

		// 用之前設定的顏色清除螢幕
		glClear( GL_COLOR_BUFFER_BIT );

		// 使用之前定義的著色器
		glUseProgram(programID);

		// 啟用頂點陣列
		glEnableVertexAttribArray(0);
		glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
		glVertexAttribPointer(      //緩衝區的配置
			0,                  // attribute 0. No particular reason for 0, but must match the layout in the shader.
			3,                  // 這裡注意是頂點屬性的個數
			GL_FLOAT,           // type
			GL_FALSE,           // normalized?
			0,                  // stride
			(void*)0            // array buffer offset
		);

		// Draw the triangle !
		glDrawArrays(GL_TRIANGLES, 0, 6); // 3 indices starting at 0 -> 1 triangle

		glDisableVertexAttribArray(0);

		// Swap buffers
		glfwSwapBuffers(window);
		glfwPollEvents();

	} // Check if the ESC key was pressed or the window was closed
	while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
		   glfwWindowShouldClose(window) == 0 );

	// Cleanup VBO
	glDeleteBuffers(1, &vertexbuffer);
	glDeleteVertexArrays(1, &VertexArrayID);

	glDeleteProgram(programID);

	// Close OpenGL window and terminate GLFW
	glfwTerminate();

	return 0;
}