簡體   English   中英

OpenGL 3.3:在3D場景上顯示2D紋理的問題

[英]OpenGL 3.3: Issues Displaying 2D Texture over 3D scene

我一直試圖在3D場景上顯示2D圖像,但絕對沒有任何結果(因為在3D場景上根本沒有任何顯示)。 我嘗試了正常的gdb調試,並使用APITrace確保所有OpenGL調用均正常運行。 我不知道為什么圖像不顯示。 經過更多測試后,我認為問題不在於紋理本身,而是有關渲染紋理的2個三角形的渲染。 以下是所有相關代碼:

// Constants
const int COORD_LOCATION = 10;
const int UV_LOCATION = 5;

// Program Loading
uint program = loadShaders("2d.fs","2d.vs") // Same function used for loading shaders for 3D objects, so the function itself should work

// Texture creation
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, temp_width, temp_height, 0, GL_RGB, GL_UNSIGNED_BYTE, image_data); // image_data is generated by libpng earlier, and it is valid according to apitrace
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

/* Later, when texture rendering is started... */
uint UV_BUFFER, cb;

glGenBuffers(1, &UV_BUFFER);
glGenBuffers(1, &cb);

const float data[] = { 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f };

glBindBuffer(GL_ARRAY_BUFFER, UV_BUFFER);
glBufferData(GL_ARRAY_BUFFER, sizeof(data), &data[0], GL_STATIC_DRAW);

vec2 f1 = vec2(-0.5f, -0.5f);
vec2 f2 = vec2(0.5f, 0.5f);

// Set the 2d coordinates for the shader
const float data2[] = { f1.x, f1.y, 
                    f1.x, f2.y, 
                    f2.x, f1.y, 
                    f2.x, f1.y, 
                    f1.x, f2.y, 
                    f2.x, f2.y };
// Load into buffer
glBindBuffer(GL_ARRAY_BUFFER, cb);
glBufferData(GL_ARRAY_BUFFER, sizeof(data2), &data2[0], GL_STATIC_DRAW);

// During rendering...
glUseProgram(program);
glEnableVertexAttribArray(COORD_LOCATION);
glBindBuffer(GL_ARRAY_BUFFER, cb);
glVertexAttribPointer(
    COORD_LOCATION,                  // attribute. No particular reason for 0, but must match the layout in the shader.
    2,                  // size
    GL_FLOAT,           // type
    GL_FALSE,           // normalized?
    0,                  // stride
    (void*)0            // array buffer offset
);

// Send uniform texture
uint tex = glGetUniformLocation(program, TEXTURE_UNIFORM);
// Bind our texture in Texture Unit 0
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
// Set our "myTextureSampler" sampler to user Texture Unit 0
glUniform1i(tex, 0);

// Send UVs to the location
glEnableVertexAttribArray(UV_LOCATION);
glBindBuffer(GL_ARRAY_BUFFER, UV_BUFFER);
glVertexAttribPointer(
    UV_LOCATION,        // attribute. No particular reason for 0, but must match the layout in the shader.
    2,                  // size
    GL_FLOAT,           // type
    GL_FALSE,           // normalized?
    0,                  // stride
    (void*)0            // array buffer offset
);

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glDrawArrays(GL_TRIANGLES, 0, 6);

glDisable(GL_BLEND);

glDisableVertexAttribArray(COORD_LOCATION);
glDisableVertexAttribArray(UV_LOCATION);

2D頂點着色器:

#version 330 core

layout(location = 10) in vec2 coords;
layout(location = 5) in vec2 inUV;

out vec2 UV;

void main(){
gl_Position.x = coords.x;
gl_Position.y = coords.y;
gl_Position.z = 0;
gl_Position.w = 1;
UV = inUV;
}

2D片段着色器:

#version 330 core

in vec2 UV;

uniform sampler2D tex;

out vec3 color;

void main(){
    color = texture(tex, UV);
}

該代碼實際上比這更分散,但這就是我已經證實的原因。 在我們的VCS中查看完整的代碼(這是我的朋友,這只是一個隨機游戲,我決定開發來練習OpenGL,我們甚至還沒有一個真實的名字)。

vec2 f1 = vec2(-0.5f, -0.5f);
vec2 f2 = vec2(-0.5f, -0.5f);

那些看起來一樣。 零(屏幕空間)區域三角形通常不進行柵格化。

嘗試將f2更改為此:

vec2 f2 = vec2(0.5f,  0.5f);

glDrawArrays(GL_TRIANGLES, 0, 10000);
                              ^^^^^

嘗試將10000更改為6 由於您的VBO中只有6個頂點。

暫無
暫無

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

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