1

我正在尝试在 OSX Mountain Lion for OpenGL 上进行一些基本演示。有些工作,但基本的不工作。具体来说,我的 X 窗口上出现了垃圾。这是演示中的代码(如果格式混乱,请见谅):

/* W B Langdon at MUN 10 May 2007
 * Program to demonstarte use of OpenGL's glDrawPixels
 */

#ifdef _WIN32
#include <windows.h>
#endif

#include <GL/gl.h>
#include <GL/glext.h>
#include <GL/glut.h>

#include <iostream>
#include <sstream>
#include "math.h"

unsigned int window_width = 512, window_height = 512;
const int size=window_width*window_height;

struct rgbf {float r; float g; float b;};
//WBL 9 May 2007 Based on
//http://www.codeguru.com/cpp/w-d/dislog/commondialogs/article.php/c1861/
//Common.h
void toRGBf(const float h, const float s, const float v,
        rgbf* rgb)
{
  /*
    RGBType rgb;
    if(!h  && !s)
    {
    rgb.r = rgb.g = rgb.b = v;
}
  */
  //rgbf* rgb = (rgbf*) out;
  double min,max,delta,hue;

  max = v;
  delta = max * s;
  min = max - delta;

  hue = h;
  if(h > 300 || h <= 60)
  {
      rgb->r = max;
      if(h > 300)
      {
        rgb->g = min;
        hue = (hue - 360.0)/60.0;
        rgb->b = ((hue * delta - min) * -1);
      }
      else
      {
        rgb->b = min;
        hue = hue / 60.0;
        rgb->g = (hue * delta + min);
      }
  }
  else
  if(h > 60 && h < 180)
  {
    rgb->g = max;
    if(h < 120)
    {
        rgb->b = min;
        hue = (hue/60.0 - 2.0 ) * delta;
        rgb->r = min - hue;
    }
    else
    {
        rgb->r = min;
        hue = (hue/60 - 2.0) * delta;
        rgb->b = (min + hue);
    }
  }
  else
  {
    rgb->b = max;
    if(h < 240)
    {
        rgb->r = min;
        hue = (hue/60.0 - 4.0 ) * delta;
        rgb->g = (min - hue);
    }
    else
    {
        rgb->g = min;
        hue = (hue/60 - 4.0) * delta;
        rgb->r = (min + hue);
    }
}
}


//Convert a wide range of data values into nice colours 
void colour(const float data, float* out) {
//convert data to angle
const float a = atan2(data,1)/(2*atan2(1,1)); // -1 .. +1
const float angle = (1+a)*180; //red=0 at -1,+1

const float saturation = 1;

const float h = (data<-1||data>1)? 1 : fabs(data);

toRGBf(angle,saturation,h,(rgbf*)out);
}

void display()
{
//Create some nice colours (3 floats per pixel) from data -10..+10
float* pixels = new float[size*3];
for(int i=0;i<size;i++) {
    colour(10.0-((i*20.0)/size),&pixels[i*3]);
} 

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

//http://msdn2.microsoft.com/en-us/library/ms537062.aspx
//glDrawPixels writes a block of pixels to the framebuffer.

glDrawPixels(window_width,window_height,GL_RGB,GL_FLOAT,pixels);
glFlush();

glutSwapBuffers();
}

int main(int argc, char** argv) {
glutInit(&argc, argv);

glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(window_width, window_height);
glutCreateWindow("OpenGL glDrawPixels demo");

glutDisplayFunc(display);
//glutReshapeFunc(reshape);
//glutMouseFunc(mouse_button);
//glutMotionFunc(mouse_motion);
//glutKeyboardFunc(keyboard);
//glutIdleFunc(idle);

glEnable(GL_DEPTH_TEST);
glClearColor(0.0, 0.0, 0.0, 1.0);
//glPointSize(2);

glutMainLoop();
}

这就是我得到的:

错误的

4

1 回答 1

1

请考虑glDrawPixels不再相关。自版本 OpenGL-2 以来,它已被 OpenGL 弃用(阅读:不要在新代码中使用)。自 OpenGL-3 核心配置文件以来,它已被贬值(不同的词,请注意“i” - 阅读:已被删除)。

改用带纹理的四边形。

于 2012-08-19T21:53:35.300 回答