Sign in to follow this  
yaazz

OpenGL Trying to translate cube thru Z axis

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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Forum Statistics

    • Total Topics
      627736
    • Total Posts
      2978856
  • Similar Content

    • By DelicateTreeFrog
      Hello! As an exercise for delving into modern OpenGL, I'm creating a simple .obj renderer. I want to support things like varying degrees of specularity, geometry opacity, things like that, on a per-material basis. Different materials can also have different textures. Basic .obj necessities. I've done this in old school OpenGL, but modern OpenGL has its own thing going on, and I'd like to conform as closely to the standards as possible so as to keep the program running correctly, and I'm hoping to avoid picking up bad habits this early on.
      Reading around on the OpenGL Wiki, one tip in particular really stands out to me on this page:
      For something like a renderer for .obj files, this sort of thing seems almost ideal, but according to the wiki, it's a bad idea. Interesting to note!
      So, here's what the plan is so far as far as loading goes:
      Set up a type for materials so that materials can be created and destroyed. They will contain things like diffuse color, diffuse texture, geometry opacity, and so on, for each material in the .mtl file. Since .obj files are conveniently split up by material, I can load different groups of vertices/normals/UVs and triangles into different blocks of data for different models. When it comes to the rendering, I get a bit lost. I can either:
      Between drawing triangle groups, call glUseProgram to use a different shader for that particular geometry (so a unique shader just for the material that is shared by this triangle group). or
      Between drawing triangle groups, call glUniform a few times to adjust different parameters within the "master shader", such as specularity, diffuse color, and geometry opacity. In both cases, I still have to call glBindTexture between drawing triangle groups in order to bind the diffuse texture used by the material, so there doesn't seem to be a way around having the CPU do *something* during the rendering process instead of letting the GPU do everything all at once.
      The second option here seems less cluttered, however. There are less shaders to keep up with while one "master shader" handles it all. I don't have to duplicate any code or compile multiple shaders. Arguably, I could always have the shader program for each material be embedded in the material itself, and be auto-generated upon loading the material from the .mtl file. But this still leads to constantly calling glUseProgram, much more than is probably necessary in order to properly render the .obj. There seem to be a number of differing opinions on if it's okay to use hundreds of shaders or if it's best to just use tens of shaders.
      So, ultimately, what is the "right" way to do this? Does using a "master shader" (or a few variants of one) bog down the system compared to using hundreds of shader programs each dedicated to their own corresponding materials? Keeping in mind that the "master shaders" would have to track these additional uniforms and potentially have numerous branches of ifs, it may be possible that the ifs will lead to additional and unnecessary processing. But would that more expensive than constantly calling glUseProgram to switch shaders, or storing the shaders to begin with?
      With all these angles to consider, it's difficult to come to a conclusion. Both possible methods work, and both seem rather convenient for their own reasons, but which is the most performant? Please help this beginner/dummy understand. Thank you!
    • By JJCDeveloper
      I want to make professional java 3d game with server program and database,packet handling for multiplayer and client-server communicating,maps rendering,models,and stuffs Which aspect of java can I learn and where can I learn java Lwjgl OpenGL rendering Like minecraft and world of tanks
    • By AyeRonTarpas
      A friend of mine and I are making a 2D game engine as a learning experience and to hopefully build upon the experience in the long run.

      -What I'm using:
          C++;. Since im learning this language while in college and its one of the popular language to make games with why not.     Visual Studios; Im using a windows so yea.     SDL or GLFW; was thinking about SDL since i do some research on it where it is catching my interest but i hear SDL is a huge package compared to GLFW, so i may do GLFW to start with as learning since i may get overwhelmed with SDL.  
      -Questions
      Knowing what we want in the engine what should our main focus be in terms of learning. File managements, with headers, functions ect. How can i properly manage files with out confusing myself and my friend when sharing code. Alternative to Visual studios: My friend has a mac and cant properly use Vis studios, is there another alternative to it?  
    • By ferreiradaselva
      Both functions are available since 3.0, and I'm currently using `glMapBuffer()`, which works fine.
      But, I was wondering if anyone has experienced advantage in using `glMapBufferRange()`, which allows to specify the range of the mapped buffer. Could this be only a safety measure or does it improve performance?
      Note: I'm not asking about glBufferSubData()/glBufferData. Those two are irrelevant in this case.
    • By xhcao
      Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness. 
  • Popular Now