c++ - 在 openGL/GLUT 中绘制多个形状

标签 c++ opengl visual-studio-2012 glut

我有这段代码,我想画两个六边形,但只有一个会出现。这是我第一次使用 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;
}

最佳答案

您没有重置转换矩阵。

试试这个

   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();

关于c++ - 在 openGL/GLUT 中绘制多个形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22290550/

相关文章:

c++ - 半圆柱的 X、Y 位置 - 射线 - 三角形 - 与空间 [-1,1] [-1,1] 的交点

c# - Visual Studio 2012 中是否有相当于 MonoDevelop 的文档大纲?

c++ - 将图像放在另一个图像上

c++ - 使用DCMTK读取3D Dicom图像并将其转换为OpenCV Mat

opengl - 四 strip 纹理失真

visual-studio - 如何阻止 Visual Studio 创建 pdb 调试文件夹?

c# - 我如何在 Visual Studio 2012 C# 中注释标记的文本? (不是线)

c++ - 使用模板将一对 vector 中的所有元素合并为一个 vector

java - 使用对象标识符作为变量名

java - 添加光照 glLightfv 方法不起作用