本文介绍了当我使用opengl在opengl纹理上绘制一个正方形时,它会消失。如何使纹理可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Button上单击opengl纹理上绘制一个正方形。我需要在广场后面的opengl纹理。广场不应该被填满,只需要画出广场的ouline。



我尝试过的事情:



我的按钮点击代码显示方块是

void OverlayDisplayDlg :: DrawImage()

{

glClear(GL_COLOR_BUFFER_BIT); //清除颜色缓冲区

glMatrixMode(GL_MODELVIEW); //操作模型 - 视图矩阵

glLoadIdentity(); //重置模型视图矩阵

glPushMatrix();

glBegin(GL_QUADS); //每组4个顶点形成一个四元组

//glColor3f(1.0f,0.0f,0.0f); //红色

glVertex2f(-0.3f,-0.3f); //以逆时针(CCW)顺序定义顶点

glVertex2f(0.3f,-0.3f); //使正常(正面)面向你

glVertex2f(0.3f,0.3f);

glVertex2f(-0.3f,0.3f);

glEnd();

glPopMatrix();

SwapBuffers(m_hDeviceContextDC);



}



void OverlayDisplayDlg :: OnDrawBnClickedButton1()

{

DrawImage();

}

并显示纹理



void OverlayDisplayDlg :: DisplayTexture()

{

GLuint glTexture;

glTexture = ApplyTexture();

glEnable(GL_TEXTURE_2D);

glBindTexture(GL_TEXTURE_2D ,glTexture);

glBegin(GL_QUADS);

glTexCoord2d(0.0,0.0);

glVertex2f(-0.8f,-0.8f);

glTexCoord2d(1.0,0.0);

glVertex2f(0.8f,-0.8f);

glTexCoord2d(1.0,1.0);

glVertex2f(0.8f,0.8f);

glTexCoord2d(0.0,1.0);

glVertex2f(-0.8f,0.8f);

glEnd();

SwapBuffers(m_hDeviceContextDC);

glDisable(GL_TEXTURE_2D);

}



GLuint OverlayDisplayDlg :: ApplyTexture()

{

ReadData(_T(D:\\ Bin Files \ \InputData_1280_690.bin));

GLuint glTexture;

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glEnable(GL_TEXTURE_2D);

glGenTextures(1,& glTexture);

glBindTexture(GL_TEXTURE_2D,glTexture);

glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE); //选择调制以将纹理与颜色混合以进行着色

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST); //当纹理区域很小时,双线性过滤最近的mipmap

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST); //当纹理区域很大时,双线性过滤原始的

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT); //纹理在边缘处包裹(重复)

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);

glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,WIDTH,HEIGHT,0 ,GL_LUMINANCE,GL_UNSIGNED_BYTE,m_pbyImageData); //将纹理应用图像显示到屏幕

glDisable(GL_TEXTURE_2D);

glDisable(GL_COLOR_MATERIAL);

返回glTexture;

}







void OverlayDisplayDlg :: Initialize(void)

{

PIXELFORMATDESCRIPTOR pfPixelFormator;

memset(& pfPixelFormator,0,sizeof(PIXELFORMATDESCRIPTOR));

pfPixelFormator.nSize = sizeof (PIXELFORMATDESCRIPTOR);

pfPixelFormator.cColorBits = 24;

pfPixelFormator.cDepthBits = 8;

pfPixelFormator.dwFlags = PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW | PFD_DOUBLEBUFFER;

pfPixelFormator.iPixelType = PFD_TYPE_RGBA;

pfPixelFormator.nVersion = 1;



// CClientDC ClientDC (this);

// m_hDeviceContextDC = ClientDC.GetSafeHdc();

CWnd * pPictureWnd = GetDlgItem(IDC_STATIC_PICTURE);

m_hDeviceContextDC = pPictureWnd - > GetDC() - > GetSafeHdc();

int nPixelFormat = ChoosePixelFormat(m_hDeviceContextDC,& pfPixelFormator);

SetPixelFormat(m_hDeviceContextDC,nPixelFormat,& pfPixelFormator );

m_hRenderContextRC = wglCreateContext(m_hDeviceContextDC);

wglMakeCurrent(m_hDeviceContextDC,m_hRenderContextRC);

glClearColor(0.1f,0.7f,0.9f ,1.0f);

glClearDepth(1.0f);

DisplayTexture();

}



请帮我解决这个问题。我在我的程序的Onpaint中调用初始化函数。

解决方案



I need to draw a square on Button click over an opengl texture . I need that opengl texture behind the square. The square should not be filled, just the ouline of square should be drawn.

What I have tried:

My button click code to display square is
void OverlayDisplayDlg :: DrawImage()
{
glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer
glMatrixMode(GL_MODELVIEW); // To operate on Model-View matrix
glLoadIdentity(); // Reset the model-view matrix
glPushMatrix();
glBegin(GL_QUADS); // Each set of 4 vertices form a quad
//glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex2f(-0.3f, -0.3f); // Define vertices in counter-clockwise (CCW) order
glVertex2f( 0.3f, -0.3f); // so that the normal (front-face) is facing you
glVertex2f( 0.3f, 0.3f);
glVertex2f(-0.3f, 0.3f);
glEnd();
glPopMatrix();
SwapBuffers( m_hDeviceContextDC );

}

void OverlayDisplayDlg::OnDrawBnClickedButton1()
{
DrawImage();
}
and to display texture is

void OverlayDisplayDlg::DisplayTexture()
{
GLuint glTexture;
glTexture = ApplyTexture();
glEnable( GL_TEXTURE_2D );
glBindTexture( GL_TEXTURE_2D, glTexture );
glBegin( GL_QUADS );
glTexCoord2d(0.0,0.0);
glVertex2f( -0.8f, -0.8f );
glTexCoord2d( 1.0,0.0 );
glVertex2f( 0.8f, -0.8f );
glTexCoord2d( 1.0,1.0 );
glVertex2f( 0.8f, 0.8f );
glTexCoord2d( 0.0,1.0 );
glVertex2f( -0.8f, 0.8f );
glEnd();
SwapBuffers( m_hDeviceContextDC );
glDisable( GL_TEXTURE_2D );
}

GLuint OverlayDisplayDlg :: ApplyTexture()
{
ReadData( _T( "D:\\Bin Files\\InputData_1280_690.bin" ));
GLuint glTexture ;
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glEnable( GL_TEXTURE_2D );
glGenTextures( 1, &glTexture );
glBindTexture( GL_TEXTURE_2D, glTexture );
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ); // Select modulate to mix texture with color for shading
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST ); // When texture area is small, bilinear filter the closest mipmap
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST ); // When texture area is large, bilinear filter the original
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT ); // The texture wraps over at the edges (repeat)
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, WIDTH, HEIGHT, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, m_pbyImageData ); // To dislay texture applied image to screen
glDisable( GL_TEXTURE_2D );
glDisable( GL_COLOR_MATERIAL );
return glTexture;
}



void OverlayDisplayDlg::Initialize(void)
{
PIXELFORMATDESCRIPTOR pfPixelFormator ;
memset( &pfPixelFormator, 0, sizeof( PIXELFORMATDESCRIPTOR ));
pfPixelFormator.nSize = sizeof( PIXELFORMATDESCRIPTOR );
pfPixelFormator.cColorBits = 24;
pfPixelFormator.cDepthBits = 8;
pfPixelFormator.dwFlags = PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW | PFD_DOUBLEBUFFER ;
pfPixelFormator.iPixelType = PFD_TYPE_RGBA;
pfPixelFormator.nVersion = 1;

//CClientDC ClientDC(this);
//m_hDeviceContextDC = ClientDC.GetSafeHdc();
CWnd* pPictureWnd = GetDlgItem( IDC_STATIC_PICTURE );
m_hDeviceContextDC = pPictureWnd->GetDC()->GetSafeHdc();
int nPixelFormat = ChoosePixelFormat( m_hDeviceContextDC, &pfPixelFormator );
SetPixelFormat( m_hDeviceContextDC,nPixelFormat, &pfPixelFormator );
m_hRenderContextRC = wglCreateContext( m_hDeviceContextDC );
wglMakeCurrent( m_hDeviceContextDC, m_hRenderContextRC );
glClearColor( 0.1f, 0.7f, 0.9f, 1.0f );
glClearDepth( 1.0f );
DisplayTexture();
}

Please help me to solve this. Im calling the initialize function in Onpaint of my program.

解决方案



这篇关于当我使用opengl在opengl纹理上绘制一个正方形时,它会消失。如何使纹理可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 14:19