glDrawElements GL_OUT_OF_MEMORY [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I try to launch next code, but it gives error with code 1285 on line glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr) (Application.cpp).
If I replace preceding line of code with glDrawArrays(GL_TRIANGLES, 0, 3) it draws a triangle, so shader, vertex buffer, vertex array binding works fine. The problem should be in IndexBuffer, but I can't find it.
Renderer.h
#pragma once
#include <GL/glew.h>
#define ASSERT(x) if (!(x)) __debugbreak();
#define GLCall(x) GLClearError();\
x;\
ASSERT(GLLogCall(#x, __FILE__, __LINE__))
void GLClearError();
bool GLLogCall(const char* function, const char* file, int line);
Renderer.cpp
#include "Renderer.h"
#include <iostream>
void GLClearError()
{
while (glGetError() != GL_NO_ERROR);
}
bool GLLogCall(const char* function, const char* file, int line)
{
while (GLenum error = glGetError())
{
std::cout << "[OpenGL Error] (" << error << "): " << function <<
" " << file << ": " << line << std::endl;
return false;
}
return true;
}
VertexBuffer.h
#pragma once
class VertexBuffer
{
private:
unsigned int m_RendererID;
public:
VertexBuffer(const void* data, unsigned int size);
~VertexBuffer();
void Bind() const;
void Unbind() const;
};
VertexBuffer.cpp
#include "VertexBuffer.h"
#include "Renderer.h"
VertexBuffer::VertexBuffer(const void* data, unsigned int size)
{
GLCall(glGenBuffers(1, &m_RendererID));
GLCall(glBindBuffer(GL_ARRAY_BUFFER, m_RendererID));
GLCall(glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW));
}
VertexBuffer::~VertexBuffer()
{
GLCall(glDeleteBuffers(1, &m_RendererID));
}
void VertexBuffer::Bind() const
{
GLCall(glBindBuffer(GL_ARRAY_BUFFER, m_RendererID));
}
void VertexBuffer::Unbind() const
{
GLCall(glBindBuffer(GL_ARRAY_BUFFER, 0));
}
IndexBuffer.h
#pragma once
class IndexBuffer
{
private:
unsigned int m_RendererID;
unsigned int m_Count;
public:
IndexBuffer(const unsigned int* data, unsigned int count);
~IndexBuffer();
void Bind() const;
void Unbind() const;
inline unsigned int GetCount() const { return m_Count; }
};
IndexBuffer.cpp
#include "IndexBuffer.h"
#include "Renderer.h"
IndexBuffer::IndexBuffer(const unsigned int* data, unsigned int count)
: m_Count(count)
{
ASSERT(sizeof(unsigned int) == sizeof(GLuint));
GLCall(glGenBuffers(1, &m_RendererID));
GLCall(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_RendererID));
GLCall(GL_ELEMENT_ARRAY_BUFFER, count * sizeof(unsigned int), data, GL_STATIC_DRAW);
}
IndexBuffer::~IndexBuffer()
{
GLCall(glDeleteBuffers(1, &m_RendererID));
}
void IndexBuffer::Bind() const
{
GLCall(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_RendererID));
}
void IndexBuffer::Unbind() const
{
GLCall(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0));
}
Application.cpp
#pragma once
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include "Renderer.h"
#include "VertexBuffer.h"
#include "IndexBuffer.h"
struct ShaderProgramSource
{
std::string VertexSource;
std::string FragmentSource;
};
static ShaderProgramSource ParseShader(const std::string& filepath)
{
std::fstream stream(filepath);
enum class ShaderType
{
NONE = -1, VERTEX = 0, FRAGMENT = 1
};
std::string line;
std::stringstream ss[2];
ShaderType type = ShaderType::NONE;
while (getline(stream, line))
{
if (line.find("#shader") != std::string::npos)
{
if (line.find("vertex") != std::string::npos)
{
type = ShaderType::VERTEX;
}
else if (line.find("fragment") != std::string::npos)
{
type = ShaderType::FRAGMENT;
}
}
else
{
ss[(int)type] << line << '\n';
}
}
return { ss[0].str(), ss[1].str() };
}
static unsigned int CompileShader(unsigned int type, const std::string& source)
{
GLCall(unsigned int id = glCreateShader(type));
const char* src = source.c_str();
GLCall(glShaderSource(id, 1, &src, nullptr));
GLCall(glCompileShader(id));
int result;
GLCall(glGetShaderiv(id, GL_COMPILE_STATUS, &result));
if (result == GL_FALSE)
{
int length;
GLCall(glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length));
char* message = (char*)alloca(length * sizeof(char));
GLCall(glGetShaderInfoLog(id, length, &length, message));
std::cout << "Failed to compile " << (type == GL_VERTEX_SHADER ? "vertex" : "fragment") << " shader!" << std::endl;
std::cout << message << std::endl;
GLCall(glDeleteShader(id));
return 0;
}
return id;
}
static unsigned int CreateProgram(const std::string& vertexShader, const std::string& fragmentShader)
{
GLCall(unsigned int program = glCreateProgram());
unsigned int vs = CompileShader(GL_VERTEX_SHADER, vertexShader);
unsigned int fs = CompileShader(GL_FRAGMENT_SHADER, fragmentShader);
GLCall(glAttachShader(program, vs));
GLCall(glAttachShader(program, fs));
GLCall(glLinkProgram(program));
GLCall(glValidateProgram(program));
GLCall(glDeleteShader(vs));
GLCall(glDeleteShader(fs));
return program;
}
int main()
{
GLFWwindow* window;
if (!glfwInit())
{
return -1;
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
window = glfwCreateWindow(640, 400, "Hello world", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
if (glewInit() != GLEW_OK)
{
std::cout << "Error while GLEW init!" << std::endl;
}
std::cout << glGetString(GL_VERSION) << std::endl;
{
float positions[] = {
-0.5f, -0.5f, // 0
0.5f, -0.5f, // 1
0.5f, 0.5f, // 2
-0.5f, 0.5f // 3
};
unsigned int indices[] = {
0, 1, 2,
2, 3, 0
};
unsigned int vao;
GLCall(glGenVertexArrays(1, &vao));
GLCall(glBindVertexArray(vao));
VertexBuffer vb(positions, 4 * 2 * sizeof(float));
GLCall(glEnableVertexAttribArray(0));
GLCall(glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0));
IndexBuffer ib(indices, 6);
ShaderProgramSource source = ParseShader("res/shaders/Basic.shader");
unsigned int program = CreateProgram(source.VertexSource, source.FragmentSource);
GLCall(glUseProgram(program));
GLCall(int location = glGetUniformLocation(program, "u_Color"));
ASSERT(location != -1);
GLCall(glUniform4f(location, 0.8f, 0.3f, 0.8f, 1.0f));
GLCall(glBindVertexArray(0));
GLCall(glUseProgram(0));
vb.Unbind();
ib.Unbind();
float r = 0;
float increment = 0.05f;
while (!glfwWindowShouldClose(window))
{
GLCall(glClear(GL_COLOR_BUFFER_BIT));
glClearColor(1, 0, 0, 1);
GLCall(glUseProgram(program));
GLCall(glUniform4f(location, r, 0.3f, 0.8f, 1.0f));
GLCall(glBindVertexArray(vao));
ib.Bind();
// glDrawArrays(GL_TRIANGLES, 0, 3);
GLCall(glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr));
if (r > 1.0f)
{
increment = -0.05f;
}
else if (r < 0.0f)
{
increment = 0.05f;
}
r += increment;
glfwSwapBuffers(window);
glfwPollEvents();
}
GLCall(glDeleteProgram(program));
}
glfwTerminate();
return 0;
}
Basic.shader
#shader vertex
#version 330 core
layout(location = 0) in vec4 position;
void main()
{
gl_Position = position;
}
#shader fragment
#version 330 core
layout(location = 0) out vec4 color;
uniform vec4 u_Color;
void main()
{
color = u_Color;
}

This does not look right. Missing a glBufferData?
GLCall(GL_ELEMENT_ARRAY_BUFFER, count * sizeof(unsigned int), data, GL_STATIC_DRAW);

Related

glGetAttribLocation() returns -1 on the position attribute of the vertex shader

I'm making an OpenGL program. However, whenever I run it, the "vertexIn" attribute of the shader returns negative when getting its location. I've searched several stack overflow posts as well as docs and other websites. Still, I could not find any reason as to why vertexIn returns negative when OpenGL tries to get its location, given that in other working examples, it is used in the same way I use it.
Edited the post to have more code in main so it's able to be reproduced.
Main.cpp
#include <SDL.h>
#include <math.h>
#include "Header Files/Shader.h"
Shader shader;
bool isAppRunning{ true };
SDL_Window* window{ nullptr };
SDL_GLContext context{ nullptr };
GLuint VAO{ 0 };
GLuint EBO{ 0 };
GLuint vertexVBO{ 0 };
GLuint colorVBO{ 0 };
int main(int argc, char* argv[])
{
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS) == -1)
{
std::cout << "SDL did not initialize properly." << std::endl;
}
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_COMPATIBILITY);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 6);
window = SDL_CreateWindow("OpenGL Project", 320, 180, 1280, 720, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
if (!window)
{
std::cout << "OpenGL Window was not created properly." << std::endl;
}
context = SDL_GL_CreateContext(window);
if (!context)
{
std::cout << "OpenGL context could not be created properly! The context is either invalid or not supported by your graphics card! Please try on Another PC!" << std::endl;
}
if (!gladLoaderLoadGL())
{
std::cout << "Error loading OpenGl extensions." << std::endl;
}
Shader::Initialize();
shader.Create("../OpenGl/Shader Files/Shader.vert", "../OpenGl/Shader Files/Shader.frag");
shader.Use();
GLfloat vertices[] = { -0.5f, 0.5f, 0.0f,
0.5f, 0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
-0.5f, -0.5f, 0.0f };
GLfloat colours[] = { 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f,
0.0f, 1.0f, 0.0f,
0.0f, 1.0f, 1.0f };
GLuint indices[] = { 0, 1, 3,
3, 1, 2 };
auto vertexAttributeID = shader.GetAttributeID("vertexIn");
auto colourAttributeID = shader.GetAttributeID("colorIn");
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &vertexVBO);
glGenBuffers(1, &colorVBO);
glGenBuffers(1, &EBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, vertexVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(vertexAttributeID, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(vertexAttributeID);
glBindBuffer(GL_ARRAY_BUFFER, colorVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(colours), colours, GL_STATIC_DRAW);
glVertexAttribPointer(colourAttributeID, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(colourAttributeID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glBindVertexArray(0);
while (isAppRunning)
{
SDL_Event events;
while (SDL_PollEvent(&events))
{
switch (events.type)
{
//close the window
case SDL_QUIT:
{
isAppRunning = false;
break;
}
}
}
glClearColor(191 / 255.0f, 87 / 255.0f, 205 / 255.0f, 255 / 255.0f);
glClear(GL_COLOR_BUFFER_BIT);
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
SDL_GL_SwapWindow(window);
}
glDeleteBuffers(1, &EBO);
glDeleteBuffers(1, &colorVBO);
glDeleteBuffers(1, &vertexVBO);
glDeleteVertexArrays(1, &VAO);
glDisableVertexAttribArray(colourAttributeID);
glDisableVertexAttribArray(vertexAttributeID);
shader.Destroy();
Shader::Shutdown();
SDL_GL_DeleteContext(context);
SDL_DestroyWindow(window);
SDL_Quit;
return 0;
}
Shader.cpp
#include "Header Files/Shader.h"
GLint Shader::vertexShaderID = 0;
GLint Shader::fragmentShaderID = 0;
bool Shader::Initialize()
{
vertexShaderID = glCreateShader(GL_VERTEX_SHADER);
if (vertexShaderID == 0)
{
std::cout << "Error! Vertex Shader could not be created! This could be because of your Graphics Card not supporting modern OpenGL!" << std::endl;
return false;
}
fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
if (fragmentShaderID == 0)
{
std::cout << "Error! Fragment Shader could not be created! This could be because of your Graphics Card not supporting modern OpenGL!" << std::endl;
return false;
}
return true;
}
void Shader::Shutdown()
{
glDeleteShader(vertexShaderID);
glDeleteShader(fragmentShaderID);
}
GLuint Shader::GetUniformID(const std::string& uniform) const
{
auto ID = glGetUniformLocation(programID, uniform.c_str());
assert(ID != -1);
return ID;
}
GLuint Shader::GetAttributeID(const std::string& attribute) const
{
auto ID = glGetAttribLocation(programID, attribute.c_str());
assert(ID != -1);
return ID;
}
bool Shader::Create(const std::string& vertexShaderFilename, const std::string& fragmentShaderFilename)
{
programID = glCreateProgram();
if (programID == 0)
{
std::cout << "Error! Shader Program could not be created! This could be because of your Graphics Card not supporting modern OpenGL!" << std::endl;
return false;
}
if (!CompileShaders(vertexShaderFilename))
{
return false;
}
if (!CompileShaders(fragmentShaderFilename))
{
return false;
}
if (!LinkProgram())
{
return false;
}
return true;
}
bool Shader::SendData(const std::string& uniform, GLint data)
{
glUniform1i(GetUniformID(uniform), data);
return true;
}
bool Shader::SendData(const std::string& uniform, GLuint data)
{
glUniform1ui(GetUniformID(uniform), data);
return true;
}
bool Shader::SendData(const std::string& uniform, GLfloat data)
{
glUniform1f(GetUniformID(uniform), data);
return true;
}
bool Shader::SendData(const std::string& uniform, GLfloat x, GLfloat y)
{
glUniform2f(GetUniformID(uniform), x, y);
return true;
}
bool Shader::SendData(const std::string& uniform, GLfloat x, GLfloat y, GLfloat z)
{
glUniform3f(GetUniformID(uniform), x, y, z);
return true;
}
bool Shader::SendData(const std::string& uniform, GLfloat x, GLfloat y, GLfloat z, GLfloat w)
{
glUniform4f(GetUniformID(uniform), x, y, z, w);
return true;
}
void Shader::Use() const
{
glUseProgram(programID);
}
void Shader::Destroy() const
{
glDeleteProgram(programID);
}
bool Shader::LinkProgram()
{
glAttachShader(programID, vertexShaderID);
glAttachShader(programID, fragmentShaderID);
glLinkProgram(programID);
glDetachShader(programID, vertexShaderID);
glDetachShader(programID, fragmentShaderID);
GLint linkResult = 0;
glGetProgramiv(programID, GL_LINK_STATUS, &linkResult);
if (linkResult == GL_FALSE)
{
GLchar error[1000];
GLsizei bufferSize = 1000;
glGetProgramInfoLog(programID, bufferSize, &bufferSize, error);
std::cout << "Error! Program did not link successfully! Error information down below: " << std::endl;
std::cout << error << std::endl;
return false;
}
std::cout << "Program Linked Successfully!" << std::endl;
return true;
}
bool Shader::CompileShaders(const std::string& filename)
{
std::ifstream file(filename);
//file.open(filename);
if (!file)
{
std::cout << "Error opening \"" + (filename)+"\"! File non - existent or unreachable! Make sure to check the spelling or the path!" << std::endl;
return false;
}
std::string line;
std::string sourceCode;
while (!file.eof())
{
std::getline(file, line);
sourceCode += line + "\n";
}
//file.close();
auto shaderID = (filename.find(".vert")) ? vertexShaderID : fragmentShaderID;
const GLchar* finalCode = reinterpret_cast<const GLchar*>(sourceCode.c_str());
//bind shader code with object
glShaderSource(shaderID, 1, &finalCode, nullptr);
//compile shader code
glCompileShader(shaderID);
GLint compileResult;
glGetShaderiv(shaderID, GL_COMPILE_STATUS, &compileResult);
if (compileResult == GL_FALSE)
{
GLchar error[1000];
GLsizei bufferSize = 1000;
glGetShaderInfoLog(shaderID, bufferSize, &bufferSize, error);
std::cout << "Error! Shader did not compile successfully! Error information down below: " << std::endl;
std::cout << error << std::endl;
return false;
}
std::cout << "Shader compiled successfully!" << std::endl;
return true;
}
Shader.h
#pragma once
#include <iostream>
#include <assert.h>
#include <fstream>
#include <string>
#include "gl.h"
class Shader
{
public:
static bool Initialize();
static void Shutdown();
GLuint GetUniformID(const std::string& uniform) const;
GLuint GetAttributeID(const std::string& attribute) const;
bool Create(const std::string& vertexShaderFilename, const std::string& fragmentShaderFilename);
bool SendData(const std::string& uniform, GLint data);
bool SendData(const std::string& uniform, GLuint data);
bool SendData(const std::string& uniform, GLfloat data);
bool SendData(const std::string& uniform, GLfloat x, GLfloat y);
bool SendData(const std::string& uniform, GLfloat x, GLfloat y, GLfloat z);
bool SendData(const std::string& uniform, GLfloat x, GLfloat y, GLfloat z, GLfloat w);
void Use() const;
void Destroy() const;
private:
static GLint vertexShaderID;
static GLint fragmentShaderID;
GLuint programID{ 0 };
bool LinkProgram();
bool CompileShaders(const std::string& filename);
};
Shader.vert
#version 460
in vec3 vertexIn;
in vec3 colorIn;
out vec3 colorOut;
void main(void)
{
colorOut = colorIn;
gl_Position = vec4(vertexIn, 1.0);
}
Shader.frag
#version 460
in vec3 colorOut;
out vec4 pixelColor;
void main(void)
{
pixelColor = vec4(colorOut, 1.0);
}

x.string Unhandled exception at 0x0014A25D

I am using Visual 2019 with Visual C++. I was following the tutorial by The Cherno. I am stuck on the end of the Eight Episode. What is causing this?
main.cpp:
#include <gl/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <fstream>
#include <sstream>
struct ShaderProgramSources
{
std::string VertexSource;
std::string FragmentSource;
};
static ShaderProgramSources ParseShader(const std::string& filepath)
{
std::ifstream stream(filepath);
enum class shaderType {
NONE = -1, VERTEX = 0, FRAGMENT = 1
};
std::string line;
std::stringstream ss[1];;
shaderType type = shaderType::NONE;
while (getline(stream, line))
{
if (line.find("#shader") != std::string::npos)
{
if (line.find("vertex") != std::string::npos)
shaderType type = shaderType::VERTEX;
else if (line.find("fragment") != std::string::npos)
shaderType type = shaderType::FRAGMENT;
}
else
{
ss[(int)type] << line << "\n";
}
}
return { ss[0].str(), ss[1].str() };
}
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)
{
int legnth;
glGetShaderiv(id, GL_INFO_LOG_LENGTH, &legnth);
char* message = (char*)alloca(legnth * sizeof(char));
glGetShaderInfoLog(id, legnth, &legnth, message);
std::cout << "Failed to Compile" << (type == GL_VERTEX_SHADER ? "Vertex: Shader:\n" : "Fragment Shader:\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);
return program;
}
int main(void)
{
GLFWwindow* window;
if (!glewInit())
return -1;
/* Initialize the library */
if (!glfwInit())
return -1;
window = glfwCreateWindow(640, 480, "Cooked Pixel", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
if (glewInit() != GLEW_OK)
{
std::cout << "Error: GLEW is not initialized \n";
}
std::cout << "GL Version: " << glGetString(GL_VERSION) << "\n";
float positions[6] = {
-0.5, -0.5,
0.0, -0.5,
0.5, 0.5
};
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 = R"glsl(
#version 330 core
layout(location = 0) in vec4 position;
void main()
{
gl_Position = position;
}
)glsl";
std::string fragmentShader = R"glsl(
#version 330 core
layout(location = 0) out vec4 color;
void main()
{
color = vec4(1.0, 0.0, 0.0, 1.0);
}
)glsl";
ShaderProgramSources source = ParseShader("res/shaders/basic.shader");
std::cout << source.FragmentSource << "\n";
std::cout << source.VertexSource << "\n";
unsigned int shader = CreateShader(vertexShader, fragmentShader);
glUseProgram(shader);
glfwMaximizeWindow(window);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, 3);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
I get the error on x.string line 433 as:
Unhandled exception at 0x00CEA25D in CookedPixel.exe: 0xC0000005: Access violation reading location 0xCCCCCCD0. occurred
You are trying to access memory which hasn't been initialized yet or there is an out-of-bound memory error. For more link1 link2. Anyway, I see there's only 178 line in the referred cpp file, not sure how you got the error on line 433.
You have at least one error. It will become very obvious when you closely look at ParseShader. You declare a variable
std::stringstream ss[1];
That's an array with just one element. Later you access it like this:
ss[(int)type] << line << "\n";
This will cause undefined behavior (that can manifest as crash) when (int)type is anything but 0. At then end you get UB anyway:
return { ss[0].str(), ss[1].str() };
is an obvious out-of-bounds access.
However, that's just from first glance. This seems like a good time to learn What a debugger is and how it can help you. Using a debugger is a very important skill.

Compiler gives SyntaxErrors, which are not shown in VisualStudio. What can I do? [duplicate]

This question already has answers here:
Resolve build errors due to circular dependency amongst classes
(12 answers)
Closed 2 years ago.
I'm about to teach myself OpenGL in C++ and VisualStudio says my Code is OK. But if I compile the Code I get many Syntax errors. I really dont understand it.
Here is the Error Message:
1>------ Erstellen gestartet: Projekt: OpenGL, Konfiguration: Release Win32 ------
1>Application.cpp
1>C:\Users\schko\source\repos\OpenGL\OpenGL\src\Engine.h(50,25): error C2061: Syntaxfehler: Bezeichner "Renderer"
1>C:\Users\schko\source\repos\OpenGL\OpenGL\src\Application.cpp(35,33): error C2660: "Engine::gameLoop": Funktion akzeptiert keine 3 Argumente
1>C:\Users\schko\source\repos\OpenGL\OpenGL\src\Engine.h(50,7): message : Siehe Deklaration von "Engine::gameLoop"
1>Buffers.cpp
1>C:\Users\schko\source\repos\OpenGL\OpenGL\src\Renderer.h(13,26): error C3646: "parseShader": Unbekannter Überschreibungsspezifizierer
1>C:\Users\schko\source\repos\OpenGL\OpenGL\src\Renderer.h(13,26): error C2059: Syntaxfehler: "("
1>C:\Users\schko\source\repos\OpenGL\OpenGL\src\Renderer.h(13,54): error C2238: Unerwartete(s) Token vor ";"
1>C:\Users\schko\source\repos\OpenGL\OpenGL\src\Renderer.h(22,39): error C2061: Syntaxfehler: Bezeichner "GameVars"
1>Engine.cpp
1>C:\Users\schko\source\repos\OpenGL\OpenGL\src\Renderer.h(13,26): error C3646: "parseShader": Unbekannter Überschreibungsspezifizierer
1>C:\Users\schko\source\repos\OpenGL\OpenGL\src\Renderer.h(13,26): error C2059: Syntaxfehler: "("
1>C:\Users\schko\source\repos\OpenGL\OpenGL\src\Renderer.h(13,54): error C2238: Unerwartete(s) Token vor ";"
1>C:\Users\schko\source\repos\OpenGL\OpenGL\src\Renderer.h(22,39): error C2061: Syntaxfehler: Bezeichner "GameVars"
1>C:\Users\schko\source\repos\OpenGL\OpenGL\src\Engine.cpp(72,12): error C2660: "Renderer::render": Funktion akzeptiert keine 2 Argumente
1>C:\Users\schko\source\repos\OpenGL\OpenGL\src\Renderer.h(22,7): message : Siehe Deklaration von "Renderer::render"
1>Renderer.cpp
1>C:\Users\schko\source\repos\OpenGL\OpenGL\src\Engine.h(50,25): error C2061: Syntaxfehler: Bezeichner "Renderer"
1>Die Erstellung des Projekts "OpenGL.vcxproj" ist abgeschlossen -- FEHLER.
========== Erstellen: 0 erfolgreich, 1 fehlerhaft, 0 aktuell, 0 übersprungen ==========
And this is my Code...
Application.cpp:
#include "Buffers.h"
#include "Renderer.h"
#include "Engine.h"
int main()
{
float vertices[] =
{
-0.5f, -0.5f, // 0
0.5f, -0.5f, // 1
0.5f, 0.5f, // 2
-0.5f, 0.5f // 3
};
unsigned int indices[] =
{
0, 1, 2,
2, 3, 0
};
Buffers buff(vertices, indices, 4 * 2, 6);
Renderer r;
Engine engine;
if (engine.err != 0)
return -1;
float g = 0.0f;
float b = 1.0f;
float iG = 0.05f;
float iB = -0.05f;
GameVars vars = { 0.0f, g, b, 1.0f };
engine.gameLoop(r, buff, engine);
while (!engine.terminated)
{
if (g > 1.0f)
iG = -0.005f;
else if (g < 0.0f)
iG = 0.005f;
if (b > 1.0f)
iB = -0.005f;
else if (b < 0.0f)
iB = 0.005f;
g += iG;
b += iB;
engine.setVars({ 0.0f, g, b, 1.0f });
}
buff.~Buffers();
r.~Renderer();
engine.~Engine();
return 0;
}
Engine.cpp:
#include "Engine.h"
void GLClearError()
{
while (glGetError() != GL_NO_ERROR);
}
bool GLLogCall(const char* function, const char* file, int line)
{
while (GLenum error = glGetError())
{
std::cout << "[OpenGL Error] (" << error << ") at " << function << " in " << file << " on line " << line << std::endl;
return false;
}
return true;
}
Engine::Engine()
{
err = init();
terminated = false;
std::cout << glGetString(GL_VERSION) << std::endl;
}
Engine::~Engine()
{
glfwTerminate();
}
int Engine::init()
{
if (!glfwInit())
{
std::cout << "Init of GLFW failed!" << std::endl;
return -1;
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
window = glfwCreateWindow(1020, 760, "MyLearningApplication", NULL, NULL);
if (!window)
{
std::cout << "Creating the Window failed!" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
if (glewInit() != GLEW_OK)
{
std::cout << "Init of GLEW failed!" << std::endl;
return -1;
}
return 0;
}
void Engine::gameLoop(Renderer renderer, Buffers buffer, Engine This)
{
std::thread thread([](Renderer renderer, Buffers buffer, Engine eng) {
while (!glfwWindowShouldClose(eng.getWindow()))
{
GLCall(glClear(GL_COLOR_BUFFER_BIT));
renderer.render(buffer, eng.getVars());
glfwSwapBuffers(eng.getWindow());
glfwPollEvents();
}
eng.terminated = true;
}, renderer, buffer, This);
}
Engine.h:
#pragma once
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include "Buffers.h"
#include "Renderer.h"
#include <iostream>
#include <string>
#include <thread>
#define ASSERT(x) if (!(x)) __debugbreak();
#define GLCall(x)\
GLClearError();\
x;\
ASSERT(GLLogCall(#x, __FILE__, __LINE__));
void GLClearError();
bool GLLogCall(const char* function, const char* file, int line);
struct ShaderSource
{
std::string VertexSource;
std::string FragmentSource;
};
struct GameVars
{
float r;
float g;
float b;
float alpha;
};
class Engine
{
private:
GLFWwindow* window;
GameVars vars;
int init();
public:
int err;
bool terminated;
Engine();
~Engine();
void gameLoop(Renderer renderer, Buffers buffer, Engine This);
inline GLFWwindow* getWindow() const { return window; }
inline GameVars getVars() const { return vars; }
inline void setVars(GameVars newVars) { vars = newVars; }
};
Renderer.cpp:
#include "Renderer.h"
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
Renderer::Renderer()
{
ShaderSource source = parseShader("res/shaders/basic.shader");
m_Shader1 = createShader(source.VertexSource, source.FragmentSource);
GLCall(glUseProgram(m_Shader1));
GLCall(m_uVar1 = glGetUniformLocation(m_Shader1, "u_Color"));
GLCall(glUseProgram(0));
}
Renderer::~Renderer()
{
GLCall(glDeleteProgram(m_Shader1));
}
ShaderSource Renderer::parseShader(const std::string filePath)
{
std::ifstream stream(filePath);
enum class ShaderType
{
NONE = -1, VERTEX = 0, FRAGMENT = 1
};
std::string line;
std::stringstream ss[2];
ShaderType type = ShaderType::NONE;
while (getline(stream, line))
{
if (line.find("#shader") != std::string::npos)
{
if (line.find("vertex") != std::string::npos)
type = ShaderType::VERTEX;
else if (line.find("fragment") != std::string::npos)
type = ShaderType::FRAGMENT;
}
else
{
ss[(int)type] << line << "\n";
}
}
return { ss[0].str(), ss[1].str() };
}
unsigned int Renderer::compileShader(unsigned int type, std::string source)
{
GLCall(unsigned int id = glCreateShader(type));
const char* src = source.c_str();
GLCall(glShaderSource(id, 1, &src, nullptr));
GLCall(glCompileShader(id));
int result;
GLCall(glGetShaderiv(id, GL_COMPILE_STATUS, &result));
if (result == GL_FALSE)
{
int length;
GLCall(glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length));
char* msg = (char*)alloca(length * sizeof(char));
GLCall(glGetShaderInfoLog(id, length, &length, msg));
std::cout << "Failed to compile " << (type == GL_VERTEX_SHADER ? "vertex" : "fragment") << "shader!" << std::endl;
std::cout << msg << std::endl;
return 0;
}
return id;
}
unsigned int Renderer::createShader(std::string vertexShader, std::string fragmentShader)
{
GLCall(unsigned int program = glCreateProgram());
unsigned int vs = compileShader(GL_VERTEX_SHADER, vertexShader);
unsigned int fs = compileShader(GL_FRAGMENT_SHADER, fragmentShader);
GLCall(glAttachShader(program, vs));
GLCall(glAttachShader(program, fs));
GLCall(glLinkProgram(program));
GLCall(glValidateProgram(program));
GLCall(glDeleteShader(vs));
GLCall(glDeleteShader(fs));
return program;
}
Buffers Renderer::createBuffer(const void* vertices, const unsigned int* indices, unsigned int vertexCount, unsigned int indexCount)
{
Buffers newB(vertices, indices, vertexCount, indexCount);
return newB;
}
void Renderer::render(Buffers buffer, GameVars vars)
{
buffer.bind();
GLCall(glUseProgram(m_Shader1));
GLCall(glUniform4f(m_uVar1, vars.r, vars.g, vars.b, vars.alpha));
GLCall(glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr));
GLCall(glUseProgram(0));
buffer.unbind();
}
Renderer.h:
#pragma once
#include "Engine.h"
#include "Buffers.h"
class Renderer
{
private:
unsigned int m_Shader1;
unsigned int m_uVar1;
ShaderSource parseShader(const std::string filePath);
unsigned int compileShader(unsigned int type, std::string source);
unsigned int createShader(std::string vertexShader, std::string fragmentShader);
public:
Renderer();
~Renderer();
Buffers createBuffer(const void* vertices, const unsigned int* indices, unsigned int vertexCount, unsigned int indexCount);
void render(Buffers buffer, GameVars vars);
};
Buffers.cpp:
#include "Buffers.h"
#include "Engine.h"
Buffers::Buffers(const void* vertices, const unsigned int* indices, unsigned int vertexCount, unsigned int indexCount)
: m_iCount(indexCount)
{
ASSERT(sizeof(unsigned int) == sizeof(GLuint));
GLCall(glGenVertexArrays(1, &m_vaoID));
GLCall(glBindVertexArray(m_vaoID));
GLCall(glGenBuffers(1, &m_vBuffID));
GLCall(glBindBuffer(GL_ARRAY_BUFFER, m_vBuffID));
GLCall(glBufferData(GL_ARRAY_BUFFER, vertexCount * sizeof(float), vertices, GL_STATIC_DRAW));
GLCall(glEnableVertexAttribArray(0));
GLCall(glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), 0));
GLCall(glGenBuffers(1, &m_iBuffID));
GLCall(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_iBuffID));
GLCall(glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexCount * sizeof(GLuint), indices, GL_STATIC_DRAW));
GLCall(glBindBuffer(GL_ARRAY_BUFFER, 0));
}
Buffers::~Buffers()
{
GLCall(glDeleteBuffers(1, &m_iBuffID));
GLCall(glDeleteBuffers(1, &m_vBuffID));
GLCall(glDeleteVertexArrays(1, &m_vaoID));
}
void Buffers::bind() const
{
GLCall(glBindVertexArray(m_vaoID));
GLCall(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_iBuffID));
}
void Buffers::unbind() const
{
GLCall(glBindVertexArray(0));
GLCall(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0));
}
Buffers.h:
#pragma once
class Buffers
{
private:
unsigned int m_iBuffID;
unsigned int m_vBuffID;
unsigned int m_vaoID;
unsigned int m_iCount;
public:
Buffers(const void* vertices, const unsigned int* indices, unsigned int vertexCount, unsigned int indexCount);
~Buffers();
void bind() const;
void unbind() const;
inline unsigned int getIndexCount() { return m_iCount; }
};
Shader was working fine.
This error occurred because you did not installed library " " graphics.h " properly..
.
You can download it from https://sourceforge.net/projects/lib-graph/

OpenGL simple mesh doesnt show

for the last two days I've been watching following (really great and informative) tutorial series: http://www.youtube.com/playlist?list=PLEETnX-uPtBXT9T-hD0Bj31DSnwio-ywh
Last night I finished the part about meshes and now a red triangle is supposed to appear on my window, but it just doesn't. I don't know if it is a problem with my mesh or with my shader class (for both of them I don't get any errors). I use GLEW 1.10.0 for loading OpenGL, GLM 0.9.5.4 for OpenGL math stuff and SDL 2.0.3 for window stuff. Everything running on Windows 8.1 in Visual Studio 2013 Ultimate with the latest Nvidia graphics drivers.
Edit: I forgot to mention that the screen doesn't stay black, so the display.Clear method works. I hope thats any help.
Here are my shader and mesh files:
Shader.h:
#pragma once
#include <iostream>
#include <fstream>
#include <string>
#include <glew\glew.h>
using namespace std;
class Shader
{
public:
Shader(const string& fileName);
virtual ~Shader();
void Bind();
private:
string LoadShader(const string& fileName);
void CheckShaderError(GLuint shader, GLuint flag, bool isProgram, const string& errorMessage);
GLuint CreateShader(const string& text, GLenum shaderType);
static const unsigned int NUM_SHADERS = 2;
GLuint m_program;
GLuint m_shaders[NUM_SHADERS];
};
Shader.cpp
#include "Shader.h"
Shader::Shader(const string& fileName)
{
m_program = glCreateProgram();
m_shaders[0] = CreateShader(LoadShader(fileName + ".vert"), GL_VERTEX_SHADER);
m_shaders[1] = CreateShader(LoadShader(fileName + ".frag"), GL_FRAGMENT_SHADER);
for (unsigned int i = 0; i < NUM_SHADERS; i++)
glAttachShader(m_program, m_shaders[i]);
glLinkProgram(m_program);
CheckShaderError(m_program, GL_LINK_STATUS, true, "Error: Program linking failed");
glValidateProgram(m_program);
CheckShaderError(m_program, GL_VALIDATE_STATUS, true, "Error: Program is invalid");
}
Shader::~Shader()
{
for (unsigned int i = 0; i < NUM_SHADERS; i++)
{
glDetachShader(m_program, m_shaders[i]);
glDeleteShader(m_shaders[i]);
}
glDeleteProgram(m_program);
}
string Shader::LoadShader(const string& fileName)
{
ifstream file;
file.open(fileName.c_str());
string line;
string output;
if (file.is_open())
{
while (file.good())
{
getline(file, line);
output += (line + "\n");
}
}
else
{
cerr << "Unable to load shader: " << fileName << endl;
}
return output;
}
GLuint Shader::CreateShader(const string& text, GLenum shaderType)
{
GLuint shader = glCreateShader(shaderType);
if (shader == 0)
cerr << "Error: Shader creation failed!";
const GLchar* shaderSourceStrings[1] = { text.c_str() };
GLint shaderSourceStringLengths[1] = { text.length() };
glShaderSource(shader, 1, shaderSourceStrings, shaderSourceStringLengths);
glCompileShader(shader);
CheckShaderError(shader, GL_COMPILE_STATUS, false, "Error: Shader compilation failed");
return shader;
}
void Shader::Bind()
{
glUseProgram(m_program);
}
void Shader::CheckShaderError(GLuint shader, GLuint flag, bool isProgram, const string& errorMessage)
{
GLint success = 0;
GLchar error[1024] = { 0 };
if (isProgram)
glGetProgramiv(shader, flag, &success);
else
glGetShaderiv(shader, flag, &success);
if (success == GL_FALSE)
{
if (isProgram)
glGetProgramInfoLog(shader, sizeof(error), NULL, error);
else
glGetShaderInfoLog(shader, sizeof(error), NULL, error);
cerr << errorMessage << ": '" << error << "'" << endl;
}
}
BasicShader.vert:
#version 420 core
attribute vec3 position;
void main()
{
gl_Position = vec4(position, 1.0);
}
BasicShader.frag:
#version 420 core
void main()
{
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
Mesh.h:
#pragma once
#include <glew\glew.h>
#include "Vertex.h"
class Mesh
{
public:
Mesh(Vertex* vertecies, unsigned int numVertecies);
virtual ~Mesh();
void Draw();
private:
enum
{
POSITION_VB,
NUM_BUFFERS
};
GLuint m_vertexArrayObject;
GLuint m_vertexArrayBuffers[NUM_BUFFERS];
unsigned int m_drawCount;
};
Mesh.cpp:
#include "Mesh.h"
Mesh::Mesh(Vertex* vertecies, unsigned int numVertecies)
{
m_drawCount = numVertecies;
glGenVertexArrays(1, &m_vertexArrayObject);
glBindVertexArray(m_vertexArrayObject);
glGenBuffers(NUM_BUFFERS, m_vertexArrayBuffers);
glBindBuffer(GL_ARRAY_BUFFER, m_vertexArrayBuffers[POSITION_VB]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertecies[0]) * numVertecies, vertecies, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glBindVertexArray(0);
}
Mesh::~Mesh()
{
glDeleteBuffers(NUM_BUFFERS, m_vertexArrayBuffers);
glDeleteVertexArrays(1, &m_vertexArrayObject);
}
void Mesh::Draw()
{
glBindVertexArray(m_vertexArrayObject);
glDrawArrays(GL_TRIANGLES, 0, m_drawCount);
glBindVertexArray(0);
}
Vertex.h:
#pragma once
#include <glm\glm.hpp>
using namespace glm;
class Vertex
{
public:
Vertex(const vec3& pos);
virtual ~Vertex();
private:
vec3 m_pos;
};
Vertex.cpp:
#include "Vertex.h"
Vertex::Vertex(const vec3& pos)
{
m_pos = pos;
}
Vertex::~Vertex()
{
}
main.cpp:
#include "Display.h"
#include "Shader.h"
#include "Mesh.h"
using namespace std;
int main(int argc, char** argv)
{
Display display(800, 600, "Hello World");
Vertex vertecies[] =
{
Vertex(vec3(-0.5, -0.5, 0)),
Vertex(vec3(0, 0.5, 0)),
Vertex(vec3(0.5, -0.5, 0))
};
Mesh mesh(vertecies, sizeof(vertecies) / sizeof(vertecies[0]));
Shader shader(".\\res\\BasicShader");
while (!display.IsClosed())
{
display.Clear(0.0f, 0.15f, 0.3f, 1.0f);
shader.Bind();
mesh.Draw();
display.Update();
}
return 0;
}
I'd be very grateful for any help...
Try changing your vertex program to:
#version 420 core
layout(location = 0) in vec3 position;
void main()
{
gl_Position = vec4(position, 1.0);
}
and your fragment program to:
#version 420 core
out vec4 frag;
void main()
{
frag = vec4(1.0, 0.0, 0.0, 1.0);
}

OpenGL - Triangle with a shader isn't showing

This program builds with no problem, and the executable starts up, but no triangle shows up. I am following a GLSL tutorial where a Shader class is made to handle GLSL files.
Shader.h
#ifndef SHADER_H_
#define SHADER_H_
#include <GL/glew.h>
#include <GL/glfw.h>
#include <string>
class Shader {
public:
Shader();
Shader(const char *vsFile, const char *fsFile);
~Shader();
void init(const char *vsFile, const char *fsFile);
void bind();
void unbind();
unsigned int id();
private:
unsigned int shader_id;
unsigned int shader_vp;
unsigned int shader_fp;
};
#endif // SHADER_H_
Shader.cpp
#include "Shader.h"
#include <cstring>
#include <iostream>
#include <ftream>
#include <cstdlib>
using namespace std;
static char* textFileRead(const char *fileName) {
char* text;
if (fileName != NULL) {
FILE *file = fopen(fileName, "rt");
if (file != NULL) {
fseek(file, 0, SEEK_END);
int count = ftell(file);
rewind(file);
if (count > 0) {
text = (char*)malloc(sizeof(char) * (count + 1));
count = fread(text, sizeof(char), count, file);
text[count] = '\0';
}
fclose(file);
}
}
return text;
}
Shader::Shader() {}
Shader::Shader(const char *vsFile, const char *fsFile)
{
init(vsFile, fsFile);
}
void Shader::init(const char *vsFile, const char *fsFile)
{
shader_vp = glCreateShader(GL_VERTEX_SHADER);
shader_fp = glCreateShader(GL_FRAGMENT_SHADER);
const char *vsText = textFileRead(vsFile);
const char *fsText = textFileRead(fsFile);
if (vsText == NULL || fsText == NULL)
{
cerr << "Either vertex shader or fragment shader file is not found" << endl;
return;
}
glShaderSource(shader_vp, 1, &vsText, 0);
glShaderSource(shader_fp, 1, &fsText, 0);
glCompileShader(shader_vp);
glCompileShader(shader_fp);
shader_id = glCreateProgram();
glAttachShader(shader_id, shader_fp);
glAttachShader(shader_id, shader_vp);
glLinkProgram(shader_id);
}
Shader::~Shader()
{
glDetachShader(shader_id, shader_fp);
glDetachShader(shader_id, shader_vp);
glDeleteShader(shader_fp);
glDeleteShader(shader_vp);
glDeleteShader(shader_id);
}
unsigned int Shader::id()
{
return shader_id;
}
void Shader::bind()
{
glUseProgram(shader_id);
}
void Shader::unbind()
{
glUseProgram(0);
}
Main.cpp
#include "Shader.h"
#include <cstdlib>
#include <iostream>
using namespace std;
Shader shader;
void init()
{
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
shader.init("shader.vert", "shader.frag");
}
void resize(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, (GLfloat)w / (GLfloat)h, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
}
int main()
{
int running = GL_TRUE;
// init GLFW
if (!glfwInit())
exit(EXIT_FAILURE);
if (!glfwOpenWindow(300, 300, 0, 0, 0, 0, 0, 0, GLFW_WINDOW))
{
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwSetWindowTitle("ohhai.");
glfwSetWindowSizeCallback(resize);
/* CHECK GLEW */
GLenum err = glewInit();
if (GLEW_OK != err)
{
/* Problem: glewInit failed, something is seriously wrong. */
cout << "Error: " << glewGetErrorString(err) << endl;
}
cout << "Status: Using GLEW " << glewGetString(GLEW_VERSION) << endl;
if (!GLEW_ARB_vertex_buffer_object)
{
cerr << "VBO not supported\n";
exit(1);
}
init();
while (running)
{
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glShadeModel(GL_SMOOTH);
shader.bind();
glBegin(GL_TRIANGLES);
//glColor3f(0.2f, 0.5f, 0.54f);
glVertex2f(0.0f, 0.5f);
//glColor3f(0.75f, 0.8f, 0.1f);
glVertex2f(-.5f, -.5f);
//glColor3f(0.0f, 0.9f, 0.2f);
glVertex2f(0.5f, -0.5f);
glEnd();
shader.unbind();
glfwSwapBuffers();
running = !glfwGetKey(GLFW_KEY_ESC) && glfwGetWindowParam(GLFW_OPENED);
}
glfwTerminate();
exit(EXIT_SUCCESS);
}
shader.vert
void main()
{
// set the posistion of the current matrix
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}
shader.frag
void main(void)
{
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
Again, when this compiles under g++, it goes through fine, but no triangle was shown.
I haven't found the part where you setup the camera (modelview matrix) with gluLookAt or glTranslate/glRotate/glScale. So you use the default, corresponding to a camera at the origin and looking into -z, thus your triangle (which lies in the z=0 plane) is behind the near plane.