I'm trying to texture the ground with grass. Would someone mind looking through my steps to get a 2D texture to show up in 3D space? I haven't had any luck with online tutorials.
Here is my steps:
1) Call the sprite class initialization to set the vertex coordinates and uv data
void Sprite::init(vec3 bottomleft, vec3 topLeft, vec3 topRight, vec3 bottomRight, std::string texturePath)
{
_texture = ImageLoader::loadPNG(texturePath);
_points = { bottomleft.x, bottomleft.y, bottomleft.z, topLeft.x, topLeft.y, topLeft.z, topRight.x, topRight.y, topRight.z, topRight.x, topRight.y, topRight.z, bottomRight.x, bottomRight.y, bottomRight.z, bottomleft.x, bottomleft.y, bottomleft.z };
_uv = { 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f };
}
I use the uniform sampler and activate the texture
//draw ground texture
glUniform1i(samplerLocation, 0);
glActiveTexture(GL_TEXTURE0);
_grassTexture.draw();
The draw function is implemented as follows, which puts the data in the buffer and draws the triangle:
void Sprite::draw()
{
if (_vboID == 0)
{
glGenBuffers(1, &_vboID);
}
glBindTexture(GL_TEXTURE_2D, _texture.id);
glBindBuffer(GL_ARRAY_BUFFER, _vboID);
glBufferData(GL_ARRAY_BUFFER, _points.size() * sizeof(float) + _uv.size() * sizeof(float), nullptr, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, _points.size() * sizeof(float), &_points.front());
glBufferSubData(GL_ARRAY_BUFFER, _points.size() * sizeof(float), _uv.size() * sizeof(float), &_uv.front());
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
//position attribute pointer
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, &_points.front());
//uv attribute pointer
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, &_uv.front());
glDrawArrays(GL_TRIANGLES, 0, 6);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
The vertex shader:
#version 400
in vec4 vertexPosition;
in vec2 vertexUV;
out vec4 fragmentColor;
out vec2 fragmentUV;
uniform mat4 MVP;
uniform vec4 COLOR;
void main()
{
gl_Position = MVP * vertexPosition;
fragmentColor = COLOR;
fragmentUV = vec2(vertexUV.x, 1.0 - vertexUV.y);
}
and fragment shader:
#version 400
in vec4 fragmentColor;
in vec2 fragmentUV;
out vec4 fragColor;
uniform sampler2D SAMPLER;
void main()
{
vec4 textureColor = texture(SAMPLER, fragmentUV);
fragColor = textureColor * fragmentColor;
}
At this point, nothing displays in the screen. I do know that the textureColor in the fragment shader is green, so it appears the uniform is being set correctly. My best guess is that I'm either missing a texture initialization step or am not filling the buffer correctly.
I think there's a problem with the pointer parameter in your calls to glVertexAttribPointer. You're passing the address of your _points and _uv arrays, but you should be passing the offset relative to the base of your vbo.
Try:
//position attribute pointer
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
//uv attribute pointer
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (const GLvoid*)(_points.size() * sizeof(float)));
Related
I have a renderer2D that is very basic but doesn't render my basic 2D quad correctly. It renders it as a single pixel at the bottom left of the window. The window size is 800x600
The orthographic projection matrices are calculated using glm::ortho(0.0f, 800.0f, 0.0f, 600.0f).
For some reason it only renders a single pixel at the wrong location. And to make it even more weirder, If i set the Model Matrix to translate to the middle of the screen, It renders that lone pixel at the middle of the screen
I call it like this in my renderer class :
m_Renderer2D.RenderQuad(glm::vec3(400.0f, 300.0f, 1.0f), &m_CrosshairTexture, &m_Camera2D);
Renderer class :
class Renderer2D
{
public:
Renderer2D();
void RenderQuad(const glm::vec2& position, GLClasses::Texture* texture, OrthographicCamera* camera);
private :
GLClasses::VertexBuffer m_VBO;
GLClasses::VertexArray m_VAO;
GLClasses::IndexBuffer m_IBO;
GLClasses::Shader m_DefaultShader;
};
cpp file :
Renderer2D::Renderer2D() : m_VBO(GL_ARRAY_BUFFER)
{
GLuint index_buffer[6] = { 0,1,2,2,3,0 };
m_VAO.Bind();
m_VBO.Bind();
m_IBO.Bind();
m_VBO.VertexAttribPointer(0, 3, GL_FLOAT, 0, 5 * sizeof(GLfloat), (void*)0);
m_VBO.VertexAttribPointer(0, 2, GL_FLOAT, 0, 5 * sizeof(GLfloat), (void*)(3 * sizeof(GLfloat)));
m_IBO.BufferData(6 * sizeof(GLuint), index_buffer, GL_STATIC_DRAW);
m_VAO.Unbind();
m_DefaultShader.CreateShaderProgramFromFile("Shaders/2DElementShaderVert.glsl", "Shaders/2DElementShaderFrag.glsl");
m_DefaultShader.CompileShaders();
}
void Renderer2D::RenderQuad(const glm::vec2& position, GLClasses::Texture* texture, OrthographicCamera* camera)
{
glDisable(GL_DEPTH_TEST);
const std::array<GLfloat, 8> texture_coords = texture->GetTextureCoords();
float x, y, w, h;
x = position.x;
y = position.y;
w = position.x + texture->GetWidth(); // The width and height of the texture is correct
h = position.y + texture->GetHeight();
GLfloat Vertices[] = {
w, y, 0.0f, texture_coords[0], texture_coords[1],
w, h, 0.0f, texture_coords[2], texture_coords[3],
x, h, 0.0f, texture_coords[4], texture_coords[5],
x, y, 0.0f, texture_coords[6], texture_coords[7],
};
m_DefaultShader.Use();
glm::mat4 proj = glm::ortho(0.0f, 800.0f, 0.0f, 600.0f);
texture->Bind(1);
m_DefaultShader.SetMatrix4("u_Projection", proj, 0);
m_DefaultShader.SetMatrix4("u_View", camera->GetViewMatrix(), 0);
m_DefaultShader.SetMatrix4("u_Model", glm::mat4(1.0f), 0);
m_DefaultShader.SetInteger("u_Texture", 1, 0);
// Draw the 2D quad
m_VAO.Bind();
m_VBO.BufferData(20 * sizeof(GLfloat), Vertices, GL_STATIC_DRAW);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, (void*)0);
m_VAO.Unbind();
glEnable(GL_DEPTH_TEST);
}
Shaders :
VERTEX SHADER :
#version 330 core
layout(location = 0) in vec3 a_Position;
layout(location = 1) in vec2 a_TexCoord;
out vec2 v_TexCoord;
uniform mat4 u_Projection;
uniform mat4 u_View;
uniform mat4 u_Model;
void main()
{
gl_Position = u_Projection * u_View * u_Model * vec4(a_Position, 1.0f);
v_TexCoord = a_TexCoord;
}
FRAGMENT SHADER :
#version 330 core
in vec2 v_TexCoord;
out vec4 o_Color;
uniform sampler2D u_Texture;
void main()
{
o_Color = texture(u_Texture, v_TexCoord);
}
The layout locations associate the vertex coordinates to attribute index 0 and the texture coordinates to attribute index 1:
layout(location = 0) in vec3 a_Position;
layout(location = 1) in vec2 a_TexCoord;
When you specify the arrays of generic vertex attribute data, then the vertex attribute array 0 is specified twice, but you missed to specify the vertex attribute array 1:
m_VBO.VertexAttribPointer(0, 3, GL_FLOAT, 0, 5 * sizeof(GLfloat), (void*)0);
m_VBO.VertexAttribPointer(0, 2, GL_FLOAT, 0, 5 * sizeof(GLfloat), (void*)(3 * sizeof(GLfloat)));
Hence the texture coordinates cover the vertex coordinates and the texture coordinate attribute is not specified at all.
The attribute index for the texture coordinates has to be 1 rather than 0:
m_VBO.VertexAttribPointer(0, 2, GL_FLOAT, 0, 5 * sizeof(GLfloat), (void*)(3 * sizeof(GLfloat)));
m_VBO.VertexAttribPointer(1, 2, GL_FLOAT, 0, 5 * sizeof(GLfloat), (void*)(3 * sizeof(GLfloat)));
I cannot get texture coordinates to have any effect on what is rendered on screen. My texture constantly seemed to rendering from 0,0 to 1,1.
The method I am using is to send the following buffer layout: x,y,u,v (xy: vertex position and u,v texture coordinates).
However, changing the values u & v have no effect on the rendered texture.
I've removed a lot of surrounding code to try and make it easier to read, but I can post it in full if the problem isn't immediately obvious to someone.
Vertex Shader:
#version 330 core
layout(location = 0) in vec2 position;
layout(location = 1) in vec2 mytextpos;
out vec2 v_TexCoord;
uniform mat4 u_Model;
void main()
{
gl_Position = u_Model * vec4(position, 0.0f, 1.0f);
v_TexCoord = mytextpos;
}
Fragment Shader:
#version 330 core
layout(location = 0) out vec4 color;
in vec2 v_TexCoord;
uniform sampler2D u_Texture;
void main()
{
color = texture(u_Texture, v_TexCoord);
}
My Rectangle Class Constructor:
// GENERATE BUFFERS
glGenVertexArrays(1, &VertexArrayObject);
glGenBuffers(1, &VertexBufferId);
glGenBuffers(1, &IndexBufferObjectId);
Indices = {
0, 1, 2,
2, 3, 0
};
unsigned int numberOfVertices = Vertices.size();
unsigned int numberOfIndices = Indices.size();
glBindVertexArray(VertexArrayObject);
glBindBuffer(GL_ARRAY_BUFFER, VertexBufferId);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IndexBufferObjectId);
Vertices = {
0.2f, 0.0f, 0.0f, 0.5f,
1.0f, 0.0f, 1.5f, 0.5f,
1.0f, 1.0f, 1.5f, 1.0f,
0.0f, 1.0f, 0.0f, 1.0f
};
// ADD BUFFER DATA
glBufferData( GL_ARRAY_BUFFER, Vertices.size() * sizeof(float), Vertices.data(), GL_STATIC_DRAW );
// ARRANGE ATTRIBUTES
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), nullptr);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), nullptr);
glEnableVertexAttribArray(1);
glBufferData( GL_ELEMENT_ARRAY_BUFFER, numberOfIndices * sizeof(unsigned int), Indices.data(), GL_STATIC_DRAW );`
Render Function:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
GLint u_Texture = glGetUniformLocation( shader.getId(), "u_Texture" );
glUniform1i( u_Texture, 0 );
GLint u_Model = glGetUniformLocation( shader.getId(), "u_Model" );
glUniformMatrix4fv( u_Model, 1, GL_FALSE, glm::value_ptr( rect.TransformMatrix() ) );
glDrawElements(GL_TRIANGLES, rect.Indices.size(), GL_UNSIGNED_INT, nullptr);
// SWAP BUFFERS
glfwSwapBuffers( _window->WindowInstance );
glfwPollEvents();
My program runs, but my texture is mapped between 0,0 and 1,0 no matter what my vertex or u,v positions are. I would expect the texture to be interpolated between the u,v coordinates for each vertex?
You attribute setup is wrong:
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), nullptr);
This tells OpenGL that it should attach the first two floats of each vertex to the mytextpos attribute. But you actually want it to read the 3rd and 4th float. Thus you have to set the offset such that it skips the first two floats:
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)(2 * sizeof(float)));
The picture is small ( black) square , should be iridescent (changing colors drawn from one to the other) but why it is stupid to black.
Thus it from was drawn, instead of threw out an error.
here's the code for drawing a small black square.
basic.vert and basic.frag standart starting textbooks (first examles).
void My_TwoSquares()
{
GLfloat vertires[] =
{
-0.2f, -0.2f, 0.0f,
-0.2f, 0.2f, 0.0f,
0.2f, 0.2f, 0.0f,
0.2f, -0.2f, 0.0f,
};
// color
GLfloat cwet[] = {
1.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f,
1.0f, 0.0f, 1.0f
};
GLuint indices[] =
{
0,1,2,
0,2,3
};
GLuint iboHandle;
GLuint vaoHandle;
GLuint vboHandles[2];
glGenBuffers(2, vboHandles);
GLuint positionBufferHandle = vboHandles[0];
GLuint colorBufferHandle = vboHandles[1];
glBindBuffer(GL_ARRAY_BUFFER, positionBufferHandle);
glBufferData(GL_ARRAY_BUFFER, 12 * sizeof(GLfloat), vertires, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, colorBufferHandle);
glBufferData(GL_ARRAY_BUFFER, 12*sizeof(GLfloat), cwet, GL_STATIC_DRAW);
glGenVertexArrays(1, &vaoHandle);
glBindVertexArray(vaoHandle);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, positionBufferHandle);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glBindBuffer(GL_ARRAY_BUFFER, colorBufferHandle);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glGenBuffers(1, &iboHandle);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, iboHandle);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6 * sizeof(GLuint), indices, GL_STATIC_DRAW);
ShaderProgram shaderprogram;
shaderprogram.loadShaders("basic.vert", "basic.frag");
shaderprogram.use();
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
}
here is the Shaders basic.frag and basic.vert
basic.frag
#version 330 core
uniform vec4 vertColor;
out vec4 frag_color;
void main()
{
frag_color = vertColor;
}
basic.vert
#version 330 core
layout (location = 0) in vec3 pos;
uniform vec2 posOffset;
void main()
{
gl_Position = vec4(pos.x + posOffset.x, pos.y + posOffset.y, pos.z, 1.0);
}
In your shader code vertColor is a Uniform variable. This uniform variable is never set and the default initialization of the uniform variable is 0 for all components. This causes that the rectangle is painted in black.
But you have vertex attributes. Each vertex coordinate is associated to a single color. You have to add an input to the Vertex Shader, for the color attribute (Vertex shader input).
#version 330 core
layout (location = 0) in vec3 pos;
layout (location = 1) in vec3 color;
This attribute has to be passed to the Fragment Shader, by an output of the vertex shader (Vertex shader output),
out vec3 vColor;
void main()
{
vColor = color;
// [...]
}
to an input to the fragment shader (Fragment shader input):
in vec3 vColor;
The outputs of the vertex shader (like the color) are interpolated according to its position on the (triangle) primitive (Barycentric coordinates). The input to the fragment shader is the interpolated attribute.
By this technique a gradient color can be achieved.
Vertex shader
#version 330 core
layout (location = 0) in vec3 pos;
layout (location = 1) in vec3 color;
out vec3 vColor;
uniform vec2 posOffset;
void main()
{
vColor = color;
gl_Position = vec4(pos.x + posOffset.x, pos.y + posOffset.y, pos.z, 1.0);
}
Fragment shader
#version 330 core
in vec3 vColor;
out vec4 frag_color;
void main()
{
frag_color = vec4(vColor.rgb, 1.0);
}
I have a vertex and fragment shader that worked fine not that long ago, but I tried to implement skeletal animation and somewhere along the line broke something. I have even gone away from using my custom functions and just set up a simple "Draw a triangle and color it" code block, but it's not working.
Drawing code:
GLfloat vertices[] = {
0.5f, 0.5f, 0.0f, // Top Right
0.5f, -0.5f, 0.0f, // Bottom Right
-0.5f, -0.5f, 0.0f, // Bottom Left
-0.5f, 0.5f, 0.0f // Top Left
};
GLuint indices[] = { // Note that we start from 0!
0, 1, 3, // First Triangle
1, 2, 3 // Second Triangle
};
GLuint VBO, VAO, EBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
// 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);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, 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);
while (!win.windowShouldClose) {
win.clearScreen(glm::vec4(1.0f,1.0f,1.0f,1.0f));
newShader.Use();
GLint mvpLoc = glGetUniformLocation(newShader.Program, "MVP");
glUniformMatrix4fv(mvpLoc, 1, GL_FALSE, glm::value_ptr(camera.proj * camera.view));
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
win.display();
}
Vertex shader:
#version 430
layout (location = 0) in vec3 position;
layout (location = 1) in vec3 normal;
layout (location = 2) in vec2 texCoords;
out vec2 TexCoords;
out vec3 ourColor;
uniform mat4 MVP;
void main()
{
gl_Position = MVP * vec4(position,1.0f);
TexCoords = texCoords;
ourColor = vec3(1.0f, 0.0f, 1.0f);
}
Fragment shader:
#version 430
in vec2 TexCoords;
in vec3 ourColor;
out vec4 color;
uniform sampler2D texture_diffuse1;
void main() {
color = vec4(ourColor,1.0f);
}
I have spent days working on this and I'm no closer to figuring out the solution. I believed that it was a problem with the data I was sending to the shaders, but even making the output of the fragment a constant as shown, the triangles are still black and not affected by the vertex shader at all, as if they were going straight from NDC to screen space.
Because of you did not show the camera attribute, I doubt there is any problem with your projection and view matrix (i.e., the MVP matrix may transform the object out of the screen). And I just have tested your program with glfw library at my computer, and the result of my test shows your code is OK. That is to say, there is no problem with "I believed that it was a problem with the data I was sending to the shaders"
Try to remove vertex shader's MVP matrix:
#version 430
layout (location = 0) in vec3 position;
layout (location = 1) in vec3 normal;
layout (location = 2) in vec2 texCoords;
out vec2 TexCoords;
out vec3 ourColor;
uniform mat4 MVP;
void main()
{
// Remove MVP matrix
gl_Position = vec4(position,1.0f);
TexCoords = texCoords;
ourColor = vec3(1.0f, 0.0f, 1.0f);
}
And the error is "Access violation reading location in nvoglv64.dll"
The program worked as intended on my old AMD Radeon HD 6970. A few days ago I bought a GTX 970. Great card, but I would like my program to work. I want to render a quad to the screen for deferred rendering. I try to use OpenGL 4.4
Source:
Preparing the quad:
modelMatrix = mat4(1.0);
vertices.push_back(vec3(-1.0f, -1.0f, 0.0f));
vertices.push_back(vec3(1.0f, -1.0f, 0.0f));
vertices.push_back(vec3(1.0f, 1.0f, 0.0f));
vertices.push_back(vec3(-1.0f, -1.0f, 0.0f));
vertices.push_back(vec3(1.0f, 1.0f, 0.0f));
vertices.push_back(vec3(-1.0f, 1.0f, 0.0f));
normals.push_back(vec3(0.0f, 0.0f, 1.0f));
normals.push_back(vec3(0.0f, 0.0f, 1.0f));
normals.push_back(vec3(0.0f, 0.0f, 1.0f));
normals.push_back(vec3(0.0f, 0.0f, 1.0f));
normals.push_back(vec3(0.0f, 0.0f, 1.0f));
normals.push_back(vec3(0.0f, 0.0f, 1.0f));
indices.push_back(0);
indices.push_back(1);
indices.push_back(2);
indices.push_back(0);
indices.push_back(2);
indices.push_back(3);
uvs.push_back(vec2(0.0f, 0.0f));
uvs.push_back(vec2(1.0f, 0.0f));
uvs.push_back(vec2(1.0f, 1.0f));
uvs.push_back(vec2(0.0f, 0.0f));
uvs.push_back(vec2(1.0f, 1.0f));
uvs.push_back(vec2(0.0f, 1.0f));
indexCount = static_cast<int>(indices.size());
is2D = true;
unsigned int handle[2];
glGenBuffers(2, handle);
glBindBuffer(GL_ARRAY_BUFFER, handle[0]);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), &vertices[0], GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, handle[1]);
glBufferData(GL_ARRAY_BUFFER, uvs.size() * sizeof(glm::vec3), &uvs[0], GL_STATIC_DRAW);
glGenVertexArrays(1, &array2D);
glBindVertexArray(array2D);
glBindBuffer(GL_ARRAY_BUFFER, handle[0]);
glVertexAttribPointer((GLuint)0, 3, GL_FLOAT, GL_FALSE, 0, ((GLubyte *)NULL + (0)));
glEnableVertexAttribArray(0); // Vertex position
glBindBuffer(GL_ARRAY_BUFFER, handle[1]);
glVertexAttribPointer((GLuint)2, 2, GL_FLOAT, GL_FALSE, 0, ((GLubyte *)NULL + (0)));
glEnableVertexAttribArray(1); // Texture coordinates
glBindVertexArray(0);
Rendering the quad:
// The following three lines are called in the render loop (as 2nd pass).
// I skip rendering meshes in the first pass to better understand this error.
//sp->useSubRoutine(srp2); // SP is the shader program
//sp->resetMatrices(); // Set matrices to mat4(1.0);
//dq->render(); // DQ is the display quad
glBindFramebuffer(GL_FRAMEBUFFER, fbo); // fbo is 0 for quad
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDisable(GL_DEPTH_TEST);
if (shader != nullptr)
{
shader->use();
shader->setModelMatrix(modelMatrix);
}
if (is2D)
{
glBindVertexArray(array2D);
glDrawArrays(GL_TRIANGLES, 0, indexCount); // ERROR HERE
return;
}
Vertex Shader:
#version 440
layout(location = 0) in vec3 vertexPosition;
layout(location = 1) in vec2 vertexUV;
layout(location = 2) in vec3 vertexNormal;
centroid out vec2 UV;
out vec4 position;
out vec3 normal;
uniform mat4 uniMatModel;
uniform mat4 uniMatView;
uniform mat4 uniMatProjection;
uniform mat4 uniMatModelView;
uniform mat4 uniMatModelViewProjection;
uniform mat3 uniMatNormal;
void main()
{
UV = vertexUV;
position = uniMatModelView * vec4(vertexPosition, 1.0);
normal = normalize(uniMatNormal * vertexNormal);
gl_Position = uniMatModelViewProjection * vec4(vertexPosition, 1.0);
}
Fragment Shader:
#version 440
struct lightInfo {
vec4 position;
vec3 intensity;
bool isActive;
};
// IN
centroid in vec2 UV;
in vec4 position;
in vec3 normal;
// OUT
layout (location = 0) out vec4 fragColor;
layout (location = 1) out vec3 positionData;
layout (location = 2) out vec3 normalData;
layout (location = 3) out vec3 colorData;
// SUBROUTINES
subroutine void renderPassType();
subroutine uniform renderPassType renderPass;
// UNIFORMS
uniform sampler2D uniSamTexture;
uniform sampler2D uniSamAlpha;
uniform sampler2D uniSamAmbient;
uniform sampler2D uniSamSpecular;
uniform sampler2D uniSamShininess;
uniform lightInfo uniPointLights[32];
uniform vec3 uniVec3AmbientEmissiveness;
layout(binding=0) uniform sampler2D positionTex;
layout(binding=1) uniform sampler2D normalTex;
layout(binding=2) uniform sampler2D colorTex;
subroutine (renderPassType)
void renderWorld()
{
if (texture2D(uniSamAlpha, UV).rgb[0] == 1.0) // Alphamaps have to be inverted
{
discard;
}
else
{
colorData = texture2D(uniSamTexture, UV).rgb;
}
positionData = vec3(position.x, position.y, position.z);
normalData = normal;
fragColor = vec4(colorData, 1.0);
}
subroutine (renderPassType)
void renderLight()
{
fragColor = vec4(1.0,0.0,0.0,1.0); // For testing purposes set to red
}
void main()
{
renderPass();
}
Such problems can occure on NVIDIA cards when you try to read from a vertex buffer that does not have enough elements in it. For example rendering 9 vertices from a buffer that only contains 6. AMD interestingly does not complain about that.
In your case you bind a vertex buffer to attribute location 2, but you activate location 1. This:
glVertexAttribPointer((GLuint)2, 2, GL_FLOAT, GL_FALSE, 0, ((GLubyte *)NULL + (0)));
glEnableVertexAttribArray(1); // Texture coordinates
should actually be
||
\/
glVertexAttribPointer((GLuint)1, 2, GL_FLOAT, GL_FALSE, 0, ((GLubyte *)NULL + (0)));
glEnableVertexAttribArray(1); // Texture coordinates
Edit:
I just saw another thing
glBufferData(GL_ARRAY_BUFFER, uvs.size() * sizeof(glm::vec3), &uvs[0], GL_STATIC_DRAW);
uvs.size() equals 6 in your application, so OpenGL will try to read 6 * 3 = 18 float. Your uvs-array only contains 12 floats (since you are using vec2 here).