Related
I was trying to draw with coordinates being of integer type, but failed. Then I tried to draw a simple triangle as if from tutorial. In the result half of the screen was of triangle's color, other was glClearColor I set. I started changing values, and corners started to show up when coordinates were smaller than 0.00000000000000000005f (which i suspect would be of higher precision than a float), though should have been already with smaller than 1.0f. Trying to search didn't have any results, perhaps because I don't know the right words.
Here's code I ended up with:
Includes and function I quickly moved here:
#include <GL/glew.h>
#include <glfw3.h>
#include <unistd.h>
#include <stdio.h>
/* function that creates shader */
GLuint makeShader(const GLchar* vertexShaderSource, const GLchar* fragmentShaderSource,
GLchar*& infoLog, const GLchar* geometryShaderSource = NULL)
{
GLint isSuccess;
infoLog = nullptr;
GLuint vertexShaderID = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShaderID, 1, &vertexShaderSource, nullptr);
glCompileShader(vertexShaderID);
glGetShaderiv(vertexShaderID, GL_COMPILE_STATUS, &isSuccess);
if(!isSuccess)
{
infoLog = new GLchar[1024];
glGetShaderInfoLog(vertexShaderID, 1024, nullptr, infoLog);
printf("Error compiling vertex shader: %s\n", infoLog);
printf("\n%s\n\n", vertexShaderSource);
return 0;
}
GLuint geometryShaderID;
if(geometryShaderSource!=NULL){
geometryShaderID = glCreateShader(GL_GEOMETRY_SHADER);
glShaderSource(geometryShaderID, 1, &geometryShaderSource, nullptr);
glCompileShader(geometryShaderID);
glGetShaderiv(geometryShaderID, GL_COMPILE_STATUS, &isSuccess);
if(!isSuccess)
{
infoLog = new GLchar[1024];
glGetShaderInfoLog(geometryShaderID, 1024, nullptr, infoLog);
printf("Error compiling geometry shader: %s\n", infoLog);
printf("\n%s\n\n", geometryShaderSource);
return 0;
}
}
GLuint fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShaderID, 1, &fragmentShaderSource, nullptr);
glCompileShader(fragmentShaderID);
glGetShaderiv(fragmentShaderID, GL_COMPILE_STATUS, &isSuccess);
if(!isSuccess)
{
infoLog = new GLchar[1024];
glGetShaderInfoLog(fragmentShaderID, 1024, nullptr, infoLog);
printf("Error compiling fragment shader: %s\n", infoLog);
printf("\n%s\n\n", fragmentShaderSource);
return 0;
}
GLuint shaderID = glCreateProgram();
glAttachShader(shaderID, vertexShaderID);
glAttachShader(shaderID, fragmentShaderID);
if(geometryShaderSource!=NULL){
glAttachShader(shaderID, geometryShaderID);
}
glLinkProgram (shaderID);
glValidateProgram(shaderID);
glDeleteShader(vertexShaderID);
glDeleteShader(fragmentShaderID);
if(geometryShaderSource!=NULL){
glDeleteShader(geometryShaderID);
}
glGetProgramiv(shaderID, GL_LINK_STATUS, &isSuccess);
if (!isSuccess)
{
infoLog = new GLchar[1024];
glGetProgramInfoLog(shaderID, 1024, nullptr, infoLog); // Generate error as infoLog
//std::cout << "Error " << infoLog << std::endl; // Display
printf("Error linking shader %s\n", infoLog);
return 0;
}
return shaderID;
}
Main:
int main(void)
{
GLFWwindow* window;
float FPSlimit = 60;
int windowSX = 800, windowSY = 600;
/* Initialize the library */
if (!glfwInit()){
return -1;}
/* Create window */
window = glfwCreateWindow(windowSX, windowSY, "abc", NULL, NULL);
if (!window)
{
printf("Failed to create window\n");
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSwapInterval(0);
/* Initialize GLEW */
if(glewInit()){
printf("Failed to initialize GLEW\n");
glfwTerminate();
return -1;
}
glClearColor(0.1, 0.3f, 0.2f, 1.0f);
double timeDeltaFPSmaintain = 0;
GLuint vao;
GLuint vbo;
glGenVertexArrays(1, &vao);
glGenBuffers(1, &vbo);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
//changing normalisation to GL_TRUE didnt do anything
float dat[] = {-0.000000000000000000024f, -0.000000000000000000024f, 0.f,
1.f, -1.f, 0.f,
0.f, 0.000000000000000000054f, 0.f};
glBufferData(GL_ARRAY_BUFFER, 3*3*sizeof(float), dat, GL_STATIC_DRAW);
GLchar* log;
const GLchar* v = "#version 330 \n layout (location = 0) in vec3 pos;\n void main(){gl_Position = vec4(pos.xyz, 0);}";
const GLchar* f = "#version 330 \n out vec4 color; void main(){color = vec4(1, 0, 0, 0);}";
GLuint sha = makeShader(v, f, log);
printf("%i\n", glGetError());//Shows 0
/* Main loop: */
while (!glfwWindowShouldClose(window))
{
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(sha);
glBindVertexArray(vao);
glDrawArrays(GL_TRIANGLES, 0, 3);
glfwSwapBuffers(window);
glfwPollEvents();
// nvidia starts to use 100% of one of CPU cores with vSync for some reason
timeDeltaFPSmaintain = (glfwGetTime() - timeDeltaFPSmaintain)*1000000;
if(FPSlimit>1){
usleep(1000000/FPSlimit-unsigned(timeDeltaFPSmaintain));}
timeDeltaFPSmaintain = glfwGetTime();
}
glfwTerminate();
return 0;
}
Compiler also tells me about corrupt .drective at the end of def file but I doubt it matters much.
The transformation in your vertex shader is just broken:
gl_Position = vec4(pos.xyz, 0);
Setting w_clip = 0 will mean that conceptually, your points are infinitly far away into the direction of x,y,z, and the GPU will clip your primitive to the intersection with the viewing volume (which still is finite).
Also:
I started changing values, and corners started to show up when coordinates were smaller than 0.00000000000000000005f (which i suspect would be of higher precision than a float),
That's not how floating point numbers work, the precision approaching zero is quite high, the smallest non-denormalized single precision floating point number will be 2^(-126), which is approximately 0.000000000000000000000000000000000000012, and you can get down to 2^-149 if you go into the de-normalized range.
I wrote a shader program that is not drawing anything to the screen, I think it's because I may be missed something, I don't know how to pass the vertices position to it.
My vertex shader is:
#version 130
in vec2 vertexPosition;
void main()
{
gl_Position.xy=vertexPosition;
gl_Position.z=-1.0;
gl_Position.w=1.0;
}
My fragment shader is:
#version 130
out vec3 color;
void main()
{
color=vec3(1.0,0.0,0.0);
}
this is the code:
GLfloat triangle []
{
200,200,
400,200,
400,400
};
//translating the coordinates
glViewport(0,0,640,480);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,640,0,480,0,1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
GLuint triangleBufferID;//store the identifier of this buffer
glGenBuffers(1, &triangleBufferID);
glBindBuffer(GL_ARRAY_BUFFER, triangleBufferID);
glBufferData(GL_ARRAY_BUFFER, sizeof(triangle), triangle, GL_STATIC_DRAW); //describe the data in the buffer
glEnableVertexAttribArray(0); //enable the buffer
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0); //Get the pointer for the buffer.
//SECOND SHADER TYPE (READ FROM FILE):
const GLchar* vertexshaderPath = "vertexshader.vsh";
const GLchar* fragmentshaderPath = "fragmentshader.fsh";
string vertexshaderSource = ""; //getting a string to store the source code of vertexshader.vsh
string fragmentshaderSource = ""; //getting a string to store the source code of fragmentshader.fsh
ifstream vertexfile; //getting a file pointer for vertexshader.vsh;
ifstream fragmentfile; //getting a file pointer for fragmentshader.fsh;
vertexfile.exceptions (ifstream::badbit); //add exceptions to the file pointer;
fragmentfile.exceptions (ifstream::badbit); //add exceptions to the file pointer;
try
{
vertexfile.open(vertexshaderPath); //open vertexshader.vsh
fragmentfile.open(fragmentshaderPath); //open fragmentshader.fsh
stringstream vfstream, ffstream; //get two stringstream object;
vfstream << vertexfile.rdbuf(); //get the content of vertexshader.vsh into a stringstream;
ffstream << fragmentfile.rdbuf(); //get the content of fragmentshader.fsh into a stringstream;
vertexfile.close(); //close the file;
fragmentfile.close(); //close the file;
vertexshaderSource=vfstream.str(); //copy the string from stringstream into vertexshaderSource;
fragmentshaderSource=ffstream.str(); //copy the string from stringstream into fragmentshaderSource;
}
catch (ifstream::failure e) //if failure caught...
{
cout << "Error, file is unreadable!" << endl;
}
const GLchar* vscode = vertexshaderSource.c_str();
//converted into c_str();
const GLchar* fscode = fragmentshaderSource.c_str();
//converted into c_str();
//THIS PART FOR ALL WAYS:
GLuint vertexshaderID=glCreateShader(GL_VERTEX_SHADER); //create a shader
glShaderSource(vertexshaderID,1,&vscode,nullptr);
glCompileShader(vertexshaderID); //compile shader;
GLint success;
GLchar infolog[512];
glGetShaderiv(vertexshaderID, GL_COMPILE_STATUS, &success);
if(!success) //check the compilation results
{
glGetShaderInfoLog(vertexshaderID,512,0,infolog);
cout << "Error vertex shader's compilation failed" << endl;
cout << infolog << endl;
}
GLuint fragmentshaderID=glCreateShader(GL_FRAGMENT_SHADER); //create a shader
glShaderSource(fragmentshaderID,1,&fscode, nullptr);
glCompileShader(fragmentshaderID); //compile shader
glGetShaderiv(fragmentshaderID,GL_COMPILE_STATUS,&success);
if(!success) //check the compilation results
{
glGetShaderInfoLog(fragmentshaderID,512,0,infolog);
cout << "Error fragment shader's compilation failed" << endl;
cout << infolog << endl;
}
GLuint programID = glCreateProgram(); //create a program;
glAttachShader(programID, vertexshaderID); //attach vertexshader to the program;
glAttachShader(programID, fragmentshaderID); //attach fragmentshader to the program;
glBindAttribLocation(programID, 0, "vertexPosition");
glUniform3f(glGetUniformLocation(programID, "color"),1.0,0.0,0.0);
glLinkProgram(programID); //link the pieces of the program;
glGetProgramiv(programID, GL_LINK_STATUS, &success);
if(!success) //check the link status;
{
glGetProgramInfoLog(programID,512,0,infolog);
cout << "Error linking the program" << endl;
cout << infolog << endl;
}
// glDeleteShader(vertexshaderID);
// glDeleteShader(fragmentshaderID);
glUseProgram(programID); //use the program;
glDrawArrays(GL_TRIANGLES, 0, 3);
SDL_GL_SwapWindow(window);
There are several problems with the code:
There is code used to initialize buffers and shaders mixed with code that has to be called in each frame. For example, shader loading should definitely be done only once, but the draw-calls have to happen every frame.
Then, you are mixing fixed-function code (glMatrixMode and related functions) with core profile code. Most probabibly the problem is, that all the transformations applied here are not used (since the matrix stack is not used when working in core profile with shaders). Thus the coordinates used (from 200 to 400) are outside of the default view frustum (ranging from -1 to 1). You will have to implement the transformations somewhere in the shader to get a correct projection of your points to NDC.
Minor: There is no guarantee that vertexPosition will be on location 0 (although this might be the case).
I am getting an error when trying to use VAO's inside of SFML and not sure if it is SFML or it is my own opengl code
GLenum err = glewInit();
if (err != GLEW_OK)
{
std::cout << "NOT WORKING" << std::endl;
}
std::vector<sf::Vector3f> g_vertext_buffer_data;
g_vertex_buffer_data.push_back({ -1.0f, -1.0f, 0.0f });
g_vertex_buffer_data.push_back({1.0f, -1.0f, 0.0f});
g_vertex_buffer_data.push_back({ 0.0f, 1.0f, 0.0f });
const char* vertexShaderSource =
"#version 330\n\
in vec4 position;\
void main(void){\ gl_Position = position;\
}";
// compile fragment shader source
const GLchar* fragmentShaderSource =
"#version 330\n\
void main(void) {\
out vec4 fragcolor; fragcolor= vec4(1.0,1.0,1.0,1.0);\
}";
/* Creating Shader */
this->programId = glCreateProgram();
this->vId = glCreateShader(GL_VERTEX_SHADER);
this->fId = glCreateShader(GL_FRAGMENT_SHADER);
/* Get Shader Size */
int vertexShaderLength = strlen(vertexShaderSource);
int fragmentShaderLength = strlen(fragmentShaderSource);
/* Loading and binding shader */
glShaderSource(this->vId, 1, &vertexShaderSource, NULL);
glShaderSource(this->fId, 1, &fragmentShaderSource, NULL);
/* Compile Shaders */
glCompileShader(vId);
glCompileShader(fId);
/* Attach Shaders */
glAttachShader(this->programId, this->vId);
glAttachShader(this->programId, this->fId);
/* Linkg program */
glLinkProgram(this->programId);
/* Use and bind attribute */
glUseProgram(this->programId);
this->positionId = glGetAttribLocation(this->programId, "position");
glUseProgram(0);
/* VAO Time */
glGenVertexArrays(1, &this->vaoId);
glBindVertexArray(this->vaoId);
/* VBO Time assigning to VAO */
glGenBuffers(1, &this->vboId);
glBindBuffer(GL_ARRAY_BUFFER, this->vboId);
glBufferData(GL_ARRAY_BUFFER, g_vertex_buffer_data.size() * sizeof(sf::Vector3f), &g_vertex_buffer_data[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(this->positionId);
glVertexAttribPointer(this->positionId, 2, GL_FLOAT, GL_FALSE, sizeof(sf::Vector3f), 0);
/* Close out bindings */
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
while(1)
{
glUseProgram(this->programId);
glBindVertexArray(this->vaoId);
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindVertexArray(0);
glUseProgram(0);
gameWindow.glPushStates();
}
The error code I get is: opengl error in user code (1282)
I have changed the size() issue that was brought up in the blBufferData() but still am getting the issue.
There is at least a problem with the size that is passed to glBufferData:
glBufferData(GL_ARRAY_BUFFER,
sizeof(g_vertex_buffer_data) * sizeof(sf::Vector3f),
g_vertex_buffer_data[0], GL_STATIC_DRAW);
sizeof(g_vertex_buffer_data) is equal to sizeof(std::vector<?>) which is the size of the vector object and not the size of the data contained. Try using
glBufferData(GL_ARRAY_BUFFER,
g_vertex_buffer_data.size() * sizeof(sf::Vector3f),
g_vertex_buffer_data[0], GL_STATIC_DRAW);
Another thing: In OpenGL 3.3 Core Profile there is no gl_FragColor variable. You will have to define an out variable.
Next: Your vertex shader seems to be empty. You have to write to gl_Position otherwise nothing will be shown.
Possible error codes for glGetAttribLocation are:
GL_INVALID_OPERATION
Which don't have a fixed value. Try to get the error string with gluErrorString() or take a look in the header to which of those 1282 maps.
• check your shader got compiled without error?
• check your shader got linked without error?
What type have positionId? All object id's must be GLuint type.
And btw allways enable shader compilation-linking error check, and debug will be more informative.
I do that in this way (OpenGL-ES 2.0):
m_nVertexShader = glCreateShader(GL_VERTEX_SHADER);
m_nPixelShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(m_nVertexShader, 1, &lpszVertexBuffer, NULL);
glShaderSource(m_nPixelShader, 1, &lpszFragmentBuffer, NULL);
glCompileShader(m_nVertexShader);
int iIsOk = 0;
glGetShaderiv(m_nVertexShader, GL_COMPILE_STATUS, &iIsOk);
if(!iIsOk)
{
GLint infoLen = 0;
glGetShaderiv(m_nVertexShader, GL_INFO_LOG_LENGTH, &infoLen);
if(infoLen > 1)
{
char* infoLog = (char*)malloc(sizeof(char) * infoLen);
glGetShaderInfoLog(m_nVertexShader, infoLen, NULL, infoLog);
QMessageBox::warning(this, QString("Error"),
QString(infoLog), QMessageBox::Yes | QMessageBox::Cancel, QMessageBox::Yes);
free(infoLog);
}
glDeleteShader(m_nVertexShader);
return;
}
glCompileShader(m_nPixelShader);
glGetShaderiv(m_nPixelShader, GL_COMPILE_STATUS, &iIsOk);
if(!iIsOk)
{
GLint infoLen = 0;
glGetShaderiv(m_nPixelShader, GL_INFO_LOG_LENGTH, &infoLen);
if(infoLen > 1)
{
char* infoLog = (char*)malloc(sizeof(char) * infoLen);
glGetShaderInfoLog(m_nPixelShader, infoLen, NULL, infoLog);
QMessageBox::warning(this, QString("Error"),
QString(infoLog), QMessageBox::Yes | QMessageBox::Cancel, QMessageBox::Yes);
free(infoLog);
}
glDeleteShader(m_nPixelShader);
return;
}
m_nProgram = glCreateProgram();
glAttachShader(m_nProgram, m_nVertexShader);
glAttachShader(m_nProgram, m_nPixelShader);
glBindAttribLocation(m_nProgram, 0, "rm_Vertex");
glLinkProgram(m_nProgram);
glGetProgramiv(m_nProgram, GL_LINK_STATUS, &iIsOk);
// Fail to pass status validation
if(!iIsOk)
{
GLint infoLen = 0;
glGetProgramiv(m_nProgram, GL_INFO_LOG_LENGTH, &infoLen);
if(infoLen > 1)
{
char* infoLog = (char*)malloc(sizeof(char) * infoLen);
glGetProgramInfoLog(m_nProgram, infoLen, NULL, infoLog);
QMessageBox::warning(this, QString("Error"),
QString(infoLog), QMessageBox::Yes | QMessageBox::Cancel, QMessageBox::Yes);
free(infoLog);
}
glDeleteProgram(m_nProgram);
return;
}
glUseProgram(m_nProgram);
As you use GLSL 3.3, fist you must specify fragment rendertarget output by calling
glBindFragDataLocation(this->programId, 0, "fragcolor");
Secondly your fragment shader must be like
"#version 330
out vec4 fragcolor;
void main(void) {
fragcolor= vec4(1.0,1.0,1.0,1.0);
}
The example of using this kind of shaders is on OpenGL 3.3 + GLSL 1.5 Sample.
I am following the guides from this site and have stopped on the lesson 2. At first I have tried wiriting my own code but after it didn't work I have simply taken the code from the site. And it still doesn't draw anything besides the glClearColor.
What I have done:
Checked compiling and linking. Works fine
Checked errors. Not sure if I have done it right but seems like everything's allright (I get a 1280 error but I have read that GLEW can cause it and it can be ignored).
Moved the glUseProgram through the main loop but didn't get any results.
Changed colors and tried modifying shaders. Still nothing
I will post the code I have at the moment (the original code from the site):
main.cpp
#include <stdio.h>
#include <stdlib.h>
#include <glew.h>
#include <glfw3.h>
#include <glm/glm.hpp>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
int max(int i, int j)
{
if (i > j) return i;
return j;
}
GLuint LoadShaders(const char * vertex_file_path, const char * fragment_file_path){
// Create the shaders
GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
// Read the Vertex Shader code from the file
std::string VertexShaderCode;
std::ifstream VertexShaderStream(vertex_file_path, std::ios::in);
if (VertexShaderStream.is_open())
{
std::string Line = "";
while (getline(VertexShaderStream, Line))
VertexShaderCode += "\n" + Line;
VertexShaderStream.close();
}
// Read the Fragment Shader code from the file
std::string FragmentShaderCode;
std::ifstream FragmentShaderStream(fragment_file_path, std::ios::in);
if (FragmentShaderStream.is_open()){
std::string Line = "";
while (getline(FragmentShaderStream, Line))
FragmentShaderCode += "\n" + Line;
FragmentShaderStream.close();
}
GLint Result = GL_FALSE;
int InfoLogLength;
// Compile Vertex Shader
printf("Compiling shader : %s\n", vertex_file_path);
char const * VertexSourcePointer = VertexShaderCode.c_str();
glShaderSource(VertexShaderID, 1, &VertexSourcePointer, NULL);
glCompileShader(VertexShaderID);
// Check Vertex Shader
glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result);
glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
std::vector<char> VertexShaderErrorMessage(InfoLogLength);
glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
fprintf(stdout, "%s\n", &VertexShaderErrorMessage[0]);
// Compile Fragment Shader
printf("Compiling shader : %s\n", fragment_file_path);
char const * FragmentSourcePointer = FragmentShaderCode.c_str();
glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer, NULL);
glCompileShader(FragmentShaderID);
// Check Fragment Shader
glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result);
glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
std::vector<char> FragmentShaderErrorMessage(InfoLogLength);
glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, &FragmentShaderErrorMessage[0]);
fprintf(stdout, "%s\n", &FragmentShaderErrorMessage[0]);
// Link the program
fprintf(stdout, "Linking program\n\n");
GLuint ProgramID = glCreateProgram();
glAttachShader(ProgramID, VertexShaderID);
glAttachShader(ProgramID, FragmentShaderID);
glLinkProgram(ProgramID);
// Check the program
glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result);
glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength);
std::vector<char> ProgramErrorMessage(max(InfoLogLength, int(1)));
std::cout << "Checking program\n";
glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, &ProgramErrorMessage[0]);
fprintf(stdout, "%s\n", &ProgramErrorMessage[0]);
glDeleteShader(VertexShaderID);
glDeleteShader(FragmentShaderID);
std::cout << "END";
return ProgramID;
}
int main(void)
{
if (!glfwInit())
{
std::cout << "Cannot init glfw";
return -1;
}
//glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // We want OpenGL 3.3
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //We don't want the old OpenGL
// Open a window and create its OpenGL context
GLFWwindow* window; // (In the accompanying source code, this variable is global)
window = glfwCreateWindow(1024, 768, "Tutorial 01", NULL, NULL);
if (window == NULL){
fprintf(stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n");
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window); // Initialize GLEW
glewExperimental = GL_TRUE; // Needed in core profile
if (glewInit() != GLEW_OK) {
fprintf(stderr, "Failed to initialize GLEW\n");
return -1;
}
// Ensure we can capture the escape key being pressed below
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
// This will identify our vertex buffer
GLuint vertexbuffer;
// Generate 1 buffer, put the resulting identifier in vertexbuffer
glGenBuffers(1, &vertexbuffer);
// The following commands will talk about our 'vertexbuffer' buffer
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
// Give our vertices to OpenGL.
static const GLfloat g_vertex_buffer_data[] = {
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
0.0f, 1.0f, 0.0f
};
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
GLuint programID = LoadShaders("res\\vertex.glsl", "res\\fragment.glsl");
do{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(programID);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(
0, // attribute 0. No particular reason for 0, but must match the layout in the shader.
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
// Draw the triangle !
glDrawArrays(GL_TRIANGLES, 0, 3); // Starting from vertex 0; 3 vertices total -> 1 triangle
glDisableVertexAttribArray(0);
// Swap buffers
glfwSwapBuffers(window);
glfwPollEvents();
} // Check if the ESC key was pressed or the window was closed
while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS &&
glfwWindowShouldClose(window) == 0);
return 0;
}
fragment.glsl
#version 330 core
out vec3 color;
void main(){
color = vec3(1,1,0);
}
vertex.glsl
#version 330 core
layout(location = 0) in vec3 vertexPosition_modelspace;
void main(){
gl_Position.xyz = vertexPosition_modelspace;
gl_Position.w = 1.0;
}
The OpenGL Core Profile requires the use of Vertex Array Objects (VAOs). This is in the "Deprecated and Removed Features" of the spec:
Client vertex and index arrays - all vertex array attribute and element array index pointers must refer to buffer objects. The default vertex array object (the name zero) is also deprecated. Calling VertexAttribPointer when no buffer object or no vertex array object is bound will generate an INVALID_OPERATION error, as will calling any array drawing command when no vertex array object is bound.
The tutorial you are using suggests to use this code as part of your initialization:
GLuint VertexArrayID;
glGenVertexArrays(1, &VertexArrayID);
glBindVertexArray(VertexArrayID);
This will be enough to get the tutorial code working. To make productive use of VAOs in more complex applications, you will probably want to create a VAO for each object. This will then track the full vertex setup state for the object, and allow you to set the state with a single glBindVertexArray() call before drawing.
I've seen other similar questions, and I now that it's usually due to wrong shader loading code, but I've tried with lots of different ways to load them, and I always get this error. Here is the loading code I'm currently using (for the Vertex shader, i do the same for the fragment shader):
GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
std::string VertexShaderCode;
std::ifstream VertexShaderStream(vertex_file_path, std::ios::in);
if(VertexShaderStream.is_open()){
std::string Line = "";
while(getline(VertexShaderStream, Line))
VertexShaderCode += "\n" + Line;
VertexShaderStream.close();
}
GLint Result = GL_FALSE;
int InfoLogLength;
// Compile Vertex Shader
printf("Compiling shader : %s\n", vertex_file_path);
char const * VertexSourcePointer = VertexShaderCode.c_str();
glShaderSource(VertexShaderID, 1, &VertexSourcePointer , NULL);
glCompileShader(VertexShaderID);
// Check Vertex Shader
glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result);
glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
std::vector<char> VertexShaderErrorMessage(InfoLogLength);
glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
fprintf(stdout, "%s\n", &VertexShaderErrorMessage[0]);
I've also tried this:
GLuint VertexShaderID = glCreateShader(GL_FRAGMENT_SHADER);
std::ifstream shaderFile(vertex_file_path);
//Error out here.
stringstream shaderData;
shaderData << shaderFile.rdbuf(); //Loads the entire string into a string stream.
shaderFile.close();
const std::string &VertexShaderCode = shaderData.str(); //Get the string stream as a std::string.
GLint Result = GL_FALSE;
int InfoLogLength;
printf("Compiling shader : %s\n", vertex_file_path);
char const * VertexSourcePointer = VertexShaderCode.c_str();
glShaderSource(VertexShaderID, 1, &VertexSourcePointer , NULL);
glCompileShader(VertexShaderID);
// Check Vertex Shader
glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result);
glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
std::vector<char> VertexShaderErrorMessage(InfoLogLength);
glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
fprintf(stdout, "%s\n", &VertexShaderErrorMessage[0]);
And this is the linking code:
// Link the program
fprintf(stdout, "Linking program\n");
GLuint ProgramID = glCreateProgram();
glAttachShader(ProgramID, VertexShaderID);
glAttachShader(ProgramID, FragmentShaderID);
glLinkProgram(ProgramID);
// Check the program
glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result);
glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength);
std::vector<char> ProgramErrorMessage( max(InfoLogLength, int(1)) );
glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, &ProgramErrorMessage[0]);
fprintf(stdout, "%s\n", &ProgramErrorMessage[0]);
I have also tried passing (GLint *)VertexShaderCode.size() instead of NULL to the length parameter of glShaderSource(), but I get the same errors. I get those errors both when compiling and when linking
In case it helps, I'm using Opengl 3.3, GLEW, GLFW, Visual c++ 2010, and windows 7 64 bits
How did you pass the length parameter to glShaderSource. The typecast you performed should trigger you a thick fat warning. The proper way would have been to put the result of size() into a variable. And then you give the pointer to that variable to glShaderSource.