I have the following opengl app which plots 2d funxtions. I want to turn it into one for plotting 3D surfaces with min. changes that is maintain the drawVertex and just change the projection. I was unable to do that as shown in the code...

First Here is the 2D function plot:

#include <math.h>
#include <GL\glut.h>

const int SCREENWIDTH = 640;
const int SCREENHEIGHT = 640;

GLdouble A, B, C, D;
GLdouble minX = -1.0, maxX = 1.0;
GLdouble minF = -1.0, maxF = 1.0;

void myInit(void) {
glClearColor(1.0, 1.0, 1.0, 0.0);
glColor3f(0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble)SCREENWIDTH, 0.0, (GLdouble)SCREENHEIGHT);
A = SCREENWIDTH/(maxX-minX);
B = -minX * SCREENWIDTH/(maxX-minX);
C = SCREENHEIGHT/(maxF-minF);
D = -minF * SCREENHEIGHT/(maxF-minF);
}

void myDisplay(void) {
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINE_STRIP);
for (GLdouble a=0.0; a<2.0*3.14159265; a+=0.001) {
// GLdouble x = (10.0+sin(15.0*a)) * cos(a);
// GLdouble f = (10.0+sin(15.0*a)) * sin(a);
GLdouble r = sin(2.0*a);
GLdouble x = r * cos(a);
GLdouble f = r * sin(a);
glVertex2d(A*x+B, C*f+D);
}
glEnd();
glFlush();
}

int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(SCREENWIDTH, SCREENHEIGHT);
glutInitWindowPosition(100, 100);
glutCreateWindow("Function Plot");
glutDisplayFunc(myDisplay);
myInit();
glutMainLoop();
return 0;
}


Now here is my unsuccessful attempt:

#include <math.h>
#include <GL\glut.h>

const int SCREENWIDTH = 640;
const int SCREENHEIGHT = 640;

GLdouble A, B, C, D, E, F;
GLdouble minX = -1.0, maxX = 1.0;
GLdouble minY = -1.0, maxY = 1.0;
GLdouble minF = 1.0, maxF = 3.0;

void myInit(void) {
glClearColor(1.0, 1.0, 1.0, 0.0);
glColor3f(0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// gluOrtho2D(0.0, (GLdouble)SCREENWIDTH, 0.0, (GLdouble)SCREENHEIGHT);
glOrtho(0.0,(GLdouble)SCREENHEIGHT,0.0,(GLdouble)S CREENWIDTH,0.0,(GLdouble)SCREENWIDTH);
//glFrustum(0.0,(GLdouble)SCREENHEIGHT,0.0,(GLdouble )SCREENWIDTH,0.0,(GLdouble)SCREENWIDTH);
//gluPerspective(45,1.0,0.5,3.5);
A = SCREENHEIGHT/(maxX-minX);
B = -minX * SCREENHEIGHT/(maxX-minX);
C = SCREENWIDTH/(maxY-minY);
D = -minY * SCREENWIDTH/(maxY-minY);
E = SCREENWIDTH/(maxF-minF);
F = -minF * SCREENWIDTH/(maxF-minF);
//glMatrixMode(GL_MODELVIEW);
// glLoadIdentity();
// gluLookAt(0,0,0,-1.5,-1.5,0,0.0,0.0,1.0);
glEnable(GL_DEPTH_TEST);
}

void myDisplay(void) {
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINE_STRIP);
for (GLdouble u=-1.0; u<=1.0; u+=0.001) {
for (GLdouble t=-1.0; t<=1.0; t+=0.001) {
GLdouble /*x*/f = t*(u*u - t*t)+2.0;
GLdouble /*y*/x = u;
GLdouble /*f*/y = u*u - t*t;
// glPushMatrix();
// glRotatef(90.0,0.0,1.0,0.0);
glVertex3d(A*x+B, C*y+D, E*f+F);
// glPopMatrix();
}
}
glEnd();
glFlush();
}

int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(SCREENWIDTH, SCREENHEIGHT);
glutInitWindowPosition(100, 100);
glutCreateWindow("3D Function Plot");
glutDisplayFunc(myDisplay);
myInit();
glutMainLoop();
return 0;
}