Hello, this is my very first post here.
Im posting because im a little lost on a proyect im trying to develop. Its a function plotting software.
Im having problems to view the plot as a whole, i tried settin gluPerspective but its like the same thing.
What im trying to do is to draw 3 axes and locate the plot there. Then view the plot something like this.
z
|
|_____ x
/
/ PLOT (x^2 + y^2)
y
Here it is the source code, its for unix*
i hope you can point me in the right direction.
Thank you very much.
PS: You need to press ‘-’ a lot to see the plot
#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>
#include <stdio.h>
typedef struct
{
double beg;
double end;
} axis;
axis dx, dy, dz;
int width = 400, length = 400;
double
xscale (axis x, double p)
{
//return (p - x.beg) * length / (x.end - x.beg);
return (p) * (x.end - x.beg)/length;
}
double
yscale (axis y, double p)
{
//return (p - y.beg) * width / (y.end - y.beg);
return (p)*(y.end - y.beg) / width ;
}
double
zscale (axis z, double p)
{
return (p - z.beg) * 5 / (z.end - z.beg);
}
void
reshape(int x, int y)
{
width = x, length =y;
glViewport(0, 0, x, y);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//glOrtho(dx.beg, dx.end, dy.beg, dy.end, -700,700);
//glOrtho(dx.beg, dx.end, dy.beg, dy.end, dz.beg,dz.end);
//glOrtho(-x/2, x/2, -y/2, y/2, -500,500);
glOrtho(0, x, 0, y, -500,500);
//gluPerspective(45.0, (float)x/y , 0.1, 32.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(1,1,1, 0,0,0, 0,1,0);
glutPostRedisplay();
}
void
keyboard(unsigned char key, int x, int y)
{
double stepx = (dx.end - dx.beg)/10;
double stepy = (dy.end - dy.beg)/10;
double stepz = (dz.end - dz.beg)/10;
switch (key)
{
case '+':
dx.beg += stepx;
dx.end -= stepx;
dy.beg += stepy;
dy.end -= stepy;
dz.beg += stepz;
dz.end -= stepz;
puts("FUCK");
break;
case '-':
dx.beg -= stepx;
dx.end += stepx;
dy.beg -= stepy;
dy.end += stepy;
dz.beg -= stepz;
dz.end += stepz;
break;
}
glutPostRedisplay ();
}
double max = 0;
static double angulo = 0;
#define POINTS 400
void
lines(void)
{
int i, j, k;
double table[POINTS][POINTS];
for(i = 0; i < POINTS; i+=4)
{
for(j = 0; j < POINTS; j+=4)
{
table[i][j] = i*i + j*j;
}
}
glLoadIdentity();
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3d (1, 1, 1);
glBegin(GL_LINES);
glVertex3d(0,0,0);
glVertex3d(0,0,400);
glVertex3d(0,0,0);
glVertex3d(0,400,0);
glVertex3d(0,0,0);
glVertex3d(400,0,0);
glEnd();
for(k = 0; k < 2; k++)
{
glBegin (GL_POINTS);
for(i = 0; i < POINTS; i+=4)
{
for(j = 0; j < POINTS; j+=4)
{
k == 0 ? glVertex3d(xscale(dx, i), yscale(dy, j), zscale(dz, table[i][j])) : glVertex3d(xscale(dx, j), yscale(dy, i), zscale(dz, table[j][i]));
}
}
glEnd();
}
glutSwapBuffers();
}
int
main(int argc, char **argv)
{
dx.beg = 0;
dx.end = 2;
dy.beg = 0;
dy.end = 2;
dz.beg = 0;
dz.end = 5;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(width, length);
glutCreateWindow("3D GRAPH");
glClearColor(0,0,0,0);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glEnable(GL_COLOR_MATERIAL);
glutKeyboardFunc(keyboard);
glutReshapeFunc(reshape);
glutDisplayFunc(lines);
glutIdleFunc(lines);
//glutDisplayFunc(lorenzAttractor);
//glutIdleFunc(lorenzAttractor);
glutMainLoop();
}