2016-11-01 14 views
0

試圖從一維紋理(.png)中採樣,得到一個具有正確紋理座標和所有模型的模型,但我無法獲得紋理顯示。幾何圖形只是呈現黑色,我必須在OpenGL中對紋理產生誤解,但看不到它。GL_TEXTURE_1D在片段着色器中的樣本

任何指針?

C++

// Setup 
GLint texCoordAttrib = glGetAttribLocation(batch_shader_program, "vTexCoord"); 
glVertexAttribPointer(texCoordAttrib, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex<float>), (const void *)offsetof(Vertex<float>, texCoord)); 
glEnableVertexAttribArray(texCoordAttrib); 

// Loading 
GLuint load_1d_texture(std::string filepath) { 
    SDL_Surface *image = IMG_Load(filepath.c_str()); 
    int width = image->w; 
    GLuint texture; 
    glGenTextures(1, &texture); 
    glBindTexture(GL_TEXTURE_1D, texture); 
    glTexImage2D(GL_TEXTURE_1D, 0, GL_RGBA, width, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels); 
    SDL_FreeSurface(image); 
    return texture; 
} 

// Rendering 
glUseProgram(batch.gl_program); 
glBindTexture(GL_TEXTURE_1D, batch.mesh.texture.gl_texture_reference); 
glDraw*** 

頂點着色器

#version 330 core 

in vec3 position; 
in vec4 vColor; 
in vec3 normal; // Polygon normal 
in vec2 vTexCoord; 

// Model 
in mat4 model; 

out vec4 fColor; 
out vec3 fTexcoord; 

// View or a.k.a camera matrix 
uniform mat4 camera_view; 

// Projection or a.k.a perspective matrix 
uniform mat4 projection; 

void main() { 
    gl_Position = projection * camera_view * model * vec4(position, 1.0); 
    fTexcoord = vec3(vTexCoord, 1.0); 
} 

片段着色器

#version 330 core 

in vec4 fColor; 
out vec4 outColor; 

in vec3 fTexcoord; // passthrough shading for interpolated textures 
uniform sampler1D sampler; 

void main() { 
    outColor = texture(sampler, fTexcoord.x); 
} 
+1

你要麼產生的貼圖或minicifaction過濾器更改爲模式那不需要mipmap? – BDL

+0

@BDL,no。這是必需的嗎? – Entalpi

+0

默認縮小比例過濾器是'GL_NEAREST_MIPMAP_LINEAR'。當不存在任何mipmap時,過濾後的值始終爲黑色。 – BDL

回答

1
glBindTexture(GL_TEXTURE_2D, texture); 

glBindTexture(GL_TEXTURE_1D, batch.mesh.texture.gl_texture_reference); 

假設這兩行代碼正在討論相同的OpenGL對象,那麼您不能這樣做。使用2D紋理目標的紋理是2D紋理。它不是1D紋理,也不是具有1層或深度爲1的3D紋理的2D陣列紋理。它是2D紋理。

在生成紋理對象後綁定紋理對象texture's target is fixed。您可以使用view textures創建具有不同目標的相同存儲的視圖,但原始紋理對象本身不受此影響。而且您無法創建2D紋理的一維視圖。

當您嘗試將2D紋理綁定爲1D時,您應該收到GL_INVALID_OPERATION錯誤。遇到OpenGL問題時,您應始終使用check for errors

0

到底有沒有問題,只有在紋理座標加載錯誤(它採取了錯誤的指數從頂點..)..