简体   繁体   中英

two triangles, line, and border in OpenGL

I'm quite confused on how I can make two triangles that are not beside each other (they have a gap between them) along with a line and a border. I have a code done already, but for some reason the triangles and line won't show up, only the border is the one showing up when I comment the triangle and line codes. The triangles, line, and border have color too, hopefully the code for that one is correct.

What do you think is wrong with my code?

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

The problem is that the color buffer is cleared before each mesh is drawn. This "clears" the previous draw mesh. Call glClear(GL_COLOR_BUFFER_BIT) once before drawing the scene in display , but don't clear the color buffer in triangle , line and border . Just remove glClear(GL_COLOR_BUFFER_BIT) from these functions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM