1

不知道怎么解决这个问题,想用这个功能做一个软渲染引擎来研究3D管线。

现在的情况

这是代码:

#define GLFW_INCLUDE_GLU
#include <GLFW/glfw3.h>

int main(int argc, const char * argv[]) {
    if(!glfwInit()){    //init error
        return -1;
    }

    int width = 200;    //screen width
    int height = 200;   //screen height

    GLFWwindow* window = glfwCreateWindow(width, height, "Hello OpenGL", NULL, NULL);  //create window

    if(!window){     //create window fail
        glfwTerminate();
        return -1;
    }

    u_char* pixels = new unsigned char[width * height * 4]; //buffer
    uint index;  //temp

    //set pixel
    for (int count_a = 0; count_a < width; count_a++) {
        for (int count_b = 0; count_b < height; count_b++) {
            index = count_b * width + count_a;

            pixels[index * 4 + 0] = 255;    //red pixel
            pixels[index * 4 + 1] = 0;      //green
            pixels[index * 4 + 2] = 0;      //blue
            pixels[index * 4 + 3] = 255;    //alpha
        }
    }

    glfwMakeContextCurrent(window);
    while (!glfwWindowShouldClose(window)) {  //close window
        glClearColor(1.0, 1.0, 1.0, 1.0);  //clear screen by white pixel
        glClear(GL_COLOR_BUFFER_BIT);
        glRasterPos2f(-1, -1);   //set origin to screen bottom left
        //glPixelZoom(2, 2); //if i use this function. renderer is right.
        glDrawPixels(width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels); //draw pixel

        glfwSwapBuffers(window); //swap buffer
    }
    glfwTerminate();
    return 0;
}
4

1 回答 1

0

我在使用 GLUT 时遇到了这个问题,所以我在使用 GLFW 重新实现我的代码之前和同时遇到了这个问题。这个问题是由 Retina 显示器引起的(正如许多其他来源已经发现的那样),以下最终为我修复了它:

int width = 1360*2;    //screen width
int height = 768*2;    //screen height

GLFWwindow* window = glfwCreateWindow(width/2, height/2, "Hello OpenGL", NULL, NULL);  //create window

// ...
于 2020-01-16T03:53:36.800 回答