Question

Hello I've been trying to find out information on how i can add a gradient from the top of the sphere to the bottom. Is it possible or do i have to texture the sphere?

    Sphere sphere = new Sphere();
    sphere.setOrientation(GLU.GLU_OUTSIDE);
    GL11.glPushMatrix();
    GL11.glColor3d(0.0D, 0.0D, 1.0D);
    sphere.draw(16.0f, 200, 16);
    GL11.glColor3d(1.0D, 1.0D, 1.0D);
    GL11.glPopMatrix();
Was it helpful?

Solution

Certainly, it's possible. It's a bit more complicated when you use a GLU sphere, as compared to if you generate your own coordinates and attributes for a sphere, since you need to work with what GLU sphere provides.

You could position a light at, say, the north pole of the sphere, and then adjust your diffuse material properties. This would provide maximal color at the north pole, with a gradual (cosine-based) gradient to the equator, and then dark from the equator to the south pole. The gradient would not be smooth between the poles, but you didn't specify that it needed to be a constant gradient.

Another approach would be to use texture mapping. In particular, if you wanted a constant gradient (i.e., equal gradation from the north to south pole), this is probably the only way to accomplish this when using a GLU sphere without shaders. The solution here is to use one-dimensional texture mapping based on the "vertical" (i.e., t) texture coordinate (e.g., if the s texture coordinate is used for the latitude direction, and the t coordinate is for longitude). Just specify the gradient in your one-dimensional texture.

Of course, if you're using shaders, then it's almost trivial to use the vertex shader to specify a parameter that's convenient for doing this kind of shading in the fragment shader.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top