同时使用纹理和灯光

时间:2012-12-01 17:42:58

标签: c opengl textures glut lighting

我正在尝试在固体茶壶上绘制一维纹理,它可以正常添加灯光。
当我调用glMaterialfv时,材质的颜色会覆盖纹理的颜色。所以在这个简单的例子中我只画了黄色的茶壶。但是如果我也使用glMaterialfv,那么茶壶会采用我在glMaterialfv中指定的颜色。
我知道如何将材料的颜色设置为在所有点上具有相同颜色的纹理。这是我尝试过的,认为它有效:

#import <OpenGL/OpenGL.h>
#import <GLUT/GLUT.h>
#import <stdlib.h>
#import <math.h>

void makeRound(GLfloat* angle);

GLuint width=640, height=480;
GLfloat angle=0.0;
GLuint texture;
GLfloat (*pixels)[3];
float coeff[]= {1, 0 ,0 ,0};

inline void makeRound(GLfloat* angle)
{
    int intValue= *angle;
    GLfloat decimal= *angle - floor(*angle);
    intValue%=360;
    *angle= intValue+decimal;
}

void init()
{

    glEnable(GL_DEPTH_TEST);
    glViewport(-500, -500, 1000, 1000);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45, width/(float)height, 1, 1000);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(0, 0, -100, 0, 0, 0, 0, 1, 0);

    // Lights

    glShadeModel(GL_FLAT);

    glMaterialfv(GL_FRONT, GL_AMBIENT, (const GLfloat[]) {1,0,0} );
    glMaterialfv(GL_FRONT, GL_DIFFUSE, (const GLfloat[]) {1,0.25,0} );
    glMaterialfv(GL_FRONT, GL_SPECULAR, (const GLfloat[]) {1,0.75,0} );
    glMaterialfv(GL_FRONT, GL_SHININESS, (const GLfloat[]) {1,1,0} );

    glLightfv(GL_LIGHT0, GL_AMBIENT, (const GLfloat[]) {0,0,0} );
    glLightfv(GL_LIGHT0, GL_DIFFUSE, (const GLfloat[]) {1,1,0} );
    glLightfv(GL_LIGHT0, GL_SPECULAR, (const GLfloat[]) {0.5,0.5,0} );

    glEnable(GL_LIGHT0);
    glEnable(GL_LIGHTING);

    // Texture

    pixels= calloc(256,sizeof(GLfloat[3]));
    for(GLuint i=0; i<256; i++)
    {
        pixels[i][0]= pixels[i][1]= 1.0;
    }

    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    glGenTextures(1, &texture);
    glBindTexture(GL_TEXTURE_1D, texture);
    glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexImage1D(GL_TEXTURE_1D, 0, GL_RGB, 256, 0, GL_RGB, GL_FLOAT, pixels);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
    glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
    glTexGenfv(GL_S, GL_OBJECT_PLANE, coeff);
    glEnable(GL_TEXTURE_GEN_S);
    glEnable(GL_TEXTURE_1D);
    glEnable(GL_CULL_FACE);
    glEnable(GL_AUTO_NORMAL);
    glEnable(GL_NORMALIZE);
    glEnable(GL_CW);
    glCullFace(GL_BACK);
}

void display()
{
    glClearColor(0, 0, 0, 0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glColor3ub(255, 255, 255);
    glPushMatrix();
    glRotatef(angle, 0, 1, 0);
    glBindTexture(GL_TEXTURE_1D, texture);
    glutSolidTeapot(10);
    glPopMatrix();

    glFlush();
}

void keyboard(unsigned char key, int x, int y)
{
    if(key=='+')
    {
        angle+=5.0;
    }
    else if(key=='-')
    {
        angle-=5.0;
    }
    else
    {
        return;
    }
    makeRound(&angle);
    glutPostRedisplay();
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_SINGLE);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(width, height);
    glutCreateWindow(argv[0]);
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    init();
    glutMainLoop();
    return 0;
}

但是茶壶不是黄色的,如果我取消所有那些初始化灯光和材料的线条,那么茶壶就是黄色。
我想做点什么:

glMaterialfv(GL_FRONT, GL_AMBIENT, color_of_the_texture_at_every_pixel );

1 个答案:

答案 0 :(得分:1)

材质的颜色乘以纹理的颜色。如果您希望所有原始纹理颜色都通过,那么您需要使材质为白色。