Jump to content
  • Advertisement
Sign in to follow this  
ilovetoeatbabies

Help me with a simple interface for heightfield fluid simulations?

This topic is 3661 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'm trying to write a simple heightfield fluid simulation with glut and opengl, but I'm getting bogged down with the actual interface. How would I accomplish drawing a cube or a platform in the center of the screen and have my keyboard arrows rotate the camera? My implementation below does not work properly. For example, holding the right arrow key does not make my cube spin 360 degrees.
#include <cmath>

#ifdef OSX
#include <GLUT/glut.h>
#include <OpenGL/glu.h>
#else
#include <GL/glut.h>
#include <GL/glu.h>
#endif
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

using namespace std;

#include "Grid.h"


float xTheta = 0, yTheta = 0, radius = 5;
Grid* grid = new Grid();


void init(void) 
{
	glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);  
	glShadeModel (GL_FLAT);   
	glEnable(GL_DEPTH_TEST);
   
   /*
	// lighting
	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT0);
	
	// Create light components
	float ambientLight[] = { 0.2f, 0.2f, 0.8f, 1.0f };
	float diffuseLight[] = { 0.8f, 0.8f, 0.8, 1.0f };
	float specularLight[] = { 0.5f, 0.5f, 0.5f, 1.0f };
	float position[] = { 10, 10, 10, 1.0f };
	
	// Assign created components to GL_LIGHT0
	glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
	glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
	glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight);
	glLightfv(GL_LIGHT0, GL_POSITION, position);
   */
   
}


void display(void)
{
	glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // depth handling
   
	glColor3f (1.0, 1.0, 1.0);
	glLoadIdentity ();             /* clear the matrix */
	gluLookAt (radius*cos(xTheta), radius*sin(yTheta), radius, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
   
   //glutWireCube(3.0);
	grid->randomize();
	grid->draw();
	
	glFlush ();
}


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


void keyboard (unsigned char key, int x, int y)
{
	switch (key) {
		case 'd':
			xTheta += 0.1;
			glutPostRedisplay();
			break;
		case 'a':
			xTheta -= 0.1;
			glutPostRedisplay();
			break;
		case 'w':
			yTheta += 0.1;
			glutPostRedisplay();
			break;
		case 's':
			yTheta -= 0.1;
			glutPostRedisplay();
			break;      
		default:
			break;
	}
}


int main(int argc, char** argv)
{
	glutInit(&argc, argv);
	glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
	glutInitWindowSize (700, 700); 
	glutInitWindowPosition (100, 100);
	glutCreateWindow (argv[0]);
	init ();
	glutDisplayFunc(display); 
	glutReshapeFunc(reshape);
	glutKeyboardFunc(keyboard);
   
	glutMainLoop();
	return 0;
}

Share this post


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

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!