I am trying to map a 1D texture on a solid teapot, it works fine I would to add the lights.
When I call glMaterialfv, the colour of the material overrides the colour of the texture.So in this trivial example I just paint the teapot of yellow.But if I also use glMaterialfv, then the teapot takes the colour that I have specified in glMaterialfv.
I would know how is possibile to set the colour of the material to have the same colour of the texture in all it’s points.This is what I’ve tried, thinking that it was working:
#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;
}
But the teapot is not yellow, and if I cancel all that lines that initialize the lights and materials the teapot is yellow.
I would like to do something like:
glMaterialfv(GL_FRONT, GL_AMBIENT, color_of_the_texture_at_every_pixel );
The color of the material is multiplied by the color of the texture. If you want all the original texture color to come through, then you need to make the material be white.