一段OpenGL代码,预期效果是通过导入stb_image系列的头文件,实现导入图片并改变大小,然后通过OpenGL一个一个像素点轮流输出的方式显示图片。BUG出现在display函数,程序运行后,虽然成功显示了图片,但是实际效果在是一格一格的,我想达到的预期是没有瑕疵的显示图片,想了很久也没想出为什么。
我的要求为:如何修改此BUG并输出和原图十分相似的图片。
原图在这:
//youtube learning unit3
#define GLUT_DISABLE_ATEXIT_HACK
#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define STB_IMAGE_RESIZE_IMPLEMENTATION
#include "old_image_resize.h"
#include <iostream>
#define convert(x) x/256.0
void display();
void reshape(int,int);
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(200,100);
glutInitWindowSize(512,512);
glutCreateWindow("Texture_practice");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
void display(){
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
int x=512,y=512,n=3;
unsigned char* idata = stbi_load("container.jpg", &x, &y, &n, 0);
int sub=2;//size of image
int nx = x / sub;
int ny = y / sub;
unsigned char* odata = (unsigned char*)malloc(nx * ny * n);
// 改变图片尺寸
stbir_resize_uint8(idata, x, y, 0, odata, nx, ny, 0, n);
glBegin(GL_POINTS);
for (int i=0;i<=nx;i++){
for (int j=0;j<=ny;j++){
GLfloat r=odata[(i * nx + j) * n];
GLfloat g=odata[(i * nx + j) * n + 1];
GLfloat b=odata[(i * nx + j) * n + 2];
glColor3f(convert(r),convert(g),convert(b));
glVertex2f(j-ny/2.0,i-nx/2.0);
}
}
glEnd();
stbi_image_free(idata);
stbi_image_free(odata);
glFlush();
}
void reshape(int w,int h){
glViewport(0,0,(GLsizei)w,(GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-216,216,-216,216);
glMatrixMode(GL_MODELVIEW);
}