【问题标题】:Trying to load 2d texture with glTexImage2D, but just getting blank尝试使用 glTexImage2D 加载 2d 纹理,但只是变得空白
【发布时间】:2015-06-10 04:18:02
【问题描述】:

我正在尝试创建一个 2d 纹理以显示在我的 Qt/OpenGL 应用程序上。然后我创建了一个填充屏幕的四边形来放置它,但我得到的只是一个没有任何错误的白屏。有人看到我的代码有什么问题吗?

#include "AppGLWidget.h"

#include <iostream>

AppGLWidget::AppGLWidget(QWidget *parent) : QGLWidget(parent),
  mnRange(100),
  mcDisplayedImage(512,512,QImage::Format_ARGB32)
{
  mcDisplayedImage.fill(qRgb(100,0,0));

  mcDisplayedImage = QGLWidget::convertToGLFormat(mcDisplayedImage);

  int value;
  glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
}

AppGLWidget::~AppGLWidget(void)
{
}

void AppGLWidget::initializeGL() {
  glClearColor(0, 1, 0, 1);
  glPushMatrix();
  glOrtho(0, size().width(), size().height(), 0, -1, 1);
  glViewport(0, 0, (GLint)size().width(), (GLint)size().height());
  glDisable(GL_DEPTH_TEST);
  glShadeModel(GL_FLAT);

  glEnable(GL_TEXTURE_2D);
  glGenTextures(1,&mcTextureObj);
  glBindTexture(GL_TEXTURE_2D,mcTextureObj);    
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);       
  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, mcDisplayedImage.width(), mcDisplayedImage.height(), 0, 
      GL_RGBA, GL_UNSIGNED_BYTE, NULL);

  glDisable(GL_TEXTURE_2D);

  // check OpenGL error
  GLenum err;
  while ((err = glGetError()) != GL_NO_ERROR) {
    std::cerr << "OpenGL error: " << err << std::endl;
  }
}

void AppGLWidget::resizeGL(int w, int h)
{
  glPopMatrix();
  glPushMatrix();
  glOrtho(0, w, h, 0, -1, 1);
  glViewport(0, 0, w, h);
}

void AppGLWidget::paintGL() {

  glBindTexture(GL_TEXTURE_2D, mcTextureObj);   

  glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, mcDisplayedImage.width(), mcDisplayedImage.height(),  
    GL_RGBA, GL_UNSIGNED_BYTE, mcDisplayedImage.bits());

  glBegin(GL_QUADS);

  glTexCoord2i(0,0); glVertex2i(0,lnHeight);  
  glTexCoord2i(0,1); glVertex2i(0,0);
  glTexCoord2i(1,1); glVertex2i(lnWidth,0);
  glTexCoord2i(1,0); glVertex2i(lnWidth,lnHeight);

  glEnd();

  // check OpenGL error
  GLenum err;
  while ((err = glGetError()) != GL_NO_ERROR) {
    std::cerr << "OpenGL error: " << err << std::endl;
  }
}

【问题讨论】:

    标签: c++ qt opengl


    【解决方案1】:

    GL_TEXTURE_2D 需要为 glEnable()'d 才能进行渲染。

    现在你在initializeGL()glDisable() 并保持这种状态。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-21
      • 2021-02-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-21
      相关资源
      最近更新 更多