使用vbo vao ebo畫三角形

2021-08-08 16:03:44 字數 3325 閱讀 2536

#include

#include

#include

void framebuffer_size_callback(glfwwindow* window, int width, int height);

void processinput(glfwwindow *window);

const

unsigned

int width = 800;

const

unsigned

int height = 600;

const

char *vertexshadersource = "#version 330 core\n"

"layout (location=0) in vec3 apos;\n"

"void main()\n"

"\n";

const

char *fragmentshadersource = "#version 330 core\n"

"out vec4 fragcolor;\n"

"void main()\n"

"\n"

;int main()

glfwmakecontextcurrent(window);

glfwsetframebuffersizecallback(window, framebuffer_size_callback);

if (!gladloadglloader((gladloadproc)glfwgetprocaddress))

//vertex shader

int vertexshader = glcreateshader(gl_vertex_shader);

glshadersource(vertexshader, 1, &vertexshadersource, null);

glcompileshader(vertexshader);

int success;

char infolog[512];

glgetshaderiv(vertexshader, gl_compile_status, &success);

if (!success)

// fragment shader

int fragmentshader = glcreateshader(gl_fragment_shader);

glshadersource(fragmentshader, 1, &fragmentshadersource, null);

glcompileshader(fragmentshader);

glgetshaderiv(fragmentshader, gl_compile_status, &success);

if (!success)

//連線shader

int shaderprogram = glcreateprogram();

glattachshader(shaderprogram, vertexshader);

glattachshader(shaderprogram, fragmentshader);

gllinkprogram(shaderprogram);

glgetprogramiv(shaderprogram, gl_link_status, &success);

if (!success)

gldeleteshader(vertexshader);

gldeleteshader(fragmentshader);

//頂點資料

float vertices = ;

//頂點索引

unsigned

int indices = ;

unsigned

int vbo, vao, ebo;

//建立vao物件

glgenvertexarrays(1, &vao);

//建立vbo物件

glgenbuffers(1,&vbo);

//建立ebo物件

glgenbuffers(1,&ebo);

//使用vao物件

glbindvertexarray(vao);

//使用vbo物件

glbindbuffer(gl_array_buffer, vbo);

//拷貝頂點資料到vbo

glbufferdata(gl_array_buffer, sizeof(vertices), vertices, gl_static_draw);

//使用ebo物件

glbindbuffer(gl_element_array_buffer,ebo);

//拷貝索引資料到ebo

glbufferdata(gl_element_array_buffer, sizeof(indices), indices, gl_static_draw);

//讓opengl知道怎麼結束頂點的屬性

glvertexattribpointer(0, 3, gl_float, gl_false, 3 * sizeof(float), (void*)0);

//實第乙個屬性(位置屬性)變為可用狀態

glenablevertexattribarray(0);

//解綁vbo物件

glbindbuffer(gl_array_buffer, 0);

//不用解綁ebo物件,因為vao物件還在啟用狀態

//glbindbuffer(gl_element_array_buffer, 0);

//解綁vao物件,在while迴圈中重新啟用

glbindvertexarray(0);

//畫成線框圖

glpolygonmode(gl_front_and_back, gl_line);

while (!glfwwindowshouldclose(window))

gldeletevertexarrays(1, &vao);

gldeletebuffers(1, &vbo);

gldeletebuffers(1, &ebo);

glfwterminate();

return0;}

//處理鍵盤的輸入

void processinput(glfwwindow *window)

}//當視窗大小改變的時候呼叫的回掉函式

void framebuffer_size_callback(glfwwindow *window, int width, int height)

效果圖如下:

使用border 畫三角形

先看看效果 step1 首先我們定義乙個div step2 設定一下div的初始樣式 效果是這個樣子 step3 修改css width 0px height 0px border width 50px 效果這個樣子 step4 修改border顏色 border color red transpa...

OpenGL畫三角形

修改paintgl函式 void nehewidget paintgl 清楚螢幕和深度快取 glclear gl color buffer bit gl depth buffer bit 重置當前的模型觀察矩陣 glloadidentity gltranslatef 1.5,0.0,6.0 開始繪製...

css border 畫三角形

前幾日在瀏覽 中無意發覺了人家用css來畫三角形,我覺得很有趣但不太會所以就問了一下師姐,發覺自己很無知,呵呵,原來很多人都是用這種方法畫三角形的,為什麼的,原因如下 1 一張小三角形的可能有幾百位元組,不過幾百位元組可以寫多少行 呢?2 太多會導致網頁載入得會很慢 先看 吧 html css bo...