1

Can anyone explain what's going on or not going on so that I can correct it. Here's the code so far from a .cpp file. Running in Visual Studio Community 2015.

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

#define M_PI 3.14159265358979323846

const int screenWidth = 640;
const int screenHeight = 480;

Here aa, bb, cc, & dd are used to map the world coordinates to screen coordinates. I printed them out to make sure it was working correctly and the coordinates are being calculated correctly. In this case 0 < radius <= 5.

void drawHex(GLdouble radius, GLdouble aa, GLdouble bb, GLdouble cc, GLdouble dd) {
  glBegin(GL_POLYGON);
  for (int i = 0; i < 6; i++) {
        glVertex2d(((cos(i / 6.0 * 2 * M_PI) * radius) * aa) + cc,
                   ((sin(i / 6.0 * 2 * M_PI) * radius) * bb) + dd); 
  }
  glEnd();
  glFlush();
}

Here I calculate A, B, C, and D to map the world to the view and pass them to the drawHex function.

void myDisplay(void) {
  glClear(GL_COLOR_BUFFER_BIT);
  GLdouble winWidth = glutGet(GLUT_WINDOW_WIDTH);
  GLdouble winHeight = glutGet(GLUT_WINDOW_HEIGHT);
  GLdouble A, B, C, D;
  glViewport((GLint)0, (GLint)0, (GLint)winWidth, (GLint)winHeight);
  A = winWidth / 10.0;
  B = winHeight / 10.0;
  C = 0 - (A * -5.0);
  D = 0 - (B * -5.0);
  drawHex(0.5, A, B, C, D);
}

Program inits and main loop.

void myInit() {
  glClearColor(0.93, 0.93, 0.93, 0.0);
  glColor3f(1.0, 0.0, 0.0);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluOrtho2D(-5.0, 5.0, -5.0, 5.0);
  glClear(GL_COLOR_BUFFER_BIT);
}

int main(int argc, char** argv) {
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  glutInitWindowSize(screenWidth, screenHeight);
  glutInitWindowPosition(100, 150);
  glutCreateWindow("Window");
  myInit();
  glutDisplayFunc(myDisplay);
  glutMainLoop();
  return 0;
}

All I get is a blank window with the background color. I'm expecting a hexagon in the middle of the view (not yet resized to maintain a proper aspect ratio).

genpfault
  • 51,148
  • 11
  • 85
  • 139
Ricca
  • 311
  • 4
  • 21

2 Answers2

2

In your call to gluOrtho2D you pass [-5, 5] as bounds on both dimensions. In your calculation, anything going beyond this would be clipped out and will not be visible. Lets see if this is the case.

The aa, bb, cc and dd you pass are constants; they always remain: 64, 48, 320 and 240. The value you'd get after correcting the above issue will always be within [-1, 1] since that's the codomain of cos and sin functions. This gets multiplied with radius * aa and added to cc. So worst case would be (1 * 0.5 * 64) + 320 = 352, which is clearly outside the -5 boundary you'd set via gluOrtho2D.

The same observations apply to y too. Do your math on paper first and then code this up. Make sure all the boundary conditions are within limits.

It's recommended that you pass GLUT_DOUBLE to avoid flickers and as the other answer mentions, you need swap buffers at the end of the display function for double buffering to work. Read the answer to Difference between single buffered(GLUT_SINGLE) and double buffered drawing(GLUT_DOUBLE) for details. Also, you don't need to call glViewport on every rendering call, instead put it in your init function.

Community
  • 1
  • 1
legends2k
  • 31,634
  • 25
  • 118
  • 222
  • I thought that the `gluOrtho2D` was used to create a virtual coordinate system? In my case a square from -5 to 5. I also thought that the `aa`, `bb`, `cc`, and `dd` values were used to map this to screen coordinates which would obviously range from (0,0) to (window width, window height) which is very far from (-5, 5), but I was expecting that. Or am I wrong about `gluOrtho2d`? – Ricca Oct 01 '15 at 15:48
  • I see now, the `glVertex2d` does this math for me. I don't need to worry about mapping it. Thanks. – Ricca Oct 01 '15 at 16:07
0

I'm unfamiliar with glut but try adding glutSwapBuffers(); to the end of your drawing function. In OpenGL, there is always one frame being displayed to the user and one frame that is being rendered. Swapbuffers swaps these two frames: it sends the rendered frame to the user and pulls back the previous frame for further rendering.

Kvaleya
  • 101
  • 5
  • I have `glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);` which means that I am only using one buffer. I have also tried switching to `GLUT_DOUBLE` and have called `glutSwapBuffers();` at the end of the `myDisplay()` function. Still no hexagon. – Ricca Oct 01 '15 at 06:59