当前位置:  开发笔记 > 编程语言 > 正文

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

如何解决《OpenGL:在图像表面上绘制的污迹》经验,为你挑选了1个好方法。

我正在尝试编写一个rubik立方体的接口.

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

立方体

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

#include 
#include 

#include 

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(因子,单位)来解决这个问题,但我无法做到这一点,原因如下:

    我不知道如何设置facor和单位(我已经尝试过1.0).

    我尝试了不同的价值观,没有结果.

这是没有出血/拼接/ z战斗的代码:

#include 
#include 

#include 

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;

}

rotoglup.. 5

我想你是把你的西洋跳棋作为主立方体面孔的共面四边形; 如果是这种情况,您遇到的问题称为"z格斗".

您可以看一下12.040深度缓冲似乎有效,但多边形似乎会渗透到它们前面的多边形.这是怎么回事?这里:http: //www.opengl.org/resources/faq/technical/depthbuffer.htm

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

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



1> rotoglup..:

我想你是把你的西洋跳棋作为主立方体面孔的共面四边形; 如果是这种情况,您遇到的问题称为"z格斗".

您可以看一下12.040深度缓冲似乎有效,但多边形似乎会渗透到它们前面的多边形.这是怎么回事?这里:http: //www.opengl.org/resources/faq/technical/depthbuffer.htm

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

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

推荐阅读
mobiledu2402852357
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有