2015-02-06 111 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