# Applying textures to a sphere. I know nothing about textures.

First of all, english is not my native language so “sorry fo the bad english”. I have a class in my school where we use OpenGL, and I’ve made a rotating Saturn with 3 moons. I have to add a texture to saturn, but I have no idea on how to do it. I’ve been searching online for hours and it seems like my teacher has taught us a different way to code in OpenGL.

Here’s my code so far:

``````
#include <math.h>
#include <iostream>
using namespace std;

#include <GL/glew.h>
#include <gl/glut.h>

#include <GLFW/glfw3.h>

GLfloat angulor = 0, anguloluna = 0, asaturno = 200; //These are default angles so Staurn and its moons can rotate.

void Saturno() { //Saturn

//This is the sphere I would like to add a texture.
glPushMatrix();
glColor3f(0.89, 0.87, 0.75);
glRotatef(angulor, 0, 1, 0);
glutSolidSphere(4.5, 20, 20);
glPopMatrix();

glPushMatrix();

//These are saturn's rings.
glRotatef(angulor, 0, 1, 0);
glRotatef(112.5, 1, 0, 0);
glRotatef(22.5, 0, 1, 0);
glColor3f(1, 1, 1);
glutSolidTorus(0.5, 7, 2, 20);
glColor3f(0.8, 0.8, 0.8);
glutSolidTorus(0.5, 6, 2, 20);

//These are saturn's moons.
glPushMatrix();
glColor3f(0.7, 0.7, 0.7);
glRotatef(-90.5, 1, 0, 0);
glRotatef(anguloluna, 0, 1, 0);
glTranslatef(-8.5, 0, 0);
glutSolidSphere(1, 20, 20);
glPopMatrix();

glPushMatrix();
glColor3f(0.7, 0.7, 0.7);
glRotatef(-112.5, 1, 0, 0);
glRotatef(90, 0, 1, 0);
glRotatef(anguloluna, 0, 1, 0);
glTranslatef(-10, 0, 0);
glutSolidSphere(0.75, 20, 20);
glPopMatrix();

glPushMatrix();
glColor3f(0.7, 0.7, 0.7);
glRotatef(-112.5, 1, 0, 0);
glRotatef(-150, 0, 1, 0);
glRotatef(anguloluna, 0, 1, 0);
glTranslatef(-10, 0, 0);
glutSolidSphere(0.75, 20, 20);
glPopMatrix();

glPopMatrix();

}

void escenario() //This is my "scene" function. Here I call the Saturn function.
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

Saturno();

glutSwapBuffers();
}

void animacion() //This function is to animate my objetcs.
{

anguloluna += 0.2;

asaturno += 0.4;

angulor += 0.15;

glutPostRedisplay();
}

void teclado(GLubyte key, GLint x, GLint y) //This function is to rotate the scene pressing keys.
{
switch (key)
{
//Eje Y
case '1':
glRotatef(5, 0.0, 1.0, 0.0);
glutPostRedisplay();
break;
case '2':
glRotatef(-5, 0.0, 1.0, 0.0);
glutPostRedisplay();
break;
//Eje X
case '3':
glRotatef(5, 1.0, 0.0, 0.0);
glutPostRedisplay();
break;
case '4':
glRotatef(-5, 1.0, 0.0, 0.0);
glutPostRedisplay();
break;
//Eje Z
case '5':
glRotatef(5, 0.0, 0.0, 1.0);
glutPostRedisplay();
break;
case '6':
glRotatef(-5, 0.0, 0.0, 1.0);
glutPostRedisplay();
break;
default:
break;
}
}

void configuracion() //Window configurations
{
glClearColor(0, 0, 0, 0);
glMatrixMode(GL_PROJECTION);
glOrtho(-25.0, 25.0, -25.0, 25.0, -25.0, 25.0);
glEnable(GL_DEPTH_TEST);
}

int main(int argc, char ** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(900, 900);
glutCreateWindow("Guía 3er Parcial");
glutDisplayFunc(escenario);
glutIdleFunc(animacion);
configuracion();
glutMainLoop();
return 0;
}

``````

I have no idea what to do:( .

… there is a #define STB_IMAGE_IMPLEMENTATION (I may have the spelling wrong…).
#define STB_IMAGE_IMPLEMENTATION

anyways, put this before your include for stb.
regards,
jeff