PDA

View Full Version : turning fixed funtion to programmable funtion



vickoza
05-05-2014, 09:46 PM
I am reworking an old school project that was written to using the OpenGL fixed function pipeline code. I want to rewrite it using GLSL rather then the fixed pipeline and modernize the project. I have written the shaders but I do not want to modify my code to test if the shaders work.
the code is:
glBegin(GL_POLYGON);
for (int i = 0; i < n; i++)
{
glNormal3dv(vn[i].buf());
glVertex3dv(p[i].buf());
}
glEnd();
I am fine using triangle fans instead of polygons because for the most part my polygon are convex and not concave. Also I am find putting the buffer data into one temp buffer while randering

Thank in advanced

vickoza
05-07-2014, 08:21 PM
I have tried putting my vertex and normal information into array buffers but I am getting no information

The following code is my first attempt if there is a better OpenGL 3.0+ way to do the I would also appreciate this.
double *pointsBuff, *normlsBuff;
pointsBuff = new double[n*3];
normlsBuff = new double[n*3];
for (int i = 0; i < n; i++) //loads data into the arrray
{
int pos = i * 3;
pointsBuff[pos] = (p[i])[0];
pointsBuff[pos+1] = (p[i])[1];
pointsBuff[pos+2] = (p[i])[2];
normlsBuff[pos] = (vn[i])[0];
normlsBuff[pos+1] = (vn[i])[1];
normlsBuff[pos+2] = (vn[i])[2];
}
glVertexPointer(3, GL_DOUBLE, 0, pointsBuff);
glNormalPointer(GL_DOUBLE, 0, normlsBuff);
glDrawArrays(GL_POLYGON, 0, n);
delete [] pointsBuff;
delete [] normlsBuff;

Thank again in advanced

carsten neumann
05-09-2014, 04:04 AM
Please use
around source code snippets.



I have written the shaders but I do not want to modify my code to test if the shaders work.


I'm afraid that is not possible, you'll at least have to include code to generate the shader and program objects, load, compile, and link the code (for shader code this is done at runtime with OpenGL commands, not your C/C++ compiler) and activate the shader before drawing.



I have tried putting my vertex and normal information into array buffers


Sorry, you've put the information into plain arrays, that is completely different from putting them into OpenGL buffer objects, which is what you want to do in "modern" OpenGL. My suggestion would be to look through some of the tutorials on the wiki (http://www.opengl.org/wiki/Getting_Started#Tutorials_and_How_To_Guides) that should get you started on how to use buffer objects and shaders.

vickoza
05-24-2014, 08:08 PM
Thank not I have a new problem


vbo_Data[i].gl_Vertex[0] = p[i].c[0];
vbo_Data[i].gl_Vertex[1] = p[i].c[1];
vbo_Data[i].gl_Vertex[2] = p[i].c[2];
vbo_Data[i].gl_Normal[0] = (vn[i])[0];
vbo_Data[i].gl_Normal[1] = (vn[i])[1];
vbo_Data[i].gl_Normal[2] = (vn[i])[2];
vbo_Data[i].ambient[0] = ambient[0];
vbo_Data[i].ambient[1] = ambient[1];
vbo_Data[i].ambient[2] = ambient[2];
vbo_Data[i].diffuse[0] = diffuse[0];
vbo_Data[i].diffuse[1] = diffuse[1];
vbo_Data[i].diffuse[2] = diffuse[2];
vbo_Data[i].specular[0] = specular[0];
vbo_Data[i].specular[1] = specular[1];
vbo_Data[i].specular[2] = specular[2];
vbo_Data[i].shininess = shininess;
}

glEnableVertexAttribArray(gl_VertexID);
glEnableVertexAttribArray(gl_NormalID);
glEnableVertexAttribArray(ambientID);
glEnableVertexAttribArray(diffuseID);
glEnableVertexAttribArray(specularID);
glEnableVertexAttribArray(shininessID);
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(VertexData)*n, vbo_Data, GL_STATIC_DRAW);
glVertexAttribPointer(gl_VertexID, 3, GL_DOUBLE, GL_FALSE, sizeof(VertexData)*n,0);
glVertexAttribPointer(gl_NormalID, 3, GL_DOUBLE, GL_FALSE, sizeof(VertexData)*n, (GLvoid*) offsetof(VertexData, gl_Normal));
glVertexAttribPointer(ambientID, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData)*n, (GLvoid*) offsetof(VertexData, ambient));
glVertexAttribPointer(diffuseID, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData)*n, (GLvoid*) offsetof(VertexData, diffuse));
glVertexAttribPointer(specularID, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData)*n, (GLvoid*) offsetof(VertexData, specular));
glVertexAttribPointer(shininessID, 1, GL_FLOAT, GL_FALSE, sizeof(VertexData)*n, (GLvoid*) offsetof(VertexData, shininess));
glDrawArrays(GL_TRIANGLE_FAN, 0, n);
glDisableVertexAttribArray(gl_VertexID);
glDisableVertexAttribArray(gl_NormalID);
glDisableVertexAttribArray(ambientID);
glDisableVertexAttribArray(diffuseID);
glDisableVertexAttribArray(specularID);
glDisableVertexAttribArray(shininessID);
delete [] vbo_Data;

glDrawArrays(GL_TRIANGLE_FAN, 0, n); generates an error when n is larger than 3

reto.koradi
05-25-2014, 01:03 PM
The 4th argument of glVertexAttribPointer() is the stride, which is the increment in bytes between two attribute values. In your case, that should be sizeof(VertexData). Your code passes sizeof(VertexData)*n, which would be the total size of the entire array, instead.

I would also discourage using GL_DOUBLE for your positions and normals. Many GPUs don't even support DOUBLE coordinates, and will have to convert the values to FLOAT if you pass in DOUBLE coordinates. It should be very rare that you really need the precision of a double for your coordinates.

If a little promotion is allowed, I have an article on my personal web site that explains some typical steps needed in the transition of legacy OpenGL code to the Core Profile: http://retokoradi.com/2014/03/30/opengl-transition-to-core-profile/.

vickoza
05-26-2014, 11:55 AM
Thank again your argument was correct