PDA

View Full Version : help me out with code



herba
04-13-2012, 08:48 PM
i got 2 view port display separate , if i display single image is fine , but when i put 2 image together , it come out 2 straight lines .

what's the problem with code
#include <stdlib.h>
#include <windows.h>
#include <iostream>
#include <glut.h>
#define _USE_MATH_DEFINES
#include <math.h>

GLfloat winWidth=800 ,winHeight=400;




void init ()
{
glClearColor(1,1,1,0);

glMatrixMode(GL_PROJECTION);
gluOrtho2D(-150,150,-150,150);


glMatrixMode(GL_MODELVIEW);
gluOrtho2D(-150,150,-150,150);
glLoadIdentity();
}
void sqrShape ()
{
glColor3f(0.0,0.0,1.0);

glBegin(GL_LINE_LOOP);
glVertex2f(100,100);
glVertex2f(-100,100);
glVertex2f(-100,-100);
glVertex2f(100,-100);
glEnd();


}
void gyroShape()
{

glColor3f(0.0,0.0,1.0);
glBegin(GL_LINE_LOOP);
glVertex2f(80,10);
glVertex2f(-80,10);
glVertex2f(-80,-10);
glVertex2f(80,-10);
glEnd();
}
void firstPic()
{
for(float i =1; i< 12; i++)
{

glPushMatrix();
gyroShape();
glScalef(0.8,1.2,1);
glColor3f(1,0,0);

}
glPopMatrix();

glFlush();

}
void scalePic()
{
sqrShape();
float i=0;
for (i=0;i<12;i++)
{
glPushMatrix();
glRotatef(5,0,0,1);
glScalef(0.9,0.9,1);
sqrShape();

}
for (i=0;i<12;i++)
{
glPopMatrix();
}

glFlush();
}
void display()
{

glClear(GL_COLOR_BUFFER_BIT);
glViewport(0.0, 0.0,winWidth/2,winHeight);
firstPic();

glViewport(winWidth/2, 0.0,winWidth/2,winHeight);
scalePic();


glFlush();
}

/*void winReshapeFcn (int newWidth, int newHeight)
{

glMatrixMode (GL_PROJECTION);
glLoadIdentity ( );
gluOrtho2D(-100,100,-100,100);


glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluOrtho2D(-100,100,-100,100);


}*/

void main(int argc, char**argv)
{
glutInit(&amp;argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowPosition(200,200);
glutInitWindowSize(winWidth,winHeight);
glutCreateWindow("Assignment 1 - 2D Image Display");

init ();
glutDisplayFunc(display);

glutMainLoop();
}

tonyo_au
04-13-2012, 11:23 PM
in firstPic your pushMatrix and popMatrix do not match

herba
04-14-2012, 03:35 AM
in firstPic your pushMatrix and popMatrix do not match

can u be more specific ?

tonyo_au
04-14-2012, 05:56 AM
your push is inside the for loop, your pop is not, so you push 12 matrices onto the stack but only pop 1. This may not be your only error, but it should be fixed

herba
04-14-2012, 08:58 AM
but if i put pop into loop , their will be only rectangle left on the viewport .
and even do as u said , the 2 image will become tiny in viewport at few second .




void firstPic()
{
for(float i =1; i< 12; i++)
{

glPushMatrix();
gyroShape();
glScalef(0.8,1.2,1);
glColor3f(1,0,0);

}
glPopMatrix();

glFlush();

}

BionicBytes
04-14-2012, 09:22 AM
There are so many problems with this code. I'm not sure whay you are trying to do - and neither I think are you.

Firstly,

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluOrtho2D(-100,100,-100,100);

gluOrtho2D is not used on the modelview matrix - only the projection.
So this should just be:

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();




next,
scalePic() and FirstPic() both have the same problem. You keep pushing the model matrix without a corresponding pop.
What are you trying to do. You could use the loop counter to cumulatively increase the rotation angle if that's what you are trying to do, for example.
Eg.

void firstPic()
{
for(float i =1; i< 12; i++)
{

glPushMatrix();
gyroShape();
glScalef(0.8,1.2,1);
glColor3f(1,0,0);

}

This could become:

void firstPic()
{
for(float i =1; i< 12; i++)
{

glPushMatrix();
glScalef(i * 0.8, i * 1.2, i * 1); //change the modelview matrix BEFORE you make glvertex calls
gyroShape();
glPopMatrix();
}

then get rid of:

for (i=0;i<12;i++)
{
glPopMatrix();
}