I have been working on making a cube and I have some balls inside it though I am having problems on how I can put or add texture to my code. I am trying to learn but it seems whatever material I'm coming across is kind of confusing me. How can I add texture to this code below? This my code below
#define GL_GLEXT_PROTOTYPES
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include <Eigen/Core>
using namespace Eigen;
struct PointMass
{
PointMass() : pos(0,0,0), vel(0,0,0) {}
PointMass( const Vector3f& pos )
: pos( pos )
, vel( 0, 0, 0 )
{}
void Integrate( float dt )
{
// "gravity" force vector
Vector3f g( 0, 0, -2 );
// semi-implicit euler
vel = vel + g * dt;
pos = pos + vel * dt;
// collision detection/response
if( pos.z() < 0 )
{
pos.z() = -pos.z();
vel.z() = -vel.z();
}
}
Vector3f pos;
Vector3f vel;
};
GLfloat WHITE[] = {1, 1, 1, 1};
GLfloat RED[] = {1, 0, 0, 1};
GLfloat GREEN[] = {0, 1, 0, 1};
GLfloat MAGENTA[] = {0, 0, 1, 1};
class Ball
{
double radius;
GLfloat* color;
PointMass pm;
public:
Ball(double r, GLfloat* c, const Vector3f& pos )
: radius(r)
, color(c)
, pm( pos )
{ }
void Integrate( float dt )
{
pm.Integrate( dt );
}
void Draw()
{
glPushMatrix();
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color);
glColor4fv( color );
glTranslatef( pm.pos.x(), pm.pos.y(), pm.pos.z() );
glutSolidSphere(radius, 30, 30);
glPopMatrix();
}
};
Ball balls[] =
{
Ball( 0.1, GREEN, Vector3f( 1, 1, 2 ) ),
Ball( 0.1, MAGENTA, Vector3f( -1, 1, 1 ) ),
Ball( 0.1, WHITE, Vector3f( 0, -1, 1.5 ) ),
};
double rotate_x = 55;
double rotate_z = 25;
void display()
{
static int last = glutGet(GLUT_ELAPSED_TIME);
int cur = glutGet(GLUT_ELAPSED_TIME);
float dt = ( cur - last ) / 1000.0f;
last = cur;
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
double w = glutGet( GLUT_WINDOW_WIDTH );
double h = glutGet( GLUT_WINDOW_HEIGHT );
gluPerspective( 60, w / h, 0.1, 100 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
GLfloat lightPosition[] = {4, 3, 7, 1};
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
glTranslatef( 0, 0, -5 );
glRotatef( -rotate_x, 1.0, 0.0, 0.0 );
glRotatef( -rotate_z, 0.0, 0.0, 1.0 );
glBegin(GL_QUADS);
glEnd();
// White side - BACK
glBegin(GL_QUADS);
glColor3f( 1.0, 0.0, 0.0 );
glVertex3f( 1.5f, 1.5f, 1.5f); //V2
glVertex3f( 1.5f,-1.5f, 1.5f); //V1
glVertex3f( 1.5f,-1.5f,-1.5f); //V3
glVertex3f( 1.5f, 1.5f,-1.5f); //V4
glEnd();
// Purple side - RIGHT
glBegin(GL_QUADS);
glColor3f( 1.0, 0.0, 1.0 );
glVertex3f( 1.5f, 1.5f,-1.5f); //V4
glVertex3f( 1.5f,-1.5f,-1.5f); //V3
glVertex3f(-1.5f,-1.5f,-1.5f); //V5
glVertex3f(-1.5f, 1.5f,-1.5f); //V6
glEnd();
// Green side - LEFT
glBegin(GL_QUADS);
glColor3f( 0.0, 1.0, 0.0 );
glVertex3f(-1.5f, 1.5f,-1.5f); //V6
glVertex3f(-1.5f,-1.5f,-1.5f); //V5
glVertex3f(-1.5f,-1.5f, 1.5f); //V7
glVertex3f(-1.5f, 1.5f, 1.5f); //V8
glEnd();
glBegin(GL_QUADS);
glColor3f( 1.0, 1.0, 0.0 );
glVertex3f(-1.5f, 1.5f,-1.5f); //V6
glVertex3f(-1.5f, 1.5f, 1.5f); //V8
glVertex3f( 1.5f, 1.5f, 1.5f); //V2
glVertex3f( 1.5f, 1.5f,-1.5f); //V4
glEnd();
for (int i = 0; i < sizeof balls / sizeof(Ball); i++)
{
balls[i].Integrate( dt );
balls[i].Draw();
}
glFlush();
glutSwapBuffers();
}
void specialKeys( int key, int x, int y )
{
if (key == GLUT_KEY_RIGHT)
rotate_z += 5;
if (key == GLUT_KEY_LEFT)
rotate_z -= 5;
if (key == GLUT_KEY_UP)
rotate_x += 5;
if (key == GLUT_KEY_DOWN)
rotate_x -= 5;
}
void timer( int extra )
{
// run display() every 16ms or so
glutTimerFunc( 16, timer, 0 );
glutPostRedisplay();
}
int main(int argc, char* argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutCreateWindow("Awesome Cube");
glEnable(GL_DEPTH_TEST);
glLightfv(GL_LIGHT0, GL_DIFFUSE, WHITE);
glLightfv(GL_LIGHT0, GL_SPECULAR, WHITE);
glMaterialfv(GL_FRONT, GL_SPECULAR, WHITE);
glMaterialf(GL_FRONT, GL_SHININESS, 30);
glutDisplayFunc(display);
glutSpecialFunc(specialKeys);
glutTimerFunc( 0, timer, 0 );
glutMainLoop();
return 0;
}
Secondly when I write this code below in the main function
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
The walls of the cube turns grey any expalination why I'm getting this??