OpenGL三角形没有出现

时间:2017-03-20 20:37:28

标签: c++ ubuntu opengl shader c++14

大家好我试图使用着色器学习openGL所以我做了一个三角形,它在移动时改变了颜色但是由于某种原因,三角形没有出现在窗口中,只是一个黑色的背景,它不会崩溃或显示任何一种错误,我相信库设置是好的,因为我之前使用过它们。 我在Ubuntu 16.04上使用CLion。这是我的代码,所以希望你可以检查出来并给我一个帮助。谢谢!

#define GLEW_STATIC

#include <stdlib.h>
#include <stdio.h>
#include <GL/glew.h>
#include <GL/freeglut.h>
#include <iostream>


int CurrentWidth = 800,
CurrentHeight = 600,
WindowHandle = 0;

GLuint
    VertexShaderId,
    FragmentShaderId,
    ProgramId;

GLuint s_vertexLoc, s_colorLoc , s_factorLoc;

GLfloat  runner = 0.0;

const GLchar* VertexShader =
    {
            "#version 150\n"
            "attribute vec3 in_vertex;"
            "attribute vec3 in_color;"

            "uniform float factor;"

            "varying vec3 intp_color;"

            "void main(void)"
            "{"
                "intp_color = in_color;"
                "gl_Position = vec4( in_vertex , 1.0 ) ;"
                "gl_Position.y += factor; "
            "}"

    };

//Telling every single pixel is going to be red
const GLchar* FragmentShader =
    {
            "#version 150\n"
            "uniform float factor;"
            "varying vec3 intp_color;"
            "void main(void){"
                "gl_FragColor = vec4( intp_color , 1.0) * factor;"
            "}"
    };


// for. dec.
void ResizeFunction(int, int);
void RenderFunction(void);
void IdleFunction(void);
void CreateShaders(void);



// set up an array for the geometry of the object
GLfloat Vertices[] = {
    -0.5f , -0.2f , 0.0f, // point A - x , y , z
    0.5f , -0.2f , 0.0f, // point B - x , y , z
    0.0f ,  0.8f , 0.0f  // point C - x , y , z
};

GLfloat Colors[] = {
    1.0f , 0.0f , 0.0f, // point A - x , y , z
    0.0f , 1.0f , 0.0f, // point B - x , y , z
    0.0f , 0.0f , 1.0f  // point C - x , y , z
};


int main( int argc , char* argv[] )
{
glutInit( &argc , argv);

//Target version 3.1
glutInitContextVersion(3 , 1 );

glutInitWindowSize(CurrentWidth, CurrentHeight);
glutInitDisplayMode( GLUT_DEPTH | GLUT_DOUBLE |     GLUT_RGBA );

WindowHandle = glutCreateWindow( "OpenGL - Shader example" );

glutReshapeFunc( ResizeFunction );
glutDisplayFunc( RenderFunction );
glutIdleFunc( IdleFunction );

/// init GLEW
GLenum GlewInitResult;
GlewInitResult = glewInit();

if (GLEW_OK != GlewInitResult)
    exit(EXIT_FAILURE);

/// Create our shaders
CreateShaders();

glutMainLoop();

exit(EXIT_SUCCESS);
}


void ResizeFunction(int Width, int Height)
{
CurrentWidth = Width;
CurrentHeight = Height;
glViewport( 0, 0, CurrentWidth, CurrentHeight );
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
}

void RenderFunction(void)
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

//We enable the shader variable
glEnableVertexAttribArray( s_vertexLoc );
glEnableVertexAttribArray( s_colorLoc );

//How to send data to the variable:
//( Where to send the data , how its grouped , data type , dont normalize the data,
// there is no offset, you find it here)

glVertexAttribPointer( s_vertexLoc , 3 , GL_FLOAT , GL_FALSE , 0 , Vertices );
glVertexAttribPointer ( s_colorLoc , 3 , GL_FLOAT , GL_FALSE , 0 , Colors);

glUniform1f( s_factorLoc , runner );
runner += 0.01;
if(runner > 1.0f)
    runner = -1.0f;

glDrawArrays( GL_TRIANGLES , 0 , 3 );

glDisableVertexAttribArray( s_vertexLoc );
glDisableVertexAttribArray( s_colorLoc );

glutSwapBuffers();
  }

void IdleFunction(void)
{
glutPostRedisplay();
}

//Error checking
void printLog(GLuint obj)
{
int infologLength = 0;
int maxLength;

if( glIsShader( obj ) )
    glGetShaderiv( obj , GL_INFO_LOG_LENGTH , &maxLength );
else
    glGetProgramiv( obj, GL_INFO_LOG_LENGTH, &maxLength);

char infoLog[1255];

if ( glIsShader(obj) )
    glGetShaderInfoLog( obj, maxLength, &infologLength, infoLog );
else
    glGetProgramInfoLog( obj, maxLength, &infologLength, infoLog );

if ( infologLength > 0 )
    printf( "\n Error detail: %s\n" , infoLog );
}


void CreateShaders(void)
{
GLenum ErrorCheckValue = glGetError();

if( glCreateShader )
    printf(" ---- shader suppot ok ---");
else
{
    printf(" ---- no shader support ---");
    return ;
}

///The VERTEX shader is created (tell it what it is)
VertexShaderId = glCreateShader( GL_VERTEX_SHADER );
//(shader we want to set the source, how many are they, the source of the shader , )
glShaderSource( VertexShaderId , 1 , &VertexShader , nullptr );
//Compile the shader
glCompileShader( VertexShaderId );

//Error checking
printLog( VertexShaderId );


///The FRAGMENT shader is created (tell it what it is)
FragmentShaderId = glCreateShader( GL_FRAGMENT_SHADER );
//(shader we want to set the source, how many are they, the source of the shader , )
glShaderSource( FragmentShaderId , 1 , &FragmentShader , nullptr );
//Compile the shader
glCompileShader( FragmentShaderId );

//Error checking
printLog( FragmentShaderId );


///Program Object links both shaders
ProgramId = glCreateProgram();
//Attach the compiled shaders to the program
glAttachShader( ProgramId , VertexShaderId );
glAttachShader( ProgramId , FragmentShaderId );
//Links the shaders to the program
glLinkProgram( ProgramId );

//Error checking
printLog( ProgramId );

//Uses the program to render
glUseProgram( ProgramId );

//( check this program , for this variable and return it)
s_vertexLoc = glGetAttribLocation( ProgramId , "in_vertex" );
s_colorLoc = glGetAttribLocation( ProgramId , "in_color" );
s_factorLoc = glGetUniformLocation( ProgramId , "factor" );
}

1 个答案:

答案 0 :(得分:0)

你有几个问题。

  • 首先,不建议使用OpenGL 3.1(GLSL 1.30)。至少尝试一下 3.2(GLSL 1.50)并在您的初始化过程中询问Core Profile。然后替换&#34; 属性&#34;使用&#34; &#34;在你的 顶点着色器和&#34; 变化&#34;使用&#34; &#34;或&#34; out &#34;根据 如果此var将用作输入或输出。

  • 二。此

    //如何将数据发送到变量:    //(在哪里发送数据,如何分组,数据类型,不规范化数据,    //没有偏移,你在这里找到

    glVertexAttribPointer(s_vertexLoc,3,GL_FLOAT,GL_FALSE,0,Vertices);    glVertexAttribPointer(s_colorLoc,3,GL_FLOAT,GL_FALSE,0,Colors);

    不是真的。最后一个值是字节偏移到缓冲区对象的数据存储数据本身。

  • 第三:你的顶点坐标需要一个缓冲区。使用 glGenBuffers()glBufferData()glBindBuffer()

如果我在你的情况下,我会按照现代OpenGL&#34;进行一些教程。在网上搜索,有很多。