PDA

View Full Version : Object disappears when trying to scale



opt!kal
04-19-2010, 07:40 PM
Hi there,

So i've come across a weird bug that I just cant seem to figure out. Basically I have code that takes in a set of verticies from a file and constructs a various triangles to form a cube. From there I am trying to scale it by some amount by either pressing the ',' or '.' keys. However whenever I press these keys (or any key for that matter) my object disappears. Any help on how to fix this would be greatly appreciated!


#include <stdio.h>

#include <stdlib.h>

#include <math.h>



#include <GL/glut.h>



typedef struct vect_{
float x;
float y;
float z;
} vect;

typedef struct tvect_{
int a;
int b;
int c;
} tvect;

typedef struct fourTup_{
int a;
int b;
int c;
int d;
} fourTup;

vect verticies[20];

tvect triangles[20];

fourTup quad[20];

int vertexCounter = 1;

int triangleCounter = 0;

int quadCounter = 0;

char outString[150];

char garbageStr[100];

int i = 0;

int tempCount = 0;

float dk_grey[4];

float lt_grey[4];

float medium_grey[4];

float notshiny[1];

float light_position[4];

float light_ambient[4];

float light_diffuse[4];

float light_specular[4];

float scale_fac = 1;

void fileHandler(){
FILE * fp;
fp = fopen("input.txt", "r");

if(fp == NULL) {
printf("Error! File Not Found!");
return;
}

while(fgets (outString, 150, fp) != NULL){
if(outString[0] == '#' || outString[0] == '\n'){
}
else if(strstr(outString, "v") != NULL){
printf(outString);
sscanf(outString, "%c %f %f %f", garbageStr, &amp;verticies[vertexCounter].x, &amp;verticies[vertexCounter].y, &amp;verticies[vertexCounter].z);

printf("%f %f %f\n\n", verticies[vertexCounter].x, verticies[vertexCounter].y, verticies[vertexCounter].z);
vertexCounter++;
}
else if(strstr(outString, "t") != NULL){
printf(outString);
sscanf(outString, "%c %d %d %d", garbageStr, &amp;triangles[triangleCounter].a, &amp;triangles[triangleCounter].b, &amp;triangles[triangleCounter].c);
printf("%d %d %d\n\n", triangles[triangleCounter].a, triangles[triangleCounter].b, triangles[triangleCounter].c);
triangleCounter++;
}
else if(strstr(outString, "q") != NULL){
printf(outString);
sscanf(outString, "%c %d %d %d %d", garbageStr, &amp;quad[quadCounter].a, &amp;quad[quadCounter].b, &amp;quad[quadCounter].c, &amp;quad[quadCounter].d);
printf("%d %d %d %d\n\n", quad[quadCounter].a, quad[quadCounter].b, quad[quadCounter].c, quad[quadCounter].d);
quadCounter++;
}
}

}

void init() {

glEnable(GL_DEPTH_TEST);
glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glMatrixMode(GL_PROJECTION);
gluPerspective(60.0, 1.0, 0.1, 10.0);
light_position[0] = 0.0;
light_position[1] = 0.0;
light_position[2] = 10.0;
light_position[3] = 1.0;

light_ambient[0] = 0.0;
light_ambient[1] = 0.0;
light_ambient[2] = 0.0;
light_ambient[3] = 1.0;

light_diffuse[0] = 1.0;
light_diffuse[1] = 1.0;
light_diffuse[2] = 1.0;
light_diffuse[3] = 1.0;

light_specular[0] = 1.0;
light_specular[1] = 1.0;
light_specular[2] = 1.0;
light_specular[3] = 1.0;

glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);

dk_grey[0] = 0.2;
dk_grey[1] = 0.2;
dk_grey[2] = 0.2;
dk_grey[3] = 1.0;

medium_grey[1] = 0.5;
medium_grey[2] = 0.5;
medium_grey[3] = 0.5;
medium_grey[4] = 1.0;

lt_grey[1] = 0.8;
lt_grey[2] = 0.8;
lt_grey[3] = 0.8;
lt_grey[4] = 1.0;

notshiny[0] = 5.0;

glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, lt_grey);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, dk_grey);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, lt_grey);
glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, notshiny);

}



void display(void){

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(1.5, 0.0, 4.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
//glTranslatef(0.0, 0,0, 0.0);
//glMultMatrixf(rmat);
glScalef(scale_fac, scale_fac, 1);
//glScalef(fit_fac, fit_fac, fit_fac);
//glTranslatef(recenter_x, recenter_y, recenter_z)

if(triangleCounter > 1){
for(i; i < triangleCounter; i++){
glBegin(GL_TRIANGLES); glNormal3f(verticies[triangles[i].a].x, verticies[triangles[i].a].y, verticies[triangles[i].a].z);
//glColor3f(1.0f,0.0f,0.0f);
glVertex3f(verticies[triangles[i].a].x, verticies[triangles[i].a].y, verticies[triangles[i].a].z);
glNormal3f(verticies[triangles[i].b].x, verticies[triangles[i].b].y, verticies[triangles[i].b].z);
//glColor3f(0.0f,1.0f,0.0f);
glVertex3f(verticies[triangles[i].b].x, verticies[triangles[i].b].y, verticies[triangles[i].b].z);
glNormal3f(verticies[triangles[i].c].x, verticies[triangles[i].c].y, verticies[triangles[i].c].z);
//glColor3f(0.0f,0.0f,1.0f);
glVertex3f(verticies[triangles[i].c].x, verticies[triangles[i].c].y, verticies[triangles[i].c].z);
glEnd();
}
}
else if(quadCounter > 1){
for(i; i < quadCounter; i++){
glBegin(GL_QUADS);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(verticies[quad[i].a].x, verticies[quad[i].a].y, verticies[quad[i].a].z);
glColor3f(0.0f,1.0f,0.0f);
glVertex3f(verticies[quad[i].b].x, verticies[quad[i].b].y, verticies[quad[i].b].z);
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(verticies[quad[i].c].x, verticies[quad[i].c].y, verticies[quad[i].c].z);
glColor3f(0.5f,0.5f,0.5f);
glVertex3f(verticies[quad[i].d].x, verticies[quad[i].d].y, verticies[quad[i].d].z);
glEnd();
}
}
glutSwapBuffers();

//glFlush();
}



void processNormalKeys(unsigned char key, int x, int y){

if(key == '.'){
scale_fac = scale_fac + 0.1;
}

if(key == ','){
scale_fac = scale_fac - 0.1;
}

if (key == 'q'){
exit(0);
}
glutPostRedisplay();

}

int main(int argc, char **argv){
fileHandler();
printf("%s %d", "The number of verticies are: ", vertexCounter);
printf("%s %d", "\nThe number of triangles are: ", triangleCounter);
printf("%s %d", "\nThe number of quads are: ", quadCounter);
printf("%s %d", "\nThis is the triangles: ", tempCount);
//printf("%s %f %f %f", "\nTHE FIRST THREE THINGS: ", verticies[triangles[0].a].x, verticies[triangles[0].a].y, verticies[triangles[0].a].z);
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Test");
init();
glutDisplayFunc(display);
glutKeyboardFunc(processNormalKeys);
glutMainLoop();
return 0;
}[/B]