我有这个代码,我试图绘制两个六边形但只有一个会出现。这是我第一次使用openGL,虽然我已经使用了C ++一段时间了。第一个六边形出现但第二个六边形无处可见。对不起那里的所有评论我试图在for循环中绘制121个六边形。
#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;
}
答案 0 :(得分:1)
您没有重置转换矩阵。
试试这个
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();