PDA

View Full Version : Object appears only during reshape. URGENT ! ! !



coldsplash
06-03-2009, 03:26 AM
I am doing a project that accepts floor plan as user input and coverts it into a 3D view of a house. This is how far it has been developed. I have temporarily programmed such that u need to click outside the input area to convert it; later will be replaced by a menu. The problem is, after the floor plan is done, and you click outside, nothing appears. But when I change the window size, during the process of changing, the 3D object appears. Once i release the mouse, it again vanishes. PLEASE HELP.

(If you have any similar projects too, please discuss your ideas)

Here is the code. Its been written using DevC++

===========================

#include <iostream.h>
#include <stdlib.h>
#include <GL/glut.h>

int mx=0, my=0;
int mode = 1; //1: 2D, 2: 3D
int firstrun=1;
int reshaped = 0;

struct coord
{
int x[70];
int y[70];
int i;
}coordinates;

struct planmesh
{
int x1, y1;
int x2, y2;
int max_i, max_j;
}mesh1;

void square(int x, int y, int d);
void square(int x, int y, int z, int d);
void display(void);

void Init()
{
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glClear(GL_COLOR_BUFFER_BIT);
}

void square(int x, int y, int d)
{
glColor3f(0.0, 0.0, 1.0);
glBegin(GL_POLYGON);
glVertex3i(x, y, 0);
glVertex3i(x+d, y, 0);
glVertex3i(x+d, y-d, 0);
glVertex3i(x, y-d, 0);
glEnd();
glFlush();
}

void square(int x, int y, int z, int d)
{
glColor3f(0.75, 0.75, 0.75);
glBegin(GL_POLYGON);
glVertex3i(x, y, z);
glVertex3i(x+d, y, z);
glVertex3i(x+d, y-d, z);
glVertex3i(x, y-d, z);
glEnd();
glFlush();
}

void mymouse(int button, int state, int x, int y)
{
int ix, iy;
int i, j;
i = 0;

if (mode == 1)
if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
if (x>=mesh1.x1 && x<=mesh1.x2 && y>=mesh1.y1 && y<=mesh1.y2) //within the mesh
{
for (ix=mesh1.x1; ix<x-15 && ix<mesh1.x2-15; ix+=15, i++);
j = mesh1.max_j;
for (iy=mesh1.y1+15; iy<y && iy<mesh1.y2; iy+=15, j--);

square(ix, iy, 15); //draw square
coordinates.x[coordinates.i] = ix;
coordinates.y[coordinates.i] = iy;
coordinates.i++;
}
else
{
mode = 2;
glutPostRedisplay();
}
}

//if (mode==2)
}

void mesh(GLint x1, int y1, int x2, int y2)
{
int i, j;
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_LINES);
for (int i=y1; i<y2; i+=15)
{
glVertex3i(x1, i, 0);
glVertex3f(x2, i, 0);
}
for (double i=x1; i<x2; i+=15)
{
glVertex3f(i, y1, 0.0);
glVertex3f(i, y2, 0.0);
}
glEnd();
glFlush();

glBegin(GL_LINE_LOOP);
glVertex3f(x1, y1, 0.0);
glVertex3f(x2, y1, 0.0);
glVertex3f(x2, y2, 0.0);
glVertex3f(x1, y2, 0.0);
glEnd();
glFlush();
}

void cube(int x, int y, int d)
{
glColor3f(1.0, 0.0, 0.0);
glPushMatrix();
glRotated(-45, 1, 0, 0);
for (int i=0; i<200; i++)
square(x, y, i, 15);
glPopMatrix();
}

void wall_x(int x1, int x2, int y)
{
glColor3f(0.75, 0.75, 0.75);
glBegin(GL_POLYGON);
glVertex3f(x1, y, 0.0);
glVertex3f(x2, y, 0.0);
glVertex3f(x2, y, 70.0);
glVertex3f(x1, y, 70.0);
glEnd();
glFlush();
}

void wall_y(int y1, int y2, int x)
{
glColor3f(0.75, 0.75, 0.75);
glBegin(GL_POLYGON);
glVertex3f(x, y1, 0.0);
glVertex3f(x, y2, 0.0);
glVertex3f(x, y2, 70.0);
glVertex3f(x, y1, 70.0);
glEnd();
glFlush();
}

void reshape(int width, int height)
{
reshaped = 1;
if (mode==1)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, width, height, 0);
glMatrixMode(GL_MODELVIEW);
glutPostRedisplay();
}
if (mode==2)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, width, 0.0, height, 0.0, 800.0);
//gluOrtho2D(0, width, height, 0);
glMatrixMode(GL_MODELVIEW);
glRotated(-25, 0, 1, 0);
glRotated(70, 1, 0, 0);
glutPostRedisplay();
}
}

void display()
{
int ix, iy, i, j;

glClearColor(1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
if (mode == 1)
{
mesh1.x1 = 0; // Set mesh coordinates
mesh1.y1 = 0;
mesh1.x2 = 795;
mesh1.y2 = 495;

if (firstrun)
{
coordinates.i = 0;

i=j=0;
for (ix=mesh1.x1; ix<mesh1.x2-15; ix+=15, i++);
mesh1.max_i = i;

for (iy=mesh1.y1+15; iy<mesh1.y2; iy+=15, j++);
mesh1.max_j = j-1;

firstrun = 0;
}

mesh(mesh1.x1, mesh1.y1, mesh1.x2, mesh1.y2);
if (reshaped)
{
reshaped = 0;

i = 0;
while (i<=coordinates.i)
{
square(coordinates.x[i], coordinates.y[i], 15);
i++;
}
}
}

if (mode==2)
{
/*glClearColor(1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glFlush();*/

glRotated(-25, 0, 1, 0);
glRotated(70, 1, 0, 0);

for (i=0; i<=coordinates.i; i++)
{
for (j=0; j<=coordinates.i; j++)
{
if (i!=j)
{
if (coordinates.x[i]==coordinates.x[j])
wall_y(coordinates.y[i], coordinates.y[j], coordinates.x[i]);
if (coordinates.y[i]==coordinates.y[j])
wall_x(coordinates.x[i], coordinates.x[j], coordinates.y[i]);
}
}
}

}
}

int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(800, 600);
glutCreateWindow("Mesh Selector");
glutMouseFunc(mymouse);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return EXIT_SUCCESS;
}

zeoverlord
06-03-2009, 05:28 AM
the only think i can see is that you forgot to put a glutPostRedisplay(); in the mymouse func, since there is no code yet for mode 2.

coldsplash
06-03-2009, 05:39 AM
thanks for your time... i found that there was an issue with the Z coordinates that I was trying to use. Here is the updated code. Its quite simple now. Do you think we can add lighting and shading to such an object? In the sense, I have done it for cubes, but don know if lighting can work for this. Please advice.

#include <iostream.h>
#include <stdlib.h>
#include <GL/glut.h>

int mx=0, my=0;
int mode = 1; //1: 2D, 2: 3D
int firstrun=1, m2firstrun=1;
int reshaped = 0;

struct coord
{
int x[70];
int y[70];
int i;
}coordinates;

struct planmesh
{
int x1, y1;
int x2, y2;
int max_i, max_j;
}mesh1;

void square(int x, int y, int d);
void square(int x, int y, int z, int d);
void display(void);

void Init()
{
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glClear(GL_COLOR_BUFFER_BIT);
}

void square(int x, int y, int d)
{
glColor3f(0.0, 0.0, 1.0);
glBegin(GL_POLYGON);
glVertex3i(x, y, 0);
glVertex3i(x+d, y, 0);
glVertex3i(x+d, y-d, 0);
glVertex3i(x, y-d, 0);
glEnd();
glFlush();
}

void square(int x, int y, int z, int d)
{
glColor3f(0.75, 0.75, 0.75);
glBegin(GL_POLYGON);
glVertex3i(x, y, z);
glVertex3i(x+d, y, z);
glVertex3i(x+d, y-d, z);
glVertex3i(x, y-d, z);
glEnd();
glFlush();
}

void mymouse(int button, int state, int x, int y)
{
int ix, iy;
int i, j;
i = 0;

if (mode == 1)
if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
if (x>=mesh1.x1 && x<=mesh1.x2 && y>=mesh1.y1 && y<=mesh1.y2) //within the mesh
{
for (ix=mesh1.x1; ix<x-15 && ix<mesh1.x2-15; ix+=15, i++);
j = mesh1.max_j;
for (iy=mesh1.y1+15; iy<y && iy<mesh1.y2; iy+=15, j--);

square(ix, iy, 15); //draw square
coordinates.x[coordinates.i] = ix;
coordinates.y[coordinates.i] = iy;
cout<<"\nX: "<<ix<<", Y: "<<iy;
coordinates.i++;
}
else
{
mode = 2;
glutPostRedisplay();
}
}

//if (mode==2)
}

void mesh(GLint x1, int y1, int x2, int y2)
{
int i, j;
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_LINES);
for (int i=y1; i<y2; i+=15)
{
glVertex3i(x1, i, 0);
glVertex3f(x2, i, 0);
}
for (double i=x1; i<x2; i+=15)
{
glVertex3f(i, y1, 0.0);
glVertex3f(i, y2, 0.0);
}
glEnd();
glFlush();

glBegin(GL_LINE_LOOP);
glVertex3f(x1, y1, 0.0);
glVertex3f(x2, y1, 0.0);
glVertex3f(x2, y2, 0.0);
glVertex3f(x1, y2, 0.0);
glEnd();
glFlush();
}

void wall_x(int x1, int x2, int y)
{
glColor3f(0.75, 0.75, 0.75);
glBegin(GL_POLYGON);
glVertex3f(x1, y, -300.0);
glVertex3f(x2, y, -300.0);
glVertex3f(x2, y, -230.0);
glVertex3f(x1, y, -230.0);
glEnd();
glFlush();
}

void wall_y(int y1, int y2, int x)
{
glColor3f(0.75, 0.75, 0.75);
glBegin(GL_POLYGON);
glVertex3f(x, y1, -300.0);
glVertex3f(x, y2, -300.0);
glVertex3f(x, y2, -230.0);
glVertex3f(x, y1, -230.0);
glEnd();
glFlush();
}

void reshape(int width, int height)
{
reshaped = 1;
if (mode==1)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, width, height, 0);
glMatrixMode(GL_MODELVIEW);
glutPostRedisplay();
}
if (mode==2)
{
cout<<"\nMode 2 reshape";
cout<<"\nWidth: "<<width<<", Height: "<<height;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, width, 0.0, height, 0.0, 800.0);
glMatrixMode(GL_MODELVIEW);
display();
}
}

void draw_floor()
{
glPushMatrix();

glRotated(45, 1, 0, 0);
glRotated(-25, 0, 0, 1);

glColor3f(0.375, 0.375, 0.375);
glBegin(GL_POLYGON);
glVertex3f(0, 0, -300.0);
glVertex3f(0, 600, -300.0);
glVertex3f(800, 600, -300.0);
glVertex3f(800, 0, -300.0);
glEnd();
glFlush();

glPopMatrix();
}

void display()
{
int ix, iy, i, j;

glClearColor(1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
if (mode == 1)
{
mesh1.x1 = 0; // Set mesh coordinates
mesh1.y1 = 0;
mesh1.x2 = 795;
mesh1.y2 = 495;

if (firstrun)
{
coordinates.i = 0;

i=j=0;
for (ix=mesh1.x1; ix<mesh1.x2-15; ix+=15, i++);
mesh1.max_i = i;

for (iy=mesh1.y1+15; iy<mesh1.y2; iy+=15, j++);
mesh1.max_j = j-1;

firstrun = 0;
}

mesh(mesh1.x1, mesh1.y1, mesh1.x2, mesh1.y2);
if (reshaped)
{
reshaped = 0;

i = 0;
while (i<=coordinates.i)
{
square(coordinates.x[i], coordinates.y[i], 15);
i++;
}
}
}

if (mode==2)
{
cout<<"\nMode 2 display";

if (m2firstrun==1)
{
m2firstrun = 0;
glViewport(0, 0, 800, 600);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 800.0, 0.0, 600.0, 0.0, 800.0);
glMatrixMode(GL_MODELVIEW);
}

//draw_floor();

glPushMatrix();
glRotated(45, 1, 0, 0);
glRotated(-25, 0, 0, 1);
/*glRotated(-25, 0, 1, 0);
glRotated(70, 1, 0, 0);*/

for (i=0; i<=coordinates.i; i++)
{
for (j=0; j<=coordinates.i; j++)
{
if (i!=j)
{
if (coordinates.x[i]==coordinates.x[j])
wall_y(coordinates.y[i], coordinates.y[j], coordinates.x[i]);
if (coordinates.y[i]==coordinates.y[j])
wall_x(coordinates.x[i], coordinates.x[j], coordinates.y[i]);
}
}
}
glPopMatrix();
}
}

int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(800, 600);
glutCreateWindow("Mesh Selector");
glutMouseFunc(mymouse);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return EXIT_SUCCESS;
}