OpenGL渲染全屏图像

时间:2016-05-06 20:13:06

标签: opengl visual-studio-2015 soil

我正在尝试创建一个应用程序,它所做的只是全屏显示图像,然后反复快速(144hz)闪烁一系列图像。我刚开始看OpenGL,已经做了一些教程,无法弄清楚我在这里做错了什么。我正在坚持的部分实际上是将图像渲染到显示器,因为它只显示为白色方块。我已经解决了其他堆栈溢出问题,但没有一个建议对我有效。

我在Visual Studio 2015中使用win32应用程序并安装了NupenGL程序包。出于测试目的,我使用的是256x256位图图像,并通过我构建并静态链接的SOIL库加载它。

我原本以为我没有正确构建/链接SOIL库,所以有些时髦的东西试图加载图像。我创建了一个自定义的BMP加载器,它没有工作,我也尝试了其他人的BMP加载器堆栈溢出无济于事。我现在相信它不是纹理的加载,而是在实际尝试渲染时我搞砸了一些东西。另外在我下面的代码中输出如果纹理无效但它总是回来好。

输出(FULLSCREEN): enter image description here

输出(WINDOWED): enter image description here

我的代码:

#include <gl/freeglut.h>
#include <stdio.h>
#include <iostream>
#include "SOIL.h"

void init();
void display(void);
void keyPressed(unsigned char key, int x, int y);
void resize(int heightY, int widthX);


//  define the window position on screen
int window_x;
int window_y;

//  variables representing the window size
int window_width = 480;
int window_height = 480;

//  variable representing the window title
char *window_title = "Resolution Enhancement via Display Vibrations";

bool fullscreen = false;

//-------------------------------------------------------------------------
//  Program Main method.
//-------------------------------------------------------------------------
void main(int argc, char **argv)
{
    //  Connect to the windowing system + create a window
    //  with the specified dimensions and position
    //  + set the display mode + specify the window title.
    glutInit(&argc, argv);
    glutInitWindowSize(window_width, window_height);
    glutInitWindowPosition(window_x, window_y);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
    glutCreateWindow(window_title);

    glutFullScreen();

    // Setup keyPressed
    glutKeyboardFunc(keyPressed);

    // Handler for when the screen resizes
    glutReshapeFunc(resize);

    //  Set OpenGL program initial state.
    init();

    // Set the callback functions
    glutDisplayFunc(display);

    //  Start GLUT event processing loop
    glutMainLoop();
}

//-------------------------------------------------------------------------
//  Set OpenGL program initial state.
//-------------------------------------------------------------------------
void init()
{
    //  Set the frame buffer clear color to black. 
    glClearColor(0.0, 0.0, 0.0, 0.0);
}

//-------------------------------------------------------------------------
//  This function is passed to glutDisplayFunc in order to display 
//  OpenGL contents on the window.
//-------------------------------------------------------------------------
void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    glTranslatef(0.0f, 0.0f, -5.0f);
    GLuint texture = SOIL_load_OGL_texture // load an image file directly as a new OpenGL texture
    (
        "C:/Users/joeja/Desktop/Grass.bmp",
        SOIL_LOAD_AUTO,
        SOIL_CREATE_NEW_ID,
        SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
    );

    if (texture == 0) {
        std::cout << "Texture not found!\n" << std::endl;
    }
    else
    {
        std::cout << "Texture is good\n" << std::endl;
    }

    glBindTexture(GL_TEXTURE_2D, texture);

    glBegin(GL_QUADS);  // front face
    glTexCoord2f(0.0f, 0.0f); glVertex3f(0.5f, -0.5f, 0.5f);
    glTexCoord2f(1.0f, 0.0f); glVertex3f(0.5f, 0.5f, 0.5f);
    glTexCoord2f(1.0f, 1.0f); glVertex3f(-0.5f, 0.5f, 0.5f);
    glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.5f, -0.5f, 0.5f);
    glEnd();

    glutSwapBuffers();
}

void resize(int heightY,int widthX) {
    const float ar = (float)widthX / (float)heightY;
    glViewport(0, 20, widthX, heightY);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glFrustum(-ar + 1, ar - 1, -1.0, 1.0, 2.0, 90.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void keyPressed(unsigned char key, int x, int y) {
    switch (key) {
    case 27:
    case 70:
    case 102: /* Fullscreen mode (Additional) : f/F */
        fullscreen = !fullscreen;
        if (fullscreen)
        {
            glutFullScreen();                /* Go to full screen */
        }
        else
        {
            glutReshapeWindow(800, 600);        /* Restore us */
            glutPositionWindow(0, 0);
        }
        break;
    }
}

1 个答案:

答案 0 :(得分:1)

不要每帧加载文件中的图像。

init你应该:

  • 使用SOIL加载图像,将ID存储为texture

display你应该,

  • glBindTexture(GL_TEXTURE_2D, texture);
  • glEnable(GL_TEXTURE_2D)
  • 画东西
  • glDisable(GL_TEXTURE_2D);
  • glBindTexture(GL_TEXTURE_2D, 0);

您可能会注意到,只需启用纹理2D并将其保留,即可跳过某些启用/禁用。我给出了试图始终工作的伪代码,跳过冗余状态更改是与问题无关的优化。