Monashree Sanil
Monashree Sanil

Reputation: 17

plotting points in opengl

I am trying to print some points on the window and nothing is getting displayed on the window.The window background is set to grey color and that is all that happens.No points appear on the screen.here is my code

#include <GL/freeglut.h>
#include <GL/gl.h>
#include<unistd.h>
#include<iostream>
using namespace std;

float a[2]={0,0.7},b[2]={-0.3,-0.5},c[2]={0.5,-0.55};
float m[2]={0.0,0.0};

int roll_die() {

int k= (rand()%6) + 1;
return k;
}

void midpoint(float p[],float q[],float *k )
{
for(int i=0;i<2;i++)
{
k[i]=(p[i]+q[i])/2;
}
}

void displaypoint(float a[])
{

glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,1.0,1.0);

glBegin(GL_POINT);
            glVertex2f(a[0], a[1]);

    glEnd();
glPointSize(8.0);
glFlush();

}

void setup()
{
 glClear(GL_COLOR_BUFFER_BIT);
 glColor3f(1.0, 1.0, 1.0);

  glBegin(GL_POINTS);
        glVertex2f(a[0], a[1]);
        glVertex2f(b[0],b[1]);
        glVertex2f(c[0],c[1]);
 glEnd();
glPointSize(8.0);
glFlush();
}

void chaos()
{
int num;
for(int i=0;i<1000;i++)   
{
num=roll_die();
if(num==1 || num==2) 
midpoint(m,a,m);
else if(num==3 || num==4) 
midpoint(m,b,m);
else
midpoint(m,c,m);
displaypoint(m);
usleep(2000);
}
}

int main(int argc, char **argv)
{
 int num;
float a[2]={0,0.7},b[2]={-0.3,-0.5},c[2]={0.5,-0.55};
float m[2]={0.0,0.0};  
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("OpenGL - Creating a triangle");
glClearColor(0.4, 0.4, 0.4, 0.4); 
glutDisplayFunc(setup);
glutDisplayFunc(chaos);
glutMainLoop();
return 0;
}

Upvotes: 0

Views: 5088

Answers (1)

Josh Parnell
Josh Parnell

Reputation: 1216

There were several problems. To find them I just compiled it myself and worked through them:

  • GL_POINT should be GL_POINTS in displaypoint. GL_POINT means something else entirely -- you still use GL_POINTS even if you only display one point
  • You were clearing each time you drew a point, so you would only ever see one point (but the point wasn't drawing in the first place due to the above). Only clear when you want to wipe the whole screen.
  • I've not used glut so I'm not positive, but it looks like you immediately overrode the displayFunc callback with chaos, so I don't think setup was ever called. I just pushed setup into the beginning of your chaos loop.
  • You defined your arrays again in main -- it doesn't matter, but those were not being used. Only the global ones were used. I recommend adding -Wall to your compiler flags if you haven't already, as the compiler would have told you this :)

Really the point of failure was that you were clearing each time you drew a point while also not actually drawing that point due to the use of GL_POINT.

Since you're new to GL, I'll mention that glGetError() is extremely useful as a first-pass debugging mechanism. You can call it and check the return value at various points in your program to see if GL is telling you that you've done something wrong. In this case, for example, calling glGetError() after your drawpoint function would have returned the error GL_INVALID_ENUM, because you used an invalid enum value (GL_POINT rather than GL_POINTS) for your call to glBegin().

Here is a working example for you in case it helps. I hope you don't mind that I reformatted & compacted it to make it easier to share here (and some was to help me read and understand it). That's a neat program, I had no idea it was going to output what it did when I got it working. Cool :)

#include <GL/freeglut.h>
#include <GL/gl.h>
#include <unistd.h>

float a[2] = {  0.0f,  0.70f };
float b[2] = { -0.3f, -0.50f };
float c[2] = {  0.5f, -0.55f };
float m[2] = {  0.0f,  0.00f };

void midpoint(float p[], float q[], float* k) {
  for (int i = 0; i < 2; i++)
    k[i] = (p[i] + q[i]) / 2;
}

void chaos() {
  glPointSize(1.0);
  glClearColor(0.0, 0.0, 0.0, 1.0); 
  glClear(GL_COLOR_BUFFER_BIT);
  glColor3f(1.0, 1.0, 1.0);

  for(int i = 0; i < 100000; i++) {
    int num = (rand() % 6) + 1;
    if (num < 3)
      midpoint(m, a, m);
    else if (num < 5)
      midpoint(m, b, m);
    else
      midpoint(m, c, m);

    glBegin(GL_POINTS);
      glVertex2f(m[0], m[1]);
    glEnd();
    glFlush();
  }
}

int main(int argc, char* argv[]) {
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_SINGLE);
  glutInitWindowSize(500, 500);
  glutInitWindowPosition(100, 100);
  glutCreateWindow("OpenGL - Creating a triangle");
  glutDisplayFunc(chaos);
  glutMainLoop();
  return 0;
}

Output:

enter image description here

Let me know if anything doesn't make sense.

Upvotes: 6

Related Questions