Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
I want draw a cricle in opengl by GL_POINT and there is my code:
C++
#include "stdafx.h"
#include <glut.h>
#include <math.h>

const float PI =3.141592653;
void init()
{
	glClearColor(1,1,1,0);
	glShadeModel(GLU_FLAT);
	glPointSize( 6.0 );
}
void display(void)
{
	glClear(GL_COLOR_BUFFER_BIT);
	// drawing

	float x,y;
	glColor3f(0,0,1);
	
		glBegin(GL_POINT);
	
		for ( float angle = 0; angle <= 2*PI; angle+=0.1)
	{
		x =( .3) * cos (angle);
		y =( .3) * sin (angle);

		glVertex2f(x,y);
		
	}
	
	
	glEnd();

	glutSwapBuffers();
}
void main(int argc , char * argv[])
{
	glutInit(& argc , argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);

	// Window of drawing
	glutInitWindowSize(500,500);
	glutInitWindowPosition(100 , 100);
	glutCreateWindow("my first program");
	init();
	glutDisplayFunc(display);
	glutMainLoop();
}

but when I run program I just see a white window!,if you have idea to solve it pliz help me.
tnx :)
Posted
Updated 13-Oct-13 10:23am
v2

Hi ,
You must put glBegin and glEnd blocks inside the loop:
C++
#include <glut.h>
#include <math.h>

const float PI =3.141592653;
void init()
{
    glClearColor(1,1,1,0);
    glShadeModel(GLU_FLAT);
    glPointSize( 6.0 );
}
void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    // drawing

    float x,y;
    glColor3f(0,0,1);



    for ( float angle = 0; angle <= 2*PI; angle+=0.1)
    {
        x =( 0.3) * cos (angle);
        y =( 0.3) * sin (angle);
        glBegin(GL_POINTS);
        glVertex2f(x,y);
        glEnd();

    }




    glutSwapBuffers();
}
void main(int argc , char * argv[])
{
    glutInit(& argc , argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);

    // Window of drawing
    glutInitWindowSize(500,500);
    glutInitWindowPosition(100 , 100);
    glutCreateWindow("my first program");
    init();
    glutDisplayFunc(display);
    glutMainLoop();
}


The circle will be nicer , if you change point size to default , and use more steps in loop:
C++
#include <glut.h>
#include <math.h>

const float PI =3.141592653;
void init()
{
    glClearColor(1,1,1,0);
    glShadeModel(GLU_FLAT);
    //glPointSize( 6.0 );
}
void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    // drawing

    float x,y;
    glColor3f(0,0,1);



    for ( float angle = 0; angle <= 2*PI; angle+=0.01)
    {
        x =( 0.3) * cos (angle);
        y =( 0.3) * sin (angle);
        glBegin(GL_POINTS);
        glVertex2f(x,y);
        glEnd();

    }




    glutSwapBuffers();
}
void main(int argc , char * argv[])
{
    glutInit(& argc , argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);

    // Window of drawing
    glutInitWindowSize(500,500);
    glutInitWindowPosition(100 , 100);
    glutCreateWindow("my first program");
    init();
    glutDisplayFunc(display);
    glutMainLoop();
}
 
Share this answer
 
Comments
enhzflep 13-Oct-13 20:41pm    
Actually, in response to your first sentence - no, you don't need to put glBegin and glEnd inside the loop - in fact, you shouldn't do that. It causes a performance-hit. You can simply put a single call to glBegin before the loop, followed by a single call to glEnd after the loop.

Try it, you'll see! :) My 4.
Mohammad Sharify 15-Oct-13 15:26pm    
hi and thanks you for replay, my problem not solved!
I dont see It!
(sorry for my poor english :D)
I'm pretty sure it doesn't work because you didn't set up your projection matrix.
Put this code inside your init function:
C++
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 500.0, 500.0, 0.0, 1.0, -1.0); //As an example
glMatrixMode(GL_MODELVIEW);


EDIT: Oh, and also add
C++
glLoadIdentity();
under your glClear(...) call
 
Share this answer
 
v2
Comments
adiqais 12-Dec-13 0:55am    
I did everything at the end it didn't work white screen
Singender Holzkuebel 12-Dec-13 2:53am    
Ok, I think i found the bug now.
Replace "glBegin(GL_POINT);" with "glBegin(GL_POINTS);". You are parsing the wrong "GL_POINT" argument, which should be "GL_POINTS".

See my new post for the working code.
adiqais 12-Dec-13 3:10am    
where in my code I have glBegin(GL_POINT);
check the below code ...
Singender Holzkuebel 12-Dec-13 3:13am    
I used the code from your original post.
C#
#include <glut.h>
//#include <glu.h>
#include <math.h>

const float PI = 3.141592653;

void init(void)
{
    glClearColor(1,1,1,0);
    glShadeModel(GLU_FLAT);
    glPointSize(6.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0, 500.0, 500.0, 0.0, 1.0, -1.0);
    glMatrixMode(GL_MODELVIEW);
    /**glBegin(GL_POINTS);
    //drawing circle with points
    glClearColor(1,1,1,0);
    glShadeModel(GLU_FLAT);
    glPointSize(6.0);
    //glVertex2f(1.0f,0.2f);
    //glVertex2f(0.5f,-0.5f);
    glEnd;*/
}


void display(void)
{

    glClear(GL_COLOR_BUFFER_BIT);
    glLoadIdentity();
    //drawing
    float x,y;
    glColor3f(0,0,1);

    for(float angle = 0; angle<=2*PI; angle+=0.1)
    {
    x =(0.3)*cos (angle);
    y=(0.)*sin (angle);
    glBegin(GL_POINTS);
    glVertex2f(x,y);
    glEnd;
    }//endFor

    glutSwapBuffers();
    /**glColor3f(1.0,0.0,0.0);
    glVertex3f(1.0f,1.0f,1.0f);
    glColor3f(1.0,0.0,0.0);
    glVertex3f(-1.0f,-1.0f,0.0f);*/

}
void main(int argc, char** argv)
{
    int mode = GLUT_RGB | GLUT_DOUBLE;
        //int mode = GLUT_SINGLE depend of parity of your card
    glutInit(& argc, argv);
    glutInitDisplayMode(mode);//function initializing display mode

    glutInitWindowSize(500,500);// size of window width & height pixel

    glutInitWindowPosition(100,100);//(0,0) left bottom corner position

    glutCreateWindow ("First Project");

    init();

    glutDisplayFunc(display);//name of the Object/Function
    //gluSphere x = new gluSphere();
    glutMainLoop();
    //return 0;
}//endmain
 
Share this answer
 
Working code!

C++
#include "stdafx.h"
#include <glut.h>
#include <math.h>

const float PI =3.141592653;
void init()
{
  glClearColor(1,1,1,0);
  glShadeModel(GLU_FLAT);
  glPointSize( 6.0 );

  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  //glOrtho(0.0, 500.0, 500.0, 0.0, 1.0, -1.0); //As an example
  glOrtho(-1.0, 1.0, 1.0, -1.0, 1.0, -1.0); //Look up glOrtho to know what this does ;)
  glMatrixMode(GL_MODELVIEW);
}
void display(void)
{
  glClear(GL_COLOR_BUFFER_BIT);
  glLoadIdentity();
  // drawing

  float x,y;
  glColor3f(0,0,1);

  glBegin(GL_POINTS);

  for ( float angle = 0; angle <= 2*PI; angle+=0.1)
  {
    x =( .3) * cos (angle);
    y =( .3) * sin (angle);

    glVertex2f(x,y);

  }


  glEnd();

  glutSwapBuffers();
}
void main(int argc , char * argv[])
{
  glutInit(& argc , argv);
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);

  // Window of drawing
  glutInitWindowSize(500,500);
  glutInitWindowPosition(100 , 100);
  glutCreateWindow("my first program");
  init();
  glutDisplayFunc(display);
  glutMainLoop();
}


Your mistakes were:
C++
glBegin(GL_POINT);

"GL_POINT" is used for other OpenGL functions. Valid arguments for glBegin are:
GL_POINTS
GL_LINES
GL_LINE_STRIP
GL_LINE_LOOP
GL_TRIANGLES
GL_TRIANGLE_STRIP
GL_TRIANGLE_FAN
GL_QUADS
GL_QUAD_STRIP
GL_POLYGON


Mistake #2
C++
void init()
{
  ...
}

In your init function you forgot to initialize the projection matrix.
This matrix does all the magic behind converting OpenGL unit coordinates to screen coordinates.

That's it. If you have any question feel free to ask.
 
Share this answer
 
v2
Comments
adiqais 12-Dec-13 3:50am    
I did exactly what you pointed above nothing change, also #include "stdafx.h" error shown
Singender Holzkuebel 12-Dec-13 4:04am    
What operating system are you working on?
adiqais 12-Dec-13 4:12am    
windows 8
Singender Holzkuebel 12-Dec-13 4:17am    
Then you probably have to change your includes to something like:
#include <gl gl.h="">
#include <gl glut.h=""> //Or maybe just glut.h, depends on where your compiler searches for glut.

If stdafx.h can't be found, remove the include.
You should know how to manage your own includes...
adiqais 12-Dec-13 4:22am    
I ve tried it all with without include thanks a lot, I ll keep trying

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900