Friday, June 15, 2012

object moving by mouse click


#include <iostream>
#include <math.h>
#include <stdlib.h>

#ifdef __APPLE__
#include <OpenGL/OpenGL.h>
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

using namespace std;

#include <GL/glut.h>

static GLfloat rectFirstPositionX=0.0f,rectFirstPositionY=20.0f,rectSecondPositionX=5.0f,rectSecondPositionY=15.0f;

static GLfloat xDiff=3,yDiff=3;

float xAxis=20.0,xPrimeAxis=-20,yAxis=20,yPrimeAxis=-20,zAxis=10,zPrimeAxis=-10;

int count = 0,direction=0; //direction=0=assending,,,,,,direction=1=dessending

void init(void)
{
    glClearColor(0.0,0.0,0.0,0.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glShadeModel(GL_FLAT);
    glOrtho(xPrimeAxis,xAxis,yPrimeAxis,yAxis,zPrimeAxis,zAxis);
}

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);

    glPushMatrix();
    glColor3f(1,1.0,1.0);

    glBegin(GL_LINES);
    glVertex2f(0,yAxis);glVertex2f(0, yPrimeAxis);
    glVertex2f(10,yAxis);glVertex2f(10, yPrimeAxis);
    glEnd();
    glPopMatrix();

        glPushMatrix();
    //glTranslatef();
    glColor3f(0.05,0.5,0.1);
    glRectf(rectFirstPositionX,rectFirstPositionY,rectSecondPositionX,rectSecondPositionY);
    glPopMatrix();

    glutSwapBuffers();
}

void reshape(int w, int h)
{
    glViewport(0,0,(GLsizei)w,(GLsizei)h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-20.0,20.0,-20.0,20.0,-1.0,1.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void spindisplay(void)
{
    glutPostRedisplay();
}

void mouse(int button, int state, int x, int y)
{
    if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
    {
        count++;
        if(rectSecondPositionX <=10 && rectSecondPositionY >= -20 && direction==0)
        {
            if(rectSecondPositionX==10 && rectSecondPositionY <=-19)
            {
                direction=1;
                cout<<"do direction=1 "<<endl;
            }

            if(count%2==1)
            {
                cout<<"in 1st if"<<endl;
                rectFirstPositionX=rectFirstPositionX+5;
                rectFirstPositionY=rectFirstPositionY-5;

                rectSecondPositionX=rectSecondPositionX+5;
                rectSecondPositionY=rectSecondPositionY-5;
            }
            else if(count%2==0)
            {
                cout<<"in 1st else if"<<endl;
                rectFirstPositionX=rectFirstPositionX-5;
                rectFirstPositionY=rectFirstPositionY-5;

                rectSecondPositionX=rectSecondPositionX-5;
                rectSecondPositionY=rectSecondPositionY-5;
            }
        }
        else if(rectFirstPositionX >=0 && rectFirstPositionY <=20 && direction==1 )
        {

            if(rectFirstPositionX==0 && rectFirstPositionY >= 20)
            {
                direction=0;

                cout<<"do direction=0 "<<endl;
            }

            if(count%2==0)
            {
                cout<<"if count="<<count<<endl;


                rectFirstPositionX=rectFirstPositionX-5;
                rectFirstPositionY=rectFirstPositionY+5;

                rectSecondPositionX=rectSecondPositionX-5;
                rectSecondPositionY=rectSecondPositionY+5;
            }
            else if(count%2==1)
            {
                cout<<"---else if------- count="<<count<<endl;


                rectFirstPositionX=rectFirstPositionX+5;
                rectFirstPositionY=rectFirstPositionY+5;

                rectSecondPositionX=rectSecondPositionX+5;
                rectSecondPositionY=rectSecondPositionY+5;

                cout<<"rectFirstPositionX="<<rectFirstPositionX<<" "<<"rectFirstPositionY="<<rectFirstPositionY<<endl<<
                    "rectSecondPositionX="<<rectSecondPositionX<<" "<<"rectSecondPositionY="<<rectSecondPositionY<<endl;
            }
        }

    }
}

int main(int argc, char **argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE);
    glutInitWindowSize(500,500);
    glutInitWindowPosition(100,100);
    glutCreateWindow("Moving squares");
    init();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMouseFunc(mouse);
    glutIdleFunc(spindisplay);
    glutMainLoop();
}



sun,earth moon rotation in 3d


#include <iostream>
#include <stdlib.h>

#ifdef __APPLE__
#include <OpenGL/OpenGL.h>
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

using namespace std;

static int slices = 30;
static int stacks = 30;

GLfloat EarthOrbitRadius = 8.0;
GLfloat MoonOrbitRadius = .9;


//Called when a key is pressed
void handleKeypress(unsigned char key, int x, int y) {
switch (key) {
case 27: //Escape key
exit(0);
}
}

//Initializes 3D rendering
void initRendering() {
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING); //Enable lighting
glEnable(GL_LIGHT0); //Enable light #0
glEnable(GL_LIGHT1); //Enable light #1
glEnable(GL_NORMALIZE); //Automatically normalize normals
glShadeModel(GL_SMOOTH); //Enable smooth shading
}

//Called when the window is resized
void handleResize(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (double)w / (double)h, 1.0, 200.0);
}

float _angle = -70.0f;

//Draws the 3D scene
void drawScene()
{
    gluLookAt( 0.0,-0.0,6.0,
0.0,0.0,3.0,
0.0,-3.0,0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glTranslatef(0.0f, 0.0f, -10.0f);

//Add ambient light
GLfloat ambientColor[] = {0.2f, 0.2f, 0.2f, 1.0f}; //Color (0.2, 0.2, 0.2)
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor);

//Add positioned light
GLfloat lightColor0[] = {0.5f, 0.5f, 0.5f, 1.0f}; //Color (0.5, 0.5, 0.5)
GLfloat lightPos0[] = {4.0f, 0.0f, 8.0f, 1.0f}; //Positioned at (4, 0, 8)
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor0);
glLightfv(GL_LIGHT0, GL_POSITION, lightPos0);

//Add directed light
GLfloat lightColor1[] = {0.5f, 0.2f, 0.2f, 1.0f}; //Color (0.5, 0.2, 0.2)
//Coming from the direction (-1, 0.5, 0.5)
GLfloat lightPos1[] = {-1.0f, 0.5f, 0.5f, 0.0f};
glLightfv(GL_LIGHT1, GL_DIFFUSE, lightColor1);
glLightfv(GL_LIGHT1, GL_POSITION, lightPos1);

glRotatef(_angle, 0.0f, 1.0f, 0.0f);
glColor3f(1.0f, 1.0f, 1.0f);


//glBegin(GL_QUADS);

    //center sun
    glPushMatrix();
        //glTranslated(0.0,0.0,-10);
        //glRotatef(_angle, 0.0f, 1.0f, 0.0f);


        glutSolidSphere(1,slices,stacks);

        //Earth
        glColor3d(0.1,0.1,.3);
        glPushMatrix();
            glTranslated(EarthOrbitRadius,0.5,-0);
            glutSolidSphere(1,slices,stacks);


            //Moon
            glRotated(_angle,0,1,0);
            glRotated(_angle,0,1,0);
            glRotated(_angle,0,1,0);
            glRotated(_angle,0,1,0);
            glRotated(_angle,0,1,0);
            glRotated(_angle,0,1,0);

            glColor3d(0.9,0.5,0.3);
            glTranslated(MoonOrbitRadius,-0.5,1);
            glRotated(_angle,0,1,0);

            glutSolidSphere(0.3,slices,stacks);


        glPopMatrix();//Earth

    glPopMatrix();// sun

    //glEnd();






glutSwapBuffers();
}

void update(int value) {
_angle += .5f;
if (_angle > 360) {
_angle -= 360;
}

glutPostRedisplay();
glutTimerFunc(25, update, 0);
}

int main(int argc, char** argv) {
//Initialize GLUT
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(400, 400);

//Create the window
glutCreateWindow("Lighting - videotutorialsrock.com");
initRendering();

//Set handler functions
glutDisplayFunc(drawScene);
glutKeyboardFunc(handleKeypress);
glutReshapeFunc(handleResize);

glutTimerFunc(25, update, 0); //Add a timer

glutMainLoop();
return 0;
}









ball bouncing in 3d


#include <iostream>
#include <math.h>
#include <stdlib.h>

#ifdef __APPLE__
#include <OpenGL/OpenGL.h>
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

using namespace std;

#include <GL/glut.h>

// this is a simple example of a 3D interactive program

float rx,ry, rw, rh;

float bx, by;
float dx, dy;

float winWid, winHeight;



void redraw( void )
{
int i;
float px, py;

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// here is the floor
glColor3f(0.6,0.6,0.0);
glPushMatrix();
glTranslatef(0.0,-10.0,-10.0);
glRotatef(90.0,1.0,0.0,0.0);
for(i=0;i<10;++i)
{
glRectf(-10.0, float(i)*2.0, 10.0, float(i)*2.0 + 1.0);
}

glColor3f(1.0,0.0,0.0);

px = rx/20.0-10.0;
py = 10.0-ry/20.0;
glTranslatef(0.0,0.0,-0.003);
glRectf(px - 2.0,py -2.0, px + 2.0, py + 2.0);

glPopMatrix();

// the ball
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glPushMatrix();
glTranslatef(bx,by,-0.150);
glRotatef(90.0,0.0,1.0,0.0);
glutSolidSphere( 2.0,10,10);
glPopMatrix();
glDisable(GL_LIGHTING);

// the shadow
glColor3f(0.4,0.4,0.0);
glPushMatrix();
glTranslatef(bx,-10.0,-0.0150);
glScalef(1.0,0.001,1.0);
glutSolidSphere( 2.0,10,10);
glPopMatrix();

glutSwapBuffers();

bx += dx;
by += dy;

if (by > 10.0) dy = -dy;
if (bx > 10.0) dx = -dx;

if (by < -8.0) dy = -dy; // floor + radius
if (bx < -10.0) dx = -dx;

dy = dy - 0.003; // this is gravity
}

void mousebutton(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
rx = x; ry = winHeight - y;
}
}

void motion(int x, int y)
// called when a mouse is in motion with a button down
{

rx = x; ry = winHeight - y;
}

void keyboard(unsigned char key, int x, int y) // x and y give the mouse pos
{
cerr << "Key " << key << " int " << int(key) << "\n";
}

void reshape(int w, int h)
{

winWid = w, winHeight = h;
glViewport(0.0,0.0,winWid,winHeight);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-10.0,10.0,-10.0,10.0, 20.0, 2000.0);
glTranslatef(0.0,0.0,-25.0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

int main(int argc, char *argv[])
{
cerr << "hello world\n";

rx = 100; ry = 200; rw = 30; rh = 15;

bx = 0; by = 0;
dx = 0.06; dy = 0.08;
winWid = 500; winHeight = 500;

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Mouse example");
glutPositionWindow(200,100);
glutReshapeWindow(winWid,winHeight);

glClearColor(0.5,0.5,0.5,1.0);

float light_position[] = {-10.0,20.0,20.0,1.0};
glLightfv(GL_LIGHT0,GL_POSITION, light_position);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-10.0,10.0,-10.0,10.0, 20.0, 2000.0);
glTranslatef(0.0,0.0,-25.0);

glMatrixMode(GL_MODELVIEW);;
glLoadIdentity();

glEnable(GL_DEPTH_TEST);

glutDisplayFunc(redraw);
glutReshapeFunc(reshape);
glutIdleFunc(redraw);
glutMouseFunc( mousebutton);
glutKeyboardFunc(keyboard);
glutMotionFunc( motion);

glutMainLoop();

return 0;
}

blending object


#include <iostream>
#include <stdlib.h>

#ifdef __APPLE__
#include <OpenGL/OpenGL.h>
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

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

using namespace std;

GLfloat angle = 0.0;

void cube (void) {
    glRotatef(angle, 1.0, 0.0, 0.0);
    glRotatef(angle, 0.0, 1.0, 0.0);
    glRotatef(angle, 0.0, 0.0, 1.0);
    glColor4f(1.0, 0.0, 0.0, 0.2); //set the color and alpha of the cube
    glutSolidCube(2);
    glColor4f(0.0, 1.0, 0.0, 0.5); //set the color and alpha of the cube
    glutSolidCube(1);
}

void display (void) {
    glClearColor (0.0,0.0,0.0,1.0);
    glClear (GL_COLOR_BUFFER_BIT);
    glEnable(GL_BLEND); //enable the blending
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); //set the blend function
    glLoadIdentity();
    gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
    cube();
    glutSwapBuffers();
    angle ++;
}

void reshape (int w, int h) {
    glViewport (0, 0, (GLsizei)w, (GLsizei)h);
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
    gluPerspective (60, (GLfloat)w / (GLfloat)h, 1.0, 100.0)
;
    glMatrixMode (GL_MODELVIEW);
}

int main (int argc, char **argv) {
    glutInit (&argc, argv);
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA); //here RGBA   A mean alpha

    glutInitWindowSize (500, 500);
    glutInitWindowPosition (100, 100);
    glutCreateWindow ("A basic OpenGL Window");
    glutDisplayFunc (display);
    glutIdleFunc (display);
    glutReshapeFunc (reshape);
    glutMainLoop ();
    return 0;
}

scale object


#include <iostream>
#include <stdlib.h>

#ifdef __APPLE__
#include <OpenGL/OpenGL.h>
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

using namespace std;

GLfloat angle = 0.0;

void cube (void) {
    //scaled
    glScalef( 2.0, 0.5, 1.0 );  //twice as wide, half the height,same depth
    //glRotatef(angle, 1.0, 0.0, 0.0);
    //glRotatef(angle, 0.0, 1.0, 0.0);
    glRotatef(angle, 0.0, 0.0, 1.0);
    glColor4f(1.0, 0.0, 0.0, 0.25); //25% visible
    glutWireCube(2);

    //non scaled
    //glRotatef(angle, 1.0, 0.0, 0.0);
    //glRotatef(angle, 0.0, 1.0, 0.0);
    glRotatef(angle, 0.0, 0.0, 1.0);
    glColor4f(0.0, 1.0, 0.0, 0.2); //25% visible
    glutSolidCube(1);
}

void display (void) {
    glClearColor (0.0,0.0,0.0,1.0);
    glClear (GL_COLOR_BUFFER_BIT);
    glEnable(GL_BLEND); //enable the blending
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // set the blending
    glLoadIdentity();
    gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
    cube();
    glutSwapBuffers();
    angle ++;
}

void reshape (int w, int h) {
    glViewport (0, 0, (GLsizei)w, (GLsizei)h);
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
    gluPerspective (60, (GLfloat)w / (GLfloat)h, 1.0, 100.0);
    glMatrixMode (GL_MODELVIEW);
}

int main (int argc, char **argv) {
    glutInit (&argc, argv);
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA); //set upfor the alpha channel
    glutInitWindowSize (500, 500);
    glutInitWindowPosition (100, 100);
    glutCreateWindow ("A basic OpenGL Window");
    glutDisplayFunc (display);
    glutIdleFunc (display);
    glutReshapeFunc (reshape);
    glutMainLoop ();
    return 0;
}