I am going through an OpenGL book trying to get the examples to work. I am using Glew, Glfw, and that's it beyond Opengl 4.3, which my I know my graphics card supports completely via the OpenGL Extension viewer.
The problem is that the triangle displays in the window, but it flashes at a fluctuating rate between black/background and completely white. I am drawing the triangle with a red, green, and a blue point. My thoughts were that the positionData were too close to the camera (floating point errors/clipping). What could be the source of this error. I've never successfully gotten modern ( 3.X or greater ) OpenGL to work. The setup is really intense.
Here is the main.cpp:
#include "shaders\tools.h"
//#define GLEW_STATIC
#define GLFW_DLL //Required for linking
#define GLFW_INCLUDE_NONE //Load our own opengl
#include "glfw\glfw3.h"
using namespace std;
int main() {
//Use glfw to create window
if (!glfwInit())
{
cout << "Error: could not init glfw" << endl;
return 1;
}
GLFWwindow* window = glfwCreateWindow(640, 480, "Shader Demo", nullptr, nullptr);
if (window == nullptr)
{
cout << "failure creating window!" << endl;
glfwTerminate();
return 1;
}
glfwMakeContextCurrent(window);
GLenum glew_err = glewInit();
if (glew_err != GLEW_OK)
{
cout << "Glew Error: " << glewGetErrorString(glew_err) << endl;
return 1;
}
GLuint vertShader_i;
GLuint fragShader_i;
bool shader_compile_error = compileShaders( &vertShader_i, &fragShader_i, "basic.vert", "basic.frag");
if (shader_compile_error)
{
cout << "shader compile error!" << endl;
return 1;
}
GLuint glslProgram_i;
bool shader_link_error = linkShaders( &glslProgram_i, vertShader_i, fragShader_i );
if (shader_link_error)
{
cout << "shader link error!" << endl;
return 1;
}
else
{
glUseProgram( glslProgram_i );
};
float positionData[] =
{
-.8f, -.8f, 0.f,
.8f, -.8f, 0.f,
.0f, .8f, 0.f
};
float colorData[] =
{
1.f, 1.f, 1.f,
0.f, 1.f, 0.f,
0.f, 0.f, 1.f
};
//Create buffer objects
GLuint vboHandles[2];
glGenBuffers(2, vboHandles);
GLuint positionBufferHandle = vboHandles[0];
GLuint colorBufferHandle = vboHandles[1];
//Populate the position buffer
glBindBuffer( GL_ARRAY_BUFFER, positionBufferHandle );
glBufferData( GL_ARRAY_BUFFER, sizeof(positionData), positionData, GL_STATIC_DRAW );
// color buffer
glBindBuffer( GL_ARRAY_BUFFER, colorBufferHandle );
glBufferData( GL_ARRAY_BUFFER, sizeof(colorData), colorData, GL_STATIC_DRAW );
GLuint vaoHandle;
glGenVertexArrays( 1, &vaoHandle );
glBindVertexArray( vaoHandle );
//Enable vertex attribute arrays
glEnableVertexAttribArray(0); //vertexPosition
glEnableVertexAttribArray(1); //vertexColor
//map index 0 to position buffer
glBindBuffer( GL_ARRAY_BUFFER, positionBufferHandle );
glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
// map index 1 to color buffer
glBindBuffer( GL_ARRAY_BUFFER, colorBufferHandle );
glVertexAttribPointer( 1, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
glBindVertexArray( vaoHandle );
glDrawArrays( GL_TRIANGLES, 0, 3);
//show version of opengl
// glVersionData gl_version_data;
// getGLVersion( &gl_version_data );
// glDisplayVersionData( gl_version_data );
while (!glfwWindowShouldClose(window))
{
// Keep running
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
basic.vert:
#version 430
layout (location = 0) in vec3 VertexPosition;
layout (location = 1) in vec3 VertexColor;
out vec3 Color;
void main()
{
Color = VertexColor;
gl_Position = vec4( VertexPosition, 1.0 );
}
and basic.frag:
#version 430
in vec3 Color;
out vec4 FragColor;
void main()
{
Color = vec4( Color, 1.0 );
}
I can include the "shaders\tool.h" which defines loadShaderCode
, compileShaders
, and linkShaders
if necessary, but I don't want to post too much code to break the flow of my question. They do what you'd expect, and I don't get any errors returned. They load the codes, link them attach them to a program, compile the program and spit out the glsl programHandle.
I'm on windows x64 using g++ 4.8.1. with C++ 11.
EDIT: here is the shader loader function:
const GLchar * loadShaderCode(const char * shaderFilename)
{
fstream shaderFile( shaderFilename, std::ios::in | std::ios::ate);
streampos end = shaderFile.tellg();
shaderFile.seekg(0, std::ios::beg );
streampos beg = shaderFile.tellg();
string code;
code.reserve(end-beg);
shaderFile.read(&code[0],end-beg);
shaderFile.close();
return code.c_str();
}
WHen I load both shaders, there is a marking at the end that doesn't make sense:
#version 430
in vec3 Color;
out vec4 FragColor;
void main()
{
FragColor = vec4( Color, 1.0 );
}o↨☺ // <--- What is this marking???
The Vert Shader is very similar. I tried to delete the last character from the returned string containing this code and it changed the closing brace at the end of main. I also tried setting it to zero - then the triangle doesn't display at all, even though the code looks perfect then. What is this character and how do I get rid of it?
THank you @Boreal for the help! Here is the (beautiful result): 1