PDA

View Full Version : Compiles--Does Not Render



81and
10-11-2010, 03:09 PM
I can compile this code, but it's not rendering. I'm using the newest version of Xcode, with the OpenGL and GLUT frameworks in C++. Any suggestions?

**Note: It has a sister program that I ran fine, and I'm able to run both in Visual Studio, but I prefer working on a Mac. It should work from what I can tell...



#include <stdlib.h>
#include <math.h>
#include <GLUT/glut.h>

int xPOINTS = 50;
int yPOINTS = 50;
const float xMIN = -10.0;
const float xMAX = 10.0;
const float yMIN = -10.0;
const float yMAX = 10.0;

const int XRES = 1;
const int YRES = 2;
const int ZMAG = 3;
const int CAMERA = 4;
int currentControl = 0;

bool axis = true;
float zScaleFactor = 50.0;
float rho = 0.0;
float sigmaX = 1.0;
float sigmaY = 1.0;
const float PI = 3.1415;
float cameraAngle = 45 * PI / 180;

float normal(float x, float y)
{
// You need change the function to the 2D normal distribution function.
// Thus you need use variables rho, sigmaX, and sigmaY here.
float result = exp(-(x * x + y * y) / 2) / 2 / PI;
return result;
}

void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glLoadIdentity();

gluLookAt(25 * cos(cameraAngle), 25 * sin(cameraAngle), 5, 0, 0, 5, 0, 0, 1);

glColor3f(1.0, 1.0, 1.0);
for (int i = 0; i < xPOINTS - 1; i++)
{
glBegin(GL_QUAD_STRIP);
for (int j = 0; j < yPOINTS; j++)
{
float x, y;
x = xMIN + (xMAX - xMIN) / xPOINTS * i;
y = yMIN + (yMAX - yMIN) / yPOINTS * j;
glVertex3f(x, y, normal(x, y) * zScaleFactor);
x = xMIN + (xMAX - xMIN) / xPOINTS * (i + 1);
glVertex3f(x, y, normal(x, y) * zScaleFactor);
}
glEnd();
}

if (axis)
{
glBegin(GL_LINES);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(1.3 * xMAX, 0.0, 0.0);
glColor3f(0.0, 1.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 1.3 * yMAX, 0.0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 0.0, 1.4 / 2 / PI * zScaleFactor / sigmaX / sigmaY / sqrt(1 - rho * rho));
glEnd();

glColor3f(1.0, 1.0, 0.0);
glRasterPos3f(1.2 * xMAX, -0.1 * yMAX, 0.0);
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, 'x');

glRasterPos3f(-0.1 * xMAX, 1.2 * yMAX, 0.0);
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, 'y');

glRasterPos3f(0.0, 0.1, 1.3 * zScaleFactor / 2 / PI / sigmaX / sigmaY / sqrt(1 - rho * rho));
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, 'z');
}

/* don't wait!
* start processing buffered OpenGL routines
*/
//glFlush ();
glutSwapBuffers();
}

void init (void)
{
/* select clearing color */
glClearColor (0.0, 0.0, 0.0, 0.0);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glEnable(GL_CULL_FACE);
//glEnable(GL_DEPTH_TEST);
}

void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, (float) w / h, 0, 100);
glMatrixMode(GL_MODELVIEW);
}

void keyboard(unsigned char key, int x, int y)
{
if (key == 'q' || key == 'Q' || key == 27)
exit(0);
}

void topMenuFunc(int id)
{
switch (id)
{
case 1:
currentControl = XRES;
break;
case 2:
currentControl = YRES;
break;
case 3:
currentControl = ZMAG;
break;
case 4:
axis = !axis;
break;
case 5:
currentControl = CAMERA;
break;
case 6:
currentControl = 0;
axis = true;
xPOINTS = yPOINTS = 50;
zScaleFactor = 50;
cameraAngle = 45 * PI / 180;
sigmaX = sigmaY = 1.0;
rho = 0.0;
break;
}
glutPostRedisplay();
}
void createMenu()
{
int topMenu;
topMenu = glutCreateMenu(topMenuFunc);
glutAddMenuEntry("X Resolution: x", 1);
glutAddMenuEntry("Y Resolution: y", 2);
glutAddMenuEntry("Z SCale: z", 3);
glutAddMenuEntry("Axes: a", 4);
glutAddMenuEntry("Camera: c", 5);
glutAddMenuEntry("Reset: r", 6);

glutAttachMenu(GLUT_RIGHT_BUTTON);
}

void motion(int x, int y)
{
static int first = 0;
static int yPrev;
if (first == 0)
{
yPrev = y;
first = 1;
return;
}

switch (currentControl)
{
case XRES:
if (y > yPrev)
xPOINTS -= 1;
else
xPOINTS += 1;
break;
case YRES:
if (y > yPrev)
yPOINTS -= 1;
else
yPOINTS += 1;
break;
case ZMAG:
if (y > yPrev)
{
if (zScaleFactor > 2)
zScaleFactor--;
}
else
zScaleFactor++;
break;
case CAMERA:
if (y < yPrev)
{
cameraAngle += 0.05;
if (cameraAngle > 2 * PI)
cameraAngle -= 2 * PI;
}
else
{
cameraAngle -= 0.05;
if ( cameraAngle < 0)
cameraAngle += 2 *PI;
}
break;
}

yPrev = y;
glutPostRedisplay();
}

int main(int argc, char** argv)
{
//int topMenu;
glutInit(&amp;argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (500, 300);
glutInitWindowPosition (100, 100);
glutCreateWindow ("Math Visualization");
createMenu();
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMotionFunc(motion);
glutMainLoop();
return 0; /* ANSI C requires main to return int. */
}

Zyx_2000
10-12-2010, 08:16 AM
I don't see why this would work different on different systems, but I'd suggest you to find any error messages given by glGetError();

arekkusu
10-12-2010, 11:33 AM
You're setting the near plane to 0 in gluPerspective, which is illegal.