1

我尝试创建具有两种颜色的墙:顶部 0.8、0.8、1 和底部 0.4、0.4、0.8,其余部分我想插入这两种颜色。

我有这些片段着色器和顶点着色器:

顶点着色器

#version 330

uniform mat4 modelMatrix,viewMatrix, projMatrix;

layout (location = 0) in vec4 position;
layout (location = 1) in vec3 color; 
out vec3 Color;

void main()
{
    Color = color;
    gl_Position = projMatrix * viewMatrix * modelMatrix * position ;
}

片段着色器

#version 330

in vec3 Color;

void main()
{

    gl_FragColor = vec4(Color,1.0);

}

我在 c++ 中的代码的某些部分:

unsigned int ProgramShader,VertexShader,FragmentShader;
Mode current_mode = GPU;
float verticesBackground[] = { -200.0f, -100.0f, -100.0f,
                         200.0f, -100.0f, -100.0f,
                        200.0f, 100.0f, -100.0f,
                        -200.0f, 100.0f, -100.0f  
                    };
int indexesBackground[] =   {   0 , 1 , 2 , 
                    2 , 3 , 0 
                };
float colorsBackground[] = {  0.8f, 0.8f, 1.0f, 
                    0.8f, 0.8f, 1.0f,
                    0.4f, 0.4f, 0.8f,
                    0.4f, 0.4f, 0.8f
                    };

// vao-uri
unsigned int BackgroundVao;
// vbo-uri
unsigned int BackgroundVboPositions, BackgroundVboColors;
// ibo-uri
unsigned int BackgroundIbo;

unsigned int ProjectionMatrixLocation, ViewMatrixLocation, ModelMatrixLocation;

mat4 ModelMatrix;
mat4 ProjectionMatrix;
mat4 ViewMatrix;

void RenderBackground()
{   //glLoadIdentity();
glUseProgram(ProgramShader);
//backgroundShader.Use();
/*glUniformMatrix4fv(ProjectionMatrixLocation,  1, false, ProjectionMatrix.m);
glUniformMatrix4fv(ViewMatrixLocation,  1, false, ViewMatrix.m);
mat4 identity;
identity.setIdentity();
glUniformMatrix4fv(ModelMatrixLocation, 1, false, identity.m);

glUniformMatrix4fv(ModelMatrixLocation, 1, false, ModelMatrix.m);
glBindVertexArray(BackgroundVao);                                       //revine la starea de desenare a cubului
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, (void*)0); //deseneaza backgroundul, 4 indecsi
*/
glBegin(GL_POLYGON);
glColor3f(0.8f,0.8f,1.0f);
glVertex3f(-200.0f,-100.0f,-100.0f);
glVertex3f(200.0f,-100.0f,-100.0f);
glColor3f(0.4f,0.4f,0.8f);  
glVertex3f(200.0f,100.0f,-100.0f);
glVertex3f(-200.0f,100.0f,-100.0f);
glEnd();
glUseProgram(0);    
}

glBegin 仅用于测试并且它有效,但是当我尝试使用 glDrawElements 时,我得到黑屏。

void display(void) {
switch(current_mode) {
    case CPU:
        RenderCPU();
    break;

    case GPU:
        RenderBackground();
    break;

    case CUDA:
        RenderCUDA();
    break;
}   
glutSwapBuffers();
glutPostRedisplay();
}

我的主要是:

int main ( int argc, char** argv ) 
{
glutInitContextFlags(GLUT_FORWARD_COMPATIBLE | GLUT_DEBUG);
glutInit( &argc, argv );
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA | GLUT_MULTISAMPLE);
glutInitWindowSize(1280, 720 ); 
glutCreateWindow( "Etapa 2 - Cloth" );

glutDisplayFunc(display);  
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);

glewExperimental =true;
glewInit();
glGetError();

glEnable(GL_MULTISAMPLE);
glEnable(GL_DEPTH_TEST);


InitShaders(std::string("shaders\\background.vs"),std::string("shaders\\background.fs"));
glBuffersInit();
ModelMatrix.setIdentity();
glutMainLoop();
}

我的着色器加载器:

void InitShaders(std::string VertexShaderFilename, std::string FragmentShaderFilename){
int InfoLogLength=0,Result=0;

//constructie vertex shader
std::cout<<std::endl<<"Constructie vertex shader din "<<VertexShaderFilename<<std::endl;
VertexShader = glCreateShader(GL_VERTEX_SHADER);                            //creez un obiect de tip vertex shader 
std::string VertexShaderCode = LoadText(VertexShaderFilename);              //citesc fiserul si il tin in string
const char *VertexShaderCodePtr = VertexShaderCode.c_str();
const int VertexShaderCodeSize = VertexShaderCode.size();
glShaderSource(VertexShader,1,&VertexShaderCodePtr,&VertexShaderCodeSize);  //atasez codul sursa din string in obiectul de tip vertex shader
glCompileShader(VertexShader);                                              //compilez obiectul
glGetShaderiv(VertexShader, GL_COMPILE_STATUS, &Result);                    //obtin rezultatul compilarii
if(Result == GL_FALSE){
    glGetShaderiv(VertexShader, GL_INFO_LOG_LENGTH, &InfoLogLength);        //obtin lungimea logului
    std::vector<char> VertexShaderLog(InfoLogLength);
    glGetShaderInfoLog(VertexShader, InfoLogLength, NULL, &VertexShaderLog[0]); //copiez logul
    std::cout<<"EROARE :\n "<<&VertexShaderLog[0]<<std::endl;                   //output log
}else std::cout<<"Constructie vertex shader terminata cu sucess"<<std::endl<<std::endl;

CHECK_GL_ERRORS

//constructie fragment shader
std::cout<<"Constructie fragment shader din "<<FragmentShaderFilename<<std::endl;
FragmentShader = glCreateShader(GL_FRAGMENT_SHADER);                        //creez un obiect de tip fragment shader 
std::string FragmentShaderCode = LoadText(FragmentShaderFilename);          //citesc fiserul si il tin in string
const char *FragmentShaderCodePtr = FragmentShaderCode.c_str();
const int FragmentShaderCodeSize = FragmentShaderCode.size();
glShaderSource(FragmentShader,1,&FragmentShaderCodePtr,&FragmentShaderCodeSize);//atasez codul sursa din string in obiectul de tip fragment shader
glCompileShader(FragmentShader);                                            //compilez obiectul
glGetShaderiv(FragmentShader, GL_COMPILE_STATUS, &Result);                  //obtin rezultatul compilarii
if(Result == GL_FALSE){
    glGetShaderiv(FragmentShader, GL_INFO_LOG_LENGTH, &InfoLogLength);      //obtin lungimea logului
    std::vector<char> FragmentShaderLog(InfoLogLength);
    glGetShaderInfoLog(FragmentShader, InfoLogLength, NULL, &FragmentShaderLog[0]); //copiez logul
    std::cout<<"EROARE :\n "<<&FragmentShaderLog[0]<<std::endl;             //output log
}else std::cout<<"Constructie fragment shader terminata cu success "<<std::endl<<std::endl;

CHECK_GL_ERRORS

//constructie program combinat
std::cout<<"Constructie program combinat "<<std::endl;
ProgramShader = glCreateProgram();                                          //creez un obiect gol de tip program
glAttachShader(ProgramShader,VertexShader);                                 //atasez obiectul de tip vertex shader
glAttachShader(ProgramShader,FragmentShader);                               //atasez obiectul de tip fragment shader
glLinkProgram(ProgramShader);                                               //link-ez programul
glGetProgramiv(ProgramShader, GL_LINK_STATUS, &Result);                     //obtin rezultatul linkarii
if(Result == GL_FALSE){                                                     
    glGetProgramiv(ProgramShader, GL_INFO_LOG_LENGTH, &InfoLogLength);      
    std::vector<char> ProgramShaderLog(InfoLogLength);
    glGetProgramInfoLog(ProgramShader, InfoLogLength, NULL, &ProgramShaderLog[0]);
    std::cout<<"EROARE :\n "<<&ProgramShaderLog[0]<<std::endl;
}else std::cout<<"Constructie program terminata cu success "<<std::endl;
glDeleteShader(VertexShader);                                               //nu mai sunt necesare daca vom lucra doar cu programul
glDeleteShader(FragmentShader);

CHECK_GL_ERRORS

//obtine locatiile uniform-urilor din programul compilat & linkat
ProjectionMatrixLocation = glGetUniformLocation(ProgramShader, "projMatrix");
ViewMatrixLocation = glGetUniformLocation(ProgramShader, "viewMatrix");
ModelMatrixLocation = glGetUniformLocation(ProgramShader, "modelMatrix");

CHECK_GL_ERRORS
}
4

0 回答 0