c++ - Opengl 无法使用 glColor3f(1.0f, 0.0f, 0.0f) 非常不透明的红色将颜色设置为纯色

标签 c++ opengl colors textures opengl-compat

在图像中出现红色不透明但我设置了 glColor3f(1.0f, 0.0f, 0.0f);

//command compiler g++ console tested with Visual Studio Code
//g++ GL01Hello.cpp -o GL01Hello.exe -L"C:/MinGW/freeglut/lib" -lglu32 -lopengl32 -lfreeglut -I"C:\MinGW\freeglut\include\GL"
/*
 * GL01Hello.cpp:With Load Background Image and Poligon Test OpenGL/GLUT C/C++ Setup
 * Tested Visual Studio Code with MinGW
 * To compile with -lfreeglut -lglu32 -lopengl32 and 
 */
#include <windows.h>  // for MS Windows
#include <stdio.h>      /* printf, scanf, puts, NULL */
#include <iostream>
#include <stdlib.h>     /* srand, rand */
#include <ctime> 
#include <freeglut.h>  // GLUT, include glu.h and gl.h
using namespace std;

float spin = 0.0;
GLuint texture = 0;
int w1 = 0;
int h1 = 0;
// for random color primitive polygon
//static GLubyte redc,greenc,bluec;
bool prim_polygonmode = false;
// glut_load_image
GLuint LoadTexture( const char * filename )
{
  GLuint texture;
  int width, height;
  unsigned char * data;
  FILE * file;
  file = fopen( filename, "rb" );
  if(!file)
    std::cout<<"File not Found"<<std::endl;
  if ( file == NULL ) return 0;
  width = 1360;
  height = 768;
  data = (unsigned char *)malloc( width * height * 3 );
  //int size = fseek(file,);
  fread( data, width * height * 3, 1, file );
  fclose( file );
 for(int i = 0; i < width * height ; ++i)
{
   int index = i*3;
   unsigned char B,R;
   B = data[index];
   R = data[index+2];
   data[index] = R;
   data[index+2] = B;
}
glGenTextures( 1, &texture );
glBindTexture( GL_TEXTURE_2D, texture );
glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );//Necessary for correct elements value 4 default
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,GL_MODULATE );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,GL_REPEAT );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,GL_REPEAT );
gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height,GL_RGB, GL_UNSIGNED_BYTE, data );
free( data );
return texture;
}
/* Initialize OpenGL Graphics just n this case for colors */
void initGL() {
   // Set "clearing" or background color
   glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Black and opaque

   //randomnumber color by ctime library
   srand(time(NULL));
   //redc = rand()%255;
   //greenc = rand()%255;
   //bluec = rand()%255;
}
/* Called back when there is no other event to be handled */
void idle() {
   spin = spin + 0.075;
   if (spin > 360.0)
         spin = 0;
   glutPostRedisplay();   // Post a re-paint request to activate display()
}
/* Handler for window re-size event. Called back when the window first appears and
   whenever the window is re-sized with its new width and height */
void reshape(GLsizei width, GLsizei height) {  // GLsizei for non-negative integer
   // Compute aspect ratio of the new window
   w1 = width;
   h1 = height;
   if (height == 0) height = 1;                // To prevent divide by 0
   GLfloat aspect = (GLfloat)width / (GLfloat)height;
   // Set the viewport to cover the new window
   glViewport(0, 0, width, height);
   // Set the aspect ratio of the clipping area to match the viewport
   glMatrixMode(GL_PROJECTION);  // To operate on the Projection matrix
   glLoadIdentity();
   if (width >= height) {
     // aspect >= 1, set the height from -1 to 1, with larger width
      gluOrtho2D(-1.0 * aspect, 1.0 * aspect, -1.0, 1.0);
   } else {
      // aspect < 1, set the width to -1 to 1, with larger height
     gluOrtho2D(-1.0, 1.0, -1.0 / aspect, 1.0 / aspect);
   }
}
void orthogonalStart() 
{
    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();
    gluOrtho2D(-w1/2, w1/2, -h1/2, h1/2);
    glMatrixMode(GL_MODELVIEW);
} 
void orthogonalEnd()
{
    glMatrixMode(GL_PROJECTION);
    glPopMatrix();
    glMatrixMode(GL_MODELVIEW);
}
void background()
{
    glBindTexture( GL_TEXTURE_2D, texture ); 
    orthogonalStart();
    glEnable(GL_POLYGON_OFFSET_FILL);
    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);                                        
    glPolygonOffset(1,1);
    // texture width/height
    const int iw = 1360;
    const int ih = 768;
    glPushMatrix();
    glTranslatef( -iw/2, -ih/2, 0 );
    glBegin(GL_QUADS);
        glColor3f(1.0f, 1.0f, 1.0f); // always default color white stars, if no this line will random color same of polygon
        glTexCoord2i(0,0); glVertex2i(0, 0);
        glTexCoord2i(1,0); glVertex2i(iw, 0);
        glTexCoord2i(1,1); glVertex2i(iw, ih);
        glTexCoord2i(0,1); glVertex2i(0, ih);
    glEnd();
    glPopMatrix();
    orthogonalEnd();
}
void display() {
   glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set background color to black and opaque
   glClear(GL_COLOR_BUFFER_BIT);// Clear the color buffer (background

   glEnable( GL_TEXTURE_2D );

   background();
   gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
   // A SQUARE PARAMETERS
   if (prim_polygonmode) { // draw polygon mode lines
             glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);     

         } else { 
             glEnable(GL_POLYGON_OFFSET_FILL);
             glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);                                        
             glPolygonOffset(1,1);
         }
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity(); 
   glPushMatrix();
   glRotatef(spin , 0., 0., 1.);
   glTranslatef(50.0, 50.0, 0);
   glTranslatef(-50.0, -50.0, 0);
   glBegin(GL_QUADS);  // Each set of 4 vertices form a quad
         //glColor3ub(redc, greenc, bluec); // Random red green blue value
         glColor3f(1.0f, 0.0f, 0.0f); // Random red green blue value
         glVertex2f(-0.5f, -0.5f);    // x, y default 0.5f values
         glVertex2f( 0.5f, -0.5f);
         glVertex2f( 0.5f,  0.5f); 
         glVertex2f(-0.5f,  0.5f);

   glEnd(); 

   //angle += 5.0f;
   glPopMatrix();
  // glFlush();  // Render now
   glutSwapBuffers();   // Double buffered - swap the front and back buffers
}
/* Callback handler for special-key event */
void specialKeys(int key, int x, int y) {
   switch (key) {
      case GLUT_KEY_F1:    // F1: Toggle wireframe and solid polygon
         prim_polygonmode = !prim_polygonmode;         // Toggle state

         break;
   }
}

/* Main function: GLUT runs as a console application starting at main()  */
int main(int argc, char** argv) {
   glutInit(&argc, argv);  // Initialize GLUT
   glutInitDisplayMode(GLUT_DOUBLE);  // Enable double buffered mode
   glutInitWindowSize(1360, 768);   // Set the window's initial width & height
   glutInitWindowPosition(0, 0);
   // Position the window's initial top-left corner
   glutCreateWindow("OpenGL Setup Test"); // Create a window with the given title
   glutSpecialFunc(specialKeys); // Register callback handler for special-key event
   glutDisplayFunc(display); // Register display callback handler for window re-paint
   glutReshapeFunc(reshape);
   glutIdleFunc(idle);
  // GLuint texture;
   texture = LoadTexture( "stars.bmp" );
   initGL();

   glutMainLoop();// Enter the event-processing loop
   //Free our texture
   glDeleteTextures( 1, &texture );
   return 0;
}

这段代码有一组背景和方形的小动画。 不知道为什么不能设置更多纯色。然后线框正方形我得到了一条非常小的红色线需要得到明亮的红色。也许是任何过滤器,你的缓冲区导致的? 如果可能,请帮助我。

最佳答案

OpenGL 是一个状态引擎。一旦设置了状态,它就会一直存在,直到再次更改为止。 这意味着如果启用 2 维纹理,则以下所有几何体都是“有纹理的”。

注意,当glVertex2f被调用然后当前纹理坐标与顶点坐标相关联。如果您没有明确设置纹理坐标,那么最后设置的纹理坐标仍然是当前纹理坐标,并且将关联到顶点坐标。这可能会导致类似随机行为。

如果启用纹理,则默认情况下纹理元素的颜色乘以当前颜色,因为默认情况下纹理环境模式 (GL_TEXTURE_ENV_MODE) 为 GL_MODULATE。参见 glTexEnv .

这一切意味着:

如果你想绘制带有纹理的几何图形,那么启用纹理并设置“白色”颜色:

glEnable(GL_TEXTURE_2D)
glColor3f(1.0f, 1.0f, 1.0f);

background();

如果你想绘制一个统一颜色的几何图形,那么设置颜色并禁用纹理:

glDisable(GL_TEXTURE_2D);
glColor3f(1.0f, 0.0f, 0.0f);

glBegin(GL_QUADS);
    // [...]
glEnd();

关于c++ - Opengl 无法使用 glColor3f(1.0f, 0.0f, 0.0f) 非常不透明的红色将颜色设置为纯色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58034216/

相关文章:

c++ - 如何将整个 vector 复制到队列中?

c++ - 为什么我们必须在 C++ 中取消分配数组数组而不是整个 "matrix"

OpenGL GBuffer 法线和深度缓冲问题

c++ - 为什么 glReadPixels 这么慢并且有其他选择吗?

c++ - 如何让纹理在 OpenGL 中工作?

c++ - T t {x} 和 T t = { x } 对于整数或枚举类型的区别?

c++ - 将基于堆的 N 元数组引用传递给函数

r - 如何反转RasterVis(Levelplot)的默认颜色?

c# - 更改禁用按钮上的文本颜色(自定义 ButtonRenderer)

c# - 我怎样才能做出新的颜色?