PDA

View Full Version : My first code doesnt work -help needed



glopengl
10-26-2008, 09:15 AM
Hello all,
i just started using openGl and have been using it in eclipse with glut library.
I just wrote this code. First i tried drawing a circle.Nothing came up on the screen.So to confirm,i tried rendering a rectangle.But nothing is coming up on the screen.I do not know,if it is a problem in my code, or i need some other library.can anyone kindly help me on this.
Here is my code

#include <windows.h>
#include <math.h>
#include <GL/glut.h>

const int WIDTH = 600;
const int HEIGHT = 400;

/* Prototypes */
void init();
void display();

/* Definitions */

/* Initializes the OpenGL state */
void init() {
glClearColor(0.0,0.0,0.0,0.0 ); /* Set the clear color */
}

/* Displays a black clear screen */
void display() {

glClear( GL_COLOR_BUFFER_BIT ); /* Clear the screen with the clear color */

glBegin(GL_LINES); //begin
glColor3f(1.0f, 1.0f, 1.0f); //draw with this color //on blackbackground of window
glRectf(0.0f,0.0f,50.0f,50.0f);//at this loc and //width and height
glEnd();
glFlush();
glutSwapBuffers(); /* Double buffering */

}




/* The main function */
int main( int argc, char *argv[] ) {
/* Glut setup function calls */
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB ); /* Use double buffering and RGB colors */
glutInitWindowPosition( 100, 100 );
glutInitWindowSize( WIDTH, HEIGHT );
glutCreateWindow( argv[0] );
init();
glutDisplayFunc( display ); /* Call back display function */
glutMainLoop(); /* Continue drawing the scene */
return 0;
}




Awaiting reply,
Many thanks

trinitrotoluene
10-26-2008, 09:20 AM
You do not define a reshape function like this:


void reshape(int w,int h)
{
width = w;
height = h;
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//gluPerspective(65.0,(GLfloat)width/(GLfloat)height,1.0,100.0);
glOrtho(0.0,(GLdouble)width,0,(GLdouble)height,-100.0,100.0);
glMatrixMode(GL_MODELVIEW);
}

Then add glutReshapeFunc(reshape); inside your main.

glopengl
10-26-2008, 09:40 AM
Hello TNT,
Thank you very much!