Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

Thread: 0(7) : error C0000 syntax error

  1. #1
    Newbie Newbie
    Join Date
    Oct 2018
    Posts
    1

    Question 0(7) : error C0000 syntax error

    I am currently watching TheChernoproject's OpenGL series so i use his code to learn.
    I am pretty sure that i had done everything done correctly but i seem to be wrong.

    Code :
    #include <GL/glew.h>
    #include <GLFW/glfw3.h>
     
    #include <iostream>
     
    static unsigned int CompileShader(unsigned int type, const std::string& source)
    {
    	unsigned int id = glCreateShader(type);
    	const char* src = source.c_str();
    	glShaderSource(id, 1, &src, nullptr);
    	glCompileShader(id);
     
    	int result;
    	glGetShaderiv(id, GL_COMPILE_STATUS, &result);
    	if (result == GL_FALSE) //or (!result)
    	{
    		int length;
    		glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);
    		char *message = (char*)alloca(length * sizeof(char));
    		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);
    		return 0;
    	}
     
    	return id;
    }
     
    static unsigned int CreateShader(const std::string& vertexShader, const std::string& fragmentShader)
    {
    	unsigned int program = glCreateProgram();
    	unsigned int vs = CompileShader(GL_VERTEX_SHADER, vertexShader);
    	unsigned int fs = CompileShader(GL_FRAGMENT_SHADER, fragmentShader);
     
    	glAttachShader(program, vs);
    	glAttachShader(program, fs);
    	glLinkProgram(program);
    	glValidateProgram(program);
     
    	glDeleteShader(vs);
    	glDeleteShader(fs);
     
    	// TODO: Error handling
     
    	return program;
    }
     
    int main(void)
    {
    	GLFWwindow* window;
     
    	/* Initialize the library */
    	if (!glfwInit())
    		return -1;
     
    	/* Create a windowed mode window and its OpenGL context */
    	window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
    	if (!window)
    	{
    		glfwTerminate();
    		return -1;
    	}
     
     
    	/* Make the window's context current */
    	glfwMakeContextCurrent(window);
     
    	//initialize glew
    	if (glewInit() != GLEW_OK)
    		std::cout << "Error!" << std::endl;
     
    	std::cout << glGetString(GL_VERSION) << std::endl;
     
    	float positions[6] = {
    		 -0.5f, -0.5f,
    		 0.0f,   0.5f,
    		 0.5f,  -0.5f
    	};
     
    	unsigned int buffer;
    	glGenBuffers(1, &buffer);
    	glBindBuffer(GL_ARRAY_BUFFER, buffer);
    	glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(float), positions, GL_STATIC_DRAW);
     
    	glEnableVertexAttribArray(0);
    	glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0);
     
    	std::string vertexShader =
    		"#version 410 core\n"
    		"\n"
    		"layout(location = 0) in vec4 position;\n"
    		"\n"
    		"void main()\n"
    		"{\n"
    		"	gl_Position = position;\n"
    		"}\n";
     
    	std::string fragmentShader =
    		"#version 410 core\n"
    		"\n"
    		"layout(location = 0) out vec4 color;\n"
    		"\n"
    		"void main()\n"
    		"{\n"
    		"	color = vec4(1.0, 0.0, 0.0, 1.0);\m" //R G B A
    		"}\n";
     
    	unsigned int shader = CreateShader(vertexShader, fragmentShader);
    	glUseProgram(shader);
     
    	/* Loop until the user closes the window */
    	while (!glfwWindowShouldClose(window))
    	{
    		/* Render here */
    		glClear(GL_COLOR_BUFFER_BIT);
     
    		glDrawArrays(GL_TRIANGLES, 0, 3);
     
    		/* Swap front and back buffers */
    		glfwSwapBuffers(window);
     
    		/* Poll for and process events */
    		glfwPollEvents();
    	}
     
    	glfwTerminate();
    	return 0;
    }

    Can anyone help me ?
    I am a newbie so dont judge me if it was a small mistake.

  2. #2
    Intern Contributor
    Join Date
    Oct 2014
    Posts
    70
    --- EDIT: Ignore what I said xD

  3. #3
    Intern Contributor
    Join Date
    Oct 2014
    Posts
    70
    color = vec4(1.0, 0.0, 0.0, 1.0);\m" //R G B A
    You wrote "\m" instead of "\n"

    Little tip to avoid these kind of mistakes. Use string literals: https://en.cppreference.com/w/cpp/la...string_literal
    This way you don't need any \n and no quotation marks in every single line. Makes reading your shader code much more easier and less error prone.

    Your code would look like this:


    Code :
        const char fragmentShader[] = R"glsl(
                    #version 410 core
     
                    layout(location = 0) out vec4 color;
     
    		void main()
    		{
    			color = vec4(1.0, 0.0, 0.0, 1.0);  //R G B A
    		}
                )glsl";

    But you have to turn on c++ 11 or higher for that.

    Greetings

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •