I am using uniform blocks and have encountered a problem when creating uniform buffers for them. The problem is that when generating a buffer in a certain place (as show in the 1st code sample, about 2/3 way down), a segfault occurs during glClear();
Interestingly the order of the glGenBuffers matters, as the segfault will not occur if I swap 2 calls to the function.
program.cpp:
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <helpful.hpp> //Contains text read and shader loading functions
using namespace std;
int main(int args, char* argv[])
{
//## Create context ##//
unsigned int a;
glfwInit();
glfwWindowHint(GLFW_DEPTH_BITS,16);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
GLFWwindow* window = glfwCreateWindow(1368, 768, "", NULL, NULL);
glfwMakeContextCurrent(window);
glewExperimental=GL_TRUE;
glewInit();
cout << "GL context created" << endl;
a = glGetError();
if (a!=0)cout << "Context error" << a << endl;
glViewport(0, 0, 1368, 768);
//## Setup shader ##//
const char* vsText = textFileRead("shaders/testShader/shader.vert"); //Reads a text file
const char* fsText = textFileRead("shaders/testShader/shader.frag");
string vsource = "#version 140 \n";
string fsource = "#version 140 \n";
vsource += vsText;
fsource += fsText;
array<GLuint, 3> out = loadShaders("testShader",vsource,fsource);
GLuint shader_id = out[0];
GLuint shader_vp = out[1];
GLuint shader_fp = out[2];
glBindAttribLocation(shader_id, 0, "in_Vertex");
glBindFragDataLocation(shader_id, 0, "out_Scene");
glAttachShader(shader_id, shader_fp);
glAttachShader(shader_id, shader_vp);
glLinkProgram(shader_id);
glUseProgram(shader_id);
cout << "s : " << shader_id << endl;
GLuint globalIndex = glGetUniformBlockIndex(shader_id, "GlobalSettings");
glUniformBlockBinding(shader_id, globalIndex, 1);
GLuint modelIndex = glGetUniformBlockIndex(shader_id, "ModelSettings");
glUniformBlockBinding(shader_id, modelIndex, 2);
//## Fill Uniform block buffers ##//
float f[16] = {
1.f, .2f, 1.f, .2f,
1.f, .2f, 1.f, .2f,
1.f, .2f, 1.f, .2f,
1.f, .2f, 1.f, .6f,
};
GLuint subo; //"GlobalSettings" block
glGenBuffers(1, &subo);
glBindBuffer(GL_UNIFORM_BUFFER, subo);
glBufferData(GL_UNIFORM_BUFFER, sizeof(float) * 16, f, GL_DYNAMIC_DRAW);
glBindBufferBase(GL_UNIFORM_BUFFER, subo, 1);
//This is what causes the error
//Comment out these 2 lines and it works
//Or swap this block with the one below
GLuint b;
glGenBuffers(1, &b);
GLuint mubo; //"ModelSettings" block
glGenBuffers(1, &mubo);
glBindBuffer(GL_UNIFORM_BUFFER, mubo);
glBufferData(GL_UNIFORM_BUFFER, sizeof(float) * 16, f, GL_DYNAMIC_DRAW);
glBindBufferBase(GL_UNIFORM_BUFFER, mubo, 2);
//## Create a simple quad to draw on screen ##//
GLuint vao;
GLuint vbo;
glGenVertexArrays(1, &vao);
glGenBuffers(1, &vbo);
std::vector<glm::vec3> v;
v.push_back(glm::vec3(-1.,-1.,0));
v.push_back(glm::vec3(1.,-1.,0));
v.push_back(glm::vec3(1.,1.,0));
v.push_back(glm::vec3(-1.,-1.,0));
v.push_back(glm::vec3(1.,1.,0));
v.push_back(glm::vec3(-1.,1.,0));
glBindVertexArray(vao);
glEnableVertexAttribArray(0);
int numVertices = v.size();
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, numVertices * sizeof(GLfloat) * 3, v.data(), GL_STATIC_DRAW);
glVertexAttribPointer((GLuint)0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glBindVertexArray(0);
glfwPollEvents();
glClearColor(0.2,0.2,0.2,1);
while (true)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glBindVertexArray(vao);
glUseProgram(shader_id);
glDrawArrays(GL_TRIANGLES, 0, numVertices);
glBindVertexArray(0);
glfwSwapBuffers(window);
}
}
Here are the shaders:
Vertex:
layout( std140 ) uniform GlobalSettings {
mat4 projectionMatrix;
};
layout( std140 ) uniform ModelSettings {
mat4 MVMatrix;
};
in vec3 in_Vertex;
in vec3 in_Normal;
out vec4 eyePosition;
out vec4 screenPosition;
out vec3 pass_Normal;
void main(void)
{
vec4 vertex=vec4(in_Vertex, 1.0);
eyePosition = MVMatrix * vertex;
screenPosition = gl_Position = vertex;
pass_Normal = in_Normal;
}
Fragment
layout( std140 ) uniform GlobalSettings {
mat4 projectionMatrix;
};
layout( std140 ) uniform ModelSettings {
mat4 MVMatrix;
};
in vec4 screenPosition;
in vec4 eyePosition;
in vec3 pass_Normal;
out vec4 out_Scene;
void main(void)
{
float d = dot(normalize(pass_Normal), vec3(0,0,-1));
float a = 1;
//if(d>0)a=1;
out_Scene = vec4(MVMatrix[3][1],MVMatrix[3][2],MVMatrix[3][3],a);
}
The shader loading function can be found here http://pastebin.com/XqewS28W in needed.
The problem is that the parameters in glBindBufferBase are the wrong way around, thus the index argument was being given a buffer name and vice versa. It only worked by coincidence as the buffer names happened to be the same as the indices, and generating in the extra buffer resulted in the indices no longer matching the buffers.
Related
i can't render multiples triangles using openGl, only 1 object is display.
I only view the first object, i have create.
For me the problem is with indices or glBufferData() or glVertexAttribPointer().
For the demo is only use vertices and indices.
Vertices are type Float and indices are type unsigned int.
void Objets::createShader() {
GLuint vertexShaderID = glCreateShader(GL_VERTEX_SHADER);
GLuint fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
const GLchar* adapter[1];
string temp = readShaderCode("VertexShaderCode.glsl");
adapter[0] = temp.c_str();
glShaderSource(vertexShaderID, 1, adapter, 0);
temp = readShaderCode("FragmentShaderCode.glsl");
adapter[0] = temp.c_str();
glShaderSource(fragmentShaderID, 1, adapter, 0);
glCompileShader(vertexShaderID);
glCompileShader(fragmentShaderID);
//checkShaders(vertexShaderID, fragmentShaderID); no error
programID = glCreateProgram();
glAttachShader(programID, vertexShaderID);
glAttachShader(programID, fragmentShaderID);
glLinkProgram(programID);
//checkProgram(programID); no error
glDeleteShader(vertexShaderID);
glDeleteShader(fragmentShaderID);
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &vertexBufferID);
glGenBuffers(1, &colorBufferID);
glGenBuffers(1, &indexBufferID);
}
I use createShader() after i have create my window with sfml.
I create 2 shapes and load my 2 shapes :
graphiques3d.createShapeAndBufferit(Shapes::loadFromFileObj("objs/Cube.004.obj"));
graphiques3d.createShapeAndBufferit(Shapes::loadFromFileObj("objs/Cube.007.obj"));
Shapes Shapes::loadFromFileObj(string nameFile) {
Shapes shape;
string newString;
string buf;
string line;
ifstream myfile(nameFile);
if (myfile.is_open())
{
while (getline(myfile, line))
{
std::stringstream ss;
if (line.rfind(shape.verticesS, 0) == 0) {
newString = line.substr(2, line.length());
ss.str(newString);
while (ss >> buf) {
shape.vertices.push_back((GLfloat)stof(buf));
}
}
else if (line.rfind(shape.indicesS, 0) == 0) {
newString = line.substr(2, line.length());
ss.str(newString);
while (ss >> buf) {
GLuint valBuffer = (GLuint)atoi(buf.c_str());
shape.indices.push_back((GLuint)(valBuffer - 1));
}
}
}
myfile.close();
}
else cout << "Unable to open file";
return shape;
}
Then it use createShapeAndBufferit(Shapes shape);
void Objets::createShapeAndBufferit(Shapes shape) {
for (int a = 0; a < shape.getVertices().size(); a++)
vertices.push_back(shape.getVertices().at(a));
for (int b = 0; b < shape.getIndices().size(); b++)
indices.push_back(shape.getIndices().at(b));
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferID);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(GLfloat), &vertices[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLuint), &indices[0], GL_STATIC_DRAW);
}
When i print my shapes, i have this :
/1/1/-3/1/-1/-3/1/1/-1/1/-1/-1/-1/1/-3/-1/-1/-3/-1/1/-1/-1/-1/-1 sizeVertices: 24
:4:2:0:2:7:3:6:5:7:1:7:5:0:3:1:4:1:5:4:6:2:2:6:7:6:4:5:1:3:7:0:2:3:4:0:1 sizeIndices: 36
/3/1/1/3/-1/1/3/1/3/3/-1/3/1/1/1/1/-1/1/1/1/3/1/-1/3 sizeVertices: 24
:4:2:0:2:7:3:6:5:7:1:7:5:0:3:1:4:1:5:4:6:2:2:6:7:6:4:5:1:3:7:0:2:3:4:0:1 sizeIndices: 36
And then i draw :
void Objets::dessiner(float x, float y, float z) {
glClearColor(0.f, 0.f, 0.f, 1.f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0, 0, width, height);
glUseProgram(programID);
mat4 projectionMatrix = glm::perspective(90.f, width / height, 0.1f, 100.f);
mat4 modelTransformMatrix = glm::translate(vec3(x, y, z));
mat4 modelFullTransformMatrix = projectionMatrix * modelTransformMatrix;
GLint modelTransformMatrixUniformLocation =
glGetUniformLocation(programID, "modelFullTransformMatrix");
glUniformMatrix4fv(modelTransformMatrixUniformLocation,
1, GL_FALSE, &modelFullTransformMatrix[0][0]);
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, (GLvoid*)0);
glBindVertexArray(0);
}
I don't know if you need the fragmentShader and the vertexShader, so i put it :
fragmentShader :
#version 330 core
out vec4 FragColor;
in vec3 color;
void main()
{
FragColor = vec4(1.0f, 0.0f, 0.0f, 1.0f);
}
vertexShader :
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;
uniform mat4 modelFullTransformMatrix;
out vec3 color;
void main()
{
vec4 v = vec4(aPos, 1.0);
gl_Position = modelFullTransformMatrix*v;
color = aColor;
}
So if I understand your explanation correctly, your problem is that you load two separate meshes ('shapes'), and for each one the indices start from zero, but you want to draw both of them in a single draw call?
Maybe glMultiDrawElementsBaseVertex is what you're after?
See also https://www.khronos.org/opengl/wiki/Vertex_Rendering for other alternatives in addition to the basic DrawArrays / DrawElements.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I try to move my object only on the y-axis. I created an array with 4 Vertex and put the data in my vertexBuffer and bind it. Also I created a vertexArray and an indexBuffer to bind them and draw my quad.
Now I change only the y-coordinate of my quad and want to overwrite the data in my vertexBuffer with glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(moveObject), moveObject); but nothing happens. Only when I fill the data before I bind my indexbuffer it will work. After that I can't change the VertexBuffer.
Here is my code of my main
#include <iostream>
#include "headerData/VertexBuffer.h"
#include "headerData/IndexBuffer.h"
#include "headerData/Shader.h"
#include "headerData/VertexLayout.h"
#include "headerData/VertexArray.h"
#include "headerData/Road.h"
#include "glm.hpp"
#include <gtc/matrix_transform.hpp>
#include <gtc/type_ptr.hpp>
static struct VertexShader {
std::string defaultShader = "#version 330 core\n"
"layout (location = 0) in vec4 aPos;\n"
"uniform mat4 scale;"
"void main()\n"
"{\n"
"gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0f)*scale;\n"
"}\0";
std::string yMoveShader = "#version 330 core\n"
"layout (location = 0) in vec4 aPos;\n"
"uniform mat4 scale;"
"uniform vec4 transform;\n"
"void main()\n"
"{\n"
"gl_Position = vec4(aPos.x, aPos.y+transform.y, aPos.z, 1.0f)*scale;\n"
"}\0";
};
static struct FragmentShader {
std::string defaultShader = "#version 330 core\n"
"out vec4 FragColor;\n"
"void main()\n"
"{\n"
" FragColor = vec4(0.5f, 0.5f, 0.5f, 1.0);\n"
"}\n\0";
std::string greenShader = "#version 330 core\n"
"out vec4 FragColor;\n"
"void main()\n"
"{\n"
" FragColor = vec4(0.5f, 1.0f, 0.5f, 1.0);\n"
"}\n\0";
};
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow* window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
}
int main()
{
// glfw: initialize and configure
// ------------------------------
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
// glfw window creation
// --------------------
GLFWwindow* window = glfwCreateWindow(800, 800, "LearnOpenGL", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
// glad: load all OpenGL function pointers
// ---------------------------------------
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
// set up vertex data (and buffer(s)) and configure vertex attributes
// ------------------------------------------------------------------
VertexShader vertexShader;
FragmentShader fragmentShader;
Shader shaderCol(vertexShader.defaultShader, fragmentShader.defaultShader);
Shader shaderMove(vertexShader.yMoveShader, fragmentShader.greenShader);
shaderCol.bind();
//shaderMove.bind();
glm::mat4 scale = glm::ortho(-100.0f, 100.0f, -100.0f, 100.0f, -1.0f, 1.0f);
//glUniformMatrix4fv(glGetUniformLocation(shaderMove.getShaderID(), "scale"), 1, GL_FALSE, glm::value_ptr(scale));
glUniformMatrix4fv(glGetUniformLocation(shaderCol.getShaderID(), "scale"), 1, GL_FALSE, glm::value_ptr(scale));
glm::vec3 transform(0.0f, 0.0f, 0.0f);
float moveObject[] = {
20.0f, -50.0f, // top right
20.0f, -90.0f, // bottom right
-20.0f, -90.0f, // bottom left
-20.0f, -50.0f // top left
};
float colObject[] = {
2.0f, 2.0f, //top right
2.0f, -2.0f, //bottom right
-2.0f, 2.0f, //top left
-2.0, -2.0f //bot left
};
unsigned int indices[] = { // note that we start from 0!
0, 1, 3, // first triangle
1, 2, 3 // second triangle
};
unsigned int VA, VB, IB;
glGenVertexArrays(1, &VA);
// bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).
//glBindBuffer(GL_ARRAY_BUFFER, VBO);
//glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
//VertexBuffer vb;
//vb.bind(moveObject, 4 * 2 * sizeof(float));
//VertexBuffer
glGenBuffers(1, &VB);
glBindBuffer(GL_ARRAY_BUFFER, VB);
glBufferData(GL_ARRAY_BUFFER, 8 * 2 * sizeof(float), nullptr, GL_DYNAMIC_DRAW);
glBindVertexArray(VA);
//LAyout vom Buffer
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)(0));
glEnableVertexAttribArray(0);
//VertexLayout layout;
//layout.layoutPush(3);
//VertexArray va;
//va.addBuffer(vb, layout);
//IndexBuffer ib;
//ib.bind(indices, 6 * sizeof(unsigned int));
glGenBuffers(1, &IB);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IB);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6 * sizeof(unsigned int), indices, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, IB);
//Parameter
//glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)(0));
//glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)(3 * sizeof(float)));
//glEnableVertexAttribArray(0);
//glEnableVertexAttribArray(1);
// uncomment this call to draw in wireframe polygons.
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
//Road road;
// render loop
// -----------
float time = 0;
while (!glfwWindowShouldClose(window))
{
// input
// -----
processInput(window);
// render
// ------
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
//shader.bind();
//glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, (void*)(sizeof(unsigned int) * 3));
//glDrawArrays(GL_TRIANGLES, 0, 3);
//glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, (void*)(sizeof(unsigned int) * 3));
//glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
//road.render();
//That not work
moveObject[1] += abs(90 * sin(glfwGetTime()));
moveObject[3] += abs(90 * sin(glfwGetTime()));
moveObject[5] += abs(90 * sin(glfwGetTime()));
moveObject[7] += abs(90 * sin(glfwGetTime()));
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(moveObject), moveObject);
//transform.y = 90 * abs(sin(glfwGetTime()));
//glUniform4f(glGetUniformLocation(shaderMove.getShaderID(), "transform"), transform.x, transform.y, 0.0f, 1.0f);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
//glDeleteBuffers(1, &IB);
//glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
// glBindVertexArray(0); // no need to unbind it every time
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
// -------------------------------------------------------------------------------
glfwSwapBuffers(window);
glfwPollEvents();
}
// optional: de-allocate all resources once they've outlived their purpose:
// ------------------------------------------------------------------------
//glDeleteVertexArrays(1, &test);
// glfw: terminate, clearing all previously allocated GLFW resources.
// ------------------------------------------------------------------
glfwTerminate();
return 0;
}
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
std::cout << "callback" << std::endl;
glViewport(0, 0, width, height);
}
When you execute this line
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(moveObject), moveObject);
the data bound to GL_ARRAY_BUFFER on GPU is updated. But what data is it bound to? Look above
glBindBuffer(GL_ARRAY_BUFFER, IB);
You seem to bind your array of indices, while you probably meant
glBindBuffer(GL_ARRAY_BUFFER, VB);
I'm trying to create a Mesh class that can generate a model for me and then calling that model in the createTriangle function. However, whenever I try creating the meshObj1 and meshObj2 and then pushing it to the meshVector, it does not want to render? Why is this happening?
Mesh.h
#ifndef MESH_H
#define MESH_H
#include "GL/glew.h"
class Mesh {
public:
Mesh();
~Mesh();
void createMesh(GLfloat *vertices, unsigned int *indices, unsigned int numOfVertices, unsigned int numOfIndices);
void renderMesh();
void clearMesh();
private:
GLuint VAO, VBO, IBO;
GLsizei indexCount;
};
#endif
Mesh.cpp
#include "Mesh.h"
Mesh::Mesh() {
VAO = 0;
VBO = 0;
IBO = 0;
indexCount = 0;
}
Mesh::~Mesh() {
clearMesh();
}
void Mesh::createMesh(GLfloat* vertices, unsigned int* indices, unsigned int numOfVertices, unsigned int numOfIndices) {
indexCount = numOfIndices;
//Binding
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
//Information
//VBO Information
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices[0] * numOfVertices), vertices, GL_STATIC_DRAW);
//IBO Information
glGenBuffers(1, &IBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices[0]) * numOfIndices, indices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0);
//Unbinding
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
void Mesh::renderMesh() {
//Binding
glBindVertexArray(VAO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
//Rendering
glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, 0);
//Unbinding
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
void Mesh::clearMesh() {
if (VAO != 0) {
glDeleteVertexArrays(1, &VAO);
VAO = 0;
}
if (VBO != 0) {
glDeleteBuffers(1, &VBO);
VBO = 0;
}
if (IBO != 0) {
glDeleteBuffers(1, &IBO);
IBO = 0;
}
indexCount = 0;
}
Main.cpp
#include "GL/glew.h"
#include "GLFW/glfw3.h"
#include "glm/glm.hpp"
#include "glm/gtc/matrix_transform.hpp"
#include "glm/gtc/type_ptr.hpp"
#include <iostream>
#include <vector>
#include <memory>
#include "Mesh.h"
//Window dimensions
const GLint SCREEN_WIDTH = 1280, SCREEN_HEIGHT = 900;
GLuint shader, uniformModel, uniformProjection;
bool isMovingLeft = true;
float triOffset = 0.f;
float triMaxOffset = 0.7;
float triIncrement = 0.005;
std::vector<Mesh*> meshVector;
//Vertex shader
static const char* vShader = " \n\
#version 460 \n\
\n\
layout(location = 0) in vec3 pos; \n\
\n\
out vec4 vColor; \n\
\n\
uniform mat4 model; \n\
uniform mat4 projection; \n\
\n\
void main() { \n\
gl_Position = projection * model * vec4(pos, 1.0); \n\
vColor = vec4(clamp(pos, 0.f, 1.f), 1.f); \n\
}; \n\
";
//Fragment shader
static const char* fShader = " \n\
#version 460 \n\
\n\
in vec4 vColor; \n\
\n\
out vec4 color; \n\
\n\
void main() { \n\
color = vColor; \n\
}; \n\
";
void createTriangle() {
unsigned int indices[] = {
0, 3, 1,
1, 3, 2,
2, 3, 0,
0, 1, 2
};
//Points of the triangle
GLfloat vertices[] = {
-1.f, -1.f, 0.f,
0.f, -1.f, 1.f,
1.f, -1.f, 0.f,
0.f, 1.f, 0.f
};
Mesh* meshObj1 = new Mesh();
meshObj1->createMesh(vertices, indices, 12, 12);
meshVector.push_back(meshObj1);
Mesh* meshObj2 = new Mesh();
meshObj2->createMesh(vertices, indices, 12, 12);
meshVector.push_back(meshObj2);
}
void addShader(GLuint theProgram, const char* shaderCode, GLenum shaderType) {
GLuint theShader = glCreateShader(shaderType);
const GLchar* theCode[1];
theCode[0] = shaderCode;
GLint codeLength[1];
codeLength[0] = strlen(shaderCode);
glShaderSource(theShader, 1, theCode, codeLength);
glCompileShader(theShader);
//Getting error information for linking
GLint result = 0;
GLchar eLog[1024] = { 0 };
glGetShaderiv(theShader, GL_COMPILE_STATUS, &result);
if (result != GL_TRUE) {
glGetShaderInfoLog(theShader, sizeof(eLog), NULL, eLog);
std::cout << "Error compiling the " << shaderType << ' ' << eLog << '\n';
}
glAttachShader(theProgram, theShader);
}
void compileShaders() {
shader = glCreateProgram();
if (shader != GL_TRUE) {
std::cout << "Shader program error!\n";
}
//Adding shaders
addShader(shader, vShader, GL_VERTEX_SHADER);
addShader(shader, fShader, GL_FRAGMENT_SHADER);
//Getting error information for linking
GLint result = 0;
GLchar eLog[1024] = { 0 };
//Linking shader
glLinkProgram(shader);
//Shader linking status
glGetProgramiv(shader, GL_LINK_STATUS, &result);
if (result != GL_TRUE) {
glGetProgramInfoLog(shader, sizeof(eLog), NULL, eLog);
std::cout << "Error linking program! " << eLog << '\n';
}
//Gets shader ID and then binds it with the variable inside shader
uniformModel = glGetUniformLocation(shader, "model");
uniformProjection = glGetUniformLocation(shader, "projection");
}
int main() {
//Initialize GLFW
if (glfwInit() != GLFW_TRUE) {
std::cout << "GLFW init failed\n";
glfwTerminate();
}
//Setup GLFW window properties
//OpenGL version
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); //Large version
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 4); //Small version
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //Detects any old OpenGL code, this will throw an error
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); //Allows forward compatibility (between differnt OS)
//Creating window
GLFWwindow* window;
window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "OpenGL Test Window", NULL, NULL);
glfwSetWindowPos(window, 250, 100);
if (window == NULL) {
std::cout << "GLFW window creation failed!\n";
glfwTerminate();
}
//Get buffer size information
int bufferWidth, bufferHeight;
glfwGetFramebufferSize(window, &bufferWidth, &bufferHeight);
//Set context for GLEW to use (can change between which window)
glfwMakeContextCurrent(window);
//Allow modern extension features
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK) {
std::cout << "Glew init failed!\n";
glfwDestroyWindow(window);
glfwTerminate();
}
glEnable(GL_DEPTH_TEST);
//Setup viewport size
glViewport(0, 0, bufferWidth, bufferHeight);
createTriangle();
compileShaders();
glm::mat4 projection = glm::perspective(45.f, (GLfloat)bufferWidth / (GLfloat)bufferHeight, 0.1f, 100.f);
//Main game loop
while (!glfwWindowShouldClose(window)) {
//Get + Handle user input events
glfwPollEvents();
//Left-Right
if (isMovingLeft) {
triOffset += triIncrement;
}
else {
triOffset -= triIncrement;
}
if (abs(triOffset) >= triMaxOffset) {
isMovingLeft = !isMovingLeft;
}
//Clear window
glClearColor(0.f, 0.f, 0.f, 1.f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(shader);
//Matrix 4x4
glm::mat4 model(1.f);
model = glm::translate(model, glm::vec3(triOffset, 0.f, -2.5f));
model = glm::scale(model, glm::vec3(0.4f, 0.4f, 1.f));
glUniformMatrix4fv(uniformModel, 1, GL_FALSE, glm::value_ptr(model));
glUniformMatrix4fv(uniformProjection, 1, GL_FALSE, glm::value_ptr(projection));
meshVector[0]->renderMesh();
model = glm::mat4(1.f);
model = glm::translate(model, glm::vec3(-triOffset, 1.f, -2.5f));
model = glm::scale(model, glm::vec3(0.4f, 0.4, 1.f));
meshVector[1]->renderMesh();
glUseProgram(0);
glfwSwapBuffers(window);
}
}
I expect two pyramids to form with color.
The GL_ELEMENT_ARRAY_BUFFER target binding is stored in the Vertex Array Object. See Index buffers.
This means, that the instruction
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
breaks the former binding of IBO to the vertex array object VAO. Remove this instruction from your code, to solve the issue.
Note, GL_ARRAY_BUFFER and the GL_ELEMENT_ARRAY_BUFFER behave differently. The current GL_ARRAY_BUFFER binding is a global state, but the GL_ELEMENT_ARRAY_BUFFER binding is stated in the current Vertex Array Object.
Also the GL_ARRAY_BUFFER is stated in the state vector of the VAO, but this happens when glVertexAttribPointer is called. When glVertexAttribPointer is called, then the buffer which is currently bound to the target GL_ARRAY_BUFFER is associated to the vertex attribute with the specified index.
That causes the difference in the behavior of GL_ELEMENT_ARRAY_BUFFER and GL_ARRAY_BUFFER. A VAO can only refer to 1 index (element) buffer, but it can refer to multiple array buffers. Each attribute (index) can be associated to a different buffer.
Before you render the 2nd mesh, you've the set the corresponding model matrix to the default uniform block of the installed program:
glUseProgram(shader);
glUniformMatrix4fv(uniformProjection, 1, GL_FALSE, glm::value_ptr(projection));
glm::mat4 model(1.f);
model = glm::translate(model, glm::vec3(triOffset, 0.f, -2.5f));
model = glm::scale(model, glm::vec3(0.4f, 0.4f, 1.f));
glUniformMatrix4fv(uniformModel, 1, GL_FALSE, glm::value_ptr(model));
meshVector[0]->renderMesh();
model = glm::mat4(1.f);
model = glm::translate(model, glm::vec3(-triOffset, 1.f, -2.5f));
model = glm::scale(model, glm::vec3(0.4f, 0.4, 1.f));
glUniformMatrix4fv(uniformModel, 1, GL_FALSE, glm::value_ptr(model)); // <---
meshVector[1]->renderMesh();
I am trying to make my first cube using open gl. Unfortunately I stucked in easy rotation. This is how it should look:
Instead I am getting those weird, do not even know how to called it : https://youtu.be/0A5Hi-8bygE
Vertex shader
#version 330 core
layout (location = 0) in vec2 a_position;
layout (location = 1) in vec2 a_uv;
layout (location = 2) in vec4 a_color;
out vec2 v_fragmentPosition;
out vec2 v_fragmentUV;
out vec4 v_fragmentColor;
uniform mat4 u_model;
uniform mat4 u_view;
uniform mat4 u_projection;
void main()
{
gl_Position = u_projection * u_view * u_model * vec4(a_position, 0.0, 1.0);
v_fragmentPosition = a_position;
v_fragmentUV = vec2(a_uv.x, 1 - a_uv.y);
v_fragmentColor = a_color;
}
Fragment
#version 330 core
out vec4 outColor;
in vec2 v_fragmentPosition;
in vec2 v_fragmentUV;
in vec4 v_fragmentColor;
void main()
{
outColor = v_fragmentColor;
}
Main
#include <glad.h>
#include <glm.hpp>
#include <gtc/matrix_transform.hpp>
#include <gtc/type_ptr.hpp>
#include <iostream>
#include "Mesh.h"
#include "Shader.h"
int main(int argc,char*args[])
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 4);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_Window* window = SDL_CreateWindow("fdas", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, SDL_WINDOW_OPENGL);
SDL_GLContext context = SDL_GL_CreateContext(window);
gladLoadGLLoader(SDL_GL_GetProcAddress);
glEnable(GL_DEPTH_TEST);
Shader shader;
std::string vertex = shader.LoadFromFile("vertex.shader");
std::string fragment = shader.LoadFromFile("fragment.shader");
shader.CreateShaderProgram(vertex.c_str(), fragment.c_str());
shader.useProgram();
Mesh mesh;
mesh.CreateVertexArray();
bool quit = false;
SDL_Event event;
while (!quit)
{
while (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
exit(0);
}
}
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glm::mat4 model;
model = glm::rotate(model, glm::radians(-55.0f), glm::vec3(1.0f, 0.0f, 0.0f));
glm::mat4 view;
view = glm::translate(view, glm::vec3(0.0f, 0.0f, -3.0f));
glm::mat4 projection;
projection = glm::perspective(glm::radians(45.0f), 1280 / 720.0f, 0.1f, 100.0f);
GLint u_model = shader.GetUniformLocation("u_model");
GLint u_view = shader.GetUniformLocation("u_view");
GLint u_projection = shader.GetUniformLocation("u_projection");
glUniformMatrix4fv(u_model, 1, GL_FALSE, glm::value_ptr(model));
glUniformMatrix4fv(u_view, 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(u_projection, 1, GL_FALSE, glm::value_ptr(projection));
Rect destRect = { -0.5f, -0.5f, 1.0f, 1.0f };
Rect uvRect = { 0.0f, 0.0f, 1.0f, 1.0f };
ColorRGBA color = { 255, 255, 128, 255 };
mesh.CreateQuad(destRect, uvRect, color);
mesh.Draw(0);
SDL_GL_SwapWindow(window);
}
return 0;
}
Mesh
void Mesh::CreateVertexArray()
{
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glGenBuffers(1, &ebo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, position));
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, uv));
glVertexAttribPointer(2, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(Vertex), (void*)offsetof(Vertex, color));
glBindVertexArray(0);
}
void Mesh::CreateQuad(Rect destRect, Rect uvRect, ColorRGBA color)
{
vertex[0].color = color; // topleft
vertex[0].setPosition(destRect.x, destRect.y + destRect.h);
vertex[0].setUV(uvRect.x, uvRect.y + uvRect.h);
vertex[1].color = color;
vertex[1].setPosition(destRect.x, destRect.y); // bottom left
vertex[1].setUV(uvRect.x, uvRect.y);
vertex[2].color = color;
vertex[2].setPosition(destRect.x + destRect.w, destRect.y); // bottom right
vertex[2].setUV(uvRect.x + uvRect.w, uvRect.y);
vertex[3].color = color;
vertex[3].setPosition(destRect.x + destRect.w, destRect.y + destRect.h); // top right
vertex[3].setUV(uvRect.x + uvRect.w, uvRect.y + uvRect.h);
GLuint indices[6] = {
0, 1, 2,
2, 3, 0
};
glBindVertexArray(vao);
glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(vertex), NULL, GL_STATIC_DRAW); // orphan the buffer
glBufferSubData(GL_ARRAY_BUFFER, 0, 6 * sizeof(vertex), vertex);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6 * sizeof(GLuint), NULL, GL_STATIC_DRAW); // orphan the buffer
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, 6 * sizeof(GLuint), indices);
glBindVertexArray(0);
}
void Mesh::Draw(GLuint texture)
{
glBindVertexArray(vao);
// glEnable(GL_CULL_FACE);
// glCullFace(GL_FRONT);
//glFrontFace(GL_CW);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, NULL);
glBindVertexArray(0);
}
Vertex
struct Rect {
float x, y;
float w, h;
};
struct Position {
float x, y;
};
struct UV {
float u, v;
};
struct ColorRGBA {
GLubyte r, g, b, a;
};
class Vertex
{
public:
Vertex();
~Vertex();
void setPosition(float x, float y);
void setcolor(ColorRGBA color);
void setUV(float u, float v);
UV uv;
ColorRGBA color;
Position position;
};
I will not show shader class, because there is all good with it.
I know it, because I did an experiment.
I created a new uniform in fragment class called u_expColor and set outColor = u_expColor and a quad's color changed.
Also I created uniform vec4 u_expPos in vertex shader and said gl_Position = e_expPos and it created quad with no problems.
Only if I want to create/multiply position by mvp in vertex shader there is a problem like on the you tube video.
How to fix it?
I have fixed it thanks http://www.opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices/#putting-it-all-together. I saw he had
glm::mat4 model = glm::mat4(1.0f)
and it was all I had to do.
Drawing with a regular VBO on older GLSL versions is not a problem but for whatever reason I get this result when using GLSL 3.3.
It should be drawing a 2x2 plane on each axis.
(Lighter colors are closer to the far plane, darker are closer to the near plane)
One of the major changes with 3.3 was that you have to provide uniforms with your Model View Projection matrixes as opposed to using the old provided ones.
I don't know what I'm doing wrong but I'm almost certain it's something to do with the Model View Projection data. Here is the relevant code.
Main draw method
float r = 0.0f;
void display() {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set background color to black and opaque
glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer
glClear(GL_DEPTH_BUFFER_BIT); // And the depth buffer
r += 0.001f;
glUseProgram(program);
glEnable(GL_DEPTH_TEST);
GLuint uniformModel = glGetUniformLocation(program, "model");
GLuint uniformView = glGetUniformLocation(program, "view");
GLuint uniformProjection = glGetUniformLocation(program, "projection");
glm::mat4 projection = glm::perspective(70.0f, 1.0f, 0.0f, 16.0f);
glUniformMatrix4fv(uniformProjection, 1, GL_FALSE, glm::value_ptr(projection));
glm::vec3 eye = glm::vec3(sin(r*0.33)*5.0f,5,cos(r*0.33)*5.0f);
glm::vec3 center = glm::vec3(0.0f,0.0f,0.0f);
glm::vec3 up = glm::vec3(0.0f,0.0f,1.0f);
glm::mat4 view = glm::lookAt(eye, center, up);
glUniformMatrix4fv(uniformView, 1, GL_FALSE, glm::value_ptr(view));
glm::mat4 model = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f,0.0f,0.0f));
glUniformMatrix4fv(uniformModel, 1, GL_FALSE, glm::value_ptr(model));
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexAttribPointer(
0,
3,
GL_FLOAT,
GL_FALSE,
0,
(void*)0
);
glDrawElements(GL_TRIANGLES, 18, GL_UNSIGNED_INT, indices);
glDisableVertexAttribArray(0);
glDisable(GL_DEPTH_TEST);
glUseProgram(0);
glFlush(); // Render now
}
Vertex/Indices Array & Shader
string vert
= "#version 330 core\n"
"layout(location = 0) in vec3 vertex;\n"
"uniform mat4 model;\n"
"uniform mat4 view;\n"
"uniform mat4 projection;\n"
"void main(){\n"
" gl_Position = projection * view * model * vec4(vertex,1.0f);\n"
"}";
string frag
= "#version 330 core\n"
"out vec3 color;\n"
"void main()\n"
"{\n"
" float lin = 1.0 / gl_FragCoord.w;\n"
" float depth = (lin - 0.1) / (16.0 - 0.1);\n"
" color = vec3(depth,depth,1.0f);\n"
"}";
float* data = new float[36] {
-1.0f,-1.0f,0.0f,
1.0f,-1.0f,0.0f,
1.0f,1.0f,0.0f,
-1.0f,1.0f,0.0f,
0.0f,-1.0f,-1.0f,
0.0f,1.0f,-1.0f,
0.0f,1.0f,1.0f,
0.0f,-1.0f,1.0f,
-1.0f,0.0f,-1.0f,
1.0f,0.0f,-1.0f,
1.0f,0.0f,1.0f,
-1.0f,0.0f,1.0f
};
GLuint* indices = new GLuint[18] {
0,1,2,
0,3,2,
4,5,6,
4,7,6,
8,9,10,
8,11,10
};
Init
const int winSize = 1024;
void glInit(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE);
glutInitContextVersion(3,3);
//glutInitContextFlags(GLUT_CORE_PROFILE | GLUT_DEBUG);
glutInitWindowSize(winSize, winSize);
glutInitWindowPosition(25, 25);
glutCreateWindow("Loading...");
glewExperimental = GL_TRUE;
glewInit();
glViewport (0, 0, winSize, winSize);
camera.setPosition(0.0f,0.0f,4.0f);
glGenVertexArrays(1, &vba);
glBindVertexArray(vba);
program = compileShader(vert, frag);
GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, 3*4*3*4, data, GL_STATIC_DRAW);
glEnable(GL_DEPTH_TEST);
while(!bExit) {
string pre;
pre.assign("Test Program - ");
pre.append(std::to_string(fps));
glutSetWindowTitle(pre.c_str());
frames++;
display();
}
}
Turns out I'm an idiot and it was just a typo kind of error.
I define GLuint vbo in the init method AND on the scope of the program as well but the actual value of the vbo is only put into local variable. display() didn't have visibility of the vbo at all.
GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
It's always the little things that cause ridiculous bugs. <_>