OpenGL:在图像表面上绘制的污迹

时间:2011-11-11 19:19:25

标签: c opengl rubiks-cube

我正在尝试将接口编写为rubik的多维数据集。

然而,当我画它时,立方体的脸上有污迹:

Cube

以下是评论良好的代码。有人可以帮助我,也许可以运行代码并告诉我哪里可能出错了?

#include <GL/glut.h>
#include <stdlib.h>

#include <stdio.h>

void init() {

  glClearColor(1.0f, 0.0f, 1.0f, 1.0f);

  glEnable(GL_DEPTH_TEST);

}

static float x_degs = 0.0f;
static float y_degs = 0.0f;

void keyboard(unsigned char key, int x, int y) {

  switch (key) {

    case 'q':

      exit(EXIT_SUCCESS);

    case 'h':

      y_degs -= 1.0f;

      glutPostRedisplay();

      glutSwapBuffers();

      break;

    case 'j':

      x_degs -= 1.0f;

      glutPostRedisplay();

      glutSwapBuffers();

      break;

    case 'k':

      x_degs += 1.0f;

      glutPostRedisplay();

      glutSwapBuffers();

      break;

    case 'l':

      y_degs += 1.0f;

      glutPostRedisplay();

      glutSwapBuffers();

      break;

  }

}

// half the length of one card

static const float card_half_size = 1.0f;

// half the space between cards

static const float space_half_size = 0.1f;

// number of cards per face

static const int NUM_CARDS_PER_FACE = 4;
/*
// start position of center of top left card 

const float start = - 3 * (card_half_size + space_half_size);

// increment between center of cards

const float incr = 2 * (card_half_size + space_half_size);

// half the size of a cube face

const float cube_half_size = 4 * (card_half_size + space_half_size);
*/
// draw a card centered at the origin

void draw_card() {

  glBegin(GL_QUADS);
    glVertex3f(- card_half_size, - card_half_size, 0.0f);
    glVertex3f(- card_half_size, card_half_size, 0.0f);
    glVertex3f(card_half_size, card_half_size, 0.0f);
    glVertex3f(card_half_size, - card_half_size, 0.0f);
  glEnd();

}

// draw a cube face made up of cards

void draw_card_face() {

  const float cube_half_size = 4 * (card_half_size + space_half_size);
  const float start = - 3 * (card_half_size + space_half_size);
  const float incr = 2 * (card_half_size + space_half_size);

  glColor3f(0.0f, 1.0f, 1.0f);

  glBegin(GL_QUADS);
    glVertex3f(- cube_half_size, - cube_half_size, -0.001f);
    glVertex3f(- cube_half_size, cube_half_size, -0.001f);
    glVertex3f(cube_half_size, cube_half_size, -0.001f);
    glVertex3f(cube_half_size, - cube_half_size, -0.001f);
  glEnd();

  glColor3f(1.0f, 1.0f, 1.0f);

  for (int i = 0; i < NUM_CARDS_PER_FACE; i++)

    for (int j = 0; j < NUM_CARDS_PER_FACE; j++) {

      glPushMatrix();
        glTranslatef(start + i * incr, start + j * incr, 0.0f);
        draw_card();
      glPopMatrix();

    }

}

// draw a cube made up of cards

void draw_card_cube() {

  const float cube_half_size = 4 * (card_half_size + space_half_size);

  // front face

  glPushMatrix();
    glTranslatef(0.0f, 0.0f, cube_half_size);
    draw_card_face();
  glPopMatrix();

  // back face

  glPushMatrix();
    glTranslatef(0.0f, 0.0f, - cube_half_size);
    glRotatef(180.0f, 0.0f, 1.0f, 0.0f);
    draw_card_face();
  glPopMatrix();

  // right face

  glPushMatrix();
    glTranslatef(cube_half_size, 0.0f, 0.0f);
    glRotatef(90.0f, 0.0f, 1.0f, 0.0f);
    draw_card_face();
  glPopMatrix();

  // left face

  glPushMatrix();
    glTranslatef(- cube_half_size, 0.0f, 0.0f);
    glRotatef(- 90.0f, 0.0f, 1.0f, 0.0f);
    draw_card_face();
  glPopMatrix();

  // top face

  glPushMatrix();
    glTranslatef(0.0f, cube_half_size, 0.0f);
    glRotatef(90.0f, 1.0f, 0.0f, 0.0f);
    draw_card_face();
  glPopMatrix();

  // bottom face

  glPushMatrix();
    glTranslatef(0.0f, - cube_half_size, 0.0f);
    glRotatef(- 90.0f, 1.0f, 0.0f, 0.0f);
    draw_card_face();
  glPopMatrix();


}

void display() {

  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glLoadIdentity();

  glRotatef(x_degs, 1.0f, 0.0f, 0.0f);
  glRotatef(y_degs, 0.0f, 1.0f, 0.0f);

  gluLookAt(-0.6f, 0.6f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
  draw_card_cube();
  glutSwapBuffers();

}

void reshape(int w, int h) {

  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(-15.0f, 15.0f, -15.0f, 15.0f, -15.0f, 15.0f);
  glViewport(0, 0, w, h);
  glMatrixMode(GL_MODELVIEW);

}

int main(int argc, char **argv) {

  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  glutCreateWindow(argv[0]);
  init();
  glutKeyboardFunc(keyboard);
  glutDisplayFunc(display);
  glutReshapeFunc(reshape);
  glutMainLoop();
  return 0;

}

好的,我已经修改了代码,以便我将0.01f单位后面的青色矩形绘制而不是0.001f单位,这似乎已经修复了z-fighting。但是,我本来希望使用glPolygonOffset(因子,单位)来解决这个问题,但我无法做到,因为以下 原因:

  1. 我不知道如何设置facor和单位(我已经尝试过1.0)。
  2. 我尝试了不同的价值观,但没有结果。
  3. 这是没有出血/拼接/ z战斗的代码:

    #include <GL/glut.h>
    #include <stdlib.h>
    
    #include <stdio.h>
    
    void init() {
    
      glClearColor(1.0f, 0.0f, 1.0f, 1.0f);
    
      glEnable(GL_DEPTH_TEST);
    
    }
    
    static float x_degs = 0.0f;
    static float y_degs = 0.0f;
    
    void keyboard(unsigned char key, int x, int y) {
    
      switch (key) {
    
        case 'q':
    
          exit(EXIT_SUCCESS);
    
        case 'h':
    
          y_degs -= 1.0f;
    
          glutPostRedisplay();
    
          glutSwapBuffers();
    
          break;
    
        case 'j':
    
          x_degs -= 1.0f;
    
          glutPostRedisplay();
    
          glutSwapBuffers();
    
          break;
    
        case 'k':
    
          x_degs += 1.0f;
    
          glutPostRedisplay();
    
          glutSwapBuffers();
    
          break;
    
        case 'l':
    
          y_degs += 1.0f;
    
          glutPostRedisplay();
    
          glutSwapBuffers();
    
          break;
    
      }
    
    }
    
    // half the length of one card
    
    static const float card_half_size = 1.0f;
    
    // half the space between cards
    
    static const float space_half_size = 0.1f;
    
    // number of cards per face
    
    static const int NUM_CARDS_PER_FACE = 4;
    /*
    // start position of center of top left card 
    
    const float start = - 3 * (card_half_size + space_half_size);
    
    // increment between center of cards
    
    const float incr = 2 * (card_half_size + space_half_size);
    
    // half the size of a cube face
    
    const float cube_half_size = 4 * (card_half_size + space_half_size);
    */
    // draw a card centered at the origin
    
    void draw_card() {
    
      glBegin(GL_QUADS);
        glVertex3f(- card_half_size, - card_half_size, 0.0f);
        glVertex3f(- card_half_size, card_half_size, 0.0f);
        glVertex3f(card_half_size, card_half_size, 0.0f);
        glVertex3f(card_half_size, - card_half_size, 0.0f);
      glEnd();
    
    }
    
    // draw a cube face made up of cards
    
    void draw_card_face() {
    
      const float cube_half_size = 4 * (card_half_size + space_half_size);
      const float start = - 3 * (card_half_size + space_half_size);
      const float incr = 2 * (card_half_size + space_half_size);
    
      glColor3f(0.0f, 1.0f, 1.0f);
    
      glBegin(GL_QUADS);
        glVertex3f(- cube_half_size, - cube_half_size, -0.001f);
        glVertex3f(- cube_half_size, cube_half_size, -0.001f);
        glVertex3f(cube_half_size, cube_half_size, -0.001f);
        glVertex3f(cube_half_size, - cube_half_size, -0.001f);
      glEnd();
    
      glColor3f(1.0f, 1.0f, 1.0f);
    
      for (int i = 0; i < NUM_CARDS_PER_FACE; i++)
    
        for (int j = 0; j < NUM_CARDS_PER_FACE; j++) {
    
          glPushMatrix();
            glTranslatef(start + i * incr, start + j * incr, 0.0f);
            draw_card();
          glPopMatrix();
    
        }
    
    }
    
    // draw a cube made up of cards
    
    void draw_card_cube() {
    
      const float cube_half_size = 4 * (card_half_size + space_half_size);
    
      // front face
    
      glPushMatrix();
        glTranslatef(0.0f, 0.0f, cube_half_size);
        draw_card_face();
      glPopMatrix();
    
      // back face
    
      glPushMatrix();
        glTranslatef(0.0f, 0.0f, - cube_half_size);
        glRotatef(180.0f, 0.0f, 1.0f, 0.0f);
        draw_card_face();
      glPopMatrix();
    
      // right face
    
      glPushMatrix();
        glTranslatef(cube_half_size, 0.0f, 0.0f);
        glRotatef(90.0f, 0.0f, 1.0f, 0.0f);
        draw_card_face();
      glPopMatrix();
    
      // left face
    
      glPushMatrix();
        glTranslatef(- cube_half_size, 0.0f, 0.0f);
        glRotatef(- 90.0f, 0.0f, 1.0f, 0.0f);
        draw_card_face();
      glPopMatrix();
    
      // top face
    
      glPushMatrix();
        glTranslatef(0.0f, cube_half_size, 0.0f);
        glRotatef(90.0f, 1.0f, 0.0f, 0.0f);
        draw_card_face();
      glPopMatrix();
    
      // bottom face
    
      glPushMatrix();
        glTranslatef(0.0f, - cube_half_size, 0.0f);
        glRotatef(- 90.0f, 1.0f, 0.0f, 0.0f);
        draw_card_face();
      glPopMatrix();
    
    
    }
    
    void display() {
    
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      glLoadIdentity();
    
      glRotatef(x_degs, 1.0f, 0.0f, 0.0f);
      glRotatef(y_degs, 0.0f, 1.0f, 0.0f);
    
      gluLookAt(-0.6f, 0.6f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
      draw_card_cube();
      glutSwapBuffers();
    
    }
    
    void reshape(int w, int h) {
    
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      glOrtho(-15.0f, 15.0f, -15.0f, 15.0f, -15.0f, 15.0f);
      glViewport(0, 0, w, h);
      glMatrixMode(GL_MODELVIEW);
    
    }
    
    int main(int argc, char **argv) {
    
      glutInit(&argc, argv);
      glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
      glutCreateWindow(argv[0]);
      init();
      glutKeyboardFunc(keyboard);
      glutDisplayFunc(display);
      glutReshapeFunc(reshape);
      glutMainLoop();
      return 0;
    
    }
    

3 个答案:

答案 0 :(得分:5)

我认为你正在把你的跳棋作为主立方体脸部的共面四边形;如果是这种情况,您遇到的问题称为“z格斗”。

你可以看看点 12.040深度缓冲似乎有效,但是多边形似乎会渗透到它们前面的多边形。发生了什么事?在这里: http://www.opengl.org/resources/faq/technical/depthbuffer.htm

基本上,您的深度缓冲区没有足够的精度来解析每个像素显示哪个四边形,从而导致问题。

您可以手动为检查器四边形添加偏移量,使其远离多维数据集;或通过glPolygonOffset使用深度偏差来解决问题。

答案 1 :(得分:2)

我认为你正在尝试进行共面渲染。查看glPolygonOffset()而不是像你一样使用小的Z偏移量。

答案 2 :(得分:1)

这是我从您的代码中获得的,使用gcc -std=c99 -lGL -lGLU -lglut a.c编译:没有污迹。你能发贴你的吗? (Ubuntu 11.10,Intel GPU)

local screenshot