OpenGL Help - displaying implicit functions

Soldato
Joined
18 Feb 2003
Posts
5,817
Location
Chester
right i was wondering if anyone could provide assistance with coding 2 implicit functions in C++/OpenGL

as an example i've got the following C++ func controlling the implicit functions -
Code:
GLfloat Implicit(GLfloat x, GLfloat y)
{
	GLfloat xs, ys;

	xs = (x - X0)/XS;	// Translate & scale.
	ys = (y - Y0)/YS;

	return  xs*xs + ys*ys - 1.0;	// Circle of (scaled) radius 1.
}

now - i have to code two functions into the same program and these new functions contain exponentials which i can't figure out -

ye^x + xsin(y) - 1 = 0
ye^-x - xe^y = 0

i've been looking at the contents of the math.h lib but i still can't really figure out how to input these functions.

any help would be apreciated :)
 
Last edited:
ah yes, sorry - didn't want to fill up the window with code....but heres the lot -
Code:
#include <gl/glut.h>
#include <gl/gl.h>
#include <stdio.h>
#include <math.h>
#include <string.h>

#define WD 1024
#define HT 768

void myInit();
void RenderScene(void);
void main_keyboard(unsigned char, int, int);
void command_menu(int);

const GLfloat X0 = ((GLfloat)WD)/2.0;	// Translate.
const GLfloat Y0 = ((GLfloat)HT)/2.0;

const GLfloat YS = ((GLfloat)HT)/8.0;	// Scale.
const GLfloat XS = YS;

GLfloat Implicit(GLfloat x, GLfloat y)
{
	GLfloat xs, ys;

	xs = (x - X0)/XS;	// Translate & scale.
	ys = (y - Y0)/YS;

	return  xs*xs + ys*ys - 1.0;	// Circle of (scaled) radius 1.
}

void main(int argc, char **argv)
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
	glutInitWindowPosition(0,0);
	glutInitWindowSize(WD, HT);
	glutCreateWindow("Lesson 2 - OpenGL Example 2");

	glutKeyboardFunc(main_keyboard);

	glutCreateMenu(command_menu);
	glutAddMenuEntry("Quit", 27);
	glutAttachMenu(GLUT_RIGHT_BUTTON);
	
	glutDisplayFunc(RenderScene);

	myInit();

	glutMainLoop();
}

void command_menu(int value)
{
	exit(0);	// End program.
}

void main_keyboard(unsigned char key, int x, int y)
{
	if (key == 27)
		exit(0);
}

void RenderScene(void)
{
	GLfloat x, y, f, fs;

	// Colour of window background.
	glClearColor(2.0, 2.0, 0.2, 0.0);

	glClear(GL_COLOR_BUFFER_BIT);

	glColor3f(1.0, 0.0, 0.0);	// x axis in red.
	glBegin(GL_LINES);
		glVertex2f(0,  Y0);
		glVertex2f(WD-1, Y0);
	glEnd();

	glColor3f(0.0, 1.0, 0.0);	// y axis in green.
	glBegin(GL_LINES);
		glVertex2f(X0, 0);
		glVertex2f(X0, HT-1);
	glEnd();

	glColor3f(0.0, 0.0, 1.0);	// Pixels in blue.
	glBegin(GL_POINTS);
		for (x = 0; x < WD; x++)
		{
			fs = Implicit(x, -1);	// Get starting sign.
			if (fs == 0)
				fs = -1;	// Make it non-zero.
			for (y = 0; y < HT; y++)
			{
				f = Implicit(x, y);
				if (f == 0)	// Hit it spot on so illuminate pixel.
				{
					fs = -fs;	// Now change the sign of fs.
					glVertex2f(x, y);
				}
				else
				{
					if(f*fs < 0)	// Just gone past it so illuminate pixel.
					{
						fs = f;	// Now change the sign of fs.
						glVertex2f(x, y);
					}
				}
			}
		}
	glEnd();

	glBegin(GL_POINTS);
		for (y = 0; y < WD; y++)
		{
			fs = Implicit(-1, y);	// Get starting sign.
			if (fs == 0)
				fs = -1;	// Make it non-zero.
			for (x = 0; x < HT; x++)
			{
				f = Implicit(y, x);
				if (f == 0)	// Hit it spot on so illuminate pixel.
				{
					fs = -fs;	// Now change the sign of fs.
					glVertex2f(y, x);
				}
				else
				{
					if(f*fs < 0)	// Just gone past it so illuminate pixel.
					{
						fs = f;	// Now change the sign of fs.
						glVertex2f(y, x);
					}
				}
			}
		}
	glEnd();

	glFlush();	// Now get it all drawn.
}

void myInit()
{
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluOrtho2D(0.0, WD, 0.0, HT);
	glMatrixMode(GL_MODELVIEW);
}
 
Last edited:
Back
Top Bottom