Domanda

While passing material data to opengl (to be used in a shader):


float white[4] ={1.0f,1.0f,1.0f,1.0f};

glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, white);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, white);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, white);

int whitei[4] = {255,255,255,255};
glMaterialiv(GL_FRONT_AND_BACK, GL_AMBIENT, whitei);
glMaterialiv(GL_FRONT_AND_BACK, GL_DIFFUSE, whitei);
glMaterialiv(GL_FRONT_AND_BACK, GL_SPECULAR, whitei);

First one works beautifuly, but the second one only gives a black mesh. Both were used in the exact same context (basically I wrote the first one and then the second one replaced the first).

Does anyone have any idea why that is? glMaterialiv would really make my life easier (if I can get it to work) because I keep my colors as rgba bytes and I'm not sure if I want to make them 4 times bigger just to convert them to float.

È stato utile?

Soluzione

Both glColor3i and glMaterialiv take colors in the range [0, INT_MAX], with INT_MAX being 2^31 - 1. This explains your mesh being black, 255 is the equivalent of passing 0.000000119 for floats.

Technically, passing it either as floats or ints shouldn't matter much. In your example, you are already storing the individual components at 4 times their original size, so no loss there.

I´m not even convinced passing skipping the int -> float conversion will yield any performance gain, as the driver might still convert things to float on the CPU side of things. I'm speculating there, testing would have to prove that.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top