OpenGL不呈现颜色

时间:2017-08-22 03:51:40

标签: c++ opengl

由于某些原因,当我运行程序时颜色不会呈现。随着glm的加入,我遇到了一些运行时出现的奇怪图像问题。它可能是一个图书馆,但它非常值得怀疑。我检查并重新检查了我的包含和库。我正在使用Eclipse。

这是我的代码

/*
 * Module5.cpp
 *
 *  Created on: Aug 21, 2017
 *      Author: 
 */

#include <iostream>
#include <Gl/glew.h>
#include <GL/freeglut.h>

#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>


using namespace std;

#define WINDOW_TITLE "Window"

#ifndef GLSL
#define GLSL(Version, Source) "#version " #Version "\n" #Source
#endif

GLint shaderProgram, WindowWidth = 800, WindowHeight = 600;

GLuint VBO, VAO; //Global variables for Buffer Object etc.

GLfloat cameraSpeed = 0.0005f;

GLchar currentKey;  //will store key pressed

glm::vec3 cameraPosition = glm::vec3(0.0f, 0.0f, 5.0f);
glm::vec3 CameraUpY = glm::vec3(0.0f, 1.0f, 0.0f);
glm::vec3 CameraForwardZ = glm::vec3(0.0f, 0.0f, -1.0f);

void UResizeWindow(int, int);
void URenderGraphics(void);
void UCreateShader(void);
void UCreateBuffers(void);
void UKeyboard(unsigned char key, int x, int y);
void UKeyReleased(unsigned char key, int x, int y);

const GLchar * vertexShaderSource = GLSL(330,

    layout(location=0) in vec3 position; //incoming data

    layout(location=1) in vec3 color;

    out vec4 mobileColor; //Attrib pointer 0
    //out vec4 colorFromVShader;

    //uniform mat4 primitiveTransform; //transform for shape

    uniform mat4 model;
    uniform mat4 view;
    uniform mat4 projection;

     void main()
     {
                 //gl_Position = primitiveTransform * vertex_Position; //move object on y-axis .5
                 gl_Position = projection * view * model * vec4(position, 1.0f); //move object on y-axis .5
                 //colorFromVShader = colorFromVBO;
                 mobileColor = color;
     }

);
const GLchar * fragmentShaderSource = GLSL(440,

        in vec3 mobileColor;
        out vec4 gpuColor;

         void main(){
            // gl_FragColor= vec4(1.0, 0.5, 0.0, 1.0);
            gpuColor= vec4(mobileColor, 1.0);
         }
);


//Main
int main(int argc, char* argv[])
{
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
        glutInitWindowSize(WindowWidth, WindowHeight);
        glutCreateWindow(WINDOW_TITLE);


        glutReshapeFunc(UResizeWindow);

        glewExperimental = GL_TRUE;

        if(glewInit() != GLEW_OK)
        {
            cout << "Failed to initialize glew!" << endl;
            return -1;
        }

        UCreateShader();

        UCreateBuffers();

        glUseProgram(shaderProgram);

        glClearColor(0.0f, 0.0f, 0.0f, 1.0f);


        glutDisplayFunc(URenderGraphics);

        glutKeyboardFunc(UKeyboard);

        glutKeyboardUpFunc(UKeyReleased);

        glutMainLoop();

        glDeleteVertexArrays(1, &VAO);//cleanup
        glDeleteBuffers(1, &VBO);//cleanup



        return 0;
}
void UResizeWindow(int w, int h)
{
    WindowWidth = w;
    WindowHeight = h;
    glViewport(0, 0, WindowWidth, WindowHeight);
}

void URenderGraphics(void)
{
     glEnable(GL_DEPTH_TEST);

     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//clears screen

     glBindVertexArray(VAO); //activate vertex array to render the vertices that render our shape

     if(currentKey == 'w')
     cameraPosition += cameraSpeed * CameraForwardZ;

     if(currentKey == 's')
     cameraPosition -= cameraSpeed * CameraForwardZ;

     if(currentKey == 'a')
     cameraPosition -= cameraSpeed * CameraForwardZ;

     if(currentKey == 'd')
     cameraPosition += cameraSpeed * CameraForwardZ;

     glm::mat4 model;
     model = glm::translate(model,glm::vec3(0.0f, 0.0f, 0.0));
     model = glm::rotate(model, glm::radians(-45.0f), glm::vec3(0.0f, 1.0f, 0.0f)); //rotate shape x-axis by 1.0f
     model = glm::scale(model, glm::vec3(2.0f, 2.0f, 2.0f)); //scale shape


     glm::mat4 view; //camera
     view = glm::lookAt(cameraPosition, cameraPosition + CameraForwardZ, CameraUpY); //move camera back by 5 (z)

     glm::mat4 projection;
     projection = glm::perspective(45.0f, (GLfloat)WindowWidth / (GLfloat)WindowHeight, 0.1f, 100.0f);
     //projection = glm::ortho(-5.0f, 5.0f, -5.0f, 5.0f, 0.1f, 100.0f);


     GLint modelLoc = glGetUniformLocation(shaderProgram, "model");
     GLint viewLoc = glGetUniformLocation(shaderProgram, "view");
     GLint projLoc = glGetUniformLocation(shaderProgram, "projection");

     glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
     glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
     glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(projection));

     //apply projection matrix
     /*

     glm::mat4 newTransform; //references 4 x 4 matrix

     newTransform = glm::translate(newTransform, glm::vec3(0.0f, 0.5f, 0.0)); //make square move up y-axis
     newTransform = glm::rotate(newTransform, glm::radians(45.0f), glm::vec3(0.0f, 0.0f, 1.0f)); //rotate shape
     //newTransform = glm::scale(newTransform, glm::vec3(0.5f, 0.5f, 0.5f)); //rotate shape

     GLuint transformInfo = glGetUniformLocation(ProgramId, "primitiveTransform"); //id for shader, name of variable shader
     glUniformMatrix4fv(transformInfo, 1, GL_FALSE, glm::value_ptr(newTransform));
*/


     glutPostRedisplay();

     glDrawArrays(GL_TRIANGLES, 0 , 36);

     glBindVertexArray(0);

     glutSwapBuffers();

}
void UCreateShader()
{


    GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
    glCompileShader(vertexShader);

    GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
    glCompileShader(fragmentShader);

    shaderProgram = glCreateProgram();
    glAttachShader(shaderProgram, vertexShader);
    glAttachShader(shaderProgram, fragmentShader);
    glLinkProgram(shaderProgram);

    glDeleteShader(vertexShader);
    glDeleteShader(fragmentShader);

}
/*void applyDepthSettings() {
   glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set background color to black and opaque
   glClearDepth(1.0f);
   glEnable(GL_DEPTH_TEST);
   glDepthFunc(GL_LEQUAL);
   glShadeModel(GL_SMOOTH);
   glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}*/

void UCreateBuffers()
{
    //specify coords for creating square
    // Positon and Color data
        GLfloat vertices[] = {
    // Vertex Positions    // Colors
                           -0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f,  // Top Right Vertex 0
                            0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f,  // Bottom Right Vertex 1
                            0.5f,  0.5f, -0.5f, 1.0f, 0.0f, 0.0f,    // Bottom Left Vertex 2
                            0.5f,  0.5f, -0.5f, 1.0f, 0.0f, 0.0f,    // Top Left Vertex 3
                           -0.5f,  0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
                           -0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f,


                           -0.5f, -0.5f,  0.5f, 0.0f, 1.0f, 0.0f,   // Top Right Vertex 0
                            0.5f, -0.5f,  0.5f, 0.0f, 1.0f, 0.0f,  // Bottom Right Vertex 1
                            0.5f,  0.5f,  0.5f, 0.0f, 1.0f, 0.0f,    // Bottom Left Vertex 2
                            0.5f,  0.5f,  0.5f, 0.0f, 1.0f, 0.0f,     // Top Left Vertex 3
                           -0.5f,  0.5f,  0.5f, 0.0f, 1.0f, 0.0f,
                           -0.5f, -0.5f,  0.5f, 0.0f, 1.0f, 0.0f,


                           -0.5f,  0.5f,  0.5f, 0.0f, 0.0f, 1.0f,   // Top Right Vertex 0
                           -0.5f,  0.5f, -0.5f, 0.0f, 0.0f, 1.0f,  // Bottom Right Vertex 1
                           -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 1.0f,    // Bottom Left Vertex 2
                           -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 1.0f,    // Top Left Vertex 3
                           -0.5f, -0.5f,  0.5f, 0.0f, 0.0f, 1.0f,
                           -0.5f,  0.5f,  0.5f, 0.0f, 0.0f, 1.0f,


                            0.5f,  0.5f,  0.5f, 1.0f, 1.0f, 0.0f,   // Top Right Vertex 0
                            0.5f,  0.5f, -0.5f, 1.0f, 1.0f, 0.0f,  // Bottom Right Vertex 1
                            0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 0.0f,    // Bottom Left Vertex 2
                            0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 0.0f,     // Top Left Vertex 3
                            0.5f, -0.5f,  0.5f, 1.0f, 0.0f, 0.0f,
                            0.5f,  0.5f,  0.5f, 1.0f, 1.0f, 0.0f,


                           -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 1.0f,  // Top Right Vertex 0
                            0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 1.0f,  // Bottom Right Vertex 1
                            0.5f, -0.5f,  0.5f, 0.0f, 1.0f, 1.0f,   // Bottom Left Vertex 2
                            0.5f, -0.5f,  0.5f, 0.0f, 1.0f, 1.0f,     // Top Left Vertex 3
                           -0.5f, -0.5f,  0.5f, 0.0f, 1.0f, 1.0f,
                           -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 1.0f,

                           -0.5f,  0.5f, -0.5f, 1.0f, 0.0f, 1.0f,   // Top Right Vertex 0
                            0.5f,  0.5f, -0.5f, 1.0f, 0.0f, 1.0f,  // Bottom Right Vertex 1
                            0.5f,  0.5f,  0.5f, 1.0f, 0.0f, 1.0f,    // Bottom Left Vertex 2
                            0.5f,  0.5f,  0.5f, 1.0f, 0.0f, 1.0f,     // Top Left Vertex 3
                           -0.5f,  0.5f,  0.5f, 1.0f, 0.0f, 1.0f,
                           -0.5f,  0.5f, -0.5f, 1.0f, 0.0f, 1.0f,



                               };


    //generate id's for buffer object
    glGenVertexArrays(1, &VAO); //generate for Vertex Array Object
    glGenBuffers(1, &VBO);      //generate for Vertex Buffer Object


    glBindVertexArray(VAO);      //activate text array object

    glBindBuffer(GL_ARRAY_BUFFER, VBO); //activating VBO buffer
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); //pass in size of array from line 128

    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)0);//send data to shader (accepts 6 arguments)GL_FALSE=not using normalization
    glEnableVertexAttribArray(0);//enable vertex attribute pointer, starting position of x,y,z

    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));//send data to shader (accepts 6 arguments)GL_FALSE=not using normalization
    glEnableVertexAttribArray(1);//specify color starting point

    glBindVertexArray(0);  //deactivate vertex array object (VBO)

}
void UKeyboard(unsigned char key, GLint x, GLint y)
{
    switch(key)
    {

    case'w':
    cout<<"You pressed W!" <<endl;
    break;

    case 's':
    cout<<"You pressed S!"<<endl;
    break;

    case'a':
        cout<<"You pressed A!"<<endl;
        break;

    case 'd':
        cout<<"You pressed D!"<<endl;
    break;

    default:
        cout<<"Press a key!"<<endl;
    }
}

/*Implements the UKeyReleased function*/
void UKeyReleased(unsigned char key, GLint x, GLint y)
{
    cout<<"Key released"<<endl;
}

1 个答案:

答案 0 :(得分:3)

您的顶点着色器无法编译,因为mobileColor的类型为vec4color的类型为vec3

变化:

mobileColor = color;

为:

mobileColor = vec4(color, 1.0); 

注意,您的着色器程序未使用,因为它未成功构建。您绘制的所有内容都是默认使用当前设置的Theory绘制的OpenGL绘制的,默认情况下为 white (1,1,1,1)。

glColor是否可以检查着色器的编译是否成功,并且可以使用glGetShaderiv检索错误消息:

GLint status = GL_TRUE;
glCompileShader( shaderStage );
glGetShaderiv( shaderStage, GL_COMPILE_STATUS, &status );
if ( status == GL_FALSE )
{
    GLint logLen;
    glGetShaderiv( shaderStage, GL_INFO_LOG_LENGTH, &logLen );
    std::vector< char >log( logLen+1 );
    GLsizei written;
    glGetShaderInfoLog( shaderStage, logLen, &written, log.data() );
    std::cout << "compile error:" << std::endl << log.data() << std::endl;
}

glGetShaderInfoLog可以检查程序是否已成功链接,并且可以使用glGetProgramiv检索错误消息:

GLint status = GL_TRUE;
glLinkProgram( shaderProgram );
glGetProgramiv( shaderProgram, GL_LINK_STATUS, &status );
if ( status == GL_FALSE )
{
    GLint logLen;
    glGetProgramiv( shaderProgram, GL_INFO_LOG_LENGTH, &logLen );
    std::vector< char >log( logLen+1 );
    GLsizei written;
    glGetProgramInfoLog( shaderProgram, logLen, &written, log.data() );
    std::cout  << "link error:" << std::endl << log.data() << std::endl;
}