Syntax error in OpenGl shader that is copied from YouTube - c++

I trying to learn OpenGl and now I wanted to make a simple red triangle on a black background with vertices and shaders. I copied the code from that YouTube video. I've changed a view things. That's my code:
#include "prec.h"
struct Vector2
{
float x, y;
};
struct TrianglePos
{
Vector2 a, b, c;
};
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)
{
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 shader, message: " << 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);
return program;
}
int main() {
if (!glfwInit())
return -1;
GLFWwindow *window = glfwCreateWindow(640, 480, "Sand Box GL", NULL, NULL);
if (!window) {
std::cout << "problems with window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;
if (GLEW_OK != glewInit()) {
std::cout << "something with glew went wrong" << std::endl;
return -1;
}
TrianglePos trianglePos = {
-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, sizeof(trianglePos), &trianglePos, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(Vector2), 0);
std::string vertexShader =
"#version 400 core\n"
"\n"
"layout(location = 0) in vec4 position;\n"
"\n"
"void main() \n"
"{\n"
" gl_Position = position;\n"
"}\n";
std::string fragmentShader =
"#version 400 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);\n"
"}\n";
const char *versionGL;
versionGL = (char *) (glGetString(GL_VERSION));
std::cout << "openGl version: " << versionGL << std::endl;
if(GL_VERSION_4_0)
std::cout << "opengl 4.0 supported" << std::endl;
unsigned int shader = createShader(vertexShader, fragmentShader);
glUseProgram(shader);
while (!glfwWindowShouldClose(window)) {
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, 3);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
and that's the precompiled header (prec.h): #pragma once
#ifndef GLEWINIT_PREC_H
#define GLEWINIT_PREC_H
#endif //GLEWINIT_PREC_H
#include "GL/glew.h"
#include <GLFW/glfw3.h>
#include <iostream>
the program prints that to the console ("OpenGL 4.0 supported" means that GL_VERSION_4_0 == true):
openGl version: 2.1 INTEL-14.5.22
opengl 4.0 supported
When I try to run it I get this error message from the shader Compiler for my vertex and fragment shader:
ERROR: 0:1: '' : version '400' is not supported
ERROR: 0:1: '' : syntax error: #version
ERROR: 0:3: 'layout' : syntax error: syntax error
When I change the #version 400 core to #version 120 I only get the syntax error for the layout. Because of that I think I messed up something with glew. What could I try?

Your system does not support OpenGL 4.0. It just supports OpenGL 2.1. See the output
openGl version: 2.1 INTEL-14.5.22
GLSL 1.20 corresponds to OpenGL 2.1. Downgrade the shader:
Vertex shader
#version 120
attribute vec4 position;
void main()
{
gl_Position = position;
}
Fragment shader
#version 120
void main()
{
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
Respectively using Raw string literals (C++11):
std::string vertexShader =
R"(#version 120
attribute vec4 position;
void main()
{
gl_Position = position;
}
)";
std::string fragmentShader =
R"(#version 120
void main()
{
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
)";

Related

First vertex renders at 0 0 instead of specified coordinates glfw

I'm following along with The Cherno's OpenGl tutorial on youtube and learned about shaders and watched and wrote the implementation in c++, but the first vertex for rendering a triangle appears at (0.0f, 0.0f) instead of the specified coordinates, which are (-0.5f, 0.5f) and i have no idea why. I thought that it didn't look right because i didn't write a shader to tell the computer how to handle the vertex data (which are just the coordinates right now). This is a photo of what the triangle looks like:
I'm using GLFW 4.6.0 - Build 31.0.101.2111. This the code that is being run:
#include <iostream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
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) {
int length;
glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);
char* message = (char*) alloca(length * sizeof(char));
glGetShaderInfoLog(id, length, &length, message);
std::cerr << "Failed to compile " <<
(type == GL_VERTEX_SHADER ? "vertex" : type == GL_FRAGMENT_SHADER ? "fragment" : "")
<< " shader!" << '\n';
std::cerr << message << '\n';
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);
return program;
}
int main(void) {
GLFWwindow* window;
if (!glfwInit()) return -1;
window = glfwCreateWindow(840, 680, "Window", NULL, NULL);
if (!window) {
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
if (glewInit() != GLEW_OK) return -1;
std::cout << glGetString(GL_VERSION) << '\n';
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, 2 * sizeof(float), 0);
std::string vertexShader = R"(
#version 330 core
layout(location = 0) in vec4 position;
void main() {
gl_Position = position;
}
)";
std::string fragmentShader =R"(
#version 330 core
layout(location = 0) out vec4 color;
void main() {
color = vec4(1.0, 0.0, 0.0, 1.0);
}
)";
unsigned int shader = CreateShader(vertexShader, fragmentShader);
glUseProgram(shader);
while (!glfwWindowShouldClose(window)) {
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 1, 3);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
Thank you in advance for any help given!
The starting index of the first vertex is 0, but not 1:
glDrawArrays(GL_TRIANGLES, 1, 3);
glDrawArrays(GL_TRIANGLES, 0, 3);

Modern OpenGL 3.3 with glfw window not displaying anything

I just get an empty window without any c++ or opengl errors. And the vao method dosen't work. Im also on a 8 year old laptop, so this also may be the cause.
Here's the code:
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
// shader stuff (do not understand lol)
static unsigned int compile_shader(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);
// ERRORS
int resoult;
glGetShaderiv(id, GL_COMPILE_STATUS, &resoult);
if (resoult == GL_FALSE) {
int lenght;
glGetShaderiv(id, GL_INFO_LOG_LENGTH, &lenght);
char* message = (char*)_malloca(lenght * sizeof(char));
glGetShaderInfoLog(id, lenght, &lenght, message);
std::cout << "ERROR !!! Failded to compile shader !!! ERROR" << (type ==
GL_VERTEX_SHADER ? "vertex" : "fragment") << std::endl;
std::cout << message << std::endl;
glDeleteShader(id);
return 0;
}
return id;
}
static unsigned int create_shader(const std::string& vertex_shader, const std::string&
fragment_shader) {
unsigned int program = glCreateProgram();
unsigned int vs = compile_shader(GL_VERTEX_SHADER, vertex_shader);
unsigned int fs = compile_shader(GL_FRAGMENT_SHADER, fragment_shader);
glAttachShader(program, vs);
glAttachShader(program, fs);
glLinkProgram(program);
glValidateProgram(program);
glDeleteShader(vs);
glDeleteShader(fs);
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(800, 600, "test", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
std::cout << glGetString(GL_VERSION) << std::endl;
// Initializing GLEW
if (glewInit() != GLEW_OK)
std::cout << "Error with glew :((((((" << std::endl;
// buffer for the triangle
float positions[6] = {
-0.5f, -0.5f,
0.0f, -0.5f,
0.5f, -0.5f
};
unsigned int vao;
glGenVertexArrays(1, &vao);
unsigned int buffer;
glGenBuffers(1, &buffer);
glBindVertexArray(vao);
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, (void*)0);
std::string vertex =
"#version 330 core \n"
"layout(location = 0 ) in vec4 position;\n"
"\n"
"void main()\n"
"{\n"
" gl_Position = position;\n"
"}\n";
std::string fragment =
"#version 330 core \n"
"layout(location = 0 ) out vec4 color;\n"
"\n"
"void main()\n"
"{\n"
" color = vec4(1.0, 0.0, 0.0, 1.0);\n"
"}\n";
unsigned int shader = create_shader(vertex, fragment);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);
// making a triangle
glUseProgram(shader);
glBindVertexArray(vao);
glDrawArrays(GL_TRIANGLES, 0, 3);
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glDeleteProgram(shader);
glfwTerminate();
return 0;
}

OpenGL not drawing as expected

I can't seem to get this program to work, I only get a black window instead of the expected red triangle and can't figure out why.
This is the code I have written:
#include "glfw3.h"
#include <iostream>
#include <OpenGL/gl3.h>
static unsigned int CompilaShader (unsigned int tipo, const std::string &source)
{
unsigned int id = glCreateShader (tipo);
const char* src = source.c_str ();
int* risultato;
int lenght;
glShaderSource (id, 1, &src, nullptr); // crea shader
glCompileShader (id); // compila shader
// controllo errori
glGetShaderiv(id, GL_COMPILE_STATUS, risultato);
if (risultato == GL_FALSE)
{
glGetShaderiv(id, GL_INFO_LOG_LENGTH, &lenght);
char* message = (char*) alloca (lenght * sizeof (char));
glGetShaderInfoLog(id, lenght, &lenght, message);
std::cout << "Errore nella compilazione nel shader" << (tipo == GL_VERTEX_SHADER ? "vertex" : "fragment") << std::endl;
std::cout << *message << std::endl;
glDeleteShader (id);
return 0;
}
return id;
};
static unsigned int CreaShader (const std::string &VertexShader, const std::string &FragmentShader)
{
unsigned int programma = glCreateProgram ();
unsigned int vs = CompilaShader(GL_VERTEX_SHADER, VertexShader);
unsigned int fs = CompilaShader(GL_FRAGMENT_SHADER, FragmentShader);
glAttachShader (programma, vs);
glAttachShader (programma, fs);
glLinkProgram (programma);
glValidateProgram (programma);
glDeleteShader (vs);
glDeleteShader (fs);
return programma;
};
int main()
{
GLFWwindow* window;
unsigned int buffer;
float posizioni [6] =
{
0.5f, 0.5f,
0.5f, 0.0f,
0.0f, 0.0f
};
/* 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;
}
std::string vertexshader =
"#version 330 core\n"
"\n"
"in vec4 position;\n"
"\n"
"void main ()\n"
"{\n"
" gl_Position = position;\n"
"}\n";
std::string fragmentshader =
"#version 330 core\n"
"\n"
"out vec4 color;\n"
"\n"
"void main ()\n"
"{\n"
" color = vec4 (1.0, 0.0, 0.0, 1.0)\n"
"}\n";
unsigned int shader = CreaShader (vertexshader, fragmentshader);
glUseProgram(shader);
unsigned int vao;
glGenVertexArrays (1, &vao);
glBindVertexArray (vao);
glGenBuffers(1, &buffer); // crea buffer
glBindBuffer(GL_ARRAY_BUFFER, buffer); // seleziona buffer come array
glBufferData(GL_ARRAY_BUFFER, 6 * sizeof (float), posizioni, GL_STATIC_DRAW); // dati buffer
glEnableVertexAttribArray (0);
glVertexAttribPointer (0, 2, GL_FLOAT, GL_FALSE, sizeof (float) * 2, 0); // crea attributo posizione
/* Make the window's context current */
glfwMakeContextCurrent(window);
/* 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;
}
I get no compilation errors/warnings and no error message from the CompilaShader function.
Can anyone help me understand what is wrong with it?
Working on a MacBook with an NVIDIA GeForce 320M 256 MB, which won't support anything older than OpenGL 3.3, running MacOS 10.13.6 High Sierra, using XCode 9.4.1.

How to fix black screen output in OpenGL 3.3

I am experiencing a black screen in my project using OpenGL and C++. I am in need of assistance as to where I went wrong rendering a red triangle to the screen.
I have tried checking for errors in the vertex and fragment shader.
#include <GL\glew.h>
#include <GL\GL.h>
#include <GLFW\glfw3.h>
#include <iostream>
#include <string>
using namespace std;
unsigned int CreateShader(int type, const string shaderSource) {
unsigned int shader = glCreateShader(type);
const char *src = shaderSource.c_str();
glShaderSource(shader, 1, &src, NULL);
glCompileShader(shader);
int result;
glGetShaderiv(shader, GL_COMPILE_STATUS, &result);
if (result == GL_FALSE) {
int length;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &length);
char *message = (char*)alloca(length * sizeof(char));
glGetShaderInfoLog(shader, length, &length, message);
cout << "Failed to compile " << (type == GL_VERTEX_SHADER ? "vertex shader " : "fragment shader ") << endl;
cout << message << endl;
}
return shader;
}
unsigned int CreateProgram(const string vertexShader, const string fragmentShader) {
unsigned int program = glCreateProgram();
unsigned int vs = CreateShader(GL_VERTEX_SHADER, vertexShader);
unsigned int fs = CreateShader(GL_FRAGMENT_SHADER, fragmentShader);
glAttachShader(program, vs);
glAttachShader(program, fs);
glDeleteShader(vs);
glDeleteShader(fs);
glLinkProgram(program);
glValidateProgram(program);
return program;
}
int main() {
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow *window = glfwCreateWindow(800, 600, "window", NULL, NULL);
glfwMakeContextCurrent(window);
glewInit();
if (glewInit() != GLEW_OK) {
cout << "Glew initialization Failed! " << endl;
glfwTerminate();
return -1;
}
float positions[] = {
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.0f, 0.5f, 0.0f,
};
cout << "Made By Rial Seebran" << endl;
cout << glfwGetVersionString() << endl;
unsigned int VAO;
unsigned int VBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(positions), positions, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
const string vertexShader =
"#version 330 core\n"
"layout (location = 0) in vec3 position;\n"
"void main() {\n"
"gl_Position = vec4(position, 0.0f);\n"
"}\n";
const string fragmentShader =
"#version 330 core\n"
"layout (location = 0) out vec4 color;\n"
"void main() {\n"
"color = vec4(1.0f, 0.0f, 0.0f, 1.0f);\n"
"}\n";
unsigned int program = CreateProgram(vertexShader, fragmentShader);
while (!glfwWindowShouldClose(window)) {
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(program);
glBindVertexArray(VAO);
glDrawArrays(GL_ARRAY_BUFFER, 0, 3);
glBindVertexArray(0);
glfwPollEvents();
glfwSwapBuffers(window);
}
glfwTerminate();
return 0;
}
I'm expecting a red triangle drawn at the coordinates specified with a blueish background.
The first parameter to glDrawArrays has to be the primitive type.
It has to be GL_TRIANGLES rather than GL_ARRAY_BUFFER:
glDrawArrays(GL_ARRAY_BUFFER, 0, 3);
glDrawArrays(GL_TRIANGLES, 0, 3);
The OpenGL enumerator constant GL_ARRAY_BUFFER is not an an accepted value for this parameter and will cause an GL_INVALID_ENUM error (The error can be get by glGetError).
When the clip space coordinate is transformed to normalized device space, then the x, y and z component is divided by the w component.
This means the w component of gl_Position has to be set 1.0 rather the 0.0:
gl_Position = vec4(position, 0.0f);
gl_Position = vec4(position, 1.0);
Glew can enable additional extensions by glewExperimental = GL_TRUE;. See the GLEW documentation which says:
GLEW obtains information on the supported extensions from the graphics driver. Experimental or pre-release drivers, however, might not report every available extension through the standard mechanism, in which case GLEW will report it unsupported. To circumvent this situation, the glewExperimental global switch can be turned on by setting it to GL_TRUE before calling glewInit(), which ensures that all extensions with valid entry points will be exposed.
glewExperimental = GL_TRUE;
if ( glewInit() != GLEW_OK ) {
cout << "Glew initialization Failed! " << endl;
glfwTerminate();
return -1;
}

Black screen output in opengl 3.3

I am trying to draw a red triangle to the screen in OpenGL. glClearColor(x, x, x, 1) works fine and changes the background color. However no triangle appears and no errors show up.
#include <GL\glew.h>
#include <GL\GL.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <string>
using namespace std;
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;
static unsigned int CompileShader(unsigned int type, const 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) {
int length;
glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);
char *message = (char*)alloca(length * sizeof(char));
glGetShaderInfoLog(id, length, &length, message);
cout << "Failed to compile " << (type == GL_VERTEX_SHADER ? "vertex shader " : "fragment shader ") << endl;
cout << message << endl;
}
return id;
}
static unsigned int CreateShader(const string &vertexShader, const 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);
return program;
}
int main() {
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow *window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "window", NULL, NULL);
if (window == NULL) {
cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
cout << glfwGetVersionString() << endl;
glfwMakeContextCurrent(window);
glewInit();
if (glewInit()) {
cout << "Glew initialization failed! " << endl;
return -1;
}
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, sizeof(positions), positions, GL_STATIC_DRAW);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0);
glEnableVertexAttribArray(0);
const string vertexShader =
"#version 330 core\n"
"\n"
"layout (location = 0) in vec4 position;\n"
"\n"
"void main() {\n"
" gl_Position = position;\n"
"}\n";
const string fragmentShader =
"#version 330 core\n"
"\n"
"layout (location = 0) out vec4 color;\n"
"\n"
"void main() \n"
"{\n"
"\n"
" color = vec4(1.0, 0.0, 0.0, 1.0);\n"
"}\n";
unsigned int shader = CreateShader(vertexShader, fragmentShader);
glUseProgram(shader);
while (!glfwWindowShouldClose(window)) {
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0.0f, 0.0f, 0.0f, 1);
glDrawArrays(GL_TRIANGLES, 0, 3);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
You need to use a Vertex Array Object. So, change your program to:
(...)
unsigned int vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
unsigned int buffer;
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(positions), positions, GL_STATIC_DRAW);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0);
glEnableVertexAttribArray(0);
glBindVertexArray(0);
(...)
And, then when rendering:
(...)
glBindVertexArray(vao);
while (!glfwWindowShouldClose(window)) {
(...)
You are missing an Vertex array object which holds those VertexAttrib settings. Also your vertex buffer format is set to vec2 points but the vertex shader expects vec4.
Correct shader:
const string vertexShader =
"#version 330 core\n"
"\n"
"layout (location = 0) in vec2 position;\n"
"\n"
"void main() {\n"
" gl_Position = vec4(position,0.0f,1.0f);\n"
"}\n";
Creating and binding the VAO:
unsigned int buffer;
unsigned int VAO;
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
glGenBuffers(1, &buffer);
You initialized glew twice, not sure if that matters.
glewExperimental = GL_TRUE;//Recommended for compatibility
if (glewInit()!= GLEW_OK) {
cout << "Glew initialization failed! " << endl;
return -1;
}