PDA

View Full Version : Sample use of shaders on Linux



mayones
07-28-2008, 09:50 AM
Hello,

I need to code a sample program which would use shaders to calculate some simple operation (e.g. transformation) and which I could then compare to similar code in standard OpenGL without the shader support. I have basic knowledge of OpenGL but I know nothing about shaders. I noticed that there are severals standarts like Cg, GLSL or CUDA to use them but I really need something as simplest as it can be.

I searched the net in order to find some samples but I really can't find anything which would go on Linux and wouldn't require huge, additional software - just C++ compiler, some extra OGL libraries and a proper makefile (I understand that the shader program should be compiled and linked in some way).

If anyone would like to help me, I would be very, very grateful. My hardware is Intel GMA900 graphic card (laptop integrated - it supports 1.4 OpenGL, is it enough?). I run it under Ubuntu 7.10 OS.

Best regards,
Sebastian Kapciak

mayones
07-28-2008, 09:53 AM
Hello,

I need to code a sample program which would use shaders to calculate some simple operation (e.g. transformation) and which I could then compare to similar code in standard OpenGL without the shader support. I have basic knowledge of OpenGL but I know nothing about shaders. I noticed that there are severals standarts like Cg, GLSL or CUDA to use them but I really need something as simplest as it can be.

I searched the net in order to find some samples but I really can't find anything which would go on Linux and wouldn't require huge, additional software - just C++ compiler, some extra OGL libraries and a proper makefile (I understand that the shader program should be compiled and linked in some way).

If anyone would like to help me, I would be very, very grateful. My hardware is Intel GMA900 graphic card (laptop integrated - it supports 1.4 OpenGL, is it enough?). I run it under Ubuntu 7.10 OS.

Best regards,
Sebastian Kapciak

--
Sorry for the second, same post, but I didn't know where to put it - linux or the shader group.

dletozeun
07-28-2008, 12:19 PM
Using shader languages such as GLSL or Cg don't require more sofware that gl libraries and headers and a C compiler. CUDA is C-like (or C? don't know) language that allow you to use the GPU for general purpose, it won't help you at the moment and is only supported by last gen nvidia cards.

I am not a hardware expert, but I first glance I don't think the GMA900 chipset supports any shader language. You can find more information about that searching further on the web.

But if you search a good glsl tutorial to starts with opengl shader the lighthouse3D one is good (http://www.lighthouse3d.com/opengl/glsl/).

EDIT:

Looking further, it looks like that GMA 900 chipset are not opengl friendly and only support DirectX shader model 2..., which won't suit you.

dletozeun
07-28-2008, 01:07 PM
double posting is not in your favor.

OneSadCookie
07-28-2008, 07:12 PM
GMA 9xx *hardware* supports GLSL; whether your drivers do is a different matter. I don't know about Linux. It does on Mac OS X. It doesn't on Windows. Since you're on Linux, use glxinfo to find out (look for GL_ARB_shading_language_100 and GL_ARB_fragment_shader).

Any boring basic GLSL tutorial using GLUT or SDL should be fine for getting you started.

oc2k1
07-28-2008, 07:13 PM
One possibility to use GLSL shaders with that laptop on linux would be to use MESA as software renderer.

Ilian Dinev
07-29-2008, 04:45 AM
In my experience Intel's cards and drivers are the worst way to study 3D/shading. Lying drivers (I did not find even one true query-result), lack of sane features, the available features are semi-complete at best, completely wrong behavior, crashes, severe real limitations. The drivers are there just for giggles! You'll be in a world of hurt!!
Your only real option is to use MESA, if you plan to stay on that botched-up laptop.

Dark Photon
07-29-2008, 06:24 AM
I searched the net in order to find some samples but I really can't find anything which would go on Linux and wouldn't require huge, additional software - just C++ compiler, some extra OGL libraries and a proper makefile
Just install GLUT (freeglut, openglut, or similar). Should be a package already there for your distro. It takes a truly trivial amount of code to get an OpenGL context created and a GL window up. Then you can do whatever you want.



void reshape(int width, int height)
{
glViewport(0, 0, width, height);
}

void display()
{
...
glutSwapBuffers();
}

int main (int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
glutCreateWindow(argv[0]);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutReshapeWindow(400,400);
glClearColor(0,0,0,0);
...
glutMainLoop();
return 0;
}

mayones
07-30-2008, 12:19 PM
Thank you very much for all your replies. I know how to write some small openGL programs using C/C++ and GLUT/GLEW but I guess I'll have to learn how to use shaders from the top.

Still, if anyone would like to help me with a sample code which use shaders (e.g. using the ARB_ extensions) and would like to tell me how to build and run it under linux I would really appreciate it.

Once again thank you all for support.

Best regards,
Sebastian

Dark Photon
07-31-2008, 04:47 AM
Still, if anyone would like to help me with a sample code which use shaders (e.g. using the ARB_ extensions) and would like to tell me how to build and run it under linux I would really appreciate it.
Here you go:


/************************************************** ***************************/
/* arb_vertex_program.c - An example of defining and using an ARB vertex */
/* program. */
/* */
/* Derived from article: http://www.devmaster.net/main.php?showtid=280 */
/************************************************** ***************************/

#include <stdio.h>
#include <stdlib.h>
#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

GLuint VP_handle = 0;

const char VP_source[] =
"!!ARBvp1.0\n"
"\n"
"#Input\n"
"ATTRIB InPos = vertex.position;\n"
"ATTRIB InColor = vertex.color;\n"
"\n"
"#Output\n"
"OUTPUT OutPos = result.position;\n"
"OUTPUT OutColor = result.color;\n"
"\n"
"PARAM MVP[4] = { state.matrix.mvp }; # Modelview Projection Matrix\n"
"TEMP Temp;\n"
"\n"
"# Transform vertex to clip space\n"
"DP4 Temp.x, MVP[0], InPos;\n"
"DP4 Temp.y, MVP[1], InPos;\n"
"DP4 Temp.z, MVP[2], InPos;\n"
"DP4 Temp.w, MVP[3], InPos;\n"
"\n"
"#Output\n"
"MOV OutPos, Temp;\n"
"MOV OutColor, InColor;\n"
"\n"
"END\n";

void checkGLError( const char hdr[] )
{
int err = glGetError();
if( err )
{
fprintf(stderr, "ERROR %s: %s\n", hdr, gluErrorString(err));
exit(1);
}
}

void initVertexProgram()
{
int err = 0;

glGenProgramsARB( 1, &amp;VP_handle ); // Allocate
checkGLError( "Allocating Vertex Program" );

glBindProgramARB( GL_VERTEX_PROGRAM_ARB, VP_handle ); // Activate
checkGLError( "Binding Vertex Program" );

glProgramStringARB( GL_VERTEX_PROGRAM_ARB, // Compile
GL_PROGRAM_FORMAT_ASCII_ARB,
strlen( VP_source ), VP_source );
if ( glGetError() == GL_INVALID_OPERATION )
{
fprintf( stderr, "ERROR Compiling Vertex Program:\n" );

// Find the error position
GLint errPos;
glGetIntegerv( GL_PROGRAM_ERROR_POSITION_ARB, &amp;errPos );

// Print implementation-dependent program errors and warnings
const GLubyte *errString = glGetString( GL_PROGRAM_ERROR_STRING_ARB );
fprintf( stderr, "Error at position: %d\n%s\n", errPos, errString );
exit(1);
}
checkGLError( "Compiling Vertex Program" );
}

void activateVertexProgram()
{
glBindProgramARB( GL_VERTEX_PROGRAM_ARB, VP_handle );
checkGLError( "Binding Vertex Program" );

glEnable( GL_VERTEX_PROGRAM_ARB );
checkGLError( "Enabling Vertex Program" );
}

void deactivateVertexProgram()
{
glDisable( GL_VERTEX_PROGRAM_ARB );
checkGLError( "Disabling Vertex Program" );
}


void reshape(int width, int height)
{
glViewport(0, 0, width, height);
}

void display()
{
int err=0;
glClear(GL_COLOR_BUFFER_BIT);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1,1,-1,1,-1,1);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glRotatef(30.0, 0,0,1);

glColor4f(1,0,0,1);

activateVertexProgram();

glRectf(-.5,-.5,.5,.5);

deactivateVertexProgram();

glutSwapBuffers();
checkGLError( "End of display()" );
}

int main (int argc, char** argv)
{
glutInit(&amp;argc, argv);
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE | GLUT_MULTISAMPLE);
glutCreateWindow(argv[0]);

glutDisplayFunc(display);
glutReshapeFunc(reshape);

glutReshapeWindow(400,400);

printf("GL_RENDERER = %s\n",glGetString(GL_RENDERER));

glClearColor(0,0,0,0);

initVertexProgram();

glutMainLoop();
return 0;
}

Compile using:

gcc -o arb_vertex_program arb_vertex_program.c -lglut -lGLU -lGL

mayones
08-05-2008, 10:17 AM
Dark Photon, thank you very, very much for the code - you saved me couple of days. It works fine on Linux and intel GMA900 graphic card. It is exactly something I was looking for.

Best Regards,
Sebastian