• Advertisement
  • Popular Tags

  • Popular Now

  • Advertisement
  • Similar Content

    • By Achivai
      Hey, I am semi-new to 3d-programming and I've hit a snag. I have one object, let's call it Object A. This object has a long int array of 3d xyz-positions stored in it's vbo as an instanced attribute. I am using these numbers to instance object A a couple of thousand times. So far so good. 
      Now I've hit a point where I want to remove one of these instances of object A while the game is running, but I'm not quite sure how to go about it. At first my thought was to update the instanced attribute of Object A and change the positions to some dummy number that I could catch in the vertex shader and then decide there whether to draw the instance of Object A or not, but I think that would be expensive to do while the game is running, considering that it might have to be done several times every frame in some cases. 
      I'm not sure how to proceed, anyone have any tips?
    • By fleissi
      Hey guys!

      I'm new here and I recently started developing my own rendering engine. It's open source, based on OpenGL/DirectX and C++.
      The full source code is hosted on github:
      https://github.com/fleissna/flyEngine

      I would appreciate if people with experience in game development / engine desgin could take a look at my source code. I'm looking for honest, constructive criticism on how to improve the engine.
      I'm currently writing my master's thesis in computer science and in the recent year I've gone through all the basics about graphics programming, learned DirectX and OpenGL, read some articles on Nvidia GPU Gems, read books and integrated some of this stuff step by step into the engine.

      I know about the basics, but I feel like there is some missing link that I didn't get yet to merge all those little pieces together.

      Features I have so far:
      - Dynamic shader generation based on material properties
      - Dynamic sorting of meshes to be renderd based on shader and material
      - Rendering large amounts of static meshes
      - Hierarchical culling (detail + view frustum)
      - Limited support for dynamic (i.e. moving) meshes
      - Normal, Parallax and Relief Mapping implementations
      - Wind animations based on vertex displacement
      - A very basic integration of the Bullet physics engine
      - Procedural Grass generation
      - Some post processing effects (Depth of Field, Light Volumes, Screen Space Reflections, God Rays)
      - Caching mechanisms for textures, shaders, materials and meshes

      Features I would like to have:
      - Global illumination methods
      - Scalable physics
      - Occlusion culling
      - A nice procedural terrain generator
      - Scripting
      - Level Editing
      - Sound system
      - Optimization techniques

      Books I have so far:
      - Real-Time Rendering Third Edition
      - 3D Game Programming with DirectX 11
      - Vulkan Cookbook (not started yet)

      I hope you guys can take a look at my source code and if you're really motivated, feel free to contribute :-)
      There are some videos on youtube that demonstrate some of the features:
      Procedural grass on the GPU
      Procedural Terrain Engine
      Quadtree detail and view frustum culling

      The long term goal is to turn this into a commercial game engine. I'm aware that this is a very ambitious goal, but I'm sure it's possible if you work hard for it.

      Bye,

      Phil
    • By tj8146
      I have attached my project in a .zip file if you wish to run it for yourself.
      I am making a simple 2d top-down game and I am trying to run my code to see if my window creation is working and to see if my timer is also working with it. Every time I run it though I get errors. And when I fix those errors, more come, then the same errors keep appearing. I end up just going round in circles.  Is there anyone who could help with this? 
       
      Errors when I build my code:
      1>Renderer.cpp 1>c:\users\documents\opengl\game\game\renderer.h(15): error C2039: 'string': is not a member of 'std' 1>c:\program files (x86)\windows kits\10\include\10.0.16299.0\ucrt\stddef.h(18): note: see declaration of 'std' 1>c:\users\documents\opengl\game\game\renderer.h(15): error C2061: syntax error: identifier 'string' 1>c:\users\documents\opengl\game\game\renderer.cpp(28): error C2511: 'bool Game::Rendering::initialize(int,int,bool,std::string)': overloaded member function not found in 'Game::Rendering' 1>c:\users\documents\opengl\game\game\renderer.h(9): note: see declaration of 'Game::Rendering' 1>c:\users\documents\opengl\game\game\renderer.cpp(35): error C2597: illegal reference to non-static member 'Game::Rendering::window' 1>c:\users\documents\opengl\game\game\renderer.cpp(36): error C2597: illegal reference to non-static member 'Game::Rendering::window' 1>c:\users\documents\opengl\game\game\renderer.cpp(43): error C2597: illegal reference to non-static member 'Game::Rendering::window' 1>Done building project "Game.vcxproj" -- FAILED. ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========  
       
      Renderer.cpp
      #include <GL/glew.h> #include <GLFW/glfw3.h> #include "Renderer.h" #include "Timer.h" #include <iostream> namespace Game { GLFWwindow* window; /* Initialize the library */ Rendering::Rendering() { mClock = new Clock; } Rendering::~Rendering() { shutdown(); } bool Rendering::initialize(uint width, uint height, bool fullscreen, std::string window_title) { if (!glfwInit()) { return -1; } /* Create a windowed mode window and its OpenGL context */ window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL); if (!window) { glfwTerminate(); return -1; } /* Make the window's context current */ glfwMakeContextCurrent(window); glViewport(0, 0, (GLsizei)width, (GLsizei)height); glOrtho(0, (GLsizei)width, (GLsizei)height, 0, 1, -1); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glfwSwapInterval(1); glEnable(GL_SMOOTH); glEnable(GL_DEPTH_TEST); glEnable(GL_BLEND); glDepthFunc(GL_LEQUAL); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); glEnable(GL_TEXTURE_2D); glLoadIdentity(); return true; } bool Rendering::render() { /* Loop until the user closes the window */ if (!glfwWindowShouldClose(window)) return false; /* Render here */ mClock->reset(); glfwPollEvents(); if (mClock->step()) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glfwSwapBuffers(window); mClock->update(); } return true; } void Rendering::shutdown() { glfwDestroyWindow(window); glfwTerminate(); } GLFWwindow* Rendering::getCurrentWindow() { return window; } } Renderer.h
      #pragma once namespace Game { class Clock; class Rendering { public: Rendering(); ~Rendering(); bool initialize(uint width, uint height, bool fullscreen, std::string window_title = "Rendering window"); void shutdown(); bool render(); GLFWwindow* getCurrentWindow(); private: GLFWwindow * window; Clock* mClock; }; } Timer.cpp
      #include <GL/glew.h> #include <GLFW/glfw3.h> #include <time.h> #include "Timer.h" namespace Game { Clock::Clock() : mTicksPerSecond(50), mSkipTics(1000 / mTicksPerSecond), mMaxFrameSkip(10), mLoops(0) { mLastTick = tick(); } Clock::~Clock() { } bool Clock::step() { if (tick() > mLastTick && mLoops < mMaxFrameSkip) return true; return false; } void Clock::reset() { mLoops = 0; } void Clock::update() { mLastTick += mSkipTics; mLoops++; } clock_t Clock::tick() { return clock(); } } TImer.h
      #pragma once #include "Common.h" namespace Game { class Clock { public: Clock(); ~Clock(); void update(); bool step(); void reset(); clock_t tick(); private: uint mTicksPerSecond; ufloat mSkipTics; uint mMaxFrameSkip; uint mLoops; uint mLastTick; }; } Common.h
      #pragma once #include <cstdio> #include <cstdlib> #include <ctime> #include <cstring> #include <cmath> #include <iostream> namespace Game { typedef unsigned char uchar; typedef unsigned short ushort; typedef unsigned int uint; typedef unsigned long ulong; typedef float ufloat; }  
      Game.zip
    • By lxjk
      Hi guys,
      There are many ways to do light culling in tile-based shading. I've been playing with this idea for a while, and just want to throw it out there.
      Because tile frustums are general small compared to light radius, I tried using cone test to reduce false positives introduced by commonly used sphere-frustum test.
      On top of that, I use distance to camera rather than depth for near/far test (aka. sliced by spheres).
      This method can be naturally extended to clustered light culling as well.
      The following image shows the general ideas

       
      Performance-wise I get around 15% improvement over sphere-frustum test. You can also see how a single light performs as the following: from left to right (1) standard rendering of a point light; then tiles passed the test of (2) sphere-frustum test; (3) cone test; (4) spherical-sliced cone test
       

       
      I put the details in my blog post (https://lxjk.github.io/2018/03/25/Improve-Tile-based-Light-Culling-with-Spherical-sliced-Cone.html), GLSL source code included!
       
      Eric
    • By Fadey Duh
      Good evening everyone!

      I was wondering if there is something equivalent of  GL_NV_blend_equation_advanced for AMD?
      Basically I'm trying to find more compatible version of it.

      Thank you!
  • Advertisement
  • Advertisement
Sign in to follow this  

OpenGL Trying to translate cube thru Z axis

This topic is 3980 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