PDA

View Full Version : HELP plZZZZzzz :confused :(



atiface
05-01-2002, 06:21 AM
i need to draw a 3d surface and i have to read the data from a file, the data is given in 2d format. i don't know where to start and where to go....i have written this code but it is not working and iam scratching my head!!!!!!!
PLZZZZZZZZZZZZZZZZZZZZ HELP iam really new to this stuff but i have to do it and i'd appreciate if anyone can help me.....post it here or email me........
THANKS A LOT.
HOPING TO GET A LOT OF REPLIES from all u GENIOUS out there!!!!!!! help this poor soul
http://www.opengl.org/discussion_boards/ubb/frown.gif

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <GL/glut.h>

//#include <sys/types.h>
#include "utility.h"
#include "glUtils.h"

#define Width 150
#define Depth 100

/* Bytes per data value */
#define ByteSize 1
typedef short MapValue;


MapValue ** MapData;


#define Near 0.5
#define Far 20.0

#define cmdNoCull 1
#define cmdCWFront 2
#define cmdCCWFront 3

#define cmdExit 99

static int AppMenu;

static GLfloat Spin;

static void drawCube()
{

glBegin(GL_TRIANGLE_STRIP);
for(int i=0,i<150;i++)
{
for(int j=0;j<100;j++)
glVertex3f((GLushort)MapData[i][j],(GLushort)MapData[i][j+1],(GLushort)MapData[i][j+2]);
}
glEnd();

}

static void display ()
{
glClear(GL_COLOR_BUFFER_BIT);

glLoadIdentity();
gluLookAt(0.0, 0.5, 2.0,
0.0, 0.0, 0.0,
0.0, 1.0, 0.0);
glRotatef(Spin, 0.0, 1.0, 0.0);
glScalef(0.5, 0.5, 0.5);
glColor3f(0.0, 1.0, 0.0);
drawCube();

CheckGL();
glutSwapBuffers();
}

/*static void spinDisplay (void)
{
Spin += 2.0;
if (Spin > 360.0)
Spin -= 360.0;
glutPostRedisplay();
}*/

static void menuChoice (int item)
{
switch (item) {
case cmdNoCull:
glDisable(GL_CULL_FACE);
break;
case cmdCWFront:
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glFrontFace(GL_CW);
break;
case cmdCCWFront:
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glFrontFace(GL_CCW);
break;
case cmdExit:
exit(0);
break;
default:
break;
}
}

static void initGraphics (void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

Spin = 0.0;

AppMenu = glutCreateMenu(menuChoice);
glutSetMenu(AppMenu);
glutAddMenuEntry("No cull", cmdNoCull);
glutAddMenuEntry("CW faces", cmdCWFront);
glutAddMenuEntry("CCW faces", cmdCCWFront);
glutAddMenuEntry("----", 0);
glutAddMenuEntry("Exit", cmdExit);
glutAttachMenu(GLUT_RIGHT_BUTTON);
}

static void resize (int width, int height)
{
GLfloat aspect;

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
aspect = (float)width / (float)height;
gluPerspective(60.0, aspect, Near, Far);
glViewport(0, 0, width, height);
glMatrixMode(GL_MODELVIEW);
}

static void asciiKey (unsigned char key, int x, int y)
{
if (key == 27) /* ESC */
exit(0);
}
static void readMap2D (char * inputName)
{
FILE * input;
int i, j;
char msg[256];

/* Allocate, this time as array of arrays */
MapData = (MapValue**) malloc(Depth * sizeof(MapValue *));
for (i = 0; i < Depth; i++)
MapData[i] = (MapValue*) malloc(Width * sizeof(MapValue));

input = fopen(inputName, "rb");
if (input == NULL) printf("Cannot open map\n");

for (i = 0; i < Depth; i++)
if (fread(MapData[i], ByteSize, Width, input) != Width) {
sprintf(msg, "Cannot read correct number of values for row %d \n", i);
printf(msg);
}

fclose(input);

/* Print out, this time with two indexes */
for (i = 0; i < Depth; i++)
{

printf("Row %d: ", i);

/* Only print first and last four */
for (j = 0; j < 4; j++)
{
printf("%4d ", MapData[i][j]);

}
printf("... ");
for (j = Width - 4; j < Width; j++)
{
printf("%4d ", MapData[i][j]);

}
printf("\n");
}

}
int main (int argc, char * argv[])
{
char fileName[256];
int i;

// Show the command line
printf("Command line:\n");
for (i = 0; i < argc; i++)
printf("argv[%d] = --%s--\n", i, argv[i]);

/*Here we copy one of the command line arguments into the file name */
if (argc > 1)
strcpy(fileName, argv[1]);
else {
printf("NO FILENAME\n");
exit(1);
}


readMap2D(fileName);

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);

glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 75);
glutCreateWindow("Cube");

initGraphics();

glutDisplayFunc(display);
glutReshapeFunc(resize);
//glutIdleFunc(spinDisplay);
glutKeyboardFunc(asciiKey);

glutMainLoop();
return 0;
}


//---------------------------------------------------------------------------

nexusone
05-01-2002, 07:47 AM
How is it not working?
Not reading the file?
Not drawning the image correctly?
How to convert 2D data to 3D data?



Originally posted by atiface:
i need to draw a 3d surface and i have to read the data from a file, the data is given in 2d format. i don't know where to start and where to go....i have written this code but it is not working and iam scratching my head!!!!!!!
PLZZZZZZZZZZZZZZZZZZZZ HELP iam really new to this stuff but i have to do it and i'd appreciate if anyone can help me.....post it here or email me........
THANKS A LOT.
HOPING TO GET A LOT OF REPLIES from all u GENIOUS out there!!!!!!! help this poor soul
http://www.opengl.org/discussion_boards/ubb/frown.gif

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <GL/glut.h>

//#include <sys/types.h>
#include "utility.h"
#include "glUtils.h"

#define Width 150
#define Depth 100

/* Bytes per data value */
#define ByteSize 1
typedef short MapValue;


MapValue ** MapData;


#define Near 0.5
#define Far 20.0

#define cmdNoCull 1
#define cmdCWFront 2
#define cmdCCWFront 3

#define cmdExit 99

static int AppMenu;

static GLfloat Spin;

static void drawCube()
{

glBegin(GL_TRIANGLE_STRIP);
for(int i=0,i<150;i++)
{
for(int j=0;j<100;j++)
glVertex3f((GLushort)MapData[i][j],(GLushort)MapData[i][j+1],(GLushort)MapData[i][j+2]);
}
glEnd();

}

static void display ()
{
glClear(GL_COLOR_BUFFER_BIT);

glLoadIdentity();
gluLookAt(0.0, 0.5, 2.0,
0.0, 0.0, 0.0,
0.0, 1.0, 0.0);
glRotatef(Spin, 0.0, 1.0, 0.0);
glScalef(0.5, 0.5, 0.5);
glColor3f(0.0, 1.0, 0.0);
drawCube();

CheckGL();
glutSwapBuffers();
}

/*static void spinDisplay (void)
{
Spin += 2.0;
if (Spin > 360.0)
Spin -= 360.0;
glutPostRedisplay();
}*/

static void menuChoice (int item)
{
switch (item) {
case cmdNoCull:
glDisable(GL_CULL_FACE);
break;
case cmdCWFront:
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glFrontFace(GL_CW);
break;
case cmdCCWFront:
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glFrontFace(GL_CCW);
break;
case cmdExit:
exit(0);
break;
default:
break;
}
}

static void initGraphics (void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

Spin = 0.0;

AppMenu = glutCreateMenu(menuChoice);
glutSetMenu(AppMenu);
glutAddMenuEntry("No cull", cmdNoCull);
glutAddMenuEntry("CW faces", cmdCWFront);
glutAddMenuEntry("CCW faces", cmdCCWFront);
glutAddMenuEntry("----", 0);
glutAddMenuEntry("Exit", cmdExit);
glutAttachMenu(GLUT_RIGHT_BUTTON);
}

static void resize (int width, int height)
{
GLfloat aspect;

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
aspect = (float)width / (float)height;
gluPerspective(60.0, aspect, Near, Far);
glViewport(0, 0, width, height);
glMatrixMode(GL_MODELVIEW);
}

static void asciiKey (unsigned char key, int x, int y)
{
if (key == 27) /* ESC */
exit(0);
}
static void readMap2D (char * inputName)
{
FILE * input;
int i, j;
char msg[256];

/* Allocate, this time as array of arrays */
MapData = (MapValue**) malloc(Depth * sizeof(MapValue *));
for (i = 0; i < Depth; i++)
MapData[i] = (MapValue*) malloc(Width * sizeof(MapValue));

input = fopen(inputName, "rb");
if (input == NULL) printf("Cannot open map\n");

for (i = 0; i < Depth; i++)
if (fread(MapData[i], ByteSize, Width, input) != Width) {
sprintf(msg, "Cannot read correct number of values for row %d \n", i);
printf(msg);
}

fclose(input);

/* Print out, this time with two indexes */
for (i = 0; i < Depth; i++)
{

printf("Row %d: ", i);

/* Only print first and last four */
for (j = 0; j < 4; j++)
{
printf("%4d ", MapData[i][j]);

}
printf("... ");
for (j = Width - 4; j < Width; j++)
{
printf("%4d ", MapData[i][j]);

}
printf("\n");
}

}
int main (int argc, char * argv[])
{
char fileName[256];
int i;

// Show the command line
printf("Command line:\n");
for (i = 0; i < argc; i++)
printf("argv[%d] = --%s--\n", i, argv[i]);

/*Here we copy one of the command line arguments into the file name */
if (argc > 1)
strcpy(fileName, argv[1]);
else {
printf("NO FILENAME\n");
exit(1);
}


readMap2D(fileName);

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);

glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 75);
glutCreateWindow("Cube");

initGraphics();

glutDisplayFunc(display);
glutReshapeFunc(resize);
//glutIdleFunc(spinDisplay);
glutKeyboardFunc(asciiKey);

glutMainLoop();
return 0;
}


//