PDA

View Full Version : Problem loading my graphic



smithy12
11-04-2008, 07:54 AM
All I am trying to do is load up a simple polygon but all I get is a blank screen.

Everything compiles correctly and runs but I just don't get my polygon coming up.


I have created a window 200 x 300 and created my 2d matrix with the glOrtho2D() function.

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



void myinit(void) // simple initialisation function
{
glClearColor(1.0, 1.0, 1.0, 0.0) ; // sets clearing (background) colour

glMatrixMode(GL_PROJECTION); // Projects the matrix
gluOrtho2D(-8.8, 8.8, -8.8, 8.8); // sets the 2D projection matrix area

}





void polygon(void)
{

glColor3f(1.0,0.0,0.0); // polygon set to red

glBegin(GL_POLYGON);
glVertex2f(0.0, 0.0);
glVertex2f(0.0, 3.0);
glVertex2f(4.0, 3.0);
glVertex2f(6.0, 1.5);
glVertex2f(4.0, 0.0);
glEnd();

}




void display(void)
{

glClear(GL_COLOR_BUFFER_BIT);
glDisable(GL_DEPTH_TEST);
glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

glPushMatrix();

polygon();



//popmatrix()

//pushmatrix()

//translate() //moves the object
//polygon()
//glFlush();



}


main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(200, 300);
glutInitWindowPosition(0,0);
glutCreateWindow("Assignment 2 polyogon");
glutDisplayFunc(display);
myinit();
glutMainLoop();

return 0 ;
}



Can you please help as I am struggling to understand why?

trinitrotoluene
11-04-2008, 08:22 AM
You lack a glutSwapBuffers at the end of your display function. You must also match the number of push and pop matrix call. And it should be good to have a reshape function: put a glViewport(...) call there with your gluOrtho2D. Then add glutReshapeFunc(reshape).

smithy12
11-04-2008, 08:36 AM
#include <stdlib.h>
#include <GL/glut.h>



void myinit(void) // simple initialisation function
{
glClearColor(1.0, 1.0, 1.0, 0.0) ; // sets clearing (background) colour

glMatrixMode(GL_PROJECTION); // Projects the matrix


}


void polygon(void)
{

glColor3f(1.0,0.0,0.0); // polygon set to red

glBegin(GL_POLYGON);
glVertex2f(0.0, 0.0);
glVertex2f(0.0, 3.0);
glVertex2f(4.0, 3.0);
glVertex2f(6.0, 1.5);
glVertex2f(4.0, 0.0);
glEnd();

}


void display(void)
{

glClear(GL_COLOR_BUFFER_BIT);
glDisable(GL_DEPTH_TEST);
glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

glPushMatrix();

polygon();


//popmatrix()

//pushmatrix()

//translate() //moves the object
//polygon()
//glFlush();

glutSwapBuffers();

}


void reshape(void)
{

glViewport-1.0, 1.0, -1.0, 1.0 );
gluOrtho2D(-8.8, 8.8, -8.8, 8.8); // sets the 2D projection matrix area

}


main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(200, 300);
glutCreateWindow("Assignment 2 polyogon");
glutInitWindowPosition(0,0);
myinit();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();

return 0 ;
}

I have made the changes you asked but all I get is an error message :

BOX.c: In function ‘reshape’:
BOX.c:68: error: invalid operands to binary - (have ‘void (*)(GLint, GLint, GLsizei, GLsizei)’ and ‘double’)
BOX.c:68: error: expected ‘;’ before ‘)’ token
BOX.c:68: error: expected statement before ‘)’ token
BOX.c: In function ‘main’:
BOX.c:82: warning: passing argument 1 of ‘glutReshapeFunc’ from incompatible pointer type
make: *** [BOX] Error 1.

Im so lost. I cant believe how hard it is to just put a shape on the screen.

PLEASE HELP

trinitrotoluene
11-04-2008, 08:39 AM
The reshape function must have a prototype like this void reshape(int width,int height). Then the glViewport call should be something like this glViewport(0,0,width,height);

Edit: In general reshape function look like this:


void reshape(int width,int height)
{
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho2D(...)
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

smithy12
11-04-2008, 08:52 AM
that worked thank you.

I am trying to create the exact same shape but obviously move it.
I am attempting to do this with the Matix push,pop and transform functions.

to my understanding the glTranslate takes the x and y coordinate that I want to change it by. therefore if i were to put glTranslate(1.0,1.0) in it woul move the new shapes position up 1 on the x and y axes?

void display(void)
{

glClear(GL_COLOR_BUFFER_BIT);
glDisable(GL_DEPTH_TEST);
glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

glPushMatrix();

polygon();

glPopMatrix();

glPushMatrix();

glTranslate(1.0,1.0); //moves the object
polygon();

//glFlush();

glutSwapBuffers();

}

trinitrotoluene
11-04-2008, 08:58 AM
Yes, but glTranslatef(x,y,z) (http://www.opengl.org/sdk/docs/man/xhtml/glTranslate.xml) take 3 parameters, then call glTranslatef(x,y,0.0).

smithy12
11-04-2008, 10:05 AM
problem solved

trinitrotoluene
11-04-2008, 10:42 AM
When you draw a shape with GL_POLYGON, it must be convex. For drawing your C letter you must subdivide it so all sub part of the letter are convex.

Edit:



*---------*
| \ 1 |
| *-----*
| |
| 2 |
| *-----*
| / 3 |
*---------*

So the letter C can be drawn with 3 polygons.

smithy12
11-04-2008, 11:07 AM
My shapes are going off the screen because my shape starts in the center of the screen. How do i change it to start in the top left???

Is it something to do with the viewpoint?

trinitrotoluene
11-04-2008, 11:25 AM
First you can modify gluOrtho2D(-20.0,20.0,-20.0,20.0); to view a larger "field". Second in your drawing function, you can use gluLookAt() (http://www.opengl.org/sdk/docs/man/xhtml/gluLookAt.xml) just after the glLoadIdentity(); at the beginning of the display function.

ZbuffeR
11-04-2008, 12:22 PM
use glTranslate