Jump to content
  • Advertisement
Sign in to follow this  
yaazz

OpenGL Trying to translate cube thru Z axis

This topic is 4062 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have created a simple program where a cube flys around the screen ala asteroids, but now I want the cube to be able to go into and out of the Z axis. Unfortunately, when I translate into the Z axis, nothing happens until it hits the boundary and then the cube disapears! Shouldnt the cube scale down to look smaller? How can I accomplish this? Code compiles with this under Ubuntu Linux. I belive the only code that has to be modified is test.cpp, but the rest is there so you can compile. Opengl$ g++ -lGLU -lGL -lglut test.cpp soabiro_Input.cpp tile.cpp test.cpp
#include <stdlib.h>
#include <stdio.h>
#include <GL/glut.h>
#include <math.h>
#include <time.h>
#include "soabiro_Input.h"

clock_t clk;
double cangle=0;
double eyeX,eyeY,eyeZ,centerX,centerY,centerZ,upX,upY,upZ;
gameInput* deviceInput;
keyboardPTR kbPTR; 
specialKeyboardPTR specKbPTR;
mouseClickPTR mouseClkPTR;
mouseMovePTR mouseMvPTR;

double SHangle=0.0;            //TEMP VARIABLE FOR MOVING THE SHIP INTO THE BACKGORUND
double Zmove=0.0;		
int vW,vH,vX=0,vY=0; //Viewport variables
double Cangle; // camera angle
double Hangle=0,Fangle=0,Bangle=0; //heading, Facing, Bullet    ANGLES
double sVelocity=0.0,sMass=10000,force=0,resultSpeed=1,acceleration=0.01;
double XComp,YComp,bullXComp,bullYComp;
double bullVelocity=0.1;
const double PI = 3.14152;
const double ROTATION_RATE = 25;

bool bulletFired=false;
bool outputEnabled=false;
double Sangle,X,Y,bullX,bullY;
GLdouble bullet[2]={0.0,0.0};
GLdouble ship[3][3]={{0.0,-0.2,0.0},{-0.1,0.1,0.0},{-0.1,0.1,0.0}};
GLdouble line[2][2]={{0.0,1.0},{0.0,-1.0,}};

GLdouble mmx=0.0;



void createObjects()
{
	/* map vertices to faces */
Sangle+=20;
 mmx+=0.01;
	glMatrixMode(GL_MODELVIEW);
	/*gluLookAt(0.0, 0.0, 0.0,  //eye
			 0.0, 0.0, 0.0,  //lookat 
			  
			 0.0, 0.1, 0.0); //up*/
       	


	if(bulletFired)
	{
		glPushMatrix();
		bullX=bullX+bullVelocity * sin(Bangle );
		bullY=bullY-bullVelocity * cos(Bangle );
		
		glTranslatef(bullX,bullY,0.0);
		glScalef(0.4,0.4,0.4);
		
		glPushMatrix();
		glRotatef(Sangle,1.0,1.0,0.5);
		glBegin(GL_QUADS);
		
		glColor3f(0.0,0.0,1.0);
		glVertex3f(-0.1,-0.1,-1.0);
		glColor3f(0.1,0.9,0.5);
		glVertex3f(0.1,-0.1,-1.0);
		glColor3f(1.0,0.5,1.0);
		glVertex3f(0.1,0.1,-1.0);
		glColor3f(1.0,0.5,0.9);
		glVertex3f(-0.1,0.1,-1.0);
		
		glColor3f(0.0,0.0,1.0);
		glVertex3f(-0.1,-0.1,1.0);
		glColor3f(0.1,0.9,0.5);
		glVertex3f(0.1,-0.1,1.0);
		glColor3f(1.0,0.5,1.0);
		glVertex3f(0.1,0.1,1.0);
		glColor3f(1.0,0.5,0.9);
		glVertex3f(-0.1,0.1,1.0);
		glEnd();
		glPopMatrix();
		
		glBegin(GL_QUADS);
		glColor3f(0.5,1.0,0.0); 
		glVertex3f(-0.1,-0.1,-1.0);
		glVertex3f(0.1,-0.1,-1.0);
		glVertex3f(0.1,0.1,-1.0);
		glVertex3f(-0.1,0.1,-1.0);
		glEnd();
		
		if (bullX<=-1 || bullX >= 1)
		{
			bulletFired=false;
			bullX=X;
			bullY=Y;
			
		}
		if (bullY<=-1 || bullY >= 1)
		{
			bulletFired=false;
			bullX=X;
			bullY=Y;
			
		}
		
	
		

		
		glPopMatrix();
	}
	else
	{
		bullX=X;
		bullY=Y;
	}
	glPushMatrix();
	
	//glScalef(0.1,0.1,0.1);
	if(deviceInput->keyLeft)
		Fangle = Fangle + ROTATION_RATE *(PI/180);
	if(deviceInput->keyRight)
		Fangle = Fangle - ROTATION_RATE * (PI/180);
	if(deviceInput->keyUp)
	{
		XComp = sVelocity * sin(Hangle) + acceleration * sin(Fangle);
        YComp = sVelocity * cos(Hangle) + acceleration * cos(Fangle);
        
      //Determine the resultant speed
       
        	sVelocity= sqrt( (XComp *XComp) + (YComp * YComp));
	if(outputEnabled)
	  printf("sVelocity %f \n",sVelocity);
        //Calculate the resultant heading, and adjust for arctangent by adding Pi if necessary
        if (YComp >= 0) Hangle = atan(XComp / YComp);
        if (YComp < 0 ) Hangle = atan(XComp / YComp) + 3.14152; //<-PI
  
	}


	X=X+sVelocity * sin(Hangle);

	Y=Y-sVelocity * cos(Hangle);
	if(outputEnabled)
	{
	  printf("X= %f \n",X);
	  printf("hangle=%fcoshangle= %f Y= %f \n",Hangle,cos(Hangle),Y);
	}  
	
	if (X<=-1 || X >= 1)
		X=-X;
	if (Y<=-1 || Y >= 1)
		Y=-Y;
	

	//*************************************************
	//This is where the ship starts getting drawn.
	//It is supposed to go into the background, getting gradually smaller but it is not doing that
	// can you fix??
	//*********************************

	glTranslatef(X,Y,Zmove);
	SHangle+=10;
	  Zmove-=0.1;
	
	glRotatef(Fangle/(PI/180),0.0,0.0,1.0);
	glRotatef(SHangle,1.0,1.0,0.0);

	glScalef(0.1,0.1,0.1);
	glBegin(GL_QUADS);

	//front and back of cube

	glColor3f(1.0,0.0,0.0);
	   glVertex3f(-0.5,0.5,0.5);
	glColor3f(1.0,0.0,0.0);
  	   glVertex3f(0.5,0.5,0.5);
 	glColor3f(1.0,0.0,0.0);
 	   glVertex3f(0.5,-0.5,0.5);
 	glColor3f(1.0,0.0,0.0);
	   glVertex3f(-0.5,-0.5,0.5);
	
	glColor3f(0.0,1.0,0.0);
	   glVertex3f(-0.5,0.5,-0.5);
	glColor3f(0.0,1.0,0.0);
	   glVertex3f(0.5,0.5,-0.5);
 	glColor3f(0.0,1.0,0.0);
	   glVertex3f(0.5,-0.5,-0.5);
 	glColor3f(0.0,1.0,0.0);
	   glVertex3f(-0.5,-0.5,-0.5);

	 //left and right faces of cube
	
	glColor3f(0.0,0.0,1.0);
	  glVertex3f(-0.5,0.5,0.5);
	glColor3f(0.0,0.0,1.0);
	  glVertex3f(-0.5,0.5,-0.5);
 	glColor3f(0.0,0.0,1.0);
	  glVertex3f(-0.5,-0.5,-0.5);
 	glColor3f(0.0,0.0,1.0);
	  glVertex3f(-0.5,-0.5,0.5);
	  
	glColor3f(1.0,1.0,1.0);
	   glVertex3f(0.5,0.5,0.5);
	glColor3f(1.0,1.0,1.0);
	   glVertex3f(0.5,-0.5,0.5);
 	glColor3f(1.0,1.0,1.0);
	   glVertex3f(0.5,-0.5,-0.5);
 	glColor3f(1.0,1.0,1.0);
       	   glVertex3f(0.5,0.5,-0.5);


	//Top then bottom of cube

	glColor3f(0.0,1.0,1.0);
	   glVertex3f(-0.5,0.5,0.5);
	glColor3f(0.0,1.0,1.0);
	   glVertex3f(0.5,0.5,0.5);
 	glColor3f(0.0,1.0,1.0);
	   glVertex3f(0.5,0.5,-0.5);
 	glColor3f(0.0,1.0,1.0);
       	   glVertex3f(-0.5,0.5,-0.5);

	glColor3f(1.0,0.0,1.0);
	   glVertex3f(-0.5,-0.5,0.5);
	glColor3f(1.0,0.0,1.0);
	   glVertex3f(0.5,-0.5,0.5);
 	glColor3f(1.0,0.0,1.0);
	   glVertex3f(0.5,-0.5,-0.5);
 	glColor3f(1.0,0.0,1.0);
       	   glVertex3f(-0.5,-0.5,-0.5);

	
	/*
	glColor3f(1.0,0.5,0.0);
	glVertex3f(ship[0][0],ship[0][1],1.0);
	glColor3f(1.0,0.5,0.0);
	glVertex3f(ship[0][1],ship[1][1],1.0);
	glColor3f(1.0,0.5,1.0);
	glVertex3f(ship[0][2],ship[2][1],1.0);*/
	glEnd();
	
	glLoadIdentity();
	glRotatef(Fangle/(PI/180),0.0,0.0,1.0);
	glBegin(GL_LINES);
	glColor3f(0.0,1.0,0.0);
	glVertex3f(0.0,1.0,0.0);
	glColor3f(1.0,1.0,1.0);
	glVertex3f(0.0,-1.0,0.0);
	glEnd();
	
	glLoadIdentity();
	glRotatef(Hangle/(PI/180),0.0,0.0,1.0);
	glBegin(GL_LINES);
	glColor3f(0.0,1.0,1.0);
	glVertex3f(0.0,1.0,0.0);
	glColor3f(1.0,1.0,1.0);
	glVertex3f(0.0,-1.0,0.0);
	glEnd();
	glPopMatrix();


	deviceInput->keyUp=false;
	deviceInput->keyLeft=false;
	deviceInput->keyRight=false;
}



void display()
{
	/* display callback, clear frame buffer and Z buffer, rotate cube and draw
	 * swap buffers */
	 
	 glMatrixMode(GL_PROJECTION);
	 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glViewport(0,0,vW,vH);
	
	
	/*eyeX-=0.1;
	eyeY=0.0;
	eyeZ=-1.0;
	centerX=0.0;
	centerY=0.0;
	centerZ=0.0;
	upX=0.0;
	upY=1.0;
	upZ=0.0;
	gluLookAt(eyeX,eyeY,eyeZ,centerX,centerY,centerZ,upX,upY,upZ);*/
	
		
	
	glMatrixMode(GL_MODELVIEW);
	//glMatrixMode(GL_MODELVIEW);
	// glLoadIdentity();
    //glRotatef(1,1.0,0.0,0.0);
	 //glTranslated(block[0][0].getX(),block[0][0].getY(),0.0);
	createObjects();
	 
	 glFlush();
	 glutSwapBuffers();
	 
}

void idle()
{
	/*display */
	if (deviceInput->charKey == 'a')
	{
	
		deviceInput->charKey=NULL;
		Bangle=Fangle;
		bulletFired=true;
	}
	
		
	 clk = clock () + 1.0 ;
 if (clock() > clk)
	glutPostRedisplay();
	
}


void myReshape(int w, int h)
{
	vW=w;
	vH=h;
	glViewport(0,0,w,h);
	glMatrixMode(GL_PROJECTION); 
	glLoadIdentity();

	gluPerspective(0.0,w/h,0.0f,-10.0f);
	

	glMatrixMode(GL_MODELVIEW);
	//	gluLookAt(0.0f,0.0,0.5,0.0,0.0,-0.5,0.0,1.0,0.0);
}

int	main(int argc, char **argv)
{
	glutInit(&argc, argv);
	
	//****************
	//A quick hack to allow for Input handling in another Cpp file
	// Deviceinput is a pointer, and the new Gameinput method gives the pointer to the other Cpp file
	//************* 
	kbPTR = keyboard;
	specKbPTR=specialKeyboard;
	mouseClkPTR=mouseClick;
	mouseMvPTR=mouseMove;
	
	deviceInput = new gameInput;
	deviceInput->windowH=vH;
	deviceInput->windowW=vW;
	
	initInput(deviceInput);
	/* need both double buffering and Z buffer */
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
	glutInitWindowSize(250,250);
	glutCreateWindow("createObjects");
	glutReshapeFunc(myReshape);
	glutDisplayFunc(display);
	glutIdleFunc(idle);
	glutMouseFunc(mouseClkPTR);
	glutKeyboardFunc(kbPTR);
	glutSpecialFunc(specKbPTR);
	glutMotionFunc(mouseMvPTR);
	glEnable(GL_DEPTH_TEST); /*enable hidden surface removal */
	glEnable(GL_TEXTURE_2D);
	glutMainLoop();
}


Tile.h
#ifndef TILE_H_
#define TILE_H_



class tile
{
	
double x,y,width,height;
float color[3];
bool blocked;
int id;
public:
	tile();
	tile(double X, double Y, double W, double H);
	void setX(double X);
	void setY(double Y);
	void setWidth(double H);
 	void setHeight(double W);
	
	double getX();
	double getY();
	double getWidth();
	double getHeight();
	
	float getRf();
	float getGf();
	float getBf();
	
	void setRf(float R);
	void setGf(float G);
	void setBf(float B);
	
	virtual ~tile();
};

#endif /*TILE_H_*/


tile.cpp
#include "tile.h"



tile::tile()
{
	x=0;
	y=0;
	width=0;
	height=0;
	blocked=false;
	id=0;
	color[0]=1.0;
	color[1]=0.0;
	color[2]=0.0;
}

tile::tile(double X, double Y, double W, double H)
{
	blocked=false;
	id=0;
	color[0]=1.0;
	color[1]=0.0;
	color[2]=0.0;
	
	x=X;
	y=Y;
	width=W;
	height=H;
}

tile::~tile()
{
}

void tile::setX(double X)
{
	x=X;
}

void	tile::setY(double Y)
{
	y=Y;
}
void	tile::setWidth(double W)
{
	width=W;
}
void	tile::setHeight(double H)
{
	height=H;
}
double	tile::getX()
{
	return x;
}
double	tile::getY()
{
	return y;
}
double	tile::getWidth()
{
	return width;
}
double	tile::getHeight()
{
	return height;
}

	float tile::getRf()
	{
		return color[0];
	}
	float tile::getGf()
	{
		return color[1];
	}
	float tile::getBf()
	{
		return color[2];
	}

	void tile::setRf(float R)
	{
		color[0]=R;
	}
	void tile::setGf(float G)
	{
		color[1]=G;
	}
	void tile::setBf(float B)
	{
		color[2]=B;
	}


soabiro_Input.cpp

#include <GL/glut.h>
#include <iostream>
#include "soabiro_Input.h"

gameInput *input;

	void initInput(gameInput *main)
	{
		input = main;
	}
	void keyboard(unsigned char key, int x, int y)
	{
		input->charKey=key;
	}
	void specialKeyboard(int key, int x, int y)
	{
		//left arrow
		if (key == GLUT_KEY_LEFT)
			input->keyLeft=true;
		else
			input->keyLeft=false;
	
		//right arrow
		if (key == GLUT_KEY_RIGHT)
			input->keyRight=true;
		else
			input->keyRight=false;
	
		//up arrow
		if (key == GLUT_KEY_UP)
			input->keyUp=true;        
		else
			input->keyUp=false; 
	
		//down arrow
		if (key == GLUT_KEY_DOWN)
			input->keyDown=true;        
		else
			input->keyDown=false; 
	}
	
	void mouseMove(int x, int y)
	{
		/*gets mouse coordinates and converts them to a num between -1 and 1 for OpenGL*/
		
		input->mouseX=(float)x;
		input->mouseX=((input->mouseX / input->windowW)*2)-1;
		input->mouseY=(float)y;
		input->mouseY=((input->mouseY / input->windowH)*2)-1;

	}
	
	void mouseClick(int btn, int state, int x, int y)
	{
		//get mouse button and state
		input->mouseBtn=btn;
		input->mouseState=state;
		
		//get coordinates in opengl format (-1 to 1)
		input->mouseX=(float)x;
		input->mouseX=((input->mouseX / input->windowW)*2)-1;
		input->mouseY=(float)y;
		input->mouseY=((input->mouseY / input->windowH)*2)-1;
	}




soabiro_Input.h
#ifndef sOABIRO_INPUT_H_
#define sOABIRO_INPUT_H_
 
//---------------------------------------------------------------------------
typedef void (*keyboardPTR)(unsigned char key, int x, int y);
typedef void (*specialKeyboardPTR)(int key, int x, int y);
typedef void (*mouseClickPTR)(int btn, int state, int x, int y);
typedef void (*mouseMovePTR)(int x, int y);

//---------------------------------------------------------------------------
typedef struct 
{
	
	bool keyUp,keyDown,keyLeft,keyRight;
	bool leftClick,rightClick,middleClick;
	int key[256]; 
	unsigned char charKey;
	int windowH,windowW; //height and width of screen window, used to get proper coordinates
	double mouseX,mouseY;
	int mouseBtn,mouseState; 
} gameInput;
//---------------------------------------------------------------------------




	void initInput(gameInput* input);
	void keyboard(unsigned char key, int x, int y);
	void specialKeyboard(int key, int x, int y);
	void mouseMove(int x, int y);
	void mouseClick(int btn, int state, int x, int y);
	



#endif /*SOABIRO_INPUT_H_*/


Thanks in advance

Share this post


Link to post
Share on other sites
Advertisement
When I render 3D scenes, this is what I do:

1. Define the viewport (glViewport)
2. Clear the back buffer (glClear)
3. Set up the projection (matrix mode GL_PROJECTION, gluProjection call)
4. Translate the viewpoint (matrix mode GL_MODELVIEW, gluLookAt if necessary)
5. Render each object by translating to each orientation and rendering it.
6. Swap buffers

You code appears to perform a gluLookAt in GL_PROJECTION mode (well, it's commented out so frankly I'm not sure where you are setting up your projection for each render). So I don't think you're setting up the matrix pipeline properly. The fact that your cubes don't change size supports that theory.

Share this post


Link to post
Share on other sites
hmm interesting to know. I went through my code and took out any crap that I saw. Would you be able to modify this code for me so that I can see what im doing wrong?



#include <stdlib.h>
#include <stdio.h>
#include <GL/glut.h>
#include <math.h>
#include <time.h>
#include "soabiro_Input.h"

clock_t clk;
double cangle=0;
double eyeX,eyeY,eyeZ,centerX,centerY,centerZ,upX,upY,upZ;
gameInput* deviceInput;
keyboardPTR kbPTR;
specialKeyboardPTR specKbPTR;
mouseClickPTR mouseClkPTR;
mouseMovePTR mouseMvPTR;

double SHangle=0.0; //TEMP VARIABLE FOR MOVING THE SHIP INTO THE BACKGORUND
double Zmove=0.0;
int vW,vH,vX=0,vY=0; //Viewport variables
double Cangle; // camera angle
double Hangle=0,Fangle=0,Bangle=0; //heading, Facing, Bullet ANGLES
double sVelocity=0.0,sMass=10000,force=0,resultSpeed=1,acceleration=0.01;
double XComp,YComp,bullXComp,bullYComp;
double bullVelocity=0.1;
const double PI = 3.14152;
const double ROTATION_RATE = 25;

bool bulletFired=false;
bool outputEnabled=false;
double Sangle,X,Y,bullX,bullY;
GLdouble bullet[2]={0.0,0.0};
GLdouble ship[3][3]={{0.0,-0.2,0.0},{-0.1,0.1,0.0},{-0.1,0.1,0.0}};
GLdouble line[2][2]={{0.0,1.0},{0.0,-1.0,}};

GLdouble mmx=0.0;



void createObjects()
{
/* map vertices to faces */
Sangle+=20;
mmx+=0.01;
glMatrixMode(GL_MODELVIEW);

//This is for drawing the bullet
if(bulletFired)
{
glPushMatrix();
bullX=bullX+bullVelocity * sin(Bangle );
bullY=bullY-bullVelocity * cos(Bangle );

glTranslatef(bullX,bullY,0.0);
glScalef(0.4,0.4,0.4);

glPushMatrix();
glRotatef(Sangle,1.0,1.0,0.5);
glBegin(GL_QUADS);

glColor3f(0.0,0.0,1.0);
glVertex3f(-0.1,-0.1,-1.0);
glColor3f(0.1,0.9,0.5);
glVertex3f(0.1,-0.1,-1.0);
glColor3f(1.0,0.5,1.0);
glVertex3f(0.1,0.1,-1.0);
glColor3f(1.0,0.5,0.9);
glVertex3f(-0.1,0.1,-1.0);

glColor3f(0.0,0.0,1.0);
glVertex3f(-0.1,-0.1,1.0);
glColor3f(0.1,0.9,0.5);
glVertex3f(0.1,-0.1,1.0);
glColor3f(1.0,0.5,1.0);
glVertex3f(0.1,0.1,1.0);
glColor3f(1.0,0.5,0.9);
glVertex3f(-0.1,0.1,1.0);
glEnd();
glPopMatrix();

glBegin(GL_QUADS);
glColor3f(0.5,1.0,0.0);
glVertex3f(-0.1,-0.1,-1.0);
glVertex3f(0.1,-0.1,-1.0);
glVertex3f(0.1,0.1,-1.0);
glVertex3f(-0.1,0.1,-1.0);
glEnd();

if (bullX<=-1 || bullX >= 1)
{
bulletFired=false;
bullX=X;
bullY=Y;

}
if (bullY<=-1 || bullY >= 1)
{
bulletFired=false;
bullX=X;
bullY=Y;

}





glPopMatrix();
}
else
{
bullX=X;
bullY=Y;
}
glPushMatrix();

//glScalef(0.1,0.1,0.1);
if(deviceInput->keyLeft)
Fangle = Fangle + ROTATION_RATE *(PI/180);
if(deviceInput->keyRight)
Fangle = Fangle - ROTATION_RATE * (PI/180);
if(deviceInput->keyUp)
{
XComp = sVelocity * sin(Hangle) + acceleration * sin(Fangle);
YComp = sVelocity * cos(Hangle) + acceleration * cos(Fangle);

//Determine the resultant speed

sVelocity= sqrt( (XComp *XComp) + (YComp * YComp));
if(outputEnabled)
printf("sVelocity %f \n",sVelocity);
//Calculate the resultant heading, and adjust for arctangent by adding Pi if necessary
if (YComp >= 0) Hangle = atan(XComp / YComp);
if (YComp < 0 ) Hangle = atan(XComp / YComp) + 3.14152; //<-PI

}


X=X+sVelocity * sin(Hangle);

Y=Y-sVelocity * cos(Hangle);
if(outputEnabled)
{
printf("X= %f \n",X);
printf("hangle=%fcoshangle= %f Y= %f \n",Hangle,cos(Hangle),Y);
}

if (X<=-1 || X >= 1)
X=-X;
if (Y<=-1 || Y >= 1)
Y=-Y;


//*************************************************
//This is where the ship starts getting drawn.
//It is supposed to go into the background, getting gradually smaller but it is not doing that
// can you fix??
//*********************************

glTranslatef(X,Y,Zmove);
SHangle+=10;
Zmove-=0.1;

glRotatef(Fangle/(PI/180),0.0,0.0,1.0);
glRotatef(SHangle,1.0,1.0,0.0);

glScalef(0.1,0.1,0.1);
glBegin(GL_QUADS);

//front and back of cube

glColor3f(1.0,0.0,0.0);
glVertex3f(-0.5,0.5,0.5);
glColor3f(1.0,0.0,0.0);
glVertex3f(0.5,0.5,0.5);
glColor3f(1.0,0.0,0.0);
glVertex3f(0.5,-0.5,0.5);
glColor3f(1.0,0.0,0.0);
glVertex3f(-0.5,-0.5,0.5);

glColor3f(0.0,1.0,0.0);
glVertex3f(-0.5,0.5,-0.5);
glColor3f(0.0,1.0,0.0);
glVertex3f(0.5,0.5,-0.5);
glColor3f(0.0,1.0,0.0);
glVertex3f(0.5,-0.5,-0.5);
glColor3f(0.0,1.0,0.0);
glVertex3f(-0.5,-0.5,-0.5);

//left and right faces of cube

glColor3f(0.0,0.0,1.0);
glVertex3f(-0.5,0.5,0.5);
glColor3f(0.0,0.0,1.0);
glVertex3f(-0.5,0.5,-0.5);
glColor3f(0.0,0.0,1.0);
glVertex3f(-0.5,-0.5,-0.5);
glColor3f(0.0,0.0,1.0);
glVertex3f(-0.5,-0.5,0.5);

glColor3f(1.0,1.0,1.0);
glVertex3f(0.5,0.5,0.5);
glColor3f(1.0,1.0,1.0);
glVertex3f(0.5,-0.5,0.5);
glColor3f(1.0,1.0,1.0);
glVertex3f(0.5,-0.5,-0.5);
glColor3f(1.0,1.0,1.0);
glVertex3f(0.5,0.5,-0.5);


//Top then bottom of cube

glColor3f(0.0,1.0,1.0);
glVertex3f(-0.5,0.5,0.5);
glColor3f(0.0,1.0,1.0);
glVertex3f(0.5,0.5,0.5);
glColor3f(0.0,1.0,1.0);
glVertex3f(0.5,0.5,-0.5);
glColor3f(0.0,1.0,1.0);
glVertex3f(-0.5,0.5,-0.5);

glColor3f(1.0,0.0,1.0);
glVertex3f(-0.5,-0.5,0.5);
glColor3f(1.0,0.0,1.0);
glVertex3f(0.5,-0.5,0.5);
glColor3f(1.0,0.0,1.0);
glVertex3f(0.5,-0.5,-0.5);
glColor3f(1.0,0.0,1.0);
glVertex3f(-0.5,-0.5,-0.5);


/*
glColor3f(1.0,0.5,0.0);
glVertex3f(ship[0][0],ship[0][1],1.0);
glColor3f(1.0,0.5,0.0);
glVertex3f(ship[0][1],ship[1][1],1.0);
glColor3f(1.0,0.5,1.0);
glVertex3f(ship[0][2],ship[2][1],1.0);*/

glEnd();

glLoadIdentity();
glRotatef(Fangle/(PI/180),0.0,0.0,1.0);
glBegin(GL_LINES);
glColor3f(0.0,1.0,0.0);
glVertex3f(0.0,1.0,0.0);
glColor3f(1.0,1.0,1.0);
glVertex3f(0.0,-1.0,0.0);
glEnd();

glLoadIdentity();
glRotatef(Hangle/(PI/180),0.0,0.0,1.0);
glBegin(GL_LINES);
glColor3f(0.0,1.0,1.0);
glVertex3f(0.0,1.0,0.0);
glColor3f(1.0,1.0,1.0);
glVertex3f(0.0,-1.0,0.0);
glEnd();
glPopMatrix();


deviceInput->keyUp=false;
deviceInput->keyLeft=false;
deviceInput->keyRight=false;
}



void display()
{
/* display callback, clear frame buffer and Z buffer, rotate cube and draw
* swap buffers */





glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);





glMatrixMode(GL_MODELVIEW);
createObjects();
glFlush();
glutSwapBuffers();

}

void idle()
{
/*display */
if (deviceInput->charKey == 'a')
{

deviceInput->charKey=NULL;
Bangle=Fangle;
bulletFired=true;
}


clk = clock () + 1.0 ;
if (clock() > clk)
glutPostRedisplay();

}


void myReshape(int w, int h)
{
vW=w;
vH=h;
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

gluPerspective(0.0,w/h,0.0f,-10.0f);


glMatrixMode(GL_MODELVIEW);

}

int main(int argc, char **argv)
{
glutInit(&argc, argv);

//****************
//A quick hack to allow for Input handling in another Cpp file
// Deviceinput is a pointer, and the new Gameinput method gives the pointer to the other Cpp file
//*************
kbPTR = keyboard;
specKbPTR=specialKeyboard;
mouseClkPTR=mouseClick;
mouseMvPTR=mouseMove;

deviceInput = new gameInput;
deviceInput->windowH=vH;
deviceInput->windowW=vW;

initInput(deviceInput);
/* need both double buffering and Z buffer */

glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(250,250);
glutCreateWindow("createObjects");
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glutIdleFunc(idle);
glutMouseFunc(mouseClkPTR);
glutKeyboardFunc(kbPTR);
glutSpecialFunc(specKbPTR);
glutMotionFunc(mouseMvPTR);
glEnable(GL_DEPTH_TEST); /*enable hidden surface removal */
glEnable(GL_TEXTURE_2D);
glutMainLoop();
}


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!