PDA

View Full Version : Having problems with displaying VBOs



UdvPeter
05-20-2013, 01:58 AM
Hello all I'm trying to use vbos in my OpenGL application and a had some difficulties.
I read a tutorial(www.opengl-tutorial.org) and now I'm trying to display a cube with my own classes but nothing displays.
The program prepares the VBOs like this:

m_nVBOid = 0;

GLsizeiptr nSizePosition = 0;
GLsizeiptr nSizeNormal = 0;
GLsizeiptr nSizeTexcoord = 0;
GLsizeiptr nSizeTangent = 0;


if(!m_tDataPosition.empty()) {
nSizePosition = m_tDataPosition.size()*sizeof(vec3);
}
else {
return false;
}



if(!m_tDataNormal.empty()) {
nSizeNormal = m_tDataNormal.size()*sizeof(vec3);
}

if(!m_tDataTexcoord.empty()) {
nSizeTexcoord = m_tDataTexcoord.size()*sizeof(vec2);
}

if(!m_tDataTangent.empty()) {
nSizeTangent = m_tDataTangent.size()*sizeof(vec3);
}


m_nVBO_OffsetPosition = 0;
m_nVBO_OffsetNormal = m_nVBO_OffsetPosition + nSizePosition;
m_nVBO_OffsetTexcoord = m_nVBO_OffsetNormal + nSizeNormal;
m_nVBO_OffsetTangent = m_nVBO_OffsetTexcoord + nSizeTexcoord;

glGenBuffers(1, &m_nVBOid);
if(m_nVBOid == 0) {
}
else {
glBindBuffer(GL_ARRAY_BUFFER, m_nVBOid);
glBufferData(GL_ARRAY_BUFFER, nSizePosition+nSizeNormal+nSizeTexcoord+nSizeTange nt, 0, usage);

glBufferSubData(GL_ARRAY_BUFFER , m_nVBO_OffsetPosition, nSizePosition, (const GLvoid*)(&m_tDataPosition[0]));

if(m_tDataNormal.size())
glBufferSubData(GL_ARRAY_BUFFER , m_nVBO_OffsetNormal, nSizeNormal, (const GLvoid*)(&m_tDataNormal[0]));

if(m_tDataTexcoord.size())
glBufferSubData(GL_ARRAY_BUFFER , m_nVBO_OffsetTexcoord, nSizeTexcoord, (const GLvoid*)(&m_tDataTexcoord[0]));

if(m_tDataTangent.size())
glBufferSubData(GL_ARRAY_BUFFER , m_nVBO_OffsetTangent, nSizeTangent, (const GLvoid*)(&m_tDataTangent[0]));

glBindBuffer(GL_ARRAY_BUFFER, 0);
}

return true;
And draws them like this:


glUseProgram(programID);
for (int j = 0; j < Lights.size(); j++)
{
std::string s = "[";
s += convertInt((j));
s += "]";
glUniform3f(glGetUniformLocation(programID,("lightPositions"+s).c_str()), Lights[j].position.x, Lights[j].position.y, Lights[j].position.z);
glUniform3f(glGetUniformLocation(programID,("lightColors"+s).c_str()), Lights[j].color.x, Lights[j].color.y, Lights[j].color.z);
glUniform1f(glGetUniformLocation(programID,("lightPowers"+s).c_str()), Lights[j].power);
glUniform1f(glGetUniformLocation(programID,("lightDiss"+s).c_str()), Lights[j].distance);
}
glUniform1i(countID,Lights.size());
glUniformMatrix4fv(ViewMatrixID, 1, GL_FALSE, &View[0][0]);
ModelMat = (ModelMat*.rotationMatrix );
mat4 MVP = (Projection * View * ModelMat);
glUniformMatrix4fv(MVPID, 1, GL_FALSE, &MVP[0][0]);
glUniformMatrix4fv(ModelMatrixID, 1, GL_FALSE, &ModelMat[0][0]);
glBindTexture(GL_TEXTURE_2D, material.textureID);
glUniform1i(glGetUniformLocation(programID,"myTextureSampler"),0);
glUniform3f(glGetUniformLocation(programID,"Position"),position.x,position.y,position.z);
glUniform3f(glGetUniformLocation(programID,"Scale"),scale.x,scale.y,scale.z);
rotationMatrix = glm::eulerAngleYXZ(rotation.y,rotation.x,rotation. z);
unsigned int slot = 0;
glBindBuffer(GL_ARRAY_BUFFER, m_nVBOid);

glEnableVertexAttribArray(0);
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,(con st GLvoid*)m_nVBO_OffsetPosition);

if(!m_tDataNormal.empty()) {
glEnableVertexAttribArray(3);
glVertexAttribPointer(3,3,GL_FLOAT,GL_FALSE,0,(con st GLvoid*)m_nVBO_OffsetNormal);
}

if(!m_tDataTexcoord.empty()) {
glClientActiveTexture(GL_TEXTURE0 + slot++);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1,2,GL_FLOAT,GL_FALSE,0,(con st GLvoid*)m_nVBO_OffsetTexcoord);
}

if(!m_tDataTangent.empty()) {
glClientActiveTexture(GL_TEXTURE0 + slot++);
glEnableVertexAttribArray(2);
glVertexAttribPointer(2,3,GL_FLOAT,GL_FALSE,0,(con st GLvoid*)m_nVBO_OffsetTangent);
}
glDrawArrays(GL_TRIANGLES,m_nVBO_OffsetPosition, m_tDataPosition.size());

with this vertex shader:

#version 330 core

layout(location = 0) in vec3 vertexPosition_modelspace;
layout(location = 1) in vec2 vertexUV;
layout(location = 3) in vec3 vertexNormal_modelspace;
out vec2 UV;
out vec3 Position_worldspace;
out vec3 Normal_cameraspace;
out vec3 EyeDirection_cameraspace;
out mat4 Vv;
uniform mat4 MVP;
uniform mat4 V;
uniform mat4 M;
uniform vec3 Position;
uniform vec3 Scale;

void main(){
Vv = V;
vec3 VertexPos = vertexPosition_modelspace;
VertexPos.x *= Scale.x;
VertexPos.y *= Scale.y;
VertexPos.z *= Scale.z;
VertexPos += Position;
gl_Position = MVP * vec4(VertexPos,1);
Position_worldspace = (M * vec4(vertexPosition_modelspace,1)).xyz;
vec3 vertexPosition_cameraspace = ( V * M * vec4(vertexPosition_modelspace,1)).xyz;
EyeDirection_cameraspace = vec3(0,0,0) - vertexPosition_cameraspace;
Normal_cameraspace = ( V * M * vec4(vertexNormal_modelspace,0)).xyz;
UV = vertexUV;
}

and this fragment shader:

#version 330 core
#define MaxLights 32
in vec2 UV;
in vec3 Position_worldspace;
in vec3 Normal_cameraspace;
in vec3 EyeDirection_cameraspace;
in mat4 Vv;
out vec3 color;
uniform sampler2D myTextureSampler;
uniform int lightCount;
uniform vec3 lightPositions[MaxLights];
uniform vec3 lightColors[MaxLights];
uniform float lightPowers[MaxLights];
uniform float lightDiss[MaxLights];
void main(){
vec3 colorTemp = vec3(1.0,1.0,1.0);
vec3 MaterialDiffuseColor = texture2D(myTextureSampler,UV).xyz;
vec3 MaterialAmbientColor = vec3(0.1,0.1,0.1) * MaterialDiffuseColor;
vec3 MaterialSpecularColor = vec3(0.3,0.3,0.3);
for(int i = 0;i<lightCount;i++)
{
vec3 LightPosition_worldspace = lightPositions[i];
vec3 LightPosition_cameraspace = ( Vv * vec4(LightPosition_worldspace,1)).xyz;
vec3 LightDirection_cameraspace = LightPosition_cameraspace + EyeDirection_cameraspace;
vec3 LightColor = lightColors[i];
float LightPower = lightPowers[i];
float dis = length( LightPosition_worldspace - Position_worldspace );
vec3 n = normalize( Normal_cameraspace );
vec3 l = normalize( LightDirection_cameraspace );
float cosTheta = clamp( dot( n,l ), 0,1 );
vec3 E = normalize(EyeDirection_cameraspace);
vec3 R = reflect(-l,n);
float cosAlpha = clamp( dot( E,R ), 0,1 );
colorTemp += (MaterialAmbientColor +
MaterialDiffuseColor * LightColor * LightPower * cosTheta / pow(dis,lightDiss[i]) +
MaterialSpecularColor * LightColor * LightPower * pow(cosAlpha,5) / pow(dis,lightDiss[i]));
}

color = colorTemp;
}

uwi2k2
05-20-2013, 03:54 AM
hi,

first of all, nice code style !
i am not shure if you just forgot to post it, but after you have set all the attrib pointers and the uniforms you miss the final Draw() command.
And its difficult to judge all without knowing the classes...
but anyway.. looking for glErrors() from time to time may give a hint.

cu
uwi

UdvPeter
05-20-2013, 04:10 AM
Thanks for the reply. Just forgot to add
glDrawArrays(GL_TRIANGLES,m_nVBO_OffsetPosition, m_tDataPosition.size()); at the end, now it's there but still doesn't works.
I cheked for gl errors but nothing.