Opengl triangle not drawn - c++

I'm trying to draw a simple triangle on the screen. The window gets properly set up and everything seems okay to the point, where I'm trying to actually draw stuff. I have the following code, but it doesn't work for some reason.
default_shader_program is program linking shaders shown after the function code
void window::run(void(*update)(), void(*draw)()) {
while (glfwWindowShouldClose(wnd) == 0)
{
update();
glClear(GL_COLOR_BUFFER_BIT);
draw();
GLuint vb;
GLfloat data[12]{
1.f, 1.f, 0.f,
1.f, -1.f, 0.f,
-1.f, 0.f, 0.f
};
glGenBuffers(1, &vb);
glBindBuffer(GL_ARRAY_BUFFER, vb);
glBufferData(GL_ARRAY_BUFFER, 12, data, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
GLuint va;
glGenVertexArrays(1, &va);
glBindVertexArray(va);
glUseProgram(default_shader_program);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vb);
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
);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(0);
glfwSwapBuffers(wnd);
glfwPollEvents();
}
glfwDestroyWindow(wnd);
}
Shaders:
vertexshader:
#version 330 core
layout(location=0) in vec3 vertexPosition;
void main() {
gl_Position = vec4(vertexPosition, 1);
}
fragmentshader:
#version 330 core
out vec3 color;
void main() {
color = vec3(1.0, 0.0, 0.0);
}
Yes, I know, that I am buffering data and doing other stuff in while loop. I know, that it shouldn't be done like that, but the actual problem occured somewhere else and I wanted to see if code will work properly directly inside the loop.

Your array contains 9 elements instead of 12, but that doesn't cause the issue.
The 2nd argument of glBufferData is the buffer size in bytes. Hence the size of the buffer is 9*sizeof(GLfloat) rather than 12:
glBufferData(GL_ARRAY_BUFFER, 12, data, GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(GLfloat), data, GL_STATIC_DRAW);

Related

How to Draw OpenMesh using OpenGL

i'm working on a project where i'm using OpenMesh to read stl and obj files and draw them on the screen using openGL.
i've been doing the following,
#include <OpenMesh/Core/Mesh/TriMesh_ArrayKernelT.hh>
#include <OpenMesh/Core/IO/MeshIO.hh>
OpenMesh::TriMesh_ArrayKernelT<> mesh;
std::vector<point> vertices;
std::vector<point> normals;
void readMesh(std::string file)
{
OpenMesh::IO::read_mesh(mesh, file);
mesh.requestFaceNormals();
mesh.request_vertex_normals();
mesh.updateNormals();
vertices.clear();
normals.clear();
for (auto face : mesh.faces())
{
for (auto vertex : mesh.fv_range(face))
{
auto point = mesh.point(vertex);
auto normal = mesh.normal(face);
vertices.push_back(point);
normals.push_back(normal);
}
}
mesh.releaseFaceNormals();
mesh.releaseVertexNormals();
}
and when drawing i just pass the vertices and normals vectors to the vertex shader like this
void paint()
{
glSetAttributeArray(0, vertices.data());
glSetAttributeArray(1, normals.data());
glDrawArrays(GL_TRIANGLES, 0, vertices.length());
}
where the vertex shader looks like this:
attribute vec3 position;
attribute vec3 normal;
uniform mat4 modelViewMatrix;
void main(void)
{
vec4 color = vec4(0.25, 0.25, 0.25, 0.0);
vec4 P = vec4(position, 0);
vec4 N = vec4(normal, 0);
vec3 L = vec3(20, 20, 20) - position;
vec3 V = -position;
N = normalize(N);
L = normalize(L);
V = normalize(V);
vec3 R = reflect(-L, vec3(N));
vec3 diffuse = max(dot(vec3(N), L), 0.0) * color.rgb;
vec3 specular = pow(max(dot(R, V), 0.0), 0.2) * vec3(0.1, 0.1, 0.1);
color = vec4(color.a * (ambient + diffuse + specular), color.a);
color = clamp(color, 0.0, 1.0);
gl_Color = color;
gl_Position = modelViewMatrix * P;
}
and the fragment shader is:
void main(void)
{
gl_FragColor = gl_Color;
}
this produces pretty good results, but the idea of having another copy of the vertices and normals stored in another location (normals and vertices) to be able to draw the mesh looks very counter-intuitive.
i was wondering if i can use openGL buffers with openMesh to optimize this. i've been searching for anything concerning this topic for a while but found nothing.
See Vertex Specification. You can create 2 Vertex Buffer Object for the verticex cooridantes and nortmal vertors:
GLuint vbos[2];
glGenBuffers(2, vbos);
glBindBuffer(GL_ARRAY_BUFFER, vbos[0]);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(vertices[0]), vertices.data(), GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, vbos[1]);
glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(normals[0]), normals.data(), GL_STATIC_DRAW);
If you use OpenGL 3.0 or later, then you can specify a Vertex Array Object a nd state the vertex specification:
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbos[0]);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
glBindBuffer(GL_ARRAY_BUFFER, vbos[1]);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
When you want to draw the mesh, then it is sufficient to bind the VAO:
glBindVertexArray(vao);
glDrawArrays(GL_TRIANGLES, 0, vertices.length());
If you use OpenGL 2.0, the you cannot create a VAO, thus you have to specify the arrays of generic vertex attribute data, before drawing the mesh:
glBindBuffer(GL_ARRAY_BUFFER, vbos[0]);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
glBindBuffer(GL_ARRAY_BUFFER, vbos[1]);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glDrawArrays(GL_TRIANGLES, 0, vertices.length());
Furthermore note, that the attribute indices are not guaranteed to be 0 and 1. The attribute indices can be any arbitrary number.
If you would use GLSL version 3.30 the it would be possible to set the attribute indices in the shader code by Layout Qualifier.
Anyway you an define the attribute indices by glBindAttribLocation before linking the program or retrieve the attribute indices by glGetAttribLocation after linking the program.

Passing colour coords into a vertex shader through a vertex attrib pointer

I am trying to get vec3 colour data to a vertex shader (as an attrib pointer) , and then passing that into the fragment to be all settup .
It all started off by not being able to pass texture coords into the vertex shader . I've looked everywhere but not been able to find a definitive soloution . I can (by setting the colour manually in the vertex shader) , get a white square to renderer, but the colour data from the VAO cannot seem to be passed to it.
Also i'm certain that the shader loading system works . My best idea is that info is not being passed to the 'layout = 1' of the vertex shader, but I'm most likely wrong.
VAO / Buffer / shader settup
float vert[] = {
//Vert Locs Normal Coords
-0.5f,-0.5f, 1.0f,1.0f,1.0f, //0
0.5f,-0.5f, 1.0f,1.0f,1.0f, //1
0.5f,0.5f, 1.0f,1.0f,1.0f, //2
-0.5f, 0.5f, 1.0f,1.0f,1.0f, //3
};
unsigned int index[] = {
0,1,2, //Order of drawing vertices
2,3,0
};
unsigned int vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
unsigned int buffer;
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, 5 * 4 * sizeof(float), vert, GL_STATIC_DRAW);
glVertexAttribPointer(0, 2, GL_FLOAT,GL_FALSE, 5 * sizeof(float), 0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(2 * sizeof(float)));
glEnableVertexAttribArray(0); // vao ^^
unsigned int ibo;
glGenBuffers(1, &ibo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6 * sizeof(unsigned int), index, GL_STATIC_DRAW);
Shader v("VertexN.shader", GL_VERTEX_SHADER);
Shader f("FragmentN.shader", GL_FRAGMENT_SHADER);
unsigned int shaders2[] = {v.id, f.id};
unsigned int program2 = Shader::getProgram(shaders2);
glUseProgram(0);
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Render loop
glClearColor(0, 0, 0, 1);
/* Render here */
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glUseProgram(program2);
//tex.bind(slot);
glBindVertexArray(vao);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); //Drawing mode, Type indecies , type of index buffer , pointer to index buffer
glfwSwapBuffers(window);
glfwPollEvents();
glFlush();
glBindVertexArray(0);
glUseProgram(0);
glActiveTexture(0);
Vertex shader
#version 330 core
layout(location = 0)in vec2 pos;
layout(location = 1)in vec3 normals;
out vec3 normal;
void main() {
normal = normals;
gl_Position = vec4(pos, 0.0f, 1.0f);
};
Fragment shader
#version 330 core
layout(location = 0)out vec4 colour;
in vec3 normal;
void main() {
colour = vec4(normal, 1.0f);
};
I have no idea what that source code comment here:
glEnableVertexAttribArray(0); // vao ^^
is supposed to mean. However, I get the impression that you misunderstood what this function does.
The GL manages a set of generic vertex attributes (the spec guarantees that there are at least 16, maybe more), and for each such attribute, the data can come from an array. Besides having to specify the attribute pointer and data formats, you also have to enable the array from each attribute individually. When a draw call is made, for each vertex index i, the GPU will fetch the i-the value in the attribute array for each attribute where the array is enabled. For attributes where the array is disabled, it will use a constant value throughout the whole draw call. (You can set it via the glVertexAttrib*() familiy of functions before issuing the draw call).
Since you never enable the array for attribute 1 (your normals input for the shader), all vertices will see the current value of attribute 1 - most likely just vec4(0) as that is the initial default, and you never seem to have changed it.
The solution of course is to enable all the attribute arrays you want to fetch from:
glEnableVertexAttribArray(1);
And, while you're at it: In your render loop, you have
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
That is not necessary. The VAO will store this information already, there is no need to re-bind it for drawing.

glDrawArrays weirdly draws array

When I try to draw circle using glDrawArrays it displays quarter of circle
The VBO, generated by code, is correct. BTW there is no such coord as 0,0,0.
It appears that it draws only positive vertices, but if I multiply vertices by -1 glDrawArrays draws it on other side of screen.
Here's my code:
uint un_vertexArraySize = 20;
float f_radius = 1.0f;
float f_degrees = 0;
GLfloat f_vert[un_vertexArraySize*3];
// Generate circle vertices
for (int i=0; i<un_vertexArraySize*3; i+=3)
{
f_vert[i] = cos(f_degrees*M_PI/180)*f_radius;
f_vert[i+1] = sin(f_degrees*M_PI/180)*f_radius;
f_vert[i+2] = 0.0f;
f_degrees += (360/un_vertexArraySize);
}
// Do buffer stuff
GLuint vbo;
glGenBuffers(1, &vbo);
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, un_vertexArraySize*3,
f_vert, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0); // unbind VBO
glBindVertexArray(0); // unbind VAO
// Start loop
glClearColor(0.0,0.0,0.0,0.0);
glClear(GL_COLOR_BUFFER_BIT);
shader.Use();
glBindBuffer(GL_ARRAY_BUFFER, vao);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glDrawArrays(GL_LINE_STRIP, 0, un_vertexArraySize*3);
glDisableVertexAttribArray(0);
shader.UnUse();
// End loop
Frag shader:
#version 130
void main(void)
{
gl_FragColor[0] = gl_FragCoord.x/640.0;
gl_FragColor[1] = gl_FragCoord.y/480.0;
gl_FragColor[2] = 0.2;
gl_FragColor[3] = 1.0f;
}
Vert shader:
#version 130
attribute vec3 coord3d;
void main(void)
{
vec4 temp = vec4(coord3d, 1.0f);
gl_Position = temp;
}
I tried to google it or find on stackoverflow, but nothing.
I can't test this right now but at glBufferData you use un_vertexArraySize*3, that is the number of elements, but I think you missed multiplying by 4 (bytes per float) so the size is specified in bytes. That would explain why you're only drawing a quarter of the circle

Issues adding instancing to a working sprite system

I'm trying to implement instancing into my 2d Game Engine so that it can support particle systems without losing any performance. My class, ISprite, is derived from a working Sprite class. I just went through and removed all the functionality affecting single sprites and replaced it with an instancing plan in mind. Unfortunately, nothing is drawing on the screen.
Here is the relevant information:
Vertex Shader
#version 330 core
layout (location = 0) in vec3 position;
layout (location = 1) in vec2 texCoords;
layout (location = 2) in vec4 colorSource;
layout (location = 3) in mat4 transform;
out vec2 TexCoords;
out vec4 Color;
uniform mat4 uniformView;
uniform mat4 uniformProjection;
void main()
{
gl_Position = uniformProjection * uniformView * transform * vec4(position, 1.0f);
TexCoords = texCoords;
Color = colorSource;
}
Fragment Shader
#version 330 core
in vec2 TexCoords;
in vec4 Color;
out vec4 color;
uniform sampler2D Texture;
uniform vec4 uniformColor;
void main()
{
vec4 texColor = texture(Texture, TexCoords) * Color;
if(texColor.a < 0.1)
discard;
color = texColor;
}
Load - Prepares all sprites for drawing, called once.
void ISprite::Load(Shader spriteShader)
{
spriteShader.Use();
GLfloat vertices[] = {
//X Y Z
0.5f, -0.5f, 0.0f,
-0.5f, 0.5f, 0.0f,
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.5f, 0.5f, 0.0f,
-0.5f, 0.5f, 0.0f
};
glGenVertexArrays(1, &vertexArray);
glGenBuffers(1, &positionBuffer);
glGenBuffers(1, &texCoordsBuffer);
glGenBuffers(1, &colorBuffer);
glGenBuffers(1, &matrixBuffer);
glBindVertexArray(vertexArray);
//The vertex data will never change, so send that data now.
glBindBuffer(GL_ARRAY_BUFFER, positionBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
//For vertex Position
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(GLfloat), (GLvoid*)0);
//For texture coordinates
glBindBuffer(GL_ARRAY_BUFFER, texCoordsBuffer);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 2*sizeof(GLfloat), (GLvoid*)0);
//For Color
glBindBuffer(GL_ARRAY_BUFFER, colorBuffer);
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), (GLvoid*)0);
//For Transformation Matrix
glBindBuffer(GL_ARRAY_BUFFER, matrixBuffer);
for (int i = 0; i < 4; ++i)
{
glEnableVertexAttribArray(3 + i);
glVertexAttribPointer(3 + i, 4, GL_FLOAT, GL_FALSE,
4 * 4 * sizeof(GLfloat), (GLvoid*)(4 * i * sizeof(GLfloat)));
}
glBindBuffer(GL_ARRAY_BUFFER, positionBuffer);
glBindBuffer(GL_ARRAY_BUFFER, texCoordsBuffer);
glBindBuffer(GL_ARRAY_BUFFER, colorBuffer);
glBindBuffer(GL_ARRAY_BUFFER, matrixBuffer);
glBindVertexArray(0);
glVertexAttribDivisor(positionBuffer, 0);
glVertexAttribDivisor(texCoordsBuffer, 1);
glVertexAttribDivisor(colorBuffer, 1);
glVertexAttribDivisor(matrixBuffer, 1);
glVertexAttribDivisor(matrixBuffer + 1, 1);
glVertexAttribDivisor(matrixBuffer + 2, 1);
glVertexAttribDivisor(matrixBuffer + 3, 1);
ISprite::shader = &spriteShader;
}
Prepare Draw - called by each sprite, each frame. Sends data to static vectors
void ISprite::prepareDraw(void)
{
//Adds their personal data to vectors shared by class
glm::mat4 transform = calculateTransorm();
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
ISprite::transformMatrices.push_back(transform[i][j]);
}
texture.updateAnimation();
for (int i = 0; i < 12; ++i)
ISprite::textureCoordinatesAll.push_back(texture.textureCoordinates[i]);
ISprite::colorValues.push_back(color.x);
ISprite::colorValues.push_back(color.y);
ISprite::colorValues.push_back(color.z);
ISprite::colorValues.push_back(color.w);
}
Draw Sprites - called once each frame, actually draws the sprites
void ISprite::drawSprites(Texture testTexture)
{
shader->Use();
for (std::vector<ISprite*>::iterator it = Isprites.begin(); it != Isprites.end(); ++it)
(*it)->prepareDraw();
glBindVertexArray(vertexArray);
glBindTexture(GL_TEXTURE_2D, testTexture.ID);
//Bind texture here if you want textures to work. if not, a single texture atlas will be bound
glBindBuffer(GL_ARRAY_BUFFER, texCoordsBuffer);
glBufferData(GL_ARRAY_BUFFER, textureCoordinatesAll.size() * sizeof(GLfloat),
textureCoordinatesAll.data(), GL_STREAM_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, colorBuffer);
glBufferData(GL_ARRAY_BUFFER, colorValues.size() * sizeof(GLfloat),
colorValues.data(), GL_STREAM_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, matrixBuffer);
glBufferData(GL_ARRAY_BUFFER, transformMatrices.size() * sizeof(GLfloat),
transformMatrices.data(), GL_STREAM_DRAW);
glDrawArraysInstanced(GL_TRIANGLE_STRIP, 0, 6, Isprites.size());
textureCoordinatesAll.clear();
colorValues.clear();
transformMatrices.clear();
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindTexture(GL_TEXTURE_2D, 0);
glBindVertexArray(0);
}
There could be a lot of reasons why nothing is rendered. Problems with the transformations, coordinates out of range, etc. But one thing related to instancing definitely looks wrong in the posted code:
glBindVertexArray(0);
glVertexAttribDivisor(positionBuffer, 0);
glVertexAttribDivisor(texCoordsBuffer, 1);
glVertexAttribDivisor(colorBuffer, 1);
...
The first argument to glVertexAttribDivisor() is the location of a vertex attribute, not the name of a buffer. Also, the state set by this call is part of the VAO state, so you should make these calls while the VAO is still bound.
So the calls should look like this:
glVertexAttribDivisor(0, 0);
glVertexAttribDivisor(1, 0);
glVertexAttribDivisor(2, 1);
...
glBindVertexArray(0);
where the first arguments to glVertexAttribDivisor() match the location values you also use as the first argument to glVertexAttribPointer() and glEnableVertexAttribArray().
The divisor value for the texture coordinates (attribute 1) should most likely be 0, since you want the texture coordinates to be set per vertex, just like the positions. For the colors and other remaining attributes, 1 is the correct value so that they are applied per instance.
Also, as I mentioned in a comment, you may also want to look into using point sprites. While they do not offer the same flexibility you can get from drawing individual quads, they can often be used for sprites. With point sprites, you only need one vertex per sprite, and the texture coordinates are generated automatically. My answer here gives an outline on how point sprites are used, including how to apply textures to them: Render large circular points in modern OpenGL.

Displaying Models in OpenGL C++

|Actually, I am trying to display two or more models on the screen at the same time. I am generating three different vertex buffers for vertices, colors, normals separately, here is the main code:
//==================From here is my implementation ==========================//
// Create and compile our GLSL program from the shaders. Todo: load shader
GLuint programID = LoadShaders("simple_shading.vertexshader", "simple_shading.fragmentshader");
// Get a handle for uniform variables such as matrices, lights, ....
GLuint ModelMatrixID = glGetUniformLocation(programID, "ModelMatrix");
GLuint ViewMatrixID = glGetUniformLocation(programID, "ViewMatrix");
GLuint NormalMatrixID = glGetUniformLocation(programID, "NormalMatrix");
GLuint lightPosition_worldspaceID = glGetUniformLocation(programID, "lightPosition_worldspace");
GLuint lightColorID = glGetUniformLocation(programID, "lightColor");
// Get a handle for vertex attribute arrays such as vertex positions, colors, normals, ....
GLuint vertexPosition_modelspaceID = glGetAttribLocation(programID, "vertexPosition_modelspace");
GLuint vertexColorID = glGetAttribLocation(programID, "vertexColor");
GLuint vertexNormal_modelspaceID = glGetAttribLocation(programID, "vertexNormal_modelspace");
// Load your scene
TRIModel model;
TRIModel model2;
model.loadFromFile("models/ball.tri");
model2.loadFromFile("models/ball.tri");
//vector<TRIModel> models;
/*** Here shows an exmple which generates three different vertex buffers for vertices, colors, normals.***/
//However, you can always incorporate them together in a single buffer.(Hint: use glBufferSubData)
GLuint vertexbuffer;
glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, model.vertices.size() * sizeof(vec3), &model.vertices[0], GL_STATIC_DRAW);
GLuint colorbuffer;
glGenBuffers(1, &colorbuffer);
glBindBuffer(GL_ARRAY_BUFFER, colorbuffer);
glBufferData(GL_ARRAY_BUFFER, model.vertices.size() * sizeof(vec3), &model.foreColors[0], GL_STATIC_DRAW);
GLuint normalbuffer;
glGenBuffers(1, &normalbuffer);
glBindBuffer(GL_ARRAY_BUFFER, normalbuffer);
glBufferData(GL_ARRAY_BUFFER, model.vertices.size() * sizeof(vec3), &model.normals[0], GL_STATIC_DRAW);
///*** **/
GLfloat rotZ;
while (!glfwWindowShouldClose(window))
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// //Tell OpenGL to use the shader program
glUseProgram(programID);
// //send matrix to shader: You can use the keyboard and mouse to close the window and control the transformation
// // (See computeMatricesFromKey(window) )
mat4 ModelMatrix;
mat4 ViewMatrix = lookAt( vec3(0.5, 0.5, 1), vec3(2, 3, 2), vec3(0, 1.0, 0.0) );
//move object to the world origin
ModelMatrix = translate(mat4(1.0), -vec3(model.center[0], model.center[1], model.center[2]));
ModelMatrix = scale( ModelMatrix, vec3(0.002, 0.002, 0.002) );
ModelMatrix = rotate( ModelMatrix, rotZ, vec3(0.0, 0.0, 1.0) );
mat4 NormalMatrix = transpose( inverse(ViewMatrix * ModelMatrix) );
glUniformMatrix4fv(ViewMatrixID, 1, GL_FALSE, &ViewMatrix[0][0]);
glUniformMatrix4fv(ModelMatrixID, 1, GL_FALSE, &ModelMatrix[0][0]);
glUniformMatrix4fv(NormalMatrixID, 1, GL_FALSE, &NormalMatrix[0][0]);
// //send lights to shader
vec3 lightPosition = vec3(4, 4, 4);
vec3 lightColor = vec3(1.0, 1.0, 1.0);
glUniform3f(lightPosition_worldspaceID, lightPosition.x, lightPosition.y, lightPosition.z);
glUniform3f(lightColorID, lightColor.r, lightColor.g, lightColor.b);
//define an vertex attribute array on the vertex buffer
glEnableVertexAttribArray(vertexPosition_modelspaceID);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(
vertexPosition_modelspaceID, // The attribute we want to configure
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*) 0 // array buffer offset
);
// //define an vertex attribute array on the color buffer
glEnableVertexAttribArray(vertexColorID);
glBindBuffer(GL_ARRAY_BUFFER, colorbuffer);
glVertexAttribPointer(
vertexColorID, // The attribute we want to configure
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*) 0 // array buffer offset
);
// //define an vertex attribute array on the normal buffer
glEnableVertexAttribArray(vertexNormal_modelspaceID);
glBindBuffer(GL_ARRAY_BUFFER, normalbuffer);
glVertexAttribPointer(
vertexNormal_modelspaceID, // The attribute we want to configure
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*) 0 // array buffer offset
);
//draw the scene by vertex attribute arrays
glDrawArrays(GL_TRIANGLES, 0, model.vertices.size());
glDisableVertexAttribArray(vertexPosition_modelspaceID);
glDisableVertexAttribArray(vertexColorID);
glDisableVertexAttribArray(vertexNormal_modelspaceID);
glfwSwapBuffers(window);
glfwPollEvents();
//
// //update rotate angle
rotZ += 0.1;
}
//// Cleanup VBO and shade //glDeleteBuffers(1, &vertexbuffer);
glDeleteBuffers(1, &colorbuffer);
glDeleteBuffers(1, &normalbuffer);
glDeleteProgram(programID);
//=================== implementation Ends here ================================//
I read in some resources that I can incorporate them together in a single buffer using glBufferSubData, but I dont know how to use it. eventually, I have successfully displayed one model as shown in the picture. Now, if I need to display another model, lets say I want to display another ball on the right side, what should I do? I tried to generate a nother set of buffers , but it is still not working. what I can see is just model one. Any ideas?