openGL画图最大化后颜色就变了,不是我想要的颜色了

时间:2022-07-06 10:52:13
我在OnDraw(CDC* pDC)函数里画了调用了两个函数
         DrawLines();//坐标
DrawColorbar();//画颜色条
当注释掉第一个函数DrawLines();//坐标   时每二个函数颜色条是正常的,当加上第一个函数后颜色就不正常了,这是什么问题啊?
如果两个函数调换一下位置,画的颜色条是正确的,如果最大化窗口一下,颜色就变了,这是什么原因呢?要怎么样解决啊?

8 个解决方案

#1


DrawLines();//坐标
DrawColorbar();//画颜色条

这两个函数内容是什么

#2


void CDrawSurfaceView::DrawLines()
{
//Draw Lines
glPushMatrix();
// glLoadIdentity();
glBegin(GL_LINES);

//X
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(0,0.0f,0.0f);
glVertex3f(15.0f,0.0f,0.0f);

//Arrow
glVertex3f(14.60f,0.3f,0.0f);
glVertex3f(15.0f,0.0f,0.0f);
glVertex3f(14.60f,-0.3f,0.0f);
glVertex3f(15.0f,0.0f,0.0f);

//Y
glColor3f(0.0f,1.0f,0.0f);
glVertex3f(0.0f,0,0.0f);
glVertex3f(0.0f,15.0f,0.0f);
//Arrow
glVertex3f(-0.3f,14.6f,0.0f);
glVertex3f(0.0f,15.0f,0.0f);
glVertex3f(0.3f,14.6F,0.0f);
glVertex3f(0.0f,15.0f,0.0f);

//Z
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(0.0f,0.0f,0);
glVertex3f(0.0f,0.0f,15.0f);
//Arrow
glVertex3f(0.3f,0.0f,14.60f);
glVertex3f(0.0f,0.0f,15.0f);
glVertex3f(-0.3f,0.0f,14.60f);
glVertex3f(0.0f,0.0f,15.0f);

glEnd();
glPopMatrix();
  glFlush(); 
}

void CDrawSurfaceView::DrawColorbar()
{
// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // 清除屏幕和深度缓存
// glLoadIdentity(); // 重置当前的模型观察矩阵
CreateColorMap();
glPushMatrix(); 
// glLoadIdentity();
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);  
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,4,checkImageWidth,checkImageHeight,
0,GL_RGBA,GL_UNSIGNED_BYTE,colorMap);//纹理的数据来源为otherImage
glEnable(GL_TEXTURE_2D);//生成纹理像素
glBegin(GL_QUADS);

glTexCoord2f(0.0f,0.0f);glVertex3f(18.0f,18.0f,0.0f);//左下
glTexCoord2f(1.0f,0.0f);glVertex3f(18.0f, 20.0f,0.0f);//右下
glTexCoord2f(1.0f,1.0f);glVertex3f( 20.0f, 20.0f,30.0f);//右上
glTexCoord2f(0.0f,1.0f);glVertex3f(20.0f, 18.0f,30.0f);//左上
glEnd();

glPopMatrix();   
    glFlush(); 
}

其中DrawColorbar()函数就是根据你博客里的文章建了一个数组,然后纹理贴图,单独用的时候能画出彩虹条的,可是加了上面的函数DrawLines()颜色就变了,这是什么原因呢?

#3


glcolor函数应该放在glbegin和glend的外面

#4


还是不行啊,我把函数改成下面的了
void CDrawSurfaceView::DrawLines()
{
//Draw Lines
glPushMatrix();
glLoadIdentity();

//X
glColor3f(1.0f,0.0f,0.0f);
glBegin(GL_LINES);

glVertex3f(0,0.0f,0.0f);
glVertex3f(15.0f,0.0f,0.0f);
//Arrow
glVertex3f(14.60f,0.3f,0.0f);
glVertex3f(15.0f,0.0f,0.0f);
glVertex3f(14.60f,-0.3f,0.0f);
glVertex3f(15.0f,0.0f,0.0f);

glEnd();

//Y
glColor3f(0.0f,1.0f,0.0f);
glBegin(GL_LINES);

glVertex3f(0.0f,0,0.0f);
glVertex3f(0.0f,15.0f,0.0f);
//Arrow
glVertex3f(-0.3f,14.6f,0.0f);
glVertex3f(0.0f,15.0f,0.0f);
glVertex3f(0.3f,14.6F,0.0f);
glVertex3f(0.0f,15.0f,0.0f);
glEnd();


//Z
glColor3f(0.0f,0.0f,1.0f);
glBegin(GL_LINES);

glVertex3f(0.0f,0.0f,0);
glVertex3f(0.0f,0.0f,15.0f);
//Arrow
glVertex3f(0.3f,0.0f,14.60f);
glVertex3f(0.0f,0.0f,15.0f);
glVertex3f(-0.3f,0.0f,14.60f);
glVertex3f(0.0f,0.0f,15.0f);

glEnd();
glPopMatrix();
glFlush(); 
}

void CDrawSurfaceView::DrawColorbar()
{

CreateColorMap();
// glPushMatrix(); 

// glLoadIdentity();

// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);// 清除屏幕和深度缓存


glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);  
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,4,checkImageWidth,checkImageHeight,
0,GL_RGBA,GL_UNSIGNED_BYTE,colorMap);//纹理的数据来源为otherImage
glEnable(GL_TEXTURE_2D);//生成纹理像素
glBegin(GL_QUADS);

glTexCoord2f(0.0f,0.0f);glVertex3f(18.0f,18.0f,0.0f);//左下
glTexCoord2f(1.0f,0.0f);glVertex3f(18.0f, 20.0f,0.0f);//右下
glTexCoord2f(1.0f,1.0f);glVertex3f( 20.0f, 20.0f,30.0f);//右上
glTexCoord2f(0.0f,1.0f);glVertex3f(20.0f, 18.0f,30.0f);//左上
glEnd();

// glPopMatrix();   
    glFlush(); 
}

void CDrawSurfaceView::OnDraw(CDC* pDC)
{
CDrawSurfaceDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
//清除颜色缓冲和深度缓冲
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//用单位矩阵替换当前矩阵
glLoadIdentity();
//视角
gluLookAt(1,1,1,0,0,0,0,0,1); 

DrawLines();//画坐标
DrawColorbar();//画颜色条
}
//强制绘图完成
glFinish();
//交换缓冲区数据
SwapBuffers(wglGetCurrentDC());
}

只要调用了函数DrawLines();后面的颜色条就不正常了

#5


刚刚的问题好了,我在DrawLines()函数的最后面加了句 glColor3f(1.0f,1.0f,1.0f);  颜色条就正常了,可是现在又遇到了新的问题,当我最大化窗口后,DrawLines()画的线条颜色就变黑了,
我在DrawColorbar()函数中
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);  
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
//注释掉glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
颜色条画不出来了,但是DrawLines()函数的线条颜色最大化后正常,没有变黑了,这又是什么原因呢?

#6


你使用 glColor的时候是否保存了当前的绘制环境?

可以试试用glPushAttrib( GL_CURRENT_BIT ) 和 glPopAttrib() 来保存和恢复绘制环境

#7


用你的方法果然行的,
可是我的函数DrawColorbar()加了glPushAttrib( GL_CURRENT_BIT ) 和 glPopAttrib() 还是会对后面画的图的颜色有影响,这是什么原因呢?函数原型如下:

void CDrawSurfaceView::DrawColorbar()
{
glPushAttrib( GL_CURRENT_BIT ) ;
CreateColorMap();
glPushMatrix(); 

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);  
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,4,checkImageWidth,checkImageHeight,
0,GL_RGBA,GL_UNSIGNED_BYTE,colorMap);//纹理的数据来源为colorMap
glEnable(GL_TEXTURE_2D);//生成纹理像素
glBegin(GL_QUADS);

glTexCoord2f(0.0f,0.0f);glVertex3f(18.0f,18.0f,0.0f);//左下
glTexCoord2f(1.0f,0.0f);glVertex3f(18.0f, 20.0f,0.0f);//右下
glTexCoord2f(1.0f,1.0f);glVertex3f( 20.0f, 20.0f,30.0f);//右上
glTexCoord2f(0.0f,1.0f);glVertex3f(20.0f, 18.0f,30.0f);//左上
glEnd();
glDeleteTextures(1, (GLuint *)colorMap);//
glPopMatrix(); 
glPopAttrib();
    glFlush(); 
}

#8


哈哈,搞定了,谢谢你告诉我glPushAttrib( GL_CURRENT_BIT ) 和 glPopAttrib()这两个函数,
我在纹理贴图函数里加入 glPushAttrib( GL_TEXTURE_BIT ) ;就行了,谢谢啊

#1


DrawLines();//坐标
DrawColorbar();//画颜色条

这两个函数内容是什么

#2


void CDrawSurfaceView::DrawLines()
{
//Draw Lines
glPushMatrix();
// glLoadIdentity();
glBegin(GL_LINES);

//X
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(0,0.0f,0.0f);
glVertex3f(15.0f,0.0f,0.0f);

//Arrow
glVertex3f(14.60f,0.3f,0.0f);
glVertex3f(15.0f,0.0f,0.0f);
glVertex3f(14.60f,-0.3f,0.0f);
glVertex3f(15.0f,0.0f,0.0f);

//Y
glColor3f(0.0f,1.0f,0.0f);
glVertex3f(0.0f,0,0.0f);
glVertex3f(0.0f,15.0f,0.0f);
//Arrow
glVertex3f(-0.3f,14.6f,0.0f);
glVertex3f(0.0f,15.0f,0.0f);
glVertex3f(0.3f,14.6F,0.0f);
glVertex3f(0.0f,15.0f,0.0f);

//Z
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(0.0f,0.0f,0);
glVertex3f(0.0f,0.0f,15.0f);
//Arrow
glVertex3f(0.3f,0.0f,14.60f);
glVertex3f(0.0f,0.0f,15.0f);
glVertex3f(-0.3f,0.0f,14.60f);
glVertex3f(0.0f,0.0f,15.0f);

glEnd();
glPopMatrix();
  glFlush(); 
}

void CDrawSurfaceView::DrawColorbar()
{
// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // 清除屏幕和深度缓存
// glLoadIdentity(); // 重置当前的模型观察矩阵
CreateColorMap();
glPushMatrix(); 
// glLoadIdentity();
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);  
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,4,checkImageWidth,checkImageHeight,
0,GL_RGBA,GL_UNSIGNED_BYTE,colorMap);//纹理的数据来源为otherImage
glEnable(GL_TEXTURE_2D);//生成纹理像素
glBegin(GL_QUADS);

glTexCoord2f(0.0f,0.0f);glVertex3f(18.0f,18.0f,0.0f);//左下
glTexCoord2f(1.0f,0.0f);glVertex3f(18.0f, 20.0f,0.0f);//右下
glTexCoord2f(1.0f,1.0f);glVertex3f( 20.0f, 20.0f,30.0f);//右上
glTexCoord2f(0.0f,1.0f);glVertex3f(20.0f, 18.0f,30.0f);//左上
glEnd();

glPopMatrix();   
    glFlush(); 
}

其中DrawColorbar()函数就是根据你博客里的文章建了一个数组,然后纹理贴图,单独用的时候能画出彩虹条的,可是加了上面的函数DrawLines()颜色就变了,这是什么原因呢?

#3


glcolor函数应该放在glbegin和glend的外面

#4


还是不行啊,我把函数改成下面的了
void CDrawSurfaceView::DrawLines()
{
//Draw Lines
glPushMatrix();
glLoadIdentity();

//X
glColor3f(1.0f,0.0f,0.0f);
glBegin(GL_LINES);

glVertex3f(0,0.0f,0.0f);
glVertex3f(15.0f,0.0f,0.0f);
//Arrow
glVertex3f(14.60f,0.3f,0.0f);
glVertex3f(15.0f,0.0f,0.0f);
glVertex3f(14.60f,-0.3f,0.0f);
glVertex3f(15.0f,0.0f,0.0f);

glEnd();

//Y
glColor3f(0.0f,1.0f,0.0f);
glBegin(GL_LINES);

glVertex3f(0.0f,0,0.0f);
glVertex3f(0.0f,15.0f,0.0f);
//Arrow
glVertex3f(-0.3f,14.6f,0.0f);
glVertex3f(0.0f,15.0f,0.0f);
glVertex3f(0.3f,14.6F,0.0f);
glVertex3f(0.0f,15.0f,0.0f);
glEnd();


//Z
glColor3f(0.0f,0.0f,1.0f);
glBegin(GL_LINES);

glVertex3f(0.0f,0.0f,0);
glVertex3f(0.0f,0.0f,15.0f);
//Arrow
glVertex3f(0.3f,0.0f,14.60f);
glVertex3f(0.0f,0.0f,15.0f);
glVertex3f(-0.3f,0.0f,14.60f);
glVertex3f(0.0f,0.0f,15.0f);

glEnd();
glPopMatrix();
glFlush(); 
}

void CDrawSurfaceView::DrawColorbar()
{

CreateColorMap();
// glPushMatrix(); 

// glLoadIdentity();

// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);// 清除屏幕和深度缓存


glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);  
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,4,checkImageWidth,checkImageHeight,
0,GL_RGBA,GL_UNSIGNED_BYTE,colorMap);//纹理的数据来源为otherImage
glEnable(GL_TEXTURE_2D);//生成纹理像素
glBegin(GL_QUADS);

glTexCoord2f(0.0f,0.0f);glVertex3f(18.0f,18.0f,0.0f);//左下
glTexCoord2f(1.0f,0.0f);glVertex3f(18.0f, 20.0f,0.0f);//右下
glTexCoord2f(1.0f,1.0f);glVertex3f( 20.0f, 20.0f,30.0f);//右上
glTexCoord2f(0.0f,1.0f);glVertex3f(20.0f, 18.0f,30.0f);//左上
glEnd();

// glPopMatrix();   
    glFlush(); 
}

void CDrawSurfaceView::OnDraw(CDC* pDC)
{
CDrawSurfaceDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
//清除颜色缓冲和深度缓冲
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//用单位矩阵替换当前矩阵
glLoadIdentity();
//视角
gluLookAt(1,1,1,0,0,0,0,0,1); 

DrawLines();//画坐标
DrawColorbar();//画颜色条
}
//强制绘图完成
glFinish();
//交换缓冲区数据
SwapBuffers(wglGetCurrentDC());
}

只要调用了函数DrawLines();后面的颜色条就不正常了

#5


刚刚的问题好了,我在DrawLines()函数的最后面加了句 glColor3f(1.0f,1.0f,1.0f);  颜色条就正常了,可是现在又遇到了新的问题,当我最大化窗口后,DrawLines()画的线条颜色就变黑了,
我在DrawColorbar()函数中
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);  
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
//注释掉glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
颜色条画不出来了,但是DrawLines()函数的线条颜色最大化后正常,没有变黑了,这又是什么原因呢?

#6


你使用 glColor的时候是否保存了当前的绘制环境?

可以试试用glPushAttrib( GL_CURRENT_BIT ) 和 glPopAttrib() 来保存和恢复绘制环境

#7


用你的方法果然行的,
可是我的函数DrawColorbar()加了glPushAttrib( GL_CURRENT_BIT ) 和 glPopAttrib() 还是会对后面画的图的颜色有影响,这是什么原因呢?函数原型如下:

void CDrawSurfaceView::DrawColorbar()
{
glPushAttrib( GL_CURRENT_BIT ) ;
CreateColorMap();
glPushMatrix(); 

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);  
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,4,checkImageWidth,checkImageHeight,
0,GL_RGBA,GL_UNSIGNED_BYTE,colorMap);//纹理的数据来源为colorMap
glEnable(GL_TEXTURE_2D);//生成纹理像素
glBegin(GL_QUADS);

glTexCoord2f(0.0f,0.0f);glVertex3f(18.0f,18.0f,0.0f);//左下
glTexCoord2f(1.0f,0.0f);glVertex3f(18.0f, 20.0f,0.0f);//右下
glTexCoord2f(1.0f,1.0f);glVertex3f( 20.0f, 20.0f,30.0f);//右上
glTexCoord2f(0.0f,1.0f);glVertex3f(20.0f, 18.0f,30.0f);//左上
glEnd();
glDeleteTextures(1, (GLuint *)colorMap);//
glPopMatrix(); 
glPopAttrib();
    glFlush(); 
}

#8


哈哈,搞定了,谢谢你告诉我glPushAttrib( GL_CURRENT_BIT ) 和 glPopAttrib()这两个函数,
我在纹理贴图函数里加入 glPushAttrib( GL_TEXTURE_BIT ) ;就行了,谢谢啊