OpenGL C ++制作三角形

时间:2015-03-07 11:03:24

标签: c++ opengl codeblocks

我正在尝试制作一个三角形,如果我右键单击它会改变它的颜色,但我的代码不起作用。我正在使用Codeblocks OpenGL GLUT项目。如果我将GLUT)GB值更改为0,则不会显示输出

#include<glut.h>
#include<gl/gl.h>
#include<gl/glu.h>
#define RED 1
#define GREEN 2
#define BLUE 3
#define GLUT_RGBA 1
// i have made changed in RGB and RGBA by changing it's value 0 or 1 but no //output
#define GLUT_DOUBLE         2
#define GLUT_DEPTH          16
// also changed these two values but still no 
float angle =0.0;
float red = 1.0, blue =1.0, green=1.0;

void renderScene(void) {
    glClear(GL_COLOR_BUFFER_BIT/GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    glRotatef(angle,0.0,1.0,0.0);
    glColor3f(red,green,blue);
    glBegin(GL_TRIANGLES);
    glVertex3f(-0.5,-0.5,0.0);
    glVertex3f(0.5,0.0,0.0);
    glVertex3f(0.0,0.5,0);
    glEnd();
    angle++;
    glutSwapBuffers();
}

void ProcessMenuEvents(int options){
switch (options){
case RED:
    red =1.0; green=0.0 ; blue=0.0;
    break;
case GREEN:
        red =0.0; green=1.0 ; blue=0.0;
    break;
case BLUE:
        red =0.0; green=0.0 ; blue=1.0;
    break;

}

}
int main(int argc, char **argv){
glutInit(&argc,argv);
//glutInitDisplayMode(GLUT_DEPTH/GLUT_DOUBLE/1.0);
glutInitDisplayMode(GLUT_DEPTH/GLUT_DOUBLE/GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(320,320);
glutCreateWindow("GHJYUGH");
glutDisplayFunc(renderScene);
glutIdleFunc(renderScene);
glutCreateMenu(ProcessMenuEvents);
glutAddMenuEntry("red",RED);
glutAddMenuEntry("green",GREEN);
glutAddMenuEntry("blue",BLUE);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutMainLoop();

}

1 个答案:

答案 0 :(得分:1)

错误是由于这个问题,因为顶角,程序给出了一些顶点错误

   void renderScene(void) {
  glClear(GL_COLOR_BUFFER_BIT/GL_DEPTH_BUFFER_BIT);
  glLoadIdentity();
   glRotatef(angle,0.0,1.0,0.0);
   glColor3f(red,green,blue);
  glBegin(GL_TRIANGLES);
  glVertex3f(-0.5,-0.5,0.0);
  glVertex3f(0.5,0.0,0.0);
  glVertex3f(0.0,0.5,0);
   glEnd();
  angle++;
  glutSwapBuffers();
  }