簡體   English   中英

OpenGL - 在同一個 object 上混合兩種紋理

[英]OpenGL - blend two textures on the same object

我想在同一個 object(實際上只是一個 2D 矩形)上應用兩個紋理以混合它們。 我想我可以通過簡單地使用第一個紋理調用glDrawElements來實現這一點,然后綁定另一個紋理並再次調用glDrawElements 像這樣:

//create vertex buffer, frame buffer, depth buffer, texture sampler, build and bind model
//...

glEnable(GL_BLEND);
glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ZERO);
glBlendEquation(GL_FUNC_ADD);

// Clear the screen
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// Bind our texture in Texture Unit 0
GLuint textureID;
//create or load texture
//...
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textureID);
// Set our  sampler to use Texture Unit 0
glUniform1i(textureSampler, 0);

// Draw the triangles !
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, (void*)0);

//second draw call
GLuint textureID2;
//create or load texture
//...
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textureID2);
// Set our sampler to use Texture Unit 0
glUniform1i(textureSampler, 0);

// Draw the triangles !
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, (void*)0);

不幸的是,第二個紋理根本沒有繪制,我只看到第一個紋理。 如果我在兩個繪制調用之間調用glClear ,它會正確繪制第二個紋理。

任何指針? 如何強制 OpenGL 進行第二次通話?

作為您到目前為止所采用的方法的替代方法,我建議您在 GLSL 着色器中使用兩個紋理采樣器並在那里執行混合。 這樣,您只需一個繪制調用即可完成,從而減少 CPU/GPU 交互。 為此,只需在着色器中定義紋理采樣器,例如

layout(binding = 0) uniform sampler2D texture_0;
layout(binding = 1) uniform sampler2D texture_1;

或者,您可以使用采樣器數組:

layout(binding = 0) uniform sampler2DArray textures;

在您的應用程序中,使用

enum Sampler_Unit{BASE_COLOR_S = GL_TEXTURE0 + 0, NORMAL_S = GL_TEXTURE0 + 2};
glActiveTexture(Sampler_Unit::BASE_COLOR_S);
glBindTexture(GL_TEXTURE_2D, textureBuffer1);
glTexStorage2D( ....)
glActiveTexture(Sampler_Unit::NORMAL_S);
glBindTexture(GL_TEXTURE_2D, textureBuffer2);
glTexStorage2D( ....)

感謝@tkausl 的提示。

我在初始化階段啟用了深度測試。

// Enable depth test
glEnable(GL_DEPTH_TEST);
// Accept fragment if it closer to the camera than the former one
glDepthFunc(GL_LESS);

在我的情況下,需要禁用該選項才能使混合操作起作用。

//make sure to disable depth test
glDisable(GL_DEPTH_TEST);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM