Question

I have this code and I am trying to draw two hexagons but only one will show up. This is my first time using openGL although i've used C++ for some time now. The first hexagon shows up but the second one is nowhere to be seen. sorry for all the comments in there I was trying to draw 121 of the hexagons in a for loop.

#include <iostream>
#include <GL/glut.h>
#include <stdlib.h>
#include <string>
#define _USE_MATH_DEFINES
#include <math.h> 
void displayCall(){
    int x = 0; 
    int y = 0;
    glClearColor(1, 1, 1, 1);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable(GL_DEPTH_TEST);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-2.0, 2.0, -2.0, 2.0, -2.0, 500.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glScalef(.005, .005, .005);

// Enable polygon offsets, and offset filled polygons forward by 2.5
glEnable(GL_POLYGON_OFFSET_FILL);
glPolygonOffset(-2.5f, -2.5f);
// Set the render mode to be line rendering with a thick line width
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glLineWidth(3.0f);
// Set the colour to be white
//glColor3f(1.0f, 1.0f, 1.0f);

//outline color
glColor3f(0, 0, 0);
//for (int i = 0; i < 11; i++){
//  for (int j = 0; j < 11; j++){
        glTranslatef(-300 + x, 300 - y, 0);
        glBegin(GL_POLYGON);
        for (int i = 0; i < 6; ++i) {
            glVertex2d(50 * sin(i / 6.0 * 2 * M_PI),
                50 * cos(i / 6.0 * 2 * M_PI));
        }
        glEnd();
        glTranslatef(-200 + x, 300 - y, 0);
        glBegin(GL_POLYGON);
        for (int i = 0; i < 6; ++i) {
            glVertex2d(50 * sin(i / 6.0 * 2 * M_PI),
                50 * cos(i / 6.0 * 2 * M_PI));
        }
        glEnd();
    //  x += 50;
//  }
    //y += 50;
    //x = 0;
//  }


glutSwapBuffers();
}

int main(int argc, char **argv){
std::cout << "Test" << std::endl;

char* buf = 0;
size_t sz = 0;
if (_dupenv_s(&buf, &sz, "DISPLAY")){
    std::string display(("DISPLAY"));
    std::cout << display << std::endl;
}
else{
    std::cout << "Empty" << std::endl;
}
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(600, 600);
glutCreateWindow("RED");
glutDisplayFunc(displayCall);
glutMainLoop();
return 0;
}
Was it helpful?

Solution

You are not resetting transformation matrix.

Try this

   glPushMatrix();
   glTranslatef(-300 + x, 300 - y, 0);
   glBegin(GL_POLYGON);
   for (int i = 0; i < 6; ++i) {
        glVertex2d(50 * sin(i / 6.0 * 2 * M_PI),
            50 * cos(i / 6.0 * 2 * M_PI));
    }
    glEnd();
    glPopMatrix();
    glPushMatrix();
    glTranslatef(-200 + x, 300 - y, 0);
    glBegin(GL_POLYGON);
    for (int i = 0; i < 6; ++i) {
        glVertex2d(50 * sin(i / 6.0 * 2 * M_PI),
            50 * cos(i / 6.0 * 2 * M_PI));
    }
    glEnd();
    glPopMatrix();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top