OpenGL 中的两个三角形、直线和边框
two triangles, line, and border in OpenGL
我很困惑如何制作两个不相邻的三角形(它们之间有间隙)以及一条线和一个边框。我已经完成了一个代码,但由于某种原因三角形和线条不会出现,当我评论三角形和线条代码时只有边框出现。三角形、线条和边框也有颜色,希望那个代码是正确的。
你认为我的代码有什么问题?
#include<GL/glut.h>
#include<iostream>
using namespace std;
void triangle();
void line();
void border();
void display();
int main(int argc, char** argv){
glutInit(&argc, argv);
glutCreateWindow("simple");
glClearColor(1.0, 1.0, 0.0, 1.0);
glutDisplayFunc(display);
glutMainLoop();
}
void display(){
glClear(GL_COLOR_BUFFER_BIT);
triangle();
line();
border();
glFlush();
}
void triangle() {
glClear(GL_COLOR_BUFFER_BIT);
glTranslatef(0.0f,0.0f,-4.0f);
glBegin(GL_TRIANGLES);
glColor3f(0.0f,0.0f,1.0f); glVertex2f(-1.0f,-0.25f);
glColor3f(0.0f,0.0f,1.0f); glVertex2f(-0.5f,-0.25f);
glColor3f(0.0f,0.0f,1.0f); glVertex2f(-0.75f,0.25f);
glColor3f(0.0f,0.0f,1.0f); glVertex2f(0.5f,-0.25f);
glColor3f(0.0f,0.0f,1.0f); glVertex2f(1.0f,-0.25f);
glColor3f(0.0f,0.0f,1.0f); glVertex2f(0.75f,0.25f);
glEnd();
glFlush();
}
void line() {
glClear(GL_COLOR_BUFFER_BIT);
glLineWidth(5.0f);
glBegin(GL_LINES);
glColor3f(0.0, 0.0, 1.0); glVertex2f(-0.3, -0.3);
glColor3f(0.0, 0.0, 1.0); glVertex2f(0.3, -0.3);
glEnd();
glFlush();
}
void border() {
glClear(GL_COLOR_BUFFER_BIT);
glLineWidth(7.0f);
glBegin(GL_LINE_LOOP);
glColor3f(0.0f, 0.0f, 0.0f);
glVertex2f(-1, -1);
glVertex2f(-1, 1);
glVertex2f(1, 1);
glVertex2f(1, -1);
glEnd();
glFlush();
}
问题是在绘制每个网格之前清除了颜色缓冲区。这会“清除”之前的绘制网格。在display
中绘制场景之前调用glClear(GL_COLOR_BUFFER_BIT)
一次,但不要在triangle
、line
和border
中清除颜色缓冲区。只需从这些函数中删除 glClear(GL_COLOR_BUFFER_BIT)
。
我很困惑如何制作两个不相邻的三角形(它们之间有间隙)以及一条线和一个边框。我已经完成了一个代码,但由于某种原因三角形和线条不会出现,当我评论三角形和线条代码时只有边框出现。三角形、线条和边框也有颜色,希望那个代码是正确的。
你认为我的代码有什么问题?
#include<GL/glut.h>
#include<iostream>
using namespace std;
void triangle();
void line();
void border();
void display();
int main(int argc, char** argv){
glutInit(&argc, argv);
glutCreateWindow("simple");
glClearColor(1.0, 1.0, 0.0, 1.0);
glutDisplayFunc(display);
glutMainLoop();
}
void display(){
glClear(GL_COLOR_BUFFER_BIT);
triangle();
line();
border();
glFlush();
}
void triangle() {
glClear(GL_COLOR_BUFFER_BIT);
glTranslatef(0.0f,0.0f,-4.0f);
glBegin(GL_TRIANGLES);
glColor3f(0.0f,0.0f,1.0f); glVertex2f(-1.0f,-0.25f);
glColor3f(0.0f,0.0f,1.0f); glVertex2f(-0.5f,-0.25f);
glColor3f(0.0f,0.0f,1.0f); glVertex2f(-0.75f,0.25f);
glColor3f(0.0f,0.0f,1.0f); glVertex2f(0.5f,-0.25f);
glColor3f(0.0f,0.0f,1.0f); glVertex2f(1.0f,-0.25f);
glColor3f(0.0f,0.0f,1.0f); glVertex2f(0.75f,0.25f);
glEnd();
glFlush();
}
void line() {
glClear(GL_COLOR_BUFFER_BIT);
glLineWidth(5.0f);
glBegin(GL_LINES);
glColor3f(0.0, 0.0, 1.0); glVertex2f(-0.3, -0.3);
glColor3f(0.0, 0.0, 1.0); glVertex2f(0.3, -0.3);
glEnd();
glFlush();
}
void border() {
glClear(GL_COLOR_BUFFER_BIT);
glLineWidth(7.0f);
glBegin(GL_LINE_LOOP);
glColor3f(0.0f, 0.0f, 0.0f);
glVertex2f(-1, -1);
glVertex2f(-1, 1);
glVertex2f(1, 1);
glVertex2f(1, -1);
glEnd();
glFlush();
}
问题是在绘制每个网格之前清除了颜色缓冲区。这会“清除”之前的绘制网格。在display
中绘制场景之前调用glClear(GL_COLOR_BUFFER_BIT)
一次,但不要在triangle
、line
和border
中清除颜色缓冲区。只需从这些函数中删除 glClear(GL_COLOR_BUFFER_BIT)
。