PDA

View Full Version : Drawing primitives:



anoop4real
09-30-2009, 09:41 PM
Hi,

I am new to opengl programming. I have started learning it with the help of red book. Please find below the code which i used to draw a triangle. The problem is that one side of the triangle is always touching the sides of the window which is drawn, irrespective of whatever co-ordinates that I provide.
What should I do to make it appear at the centre of the window?


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


void display (void){
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,0.0,0.0);
glBegin(GL_TRIANGLES);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(-10.0f,-10.0f, 0.0f);
glVertex3f(10.0f, -10.0f, 10.0f);
glEnd();
glFlush();

}

void init (void)
{
/* select clearing (background) color */
glClearColor (0.0, 0.0, 0.0, 0.0);
/* initialize viewing values */
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

/*Main*/

int main (int argc,char** argv){
glutInit(&amp;argc,argv);
glutInitDisplayMode(GLUT_SINGLE);
glutInitWindowSize(500,500);
/*Set the posotion of window*/
glutInitWindowPosition(0,0);
glutCreateWindow("My Vindow");
glutDisplayFunc(display);
init();
glutMainLoop();
return 0;

}

rakesh_thp
10-01-2009, 12:39 AM
Is this example taken from somewhere else or written by ur self..??

first thing is the projection type is missing in init() function...

And also as far as my knowledge is concerned,

glutMainLoop() must take display as its input..

anoop4real
10-01-2009, 04:34 AM
Hi thanks for the reply, the problem was something related to the co-ordinates that I used to draw the triangle. The below code worked fine.

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


void display (void){
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,0.0,0.0);
glBegin(GL_TRIANGLES);
glVertex3f(-0.35f,-0.25f, 0.0f); // lower left vertex
glVertex3f( 0.35f,-0.25f, 0.0f); // lower right vertex
glVertex3f( 0.0f, 0.35f, 0.0f); // Upper vertex.
glEnd();
glFlush();

}

void init (void)
{
/* select clearing (background) color */
glClearColor (0.0, 0.0, 0.0, 0.0);
/* initialize viewing values */
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

/*Main*/

int main (int argc,char** argv){
glutInit(&amp;argc,argv);
glutInitDisplayMode(GLUT_SINGLE);
glutInitWindowSize(500,500);
/*Set the posotion of window*/
glutInitWindowPosition(0,0);
glutCreateWindow("My Vindow");
glutDisplayFunc(display);
init();
glutMainLoop();
return 0;

}