PDA

View Full Version : Rectangle drawn outside the view volume



swethasharma
01-28-2012, 02:02 AM
Hi,

I want to draw a rectangle with vertices (200,200),(200,250),(250,250),(250,200).I have fixed the frustum to be (0,480,0,640).

The rectangle is drawn outside .How do i fix the code?



//#define GLFW_DLL
//#pragma comment (lib,"glew32.lib")
#define GLEW_STATIC
#include <stdio.h>
#include <gl/glew.h>
#include <glfw.h>


#define BUFFER_OFFSET(offset)((char*)NULL+offset)

void init(void)
{
int glewinitialize;
//float aspect_ratio;
glClearColor(0.0,0.0,0.0,0.0);
glShadeModel(GL_FLAT);
glewinitialize=glewInit();
if(glewinitialize==GLEW_OK)
{
printf("GLEW is available\n");
}

}

void GLFWCALL reshape(int width,int height)
{
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// gluOrtho2D(0.0,(GLdouble)width,0.0,(GLdouble)heigh t);
glFrustum(0.0,(GLdouble)width,0.0,(GLdouble)height ,0.0,20.0);
// gluPerspective(270.0,2.0,0.0,20.0);
glMatrixMode(GL_MODELVIEW);
}
void display(void)
{

glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,0.0,0.0);
glLoadIdentity();
gluLookAt(0.0,0.0,1.0,0.0,0.0,0.0,0.0,1.0,0.0);
//glTranslatef(0.0,0.0,-1.0);
//glScalef(1.0,2.0,1.0);
/*glPointSize(5.0);
glBegin(GL_POINTS);
glVertex3f(200.0,200.0,0.0);
glEnd();*/
glBegin(GL_QUADS);
glVertex3f(200.0,200.0,0.0);
glVertex3f(200.0,250.0,0.0);
glVertex3f(250.0,250.0,0.0);
glVertex3f(250.0,200.0,0.0);
glEnd();


glfwSwapBuffers();
}
int main(int argc,char** argv)
{

int wndstate,wndopenstate;

wndstate=glfwInit();

if(wndstate==1)
{
wndopenstate=glfwOpenWindow(640,480,0,0,0,0,4,0,GL FW_WINDOW);

if(wndopenstate==0)
{
glfwTerminate();
}
else
{
glfwSetWindowTitle("Hello Opengl");
glfwSetWindowPos(30,30);
init();
glfwSetWindowSizeCallback(reshape);

while(1)
{
display();
if(glfwGetKey(GLFW_KEY_ESC))
{
glfwTerminate();

}

}
}
}
glfwTerminate();
return 0;

}

V-man
01-28-2012, 07:43 AM
glFrustum(0.0,(GLdouble)width,0.0,(GLdouble)height ,0.0,20.0);

why are you using a znear of 0.0?
That should generate an GL_INVALID_VALUE.

menzel
01-28-2012, 02:39 PM
V-man: I don't see any error checks, so the glFrustum will fail silently...

Here: http://www.opengl.org/sdk/docs/man/xhtml/glFrustum.xml you can see the resulting matrix and you can see that setting near to 0 will screw up your matrix.

Also: check that the reshape will get called without changing the window size manually.

On a side note: If you're not bond to anique OpenGL implementations you might want to look into more up-to-date literature. Beside glClear(color) and glViewport I'm only seeing deprecated functions and the 90th are over. Sure, it's still widely supported but I doubt it's the right way to learn OpenGL nowadays.

swethasharma
01-28-2012, 11:53 PM
Thankyou for your response v-man and menzel.I will read up on glFrustum.

menzel-I am using Opengl Programming guide-seventh edition as reference to learn Opengl.
Can you suggest any other book which offers the updated literature.

menzel
01-29-2012, 06:28 AM
swethasharma: take a look at http://duriansoftware.com/joe/An-intro-to-modern-OpenGL.-Table-of-Contents.html or http://www.arcsynthesis.org/gltut/ .