2 Replies - 604 Views - Last Post: 20 March 2018 - 05:14 AM Rate Topic: -----

#1 Bihar Shooter   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 39
  • Joined: 13-February 18

C++ Open GL , GLEW , GLFW triangle program doesn't work ;

Posted 18 March 2018 - 05:08 PM

Error in console window says -
Failed to compile vertex shader
0(1) : error C000 :syntax error,unexpected integer constant,expecting "::" at token "<int-const>"

same shows for fragment shader also

Here's the code -
#include <GL/glew.h>
#include "GLFW/glfw3.h"
#include <conio.h>
#include <iostream>
#include <string>

static int CompileShader (const std::string & source , unsigned int  type)
{
    unsigned int id = glCreateShader(type);
    const char* src = &source[0];
    glShaderSource(id , 1 , &src ,nullptr);
    glCompileShader(id);

    int result;
    glGetShaderiv(id , GL_COMPILE_STATUS , &result);

    if(!result)
    {
        int length;
        glGetShaderiv(id , GL_INFO_LOG_LENGTH ,&length);

        char *message = new char [length + 1];
        glGetShaderInfoLog(id,length ,&length ,message);
        std::cout<<"Failed to compile "<<(type == GL_VERTEX_SHADER ? "vertex" : "fragment") <<" shader!"<<std::endl;
        std::cout<<message<<std::endl;
        glDeleteShader(id);
        delete [] message ;
        return 0;
    }



    return id;
}

static unsigned int CreateShader1 (const std::string vshader , const std::string fshader)
{
    unsigned int program = glCreateProgram();
    unsigned int vs = CompileShader(vshader,GL_VERTEX_SHADER);
    unsigned int fs = CompileShader(fshader,GL_FRAGMENT_SHADER);

     glAttachShader(program,vs);
     glAttachShader(program,fs);
     glLinkProgram(program);
     glValidateProgram(program);

    glDeleteShader(vs);
    glDeleteShader(fs);

    return program;
}

int main()
{
    GLFWwindow * window ;

    glfwInit();

    if(!glfwInit())
        return -1;

    window = glfwCreateWindow(800,600,"Bihar Shooter",NULL,NULL);

      if(!window)
    {
        glfwTerminate();
        return -1;
    }


    glfwMakeContextCurrent(window);

        if(glewInit() != GLEW_OK )
        std::cout<<"Glew not initialized !";

        float pos[6] = {

            0 , 0.5 ,

            -0.5 , -.5,

            0.5  , -0.5
        };

        unsigned int buffer;
        glGenBuffers(1 , &buffer);
        glBindBuffer(GL_ARRAY_BUFFER , buffer);
        glBufferData(GL_ARRAY_BUFFER,6 * sizeof(float) ,pos ,GL_STATIC_DRAW);

        glEnableVertexAttribArray(0);
        glVertexAttribPointer(0 , 3 , GL_FLOAT ,GL_FALSE,8,0);

        char * vshader =
                "version 330 core \n   layout (location =0) in vec4 position; \n                   "
                "  void main() \n                     "
                "  {\n                     "
                "        gl_position = position ;\n               "
                "                       "
                "     }\n                  ";


        char * fshader =
                "version 330 core   \n                    "
                "   layout (location = 0) out vec4 color;\n                    "
                " void main()\n                      "
                " {\n                      "
                "         color  =  vec4(1.0 , 0.0, 0.0 , 1.0);\n              "
                " }\n                      ";


        unsigned int shader = CreateShader1(vshader  , fshader);
        glUseProgram(shader);

    while(!glfwWindowShouldClose(window))
    {
        glClear(GL_COLOR_BUFFER_BIT);

        /*   Render Here    */

        glDrawArrays(GL_TRIANGLES,0,3);

        /*                  */
        glfwSwapBuffers(window);

        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}




Any help would be appreciated
(Moment when you realize you can't even draw a colored triangle on the screen :
Sigh! :"Good Times ")

Thanks :gun_bandana:

Is This A Good Question/Topic? 0
  • +

Replies To: C++ Open GL , GLEW , GLFW triangle program doesn't work ;

#2 Bihar Shooter   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 39
  • Joined: 13-February 18

Re: C++ Open GL , GLEW , GLFW triangle program doesn't work ;

Posted 20 March 2018 - 02:37 AM

I found out the error and now the program works
lol :gun_bandana: :gun_bandana:
Was This Post Helpful? 0
  • +
  • -

#3 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14929
  • View blog
  • Posts: 59,607
  • Joined: 12-June 08

Re: C++ Open GL , GLEW , GLFW triangle program doesn't work ;

Posted 20 March 2018 - 05:14 AM

What was the fix for the error?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1