Go 中的頂點緩衝區和顯示三角形
問題:
使用Go github.com/banthar/gl包,http://www.opengl-tutorial.org/beginners-tutorials/tutorial-2-the-first-triangle/ 中的教程代碼成功創建了一個視窗並設定了背景顏色,但無法顯示三角形。等效的 C 代碼確實顯示了三角形。為什麼?
答案:
錯誤可能是由於傳遞給 AttribPointer 函數的參數不正確造成的。已確定以下調整可解決此問題:
- AttribPointer 參數: 為陣列緩衝區偏移傳遞 nil,而非 (void*)0。這告訴 OpenGL 函式庫使用目前緩衝區綁定作為頂點屬性的起點。
- BufferData Size: 指定頂點緩衝區的大小(以位元組為單位)而不是元素的數量。例如,如果頂點緩衝區包含 n 個 32 位元浮點值,則將 4 * n 傳遞給 BufferData。
修正的 Go 程式碼:
package main import ( "github.com/banthar/gl" "github.com/go-gl/glfw/v3.2/glfw" "log" ) func main() { // ... ( GLFW window setup, GL initialization code) ... // Create vertex buffer gVertexBufferData := []float32{-1.0, -1.0, 0.0, 1.0, -1.0, 0.0, 0.0, 1.0, 0.0} vertexBuffer := gl.GenBuffer() vertexBuffer.Bind(gl.ARRAY_BUFFER) gl.BufferData(gl.ARRAY_BUFFER, len(gVertexBufferData)*4, gVertexBufferData, gl.STATIC_DRAW) for { // ... (Clear screen, enable/disable vertex attrib, draw triangle) ... vertexAttrib.AttribPointer( 3, // Size gl.FLOAT, // Type false, // Normalized? 0, // Stride nil) // Array buffer offset // ... (Continue drawing loop) ... } }
以上是儘管視窗和背景設定成功,為什麼我無法使用 go-gl/glfw 和 github.com/banthar/gl 顯示三角形?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

whentestinggocodewithinitfunctions,useexplicitseTupfunctionsorseParateTestFileSteSteTepteTementDippedDependendendencyOnInItfunctionsIdeFunctionSideFunctionsEffect.1)useexplicitsetupfunctionStocontrolglobalvaribalization.2)createSepEpontrolglobalvarialization

go'serrorhandlingurturnserrorsasvalues,與Javaandpythonwhichuseexceptions.1)go'smethodensursexplitirorhanderling,propertingrobustcodebutincreasingverbosity.2)

AnefactiveInterfaceingoisminimal,clear and promotesloosecoupling.1)minimizeTheInterfaceForflexibility andeaseofimplementation.2)useInterInterfaceForabStractionToswaPimplementations withoutchangingCallingCode.3)

集中式錯誤處理在Go語言中可以提升代碼的可讀性和可維護性。其實現方式和優勢包括:1.將錯誤處理邏輯從業務邏輯中分離,簡化代碼。 2.通過集中處理錯誤,確保錯誤處理的一致性。 3.使用defer和recover來捕獲和處理panic,增強程序健壯性。

Ingo,替代詞InivestoIniTfunctionsIncludeCustomInitializationfunctionsandsingletons.1)customInitializationfunctions hownerexpliticpliticpliticconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconcontirization curssetupssetupssetups.2)單次固定無元素限制ininconconcurrent

Gohandlesinterfacesandtypeassertionseffectively,enhancingcodeflexibilityandrobustness.1)Typeassertionsallowruntimetypechecking,asseenwiththeShapeinterfaceandCircletype.2)Typeswitcheshandlemultipletypesefficiently,usefulforvariousshapesimplementingthe

Go語言的錯誤處理通過errors.Is和errors.As函數變得更加靈活和可讀。 1.errors.Is用於檢查錯誤是否與指定錯誤相同,適用於錯誤鏈的處理。 2.errors.As不僅能檢查錯誤類型,還能將錯誤轉換為具體類型,方便提取錯誤信息。使用這些函數可以簡化錯誤處理邏輯,但需注意錯誤鏈的正確傳遞和避免過度依賴以防代碼複雜化。

tomakegoapplicationsRunfasterandMorefly,useProflingTools,leverageConCurrency,andManageMoryfectily.1)usepprofforcpuorforcpuandmemoryproflingtoidentifybottlenecks.2)upitizegorizegoroutizegoroutinesandchannelstoparalletaparelalyizetasksandimproverperformance.3)


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

禪工作室 13.0.1
強大的PHP整合開發環境

SublimeText3漢化版
中文版,非常好用

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。