2015-02-06 53 views
1

我一直在試圖繪製一個小屋(如頂部有一個圓錐體的圓柱體),並向牆體和屋頂瓦片紋理添加磚砌體。但是,我只獲得我加載的第一個紋理(磚塊)。opengl - 只有一個紋理顯示

這裏是我的代碼(請注意,我已經嘗試使用glActiveTexture紋理之間切換):

void drawCylinder() 
{ 
int width, height; 

unsigned char * data_for_wall = SOIL_load_image("./bricks.jpg", &width, &height, NULL, 0); 

glDisable(GL_LIGHTING); 

glGenTextures(2, textures); 

glActiveTexture(GL_TEXTURE0); 

glEnable(GL_TEXTURE_2D); 

glBindTexture(GL_TEXTURE_2D, textures[0]); 

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR); 
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); 
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 

// Generate mipmaps, by the way. 
glGenerateMipmap(GL_TEXTURE_2D); 

gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, width, height, GL_RGB, GL_UNSIGNED_BYTE, data_for_wall); 
for(float theta = 0.0; theta <= 360.0; theta += 10.0) 
{ 
    //colors[k] = color4(0.69, 0.35, 0.0, 1.0); //This color is brown. 
    tex_coords[global_index]=vec2(theta*DegreesToRadians, 0.0); 
    float x = 0.15*sin(theta*DegreesToRadians); 
    float y = 0.15*cos(theta*DegreesToRadians); 
    points[global_index] = Translate(0.6, 0.0, 0.35)*point4(x, 0.0, y, 1.0); 

    // This is the 
    // bottom of the cylinder. The points are plotted in a full circle. The first three numbers are the x,y and z values 
    // respectively. The last number (ie. 1.0) is not important - it just converts to homogeneous coordinates. 
    ++global_index; 
    tex_coords[global_index] = vec2(theta*DegreesToRadians, 0.25); 
    points[global_index] = Translate(0.6, 0.0, 0.35)*point4(x, 0.25, y, 1.0); 
    // This is the 
    // top of the cylinder 
    ++global_index; 
} 
} 

//The roof of the hut 
void drawCone() 
{ 

int width, height; 

unsigned char * data_for_roof = SOIL_load_image("./roof_tiles.jpg", &width, &height, NULL, 0); 
glDisable(GL_TEXTURE_2D); 


glActiveTexture(GL_TEXTURE1); 


glBindTexture(GL_TEXTURE_2D, textures[1]); 

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR); 
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); 
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 

// Generate mipmaps, by the way. 
glGenerateMipmap(GL_TEXTURE_2D); 

gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, width, height, GL_RGB, GL_UNSIGNED_BYTE, data_for_roof); 
int index = 0; 
    int l = 0; 
    for(float theta = 0.0; theta <= 360.0; theta += 10.0) 
    { 
    tex_coords[global_index]=vec2(theta*DegreesToRadians, 0.25); 

    points[global_index] = Translate(0.6, 0.0, 0.35)*point4(0.0, 0.5, 0.0, 1.0); // This is the top of the cone. 
    ++global_index; 

    tex_coords[global_index] = vec2(theta*DegreesToRadians, 0.5); 

    points[global_index] = Translate(0.6, 0.0, 0.35)*point4(0.25*sin(theta*DegreesToRadians), 0.25, 0.25*cos(theta*DegreesToRadians), 1.0); 
    // This is the 
    // bottom of the cone. 
    ++global_index; 

    } 
} 

,這裏是顯示功能:

void 
display(void) 
{ 
mat4 mv = Translate(0.0, -0.065, -rad)*RotateX(Theta[0])*RotateY(Theta[1])*RotateZ(Theta[2]); 
mat4 p = Perspective(10.0, aspectRatio, zNear, zFar); 
glUniformMatrix4fv(matrix_loc, 1, GL_TRUE, mv); 
glUniformMatrix4fv(projection_loc, 1, GL_TRUE, p); 
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

glUniform3fv(theta, 1, Theta); 

glActiveTexture(GL_TEXTURE0); 
glBindTexture(GL_TEXTURE_2D, textures[0]); 
glDrawArrays(GL_TRIANGLE_STRIP, 0, 74); 

glActiveTexture(GL_TEXTURE1); 
glBindTexture(GL_TEXTURE_2D, textures[1]); 
glDrawArrays(GL_TRIANGLE_STRIP, 74, 74); 

glutSwapBuffers(); 
} 

我不知道這是否對於包含片段着色器很重要,但是我會反正這樣做: #version 150

in vec2 texCoord; 

out vec4 fColor; 

uniform sampler2D texture; 

void main() 
{ 
fColor = texture2D(texture, texCoord); 
} 

我一直在掙扎幾個小時纔得到這個權利。有誰知道我做錯了什麼?

回答

1

glActiveTexture()用於在多紋理(一次渲染多個紋理)時設置要綁定紋理的紋理槽。例如,你可能有一個紋理貼圖,另一個用於法線貼圖等等。

但是,您不是多紋理的,因爲您在單獨的通道中渲染牆和錐體(即對glDrawArrays()進行兩次單獨調用),並且着色器每次只使用一個紋理。因此,在每次傳遞過程中,您只渲染單個紋理,它將位於插槽0中。

如果將活動貼圖設置爲GL_TEXTURE1,然後調用glBindTexture(),則紋理將被綁定到插槽1,並且插槽0將保持不變。

因此,兩次將活動紋理槽設置爲0。從顯示中刪除該行()函數:

glActiveTexture(GL_TEXTURE1); 
+0

另一種方法是向'texture'均勻的值設置爲1用於第二繪製調用,以便使用綁定到單元1的質地。 – 2015-02-06 08:16:46

+0

太棒了!謝謝你們兩位! – user3507499 2015-02-06 11:12:18