-1

I am trying to draw a trigonometric graph with OpenGL. This is a part of my code:

   double x = -1;
   gl.glColor3d(0, 0, 0);
   gl.glBegin(gl.GL_LINES);
    gl.glVertex2d(x, Math.sin(Math.toRadians(x))*0.01);
    gl.glVertex2d(x+0.01, Math.sin(Math.toRadians(x+0.01))*0.01);
   gl.glEnd();

   x += 0.01;

This part is repeated in my full code. When this is executed, I see nothing. Can anybody tell me why this might be happening?

genpfault
  • 51,148
  • 11
  • 85
  • 139
ianc1339
  • 124
  • 1
  • 11

1 Answers1

0
  1. I do not see any loop in your code

    and also the scales are suspicious (without matrices hard to tell) try this instead:

    double x;
    gl.glColor3d(0.0, 0.0, 0.0);
    gl.glBegin(gl.GL_LINE_STRIP);
    for (x=-180.0;x<=180.0;x+=0.01)
     gl.glVertex2d(x/180.0, Math.sin(Math.toRadians(x)));
    gl.glEnd();
    

    it uses LINE_STRIP instead of LINES and the graph is scaled to <-1,+1> which is most likely your viewing area...

  2. settings

    Also there might be other problems like the comments suggest. On top of them check the obvious:

    gl.glDisable(gl.GL_DEPTH_TEST);
    gl.glDisable(gl.GL_TEXTURE_2D);
    

    in case they has been set from other part of your code...

  3. wrong camera settings

    I do not see any matrix code so I hope your camera is facing the correct direction and your graph is inside frustrum or whatever you use...

  4. GL rendering

    In case your GL code is missing or has wrong structure then it should look like this (puted together all the bullets):

    // (double) buffering
    gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
    gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT); // non eed for the depth although
    // here set matrices in case they are not persistent
    gl.glMatrixMode(gl.GL_MODELVIEW);
    gl.glLoadIdentity();
    gl.glMatrixMode(gl.GL_PROJECTION);
    gl.glLoadIdentity();
    // here set/reset config the pipeline
    gl.glDisable(gl.GL_DEPTH_TEST);
    gl.glDisable(gl.GL_TEXTURE_2D);
    gl.glLineWidth(1.0);
    // here render
    double x;
    gl.glColor3d(0.0, 0.0, 0.0);
    gl.glBegin(gl.GL_LINE_STRIP);
    for (x=-180.0;x=<180.0;x+=0.01)
     gl.glVertex2d(x/180.0, Math.sin(Math.toRadians(x)));
    gl.glEnd();
    // (double) buffering
    gl.glFlush();
    SwapBuffers(hdc);
    

    I do not code in JAVA so instead of SwapBuffers(hdc); use whatever you got in JAVA for the same purpose. Hope I did not make any mistake while translating to JAVA.

For more info see:

Community
  • 1
  • 1
Spektre
  • 49,595
  • 11
  • 110
  • 380
  • Thank you @Spektre It Works! – ianc1339 Apr 23 '17 at 20:13
  • @kimchiboy03 now you can rem out line by line to find out what was the problem in the first place. My bet is your matrices was not set for the scale you was rendering or at all. – Spektre Apr 24 '17 at 06:10