I've just started learning c++ and I'm trying to make a program with shaders that draws a triangle with colors.
This is my vertex shader:
const GLchar* vertexSource =
"#version 150 core\n"
"in vec4 position;"
"in vec4 color;"
"out vec4 Color;"
"out vec4 gl_Position;"
"void main(){"
" gl_Position = position;"
" Color = color;"
"}";
and this is my fragment shader:
const GLchar* fragmentSource =
"#version 150 core\n"
"in vec4 Color;"
"out vec4 gl_FragColor"
"void main(){"
" gl_FragColor = Color;"
"}";
I have a list of values for the co-ords and colors respectively:
float vertices[]{
-1.0f, -1.0f, 0.0f,1.0f, 1.0f,0.0f,0.0f,1.0f,
1.0f, -1.0f, 0.0f,1.0f, 0.0f,1.0f,0.0f,1.0f,
0.0f, 1.0f, 0.0f,1.0f, 0.0f,0.0f,1.0f,1.0f,
};
I initialise a 'vertex buffer object' and a 'vertex array object'
GLuint vao; //Initialise vertex array object
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
GLuint vbo; //Initialise vertex buffer object
glGenBuffers(1,&vbo);
glBindBuffer(GL_ARRAY_BUFFER,vbo);
glBufferData(GL_ARRAY_BUFFER,sizeof(vertices),vertices,GL_STATIC_DRAW);
I compile a shader program:
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexSource, NULL);
glCompileShader(vertexShader);
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentSource, NULL);
glCompileShader(fragmentShader);
GLuint shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
glUseProgram(shaderProgram);
I define some attributes for the shaders:
glBindAttribLocation(shaderProgram, 0, "position");
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 8 * sizeof(float), 0);
glEnableVertexAttribArray(0);
glBindAttribLocation(shaderProgram, 1 ,"color");
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(4 * sizeof(float)));
glEnableVertexAttribArray(1);
and finally I draw the triangle in a 'do, while' loop
glDrawArrays(GL_TRIANGLES, 0, 3);
However, it doesn't print any triangles, but instead just shows a black screen.
I got most of this code from tutorials online, and I'm trying to rewrite everything out to understand how everything works.
Could anyone see where I went wrong? Any advice would be greatly appreciated :)
Edit:
I've added some code to check for errors:
if (status == GL_FALSE)
{
GLint infoLogLength;
glGetShaderiv(shaderProgram, GL_INFO_LOG_LENGTH, &infoLogLength);
GLchar* strInfoLog = new GLchar[infoLogLength + 1];
glGetProgramInfoLog(shaderProgram, infoLogLength, NULL, strInfoLog);
std::cout << strInfoLog;
delete[] strInfoLog;
}
...but nothing prints
gl_Position is a pre defined built in variable and your GLSL compiler surely is complaining about trying to redefining it and errors out. Similarly gl_FragColor is in the reserved namespace.
Of course your code lacks queries of the shader info log, so you don't see that compiler output; your addendum just queries the program linker log.
The solution is probably as easy as simply omitting the out vec4 gl_Position and out vec4 gl_FragColor from your shader sources.
Related
From what I understand, OpenGL always accepts coordinates from -1.0 to 1.0. When the viewport changes, when a window is resized, the distances between points are not preserved.
For example, when the viewport width is 400, the distance between points (0.5,0.0) and (-0.5,0.0) is 200, and when the width is 600, it is 300.
I need to draw a shape (of a scrollbar or something similar), that shouldn't change its width, when the window is resized. I've seen on other posts, that people calculate the floating point coordinates using the window width, but that introduces the problem of floating point accuracy, which can lead to the shape being nearly constant size, but sometimes not exactly.
How to make a shape, that will stay exactly the same width, no matter the width of the window?
The code I'm running is really just a simple triangle appearing on the screen that can be resized. The code is nonetheless a bit long.
#include <iostream>
#define GLEW_STATIC
#include <GL/glew.h>
#include <GLFW/glfw3.h>
const GLchar* vertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 position;\n"
"void main()\n"
"{\n"
"gl_Position = vec4(position.x, position.y, position.z, 1.0);\n"
"}\0";
const GLchar* fragmentShaderSource = "#version 330 core\n"
"out vec4 color;\n"
"void main()\n"
"{\n"
"color = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
"}\n\0";
void onSize (GLFWwindow* window, int width, int height)
{
glViewport (0,0,width, height);
}
int main()
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);
GLFWwindow* window = glfwCreateWindow(800, 600, "hello", nullptr, nullptr);
glfwMakeContextCurrent(window);
glfwSetWindowSizeCallback(window, onSize);
glewExperimental = GL_TRUE;
glewInit();
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
glCompileShader(vertexShader);
GLint success;
GLchar infoLog[512];
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
std::cout << "error with vertex shader compilation\n" << infoLog << std::endl;
}
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(fragmentShader);
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
std::cout << "error with fragment shader compilation\n" << infoLog << std::endl;
}
GLuint shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
std::cout << "error with linking program\n" << infoLog << std::endl;
}
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
GLfloat vertices[] = {
-0.5f, -0.5f, 0.0f, // Left
0.5f, -0.5f, 0.0f, // Right
0.5f, 0.5f, 0.0f // Top
};
GLuint VBO, VAO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
while (!glfwWindowShouldClose(window))
{
glfwPollEvents();
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(shaderProgram);
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindVertexArray(0);
glfwSwapBuffers(window);
}
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glfwTerminate();
return 0;
}
So how do I make it so that this triangle has constant width and height regardless of resizing the window?
My answer is related to the phrase in your question: "I need to draw a shape [...], that shouldn't change its width, when the window is resized."
You have to use Orthographic projection.
Add a Uniform variable of type mat4 to the vertex shader and multiply the vertex coordinate with the matrix uniform:
#version 330 core
layout (location = 0) in vec3 position;
uniform mat4 u_projection;
void main()
{
gl_Position = u_projection * vec4(position, 1.0);
}
Define the vertices in pixel unit:
GLfloat vertices[] = {
-100.0f, -100.0f, 0.0f, // Left
100.0f, -100.0f, 0.0f, // Right
100.0f, 100.0f, 0.0f // Top
};
Get the current width and height of the viewport and set the matrix using glm::ortho:
GLint proj_loc = glGetUniformLocation(shaderProgram, "u_projection");
while (!glfwWindowShouldClose(window))
{
int width, height;
glfwGetWindowSize(window, &width, &height);
// [...]
glUseProgram(shaderProgram);
glm::mat4 projection = glm::ortho(
-width/2.0f, width/2.0f, -height/2.0f, height/2.0f, -1.0f, 1.0f);
glUniformMatrix4fv(proj_loc, 1, GL_FALSE, glm::value_ptr(projection));
// [...]
}
I am trying to create a simple program with openGL. The goal is to create a triangle with colours for every vertex. The problem is that I can not figure out how to apply the colours to the triangle's vertices.
My vertex shader:
#version 330
layout(location = 0) in vec4 vertex_position;
layout(location = 1) in vec4 vertex_color;
smooth out vec4 theColor;
void main()
{
gl_Position = vec4(vertex_position.x, vertex_position.y, vertex_position.z, 1.0);
theColor = vertex_color;
}
My fragment shader:
#version 330
smooth in vec4 theColor;
out vec4 outColor;
void main()
{
outColor = theColor;
}
main code:
float vertices[] = {
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.0f, 0.5f, 0.0f,
0.5f, 0.6f, 0.1f,
0.2f, 0.6, 0.3f,
0.1f, 0.23f, 0.78f
};
GLuint my_vao, my_vbo;
glGenVertexArrays(1, &my_vao);
glBindVertexArray(my_vao);
glGenBuffers(1, &my_vbo);
glBindBuffer(GL_ARRAY_BUFFER, my_vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
// glEnableVertexAttribArray(1);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), (void*)0);
// glVertexAttribPointer(3,4, GL_UNSIGNED_BYTE, GL_TRUE, 0, (void*)0);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(shaderProgram);
glBindVertexArray(vao);
glViewport(0, 0, viewport_width, viewport_height);
glDrawArrays(GL_TRIANGLES, 0, 3);
swapBuffers();
The array vertices consists of the vertex points (first 3 vectors) and the color values (second 3 vectors). You can see my result in the following picture:
I have been trying to apply the colours with the following code:
glEnableVertexAttribArray(1);
glVertexAttribPointer(3,4, GL_UNSIGNED_BYTE, GL_TRUE, 0, (void*)0);
Unfortunately it does not work and I have no idea how to fix that. Can anyone help me out here?
For compiling and loading my shaders I use:
std::string loadVertexShader(const char* vertexPath)
{
std::string vertexCode;
std::ifstream vShaderFile;
vShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
try
{
vShaderFile.open(vertexPath);
std::stringstream vShaderStream;
vShaderStream << vShaderFile.rdbuf();
vShaderFile.close();
vertexCode = vShaderStream.str();
}
catch (std::ifstream::failure e)
{
std::cout << "ERROR::VERTEXSHADER::FILE_NOT_SUCCESFULLY_READ" << std::endl;
}
return vertexCode;
}
int vertexShader = glCreateShader(GL_VERTEX_SHADER);
int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
std::string vertexVal = loadVertexShader("shader/vertexshader.shader");
const char* vShaderCode = vertexVal.c_str();
std::string fragmentVal = loadFragmentShader("shader/fragmentshader.shader");
const char* fShaderCode = fragmentVal.c_str();
glShaderSource(vertexShader, 1, &vShaderCode, NULL);
glCompileShader(vertexShader);
glShaderSource(fragmentShader, 1, &fShaderCode, NULL);
glCompileShader(fragmentShader);
GLint vertexStatus;
char vertexInfoLog[512];
char fragmentInfoLog[512];
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &vertexStatus);
GLint fragmentStatus;
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &fragmentStatus);
if (!vertexStatus)
{
glGetShaderInfoLog(vertexShader, 512, NULL, fragmentInfoLog);
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << fragmentInfoLog << std::endl;
}
if (!fragmentStatus)
{
glGetShaderInfoLog(fragmentShader, 512, NULL, vertexInfoLog);
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << vertexInfoLog << std::endl;
}
First of all, as was revealed in the discussion in comments, you don't link your program. A typical shader program setup looks like this:
GLuint program = glCreateProgram();
glAttachShader(program, shader1);
glAttachShader(program, shader2);
...
glAttachShader(program, shaderN);
glLinkProgram(program); // <<<<<<<<< a-ha!
After that, a good idea is to check if program linkage was OK. A good example of the full shaders & program setup can be found on OpenGL wiki.
After successful program linkage, you can even detach the shaders from the program (glDetachShader) and delete them (glDeleteShader) - the program is already linked, shaders are of no use now (unless you intend to attach them to another program, of course).
Secondly, your arguments to glVertexAttribPointer are slightly messed up (which is totally OK - it takes time and effort to get used to them).
void glVertexAttribPointer(GLuint index, GLint size, GLenum type, GLboolean normalized,
GLsizei stride, const GLvoid * pointer);
index is the attribute index - same as location as specified in shader, so for your color attribute it should be 1.
size is the attribute's dimension, i.e. number of components; supposing that you are using 3-float RGB, size should be 3
type is the actual type of the components; in you case these are floating-points too, so GL_FLOAT
stride is the distance between attributes of adjacent vertices, which is 3 floats in your case, i.e. 12 bytes; since there's nothing in between adjacent attributes in your vertices array, we can actually leave this zero (both for coordinates & colors)
pointer is the pointer to actual attributes data (or, in your case, offset in VBO); you are passing the same pointer to both glVertexAttribPointer calls, while your color values are 9 floats after the beginning of coordinate values
So, it should be
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (float const *)(nullptr) + 9);
I start to create a Qt OpenGL application. And I followed the tutorial from Learnopengl In this tutorial, it gives the a static C-String corresponding to the vertex and fragment shader. When I give to my application a static C-String shader there is no error and my triangle appears in the screen.
But when I try to process all shader code to separate files, I have an error during the glLinkProgram command such as must write to gl_Position
Here is my C++ code:
#include "widget.h"
Widget::Widget(QOpenGLWidget *parent)
: QOpenGLWidget(parent)
{
this->setWindowTitle("Learning OpenGL");
QString pathV="shaders/main.vert";
QString pathF="shaders/main.frag";
vertexShaderSource=loadShader(pathV);
fragmentShaderSource=loadShader(pathF);
}
Widget::~Widget()
{
}
void Widget::initializeGL()
{
this->initializeOpenGLFunctions();
qDebug()<<"Vous travaillez sur une OpenGL version:"<<QString((char*)glGetString(GL_VERSION));
compileShaders();
glClearColor(0.0,0.0,0.0,1.0);
glClear(GL_COLOR_BUFFER_BIT);
}
void Widget::paintGL()
{
GLfloat vertices[] = {
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.0f, 1.0f, 0.0f
};
GLuint VBO,VAO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
// Bind the Vertex Array Object first, then bind and set vertex buffer(s) and attribute pointer(s).
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0); // Note that this is allowed, the call to glVertexAttribPointer registered VBO as the currently bound vertex buffer object so afterwards we can safely unbind
glBindVertexArray(0); // Unbind VAO (it's always a good thing to unbind any buffer/array to prevent strange bugs)
// Draw our first triangle
glUseProgram(shaderProgram);
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindVertexArray(0);
}
QString Widget::loadShader(QString &path)
{
QFile file(path);
file.open(QIODevice::ReadOnly);
QTextStream stream(&file);
QString lines;
while(!stream.atEnd())
{
lines.append(stream.readLine()+"\n");
}
//lines.append('\0');
file.close();
return lines;
}
void Widget::compileShaders()
{
/* //Static shaders
// Shaders
vs = "#version 330 core\n"
"layout (location = 0) in vec3 position;\n"
"void main()\n"
"{\n"
"gl_Position = vec4(position.x, position.y, position.z, 1.0);\n"
"}\0";
fs = "#version 330 core\n"
"out vec4 color;\n"
"void main()\n"
"{\n"
"color = vec4(0.0f, 1.0f, 0.0f, 1.0f);\n"
"}\n\0";
//*/
//Vertex shader
GLuint vertexShader;
vertexShader = glCreateShader(GL_VERTEX_SHADER);
vs=vertexShaderSource.toStdString().c_str();
glShaderSource(vertexShader, 1,(const GLchar **) &vs, 0);
glCompileShader(vertexShader);
//check if any error during compilation
GLint success;
GLchar infoLog[512];
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
if(!success)
{
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
qDebug()<< "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog ;
}
else qDebug()<<"compile vertex ok";
//Fragment shader
GLuint fragmentShader;
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
fs=fragmentShaderSource.toStdString().c_str();
glShaderSource(fragmentShader, 1,(const GLchar **) &fs, 0);
glCompileShader(fragmentShader);
//Check if any error during compilation
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
if(!success)
{
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
qDebug()<< "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog ;
}
else qDebug()<<"compile fragment ok";
shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
if(!success) {
glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
qDebug()<< "Linkage error\n" << infoLog;
}
}
When I display on the console the data I read from shader files it is the same as the static C-String shader code.
So I don't see why my shader files crush.
This code:
fragmentShaderSource.toStdString()
Results in a temporary copy of the string. That temporary will be destroyed after the expression that generates it. So when you call c_str() on that string, you're getting a pointer to memory that is about to be deleted.
Instead, your fragmentShaderSource and similar vertex shader types should just be std::string itself. As such, loadShader should return a std::string (returning lines.toStdString() instead of just lines).
Once done, you can take fragmentShaderSource.c_str() and freely pass that to OpenGL.
Your shader source seems to be empty or invalid. Most probably its due to the fact that you're assigning a temporary copy a buffer to vs/fs variables:
vs=vertexShaderSource.toStdString().c_str();
fs=fragmentShaderSource.toStdString().c_str();
These pointers will probably be invalid right after these lines, so passing them to glShaderSource function might not be the best idea.
You have to copy buffer contents. Try something like this:
char* vs = new char[vertexShaderSource.size() + 1];
strcpy(vs, vertexShaderSource.toStdString().c_str());
I am fallowing this tutorial http://learnopengl.com/#!Getting-started/Hello-Triangle and problem with this tutorials is that there is no full code example, and it's little hard to fallow. My code run successful, but my triangle is blank / white. And I think some functions is called twice, and program still can run successful if I delete some of that commands.
// SHADERS
GLuint VBO;
GLuint VAO = 0;
GLuint program;
GLuint vertexShader;
GLuint fragmentShader;
const char* vertexShaderSource =
"#version 330"
"layout (location = 0) in vec3 position;"
"void main(){"
"gl_Position = vec4(position.x, position.y, position.z, 1.0);"
"}";
const char* fragmentShaderSource =
"#version 330"
"out vec4 color;"
"void main(){"
"color = vec4(1.0, 0.5, 1.0, 1.0);"
"}";
void compileShaders() {
vertexShader = glCreateShader(GL_VERTEX_SHADER); // pravi vertex shader
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL); // Odredjuje izvor vertex shadera
glCompileShader(vertexShader); // kompajlira shader
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(vertexShader, 1, &fragmentShaderSource, NULL);
glCompileShader(fragmentShader);
}
void linkShaders() {
program = glCreateProgram(); // pravi program ID
// Linkuje shaderse i program
glAttachShader(program, vertexShader);
glAttachShader(program, fragmentShader);
glLinkProgram(program);
glUseProgram(program); // koristi program
// Brise shaderse jer vise nisu potrebni i tako oslobadja memoriju
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0); // MISLIM DA JE OVO VISAK
}
void initVBO() {
GLfloat vertices[] = {
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.0f, 0.5f, 0.0f
};
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
}
void initVAO() {
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
}
void Init() {
glClearColor(0.20, 0.63, 0.67, 1.0);
glViewport(0, 0, WIDTH, HEIGHT);
compileShaders();
linkShaders();
}
void Render() {
glClear(GL_COLOR_BUFFER_BIT);
initVAO();
initVBO();
glUseProgram(program);
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindVertexArray(0); // oslobadja momoriju
glutSwapBuffers();
}
Your shaders shouldn't compile. As a result, you don't have a valid program. Depending on the profile of the GL context - and the actual GL implementation - you will get different results. With nvidia drivers, you will typically see a white triangle in that scenario.
The reason why your shaders are invalid is because you forgot to add a newline \n after the #version 330 directive.
You are using C/C++'s syntax for concatenating strings by combining "string1" "string2". The fact that you have a newline outside of the quotes is totally irrelevant, the string you are specifying is
#version 330layout (location = 0) in vec3 position;[...]
all in one line, and that is not a valid preprocessor statement...
You always should check the compile status of your shaders, and the link status of your program. And you should always query the compiler and linker info logs. Have a look at the OpenGL wiki artice about shader compilation for details of how to do that.
I am trying to use OpenGL shaders to draw a simple triangle. I have tried to split up the code into separate methods inside one overarching rendering class, however, I cannot seem to get my triangle to render on the screen. This is my compileShaders() method which compiles and loads my shader source code:
static const GLchar* vertexShaderSource[] = {
"#version 430 core \n"
"in vec4 vPosition; \n"
"in vec4 vColor; \n"
"out vec4 color; \n"
" \n"
"void main() { \n"
" color = vColor; \n"
" gl_Position = vPosition; \n"
"} \n"
};
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, vertexShaderSource, NULL);
glCompileShader(vertexShader);
program = glCreateProgram();
glAttachShader(program, vertexShader);
glLinkProgram(program);
glDeleteShader(vertexShader);
return program;
In a different method I am calling glewInit(), compileShaders() and am then setting up the vertex array and vertex buffer objects. In this method triangleVertices[] and triangleColors[] are two arrays containing the position (x,y,z) and colour values (r,g,b,a) for each vertex the triangle.
glewInit();
renderingProgram = compileShaders();
glEnable(GL_ARRAY_BUFFER);
glGenVertexArrays(1, &vertexArrayObject);
glBindVertexArray(vertexArrayObject);
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, numberOfVertices*7*sizeof(GLfloat), NULL, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, numberOfVertices*3*sizeof(GLfloat), triangleVertices);
glBufferSubData(GL_ARRAY_BUFFER, numberOfVertices*3*sizeof(GLfloat), numberOfVertices*4*sizeof(GLfloat), triangleColors);
GLuint vpos = glGetAttribLocation(renderingProgram, "vPosition");
glVertexAttribPointer(vpos, 3, GL_FLOAT, GL_FALSE, 0, 0);
GLuint cpos = glGetAttribLocation(renderingProgram, "vColor");
glVertexAttribPointer(cpos, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(numberOfVertices*3*sizeof(GLfloat)));
glUseProgram(renderingProgram);
glEnableVertexAttribArray(vpos);
glEnableVertexAttribArray(cpos);
Finally my render() method in which I try and draw the Triangle loaded into the Array Buffer:
const GLfloat color[] = {0.0f, 0.0f, 1.0f, 1.0f};
glClearBufferfv(GL_COLOR, 0, color);
glUseProgram(renderingProgram);
glDrawArrays(GL_TRIANGLES, 0, 3);
SwapBuffers(hDC_);
I am rendering to a Win32 window which is setup in a different class. After rendering I can see a blue screen, so it appears as if the glClearBufferfv call is working correctly. However my triangle is nowhere to be seen. If I change the rendering mode to glDrawArrays(GL_POINTS, 0, 1) I see one white pixel in the middle of the screen.
What am I doing wrong?
As others have pointed out, you need a fragment shader too, e.g.,
#version 430
in vec4 color;
out vec4 frag_rgba; // default index (0) (glFragBindDataLocation)
void main (void) {
frag_rgba = color;
}
Attach this shader to the program along with the vertex shader prior to linking. You can detach the shaders from the program after linking. You might also consider using explicit attribute indices in the vertex shader.