Updating this vertex program to GLSL

Started by
7 comments, last by blueshogun96 10 years, 3 months ago

Right now, I'm trying really hard to update this really old vertex program to GLSL, and so far, it's been a complete nightmare. In fact, I've tried to convert this many times for a few years and never could get it to work! I even tried converting to an ARB vertex program and to a Direct3D vertex shader, but that didn't work either.

To be more specific, I have a really old OpenGL project that uses GL_NV_vertex_program for procedural animation of a jellyfish. This shouldn't be a challenge, really, because having a good understanding of both GL_NV_vertex_program and GLSL makes it less troublesome. Hopefully my problem is just something simple, but I can't pinpoint it to save my life. I had a friend on this forum that managed to do this successfully, but I've asked him twice, and he told me that not only did he forget how he did it, but he doesn't have the source code anymore! Aside from that, he's rarely on Skype. sad.png

Now, I really hate just "dumping" some code on you all and saying "plz help meee1!!!111!11", but I'm getting desperate, and my next course of action would be to bang my head against the wall. I'll only share the most relevant code because I converted much of the code to work on MacOSX, and chances are, you're a Windows user.

I'll start with the original GL_NV_vertex_program and the GLSL vertex program I wrote:


/* Vertex attributes */
attribute vec4 v_pos;
attribute vec4 v_normal;
varying float v_tex;

/* Texture sampler */
uniform sampler1D texture;

/* General purpose vectors */
vec4 R0, R1, R2, R3, R4, R5, R6;

/* Constants */
uniform vec4 c8;
uniform vec4 c9;
uniform vec4 c10;
uniform vec4 c11;
uniform vec4 c12;
uniform vec4 c13;
uniform vec4 c14;
uniform vec4 c15;
uniform vec4 c16;
uniform vec4 c17;

/* Matrices */
mat4 mvp = gl_ModelViewProjectionMatrix;
mat3 itmm = gl_NormalMatrix; //transpose(gl_ModelViewInverseMatrix);

void main()
{
    R0.x = ( v_pos.y * c16.x ) + c16.z;
    
    R0.x *= c11.w;
    R0.y = exp( R0.x );
    R2.x = ( R0.y < c11.y ) ? 1.0f : 0.0f;
    R2.y = R2.z = ( R0.y > c11.y ) ? 1.0f : 0.0f;
    R2.y = dot( R2, c14.zwzw );
    R5/*.xyz*/ = -R0.y + c10;
    R5 = R5 * R5;
    R0 = ( c12.xyxy * R5 ) + c12.zwzw;
    R0 = ( R0 * R5 ) + c13.xyxy;
    R0 = ( R0 * R5 ) + c13.zwzw;
    R0 = ( R0 * R5 ) + c14.xyxy;
    R0 = ( R0 * R5 ) + c14.zwzw;
    R1.x = dot( R0, -R2 );
    
    R2.x = ( R1.x * c10.w ) + c10.z;
    
    R2.yw = c16.yw;
    R3 = v_pos * R2.xyxw;
    R4 = v_normal * R2.yxyw;
    
    gl_Position = R3 * mvp;
    
    R5.xyz = R4.xyz * itmm;
    R5.w = dot( R5, R5 );
    R5.w = inversesqrt( R5.w );
    R5 = R5 * R5.w;

    R6 = c8 + v_pos;
    R6.w = dot( R6, R6 );
    R6.w = inversesqrt( R6.w );
    R6 = R6 * R6.w;
    
    R6 = R6 * c10.y;
    
    v_tex = dot( R5, R6 );
    
    gl_FrontColor = gl_Color;
}

/* Original vertex program:

# R0 = (y coordinate of vertex * k) + (w * t)
MAD R0.x, v[OPOS].y, c[16].x, c[16].z;

# R1 = cos(R0), R0 = any floating point number
MUL R0.x, c[11].w, R0.x; 
EXP R0.y, R0.x; 
SLT R2.x, R0.y, c[11]; 
SGE R2.yz, R0.y, c[11]; 
DP3 R2.y, R2, c[14].zwzw;   
ADD R5.xyz, -R0.y, c[10]; 
MUL R5, R5, R5;
MAD R0, c[12].xyxy, R5, c[12].zwzw; 
MAD R0, R0, R5, c[13].xyxy;      
MAD R0, R0, R5, c[13].zwzw;      
MAD R0, R0, R5, c[14].xyxy;      
MAD R0, R0, R5, c[14].zwzw;      
DP3 R1.x, R0, -R2; 

# R2 = R1 * c[10] + 1.0
MAD R2.x, R1.x, c[10].w, c[10].z;

# R3 = perturbed vertex
# R4 = perturbed normal
MOV R2.yw, c[16];
MUL R3, v[OPOS], R2.xyxw;
MUL R4, v[NRML], R2.yxyw;

# Transform vertices into clip space via modelview-projection matrix
DP4 o[HPOS].x, R3, c[0];
DP4 o[HPOS].y, R3, c[1];
DP4 o[HPOS].z, R3, c[2];
DP4 o[HPOS].w, R3, c[3];

# Transform normals via inverse transpose modelview matrix & normalize
# R5 = transformed normal
DP3 R5.x, R4, c[4];
DP3 R5.y, R4, c[5];
DP3 R5.z, R4, c[6];
DP3 R5.w, R5, R5;
RSQ R5.w, R5.w;
MUL R5, R5, R5.w;

# Get unit length eye vector to vertex....this is how we texture map
# R6 = vector
ADD R6, c[8], v[OPOS];
DP3 R6.w, R6, R6;
RSQ R6.w, R6.w;
MUL R6, R6, R6.w;

# Multiply by 0.5 for texture wrapping
MUL R6, R6, c[10].y;
	    
# Texture coord is dot product of normal and vector from eye to vertex
DP3 o[TEX0].x, R5, R6;

# Pass color through
MOV o[COL0], v[COL0];
*/

The fragment program I wrote:


varying float v_tex;
uniform sampler1D texture;

void main()
{
    vec4 texel_colour = texture1D( texture, v_tex );
    vec4 fragment_colour = gl_Color;
    
    gl_FragColor = texel_colour * fragment_colour;
}

My modified jellyfish code (I commented out the shader code that needed to be changed; also, I couldn't get the ARB stuff working either):


#include "Jellyfish.h"
//#include <glh_genext.h>
#include "constants.h"
#ifndef _WIN32
#include <sys/time.h>
#endif

extern GLuint vertexProgramsNV[1];
extern GLuint uniform_constants[18]; // Laziness...
extern GLuint uniform_texture;
extern GLuint attrib_v_pos;
extern GLuint attrib_v_normal;

/*extern PFNGLPROGRAMPARAMETER4FNVPROC		glProgramParameter4fNV;
extern PFNGLPROGRAMLOCALPARAMETER4FARBPROC		glProgramLocalParameter4fARB;
extern PFNGLBINDPROGRAMNVPROC				glBindProgramNV;*/

float queryElapsedTime();

inline float myRandom( )
{
  static unsigned int n = 0;

  n += 7;
  n = (n<<13) ^ n;

  return 0.5f * ( 2.0f - ( (n * (n * n * 15731 + 789221)
    + 1376312589) & 0x7fffffff) * 9.313225746e-10f);
}


Jellyfish::Jellyfish( )
{
  Vector3 a,b,c;

  nVertices = 0;
 
  generateLightingTexture( );
  create( );
}

Jellyfish::~Jellyfish( )
{
  delete [] strip;
  delete [] normals;
  delete [] segments;
  delete [] points;

  glDeleteTextures(1, &texture);
}

void Jellyfish::create( )
{
  // jellyfish body
  float crossSection[118] = {
    0.0f,9.44361f,0.38913f,9.44208f,1.0702f,9.38564f,1.77009f,9.26147f,
    2.25174f, 9.11471f,2.92905f,8.79864f,3.42198f,8.45245f,4.0278f,7.89932f,
    4.35141f,7.52679f,4.65243f,7.07149f,4.9798f,6.48072f,5.2849f,5.67923f,
    5.46437f,5.16185f,5.66547f,4.77636f,5.93869f,4.45945f,6.28112f,4.16573f, 
    6.74834f, 3.90317f,7.27513f,3.53442f,7.54982f,3.28982f,7.8358f,2.95494f, 
    8.0277f,2.60876f,8.1293f,2.24376f,8.18051f,1.76588f,8.12177f,1.20145f, 
    7.98255f,0.719805f,7.82451f,0.471455f,7.6025f,0.264505f,7.30147f,0.0801254f, 
    7.0f,0.0f,6.54744f,0.0245184f,6.0959f,0.202837f,5.85738f,0.454947f, 
    5.68345f,0.817635f,5.54046f,1.53635f,5.41629f,2.01047f,5.1231f,2.5636f, 
    4.63738f,3.14684f,4.26862f,3.44818f,3.80579f,3.71534f,3.17363f,3.94456f, 
    2.56405f,4.14023f,2.13133f,4.3961f,1.92061f,4.66327f,1.84911f,4.96429f, 
    1.93942f,5.22769f,2.15767f,5.42712f,2.55653f,5.56635f,2.94034f,5.62655f, 
    3.23384f,5.72439f,3.39188f,5.87491f,3.44456f,6.10443f,3.38435f,6.35654f, 
    3.28276f,6.52588f,3.07204f,6.72155f,2.60168f,6.95484f,2.14261f,7.13546f, 
    1.40102f,7.32328f,0.58073f,7.41766f,0.0f,7.43333f
  };

  nVertices = 118;
  float rotAngle = 2.0f * M_PI / 24.0f;
  float cosAngle = cosf(rotAngle);
  float sinAngle = sinf(rotAngle);
  float dy, dx, slope;

  strip = new Vector3[nVertices];
  normals = new Vector3[nVertices];
  tex = new Vector3[nVertices];
  unsigned int i;
  for ( i = 0; i < nVertices / 2; i++ )
  {
    strip[2*i].set(crossSection[2*i], crossSection[2*i+1]-10.0f, 0.0f );
    tex[2*i].set(0.0f, strip[2*i](1) / 10.0f, 0.0f);

    strip[2*i+1].set(cosAngle * crossSection[2*i], crossSection[2*i+1]-10.0f, 
                     sinAngle * crossSection[2*i] );
    tex[2*i+1].set(rotAngle / (2.0f * M_PI), strip[2*i+1](1) / 10.0f, 0.0f);

    if ( i == 0 || i == nVertices / 2 - 1)
    {
      normals[2*i].set(0.0f, 1.0f, 0.0f);
      normals[2*i+1].set(0.0f, 1.0f, 0.0f);
    }
    else
    {
      dx = crossSection[2*(i+1)] - crossSection[2*(i-1)];
      dy = crossSection[2*(i+1)+1] - crossSection[2*(i-1)+1];
      slope = dy / dx;

      normals[2*i].set( 1.0f, -1.0f / slope, 0.0f );
      normals[2*i+1].set( cosAngle, -1.0f / slope, sinAngle );
    }

    normals[2*i].normalize();
    normals[2*i+1].normalize();
  }

  //  tentacles 
  nSegments = 20;
  segments = new Vector3[nSegments];
  for ( i = 0; i < nSegments; i++ )
  {
    segments[i].set(7.0f, -10.0f - float(i), 0.0f);
  }

  // water debris
  nPoints = 1000;
  points = new Vector3[nPoints];
  for ( i = 0; i < nPoints; i++ )
  {
    points[i](0) = 700.0f * (myRandom( ) - 0.5f);
    points[i](1) = 700.0f * (myRandom( ) - 0.5f);
    points[i](2) = 700.0f * (myRandom( ) - 0.5f);
  }


  // Create jellyfish display list
  const unsigned int nSlices = 24;
  const float nRadPerSlice = 2.0f * M_PI / 24.0f;
  float angle, temp, texOffset;
  Vector3 v,n;

  jellyfishList = glGenLists(1);
  glNewList(jellyfishList, GL_COMPILE);
	
	glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  glDisable(GL_TEXTURE_2D);
	glEnable(GL_TEXTURE_1D);
  glBindTexture(GL_TEXTURE_1D, texture);
    
    glUniform1i( uniform_texture, 0 );

  glEnable(GL_BLEND);
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  glDepthMask(GL_FALSE);

/*#if USE_ARB_VERTEXPROGRAM
	glEnable(GL_VERTEX_PROGRAM_ARB);
	glBindProgramARB(GL_VERTEX_PROGRAM_ARB, vertexProgramsNV[0]);
#else
	glEnable(GL_VERTEX_PROGRAM_NV);
	glBindProgramNV(GL_VERTEX_PROGRAM_NV, vertexProgramsNV[0]);
#endif*/
    glUseProgram(vertexProgramsNV[0]);
 
  // draw the body
  glColor4f(1.0f, 0.0f, 1.0f, 0.4f);

  for (  i = 0; i < nSlices; i++)
  {
    angle = float(i) * nRadPerSlice;
    texOffset = 1.0f - angle / (2.0f * M_PI);
    cosAngle = cosf(angle);
    sinAngle = sinf(angle);
    unsigned int j;
    glBegin(GL_TRIANGLE_STRIP);
    for (  j = 0; j < nVertices; j++)
    {
      v = strip[j];
      temp = cosAngle * v(0) + sinAngle * v(2);
      v(2) = -sinAngle * v(0) + cosAngle * v(2);
      v(0) = temp;
      n = normals[j];
      temp = cosAngle * n(0) + sinAngle * n(2);
      n(2) = -sinAngle * n(0) + cosAngle * n(2);
      n(0) = temp;

        glVertexAttrib3fv( attrib_v_normal, n.get() );
        glVertexAttrib3fv( attrib_v_pos, v.get() );
      //glNormal3fv( n.get() );
      //glVertex3fv( v.get() );
    }
    glEnd( );


    // draw the tentacles
    glBegin(GL_TRIANGLE_STRIP);
    Vector3 w(0.0f, 0.0f, 1.0f);

      glVertexAttrib3f( attrib_v_normal, cosAngle, 0.0f, -sinAngle );
    //glNormal3f( cosAngle, 0.0f, -sinAngle);

    for ( j = 0; j < nSegments; j++)
    {
      w *= 0.9f;

      v = segments[j] + w;
      temp = cosAngle * v(0) + sinAngle * v(2);
      v(2) = -sinAngle * v(0) + cosAngle * v(2);
      v(0) = temp;

        glVertexAttrib3fv( attrib_v_pos, v.get() );
      //glVertex3fv( v.get() );

      v = segments[j] - w;
      temp = cosAngle * v(0) + sinAngle * v(2);
      v(2) = -sinAngle * v(0) + cosAngle * v(2);
      v(0) = temp;

        glVertexAttrib3fv( attrib_v_pos, v.get() );
//      glVertex3fv( v.get() );
    }
    glEnd( );
  }

/*#if USE_ARB_VERTEXPROGRAM
	glDisable(GL_VERTEX_PROGRAM_ARB);
#else
	glDisable(GL_VERTEX_PROGRAM_NV);
#endif*/
    glUseProgram(0);
	
  glDisable(GL_TEXTURE_1D);

  glDepthMask(GL_TRUE);
  glEndList( );
}

void Jellyfish::render( )
{
  static float lastTime = 0.0f;
  float dt;
  float time = 0.0f;
  static float ourTime = 0.0f;
  float k = 0.25f;    // inverse wavelength of ripple
  float w = 3.0f;     // inverse period of ripple

  // What follows is the CPU's only per-frame computations
  // for animating the jellyfish
/*
#ifdef _WIN32
  time = queryElapsedTime( );
#else
  timeval tmptime;
  gettimeofday (&tmptime,NULL);
  time = tmptime.tv_sec+tmptime.tv_usec*.000001;
#endif
*/
  time = queryElapsedTime( );

  dt = time - lastTime;
  lastTime = time;
  float hfactor = .805f - 0.195f * sinf(w * ourTime);
  if ( cosf(w * ourTime) < 0.0f )
  {
    dt *= 2.0f;
  }
  ourTime += dt;

  // Begin drawing, water debris first
/*  glDisable(GL_BLEND);
  glBegin(GL_POINTS);
  glColor3f( 1.0f, 1.0f, 1.0f );
  for ( unsigned int i = 0; i < nPoints; i++ )
  {
    points[i](1) -= w * dt;
    if ( points[i](1) < -50.0f )
    {
      points[i](1) += 1000.0f;
    }

    glVertex3fv( points[i].get() );
  }
  glEnd( );*/

  // Now send some parameters and draw the jellyfish
/*#if USE_ARB_VERTEXPROGRAM
    float itmtx[16], imtx[16], mtx[16];
    glGetFloatv( GL_MODELVIEW, mtx );
    //gluInvertMatrix( mtx, imtx );
    gluTransposeMatrix( mtx, itmtx );
    
  glProgramLocalParameter4fvARB(GL_VERTEX_PROGRAM_ARB, 4, &itmtx[0]);
  glProgramLocalParameter4fvARB(GL_VERTEX_PROGRAM_ARB, 5, &itmtx[4]);
  glProgramLocalParameter4fvARB(GL_VERTEX_PROGRAM_ARB, 6, &itmtx[8]);
  glProgramLocalParameter4fARB(GL_VERTEX_PROGRAM_ARB, 16, k, hfactor, w * ourTime, 1.0f);
#else
  glProgramParameter4fNV(GL_VERTEX_PROGRAM_NV, 16, k, hfactor, w * ourTime, 1.0f);
#endif*/
    glUniform4f(uniform_constants[16], k, hfactor, w * ourTime, 1.0f);
    
  glCallList(jellyfishList);
}

void Jellyfish::generateLightingTexture( )
{
  const unsigned int size = 256;
  unsigned int index;
  float *data;
  data = new float[4 * size];

  for ( unsigned int i = 0; i < size; i++ )
  {
    index = 4 * i;

    data[index] = 1.0f - float(i) / float(size);
    data[index] *= data[index];
    data[++index] = 0.0f;
    data[++index] = 1.0f;
    data[++index] = 0.7f - float(i) / float(size);
  }

  glGenTextures(1, &texture);
  glBindTexture(GL_TEXTURE_1D, texture);
  glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA, size, 0, GL_RGBA, GL_FLOAT, data);

  delete [] data;
}

And this is how I setup the GLSL program, attributes, constants, etc. This time, I only included the relevant stuff. The unchanged code works fine without any problems or issues on Windows.


//-----------------------------------------------------------------------------
// Name: loadVertexProgramsNV
// Desc: Loads any vertex programs used in this demo.  Instead of using NVParse,
//		 the shader is loaded from a text file on disk.
//-----------------------------------------------------------------------------
#if 0
void loadVertexProgramsNV()
{
	// Load the vertex programs
	if( !( vertexProgramsNV[0] = LoadProgramNV( GL_VERTEX_PROGRAM_NV, "..\\programs\\jellyfish_nv.vp" ) ) )
		return;
	
	// Track the Projection matrix in c[0] to c[3].
	glTrackMatrixNV(GL_VERTEX_PROGRAM_NV, 0, GL_MODELVIEW_PROJECTION_NV, GL_IDENTITY_NV);

	// Track the inverse of the Modelview matrix in c[4] to c[7]
	glTrackMatrixNV(GL_VERTEX_PROGRAM_NV, 4, GL_MODELVIEW, GL_INVERSE_TRANSPOSE_NV);

	// Load the view position into c[8]
	glProgramParameter4fNV(GL_VERTEX_PROGRAM_NV, 8, 0.0f, 0.0f, 100.0f, 1.0f);	

	// Load cosine series constants into c[9]
 	glProgramParameter4fNV(GL_VERTEX_PROGRAM_NV, 9, 1.0f, -1.0f/2.0f, 1.0f/24.0f, -1.0f/720.0f);	

	// Constants for cosine
	glProgramParameter4fNV(GL_VERTEX_PROGRAM_NV, 10, 0.0f, 0.5f, 1.0f, 0.2f);
	glProgramParameter4fNV(GL_VERTEX_PROGRAM_NV, 11, 0.25f, -9.0f, 0.75f, 1.0f / (2.0f * M_PI) );
	glProgramParameter4fNV(GL_VERTEX_PROGRAM_NV, 12, 24.9808039603f, -24.9808039603f, -60.1458091736f, 60.1458091736f );
	glProgramParameter4fNV(GL_VERTEX_PROGRAM_NV, 13, 85.4537887573f, -85.4537887573f, -64.9393539429f, 64.9393539429f );
	glProgramParameter4fNV(GL_VERTEX_PROGRAM_NV, 14, 19.7392082214f, -19.7392082214f, - 1.0f,	1.0f );

	// Light position
	glProgramParameter4fNV(GL_VERTEX_PROGRAM_NV, 15, 0.0f, 0.7f, 0.7f, 1.0f );

	glProgramParameter4fNV(GL_VERTEX_PROGRAM_NV, 17, 0.3f, 0.3f, 0.3f, 1.0f );
}
#endif

void loadVertexProgramsGLSL()
{
    GLuint vs, fs;
    
    /* Post-processing */
    if ((vs = create_shader("programs/jellyfish_glsl_vp.txt", GL_VERTEX_SHADER))   == 0) return;
    if ((fs = create_shader("programs/jellyfish_glsl_fp.txt", GL_FRAGMENT_SHADER)) == 0) return;
    
    int link_ok, validate_ok;
    
    vertexProgramsNV[0] = glCreateProgram();
    glAttachShader(vertexProgramsNV[0], vs);
    glAttachShader(vertexProgramsNV[0], fs);
    glLinkProgram(vertexProgramsNV[0]);
    glGetProgramiv(vertexProgramsNV[0], GL_LINK_STATUS, &link_ok);
    if (!link_ok) {
        fprintf(stderr, "glLinkProgram:");
        //print_log(program_postproc);
        return;
    }
    glValidateProgram(vertexProgramsNV[0]);
    glGetProgramiv(vertexProgramsNV[0], GL_VALIDATE_STATUS, &validate_ok);
    if (!validate_ok) {
        fprintf(stderr, "glValidateProgram:");
        //print_log(program_postproc);
    }
    
    attrib_v_pos = glGetAttribLocation(vertexProgramsNV[0], "v_pos");
    if (attrib_v_pos == -1) {
        fprintf(stderr, "Could not bind attribute v_pos\n");
        return;
    }
    
    attrib_v_normal = glGetAttribLocation(vertexProgramsNV[0], "v_normal");
    if (attrib_v_normal == -1) {
        fprintf(stderr, "Could not bind attribute v_normal\n");
        return;
    }
    
    uniform_texture = glGetUniformLocation(vertexProgramsNV[0], "texture");
    if (uniform_texture == -1) {
        fprintf(stderr, "Could not bind uniform texture\n");
        return;
    }
    
    /* Constants */
    uniform_constants[8] = glGetUniformLocation(vertexProgramsNV[0], "c8");
    if (uniform_constants[8] == -1) {
        fprintf(stderr, "Could not bind uniform c8\n");
        return;
    }
    
    uniform_constants[9] = glGetUniformLocation(vertexProgramsNV[0], "c9");
    if (uniform_constants[9] == -1) {
        fprintf(stderr, "Could not bind uniform c9\n");
        //return;
    }
    
    uniform_constants[10] = glGetUniformLocation(vertexProgramsNV[0], "c10");
    if (uniform_constants[10] == -1) {
        fprintf(stderr, "Could not bind uniform c10\n");
        return;
    }
    
    uniform_constants[11] = glGetUniformLocation(vertexProgramsNV[0], "c11");
    if (uniform_constants[11] == -1) {
        fprintf(stderr, "Could not bind uniform c11\n");
        return;
    }
    
    uniform_constants[12] = glGetUniformLocation(vertexProgramsNV[0], "c12");
    if (uniform_constants[12] == -1) {
        fprintf(stderr, "Could not bind uniform c12\n");
        return;
    }
    
    uniform_constants[13] = glGetUniformLocation(vertexProgramsNV[0], "c13");
    if (uniform_constants[13] == -1) {
        fprintf(stderr, "Could not bind uniform c13\n");
        return;
    }
    
    uniform_constants[14] = glGetUniformLocation(vertexProgramsNV[0], "c14");
    if (uniform_constants[14] == -1) {
        fprintf(stderr, "Could not bind uniform c14\n");
        return;
    }
    
    uniform_constants[15] = glGetUniformLocation(vertexProgramsNV[0], "c15");
    if (uniform_constants[15] == -1) {
        fprintf(stderr, "Could not bind uniform c15\n");
        //return;
    }
    
    uniform_constants[16] = glGetUniformLocation(vertexProgramsNV[0], "c16");
    if (uniform_constants[16] == -1) {
        fprintf(stderr, "Could not bind uniform c16\n");
        return;
    }
    
    uniform_constants[17] = glGetUniformLocation(vertexProgramsNV[0], "c17");
    if (uniform_constants[17] == -1) {
        fprintf(stderr, "Could not bind uniform c17\n");
        //return;
    }
    
    // Load the view position into c[8]
	glUniform4f( uniform_constants[8], 0.0f, 0.0f, 100.0f, 1.0f);
    
	// Load cosine series constants into c[9]
 	glUniform4f( uniform_constants[9], 1.0f, -1.0f/2.0f, 1.0f/24.0f, -1.0f/720.0f);
    
	// Constants for cosine
	glUniform4f( uniform_constants[10], 0.0f, 0.5f, 1.0f, 0.2f);
	glUniform4f( uniform_constants[11], 0.25f, -9.0f, 0.75f, 1.0f / (2.0f * M_PI) );
	glUniform4f( uniform_constants[12], 24.9808039603f, -24.9808039603f, -60.1458091736f, 60.1458091736f );
	glUniform4f( uniform_constants[13], 85.4537887573f, -85.4537887573f, -64.9393539429f, 64.9393539429f );
	glUniform4f( uniform_constants[14], 19.7392082214f, -19.7392082214f, - 1.0f,	1.0f );
    
	// Light position
	glUniform4f( uniform_constants[15], 0.0f, 0.7f, 0.7f, 1.0f );
	glUniform4f( uniform_constants[17], 0.3f, 0.3f, 0.3f, 1.0f );
    
    glDeleteShader(vs);
    glDeleteShader(fs);
}

Now, one of your first thoughts might be "why on earth are you using glVertex3fv and friends?" FYI I didn't write the original code, this came from a [very] old version of the NVSDK and this code was invaluable to me and my future endeavours, so I wanted to expand on it. happy.png

Also, I wanted to state that instead of inverting and transposing the modelview matrix every frame, I thought that using gl_NormalMatrix would suffice (since it's essentially the same thing in a 3x3 matrix). Not sure if that matters, but I thought I'd add that in there anyway.

Lastly, I'm using GLSL 1.2 for the time being because I want to port this to OpenGL ES 2.x+ later on. Of course, my main goal is just getting it to work, so I can move on to more important things. After this works, I'll be moving on to GLSL 4.3, of course.

Hopefully this is enough information, and sorry if I started going off on tangents. I'm almost to the point where I'm ready to hire someone to help me with this! blink.png This really sucks. Any ideas? Thanks, I really appreciate it.

Shogun.

Advertisement

I'm not familiar with NV_vertex_program, but I am with ARB_vertex_program, so I'm going to confine myself to the ASM-to-GLSL conversion part of your problem. I can say upfront that the ASM code looks quite straightforward but you are making things a little more difficult on yourself than you need to.

Take the following block of code for example:


DP3 R5.w, R5, R5;
RSQ R5.w, R5.w;
MUL R5, R5, R5.w;

That's just normalizing R5, but yet you've converted it line-by-line to GLSL. A much better way of converting this would be:


R5 = normalize (R5);

You've similar in the block below it, and a similar solution would apply; i.e. "R6 = normalize (c[8] + v[OPOS]);" instead of translating the ASM line-by-line.

Once you recognise patterns like this you'll be able to greatly simplify your GLSL conversion, which should assist in troubleshooting.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

Thank you for your response, I do appreciate your advice. I'm kinda surprised I didn't catch that little detail. There were some things that were rather obvious, and that should have been one of them. laugh.png

GL_NV_vertex_program isn't much different from GL_ARB_vertex_program, only the former is a bit simplified and IMO easier to deal with. I also assume it has to deal with my ASM -> GLSL conversion somewhere, that or I'm not using the GLSL extension functions properly...

As far as debugging OpenGL, I'm not sure where to begin since it doesn't look like I can debug it like I would by setting breakpoints in XCode or Visual Studio, or even get some sort of visual of the problem? I just downloaded the gfx tools for XCode, so hopefully that will help in some form or another. Unfortunately, there's not much out there for us MacOSX users.

Any other ideas?

Shogun.


I just downloaded the gfx tools for XCode, so hopefully that will help in some form or another.

There is a moderately robust OpenGL Profiler application in there. And somewhere within that application is an OpenGL Driver Monitor application, which should let you inspect your shaders, etc.

Sadly the flagship program for this sort of thing (gDEBugger) is no longer available for Mac, since AMD bought it out.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

EDIT: I just downloaded those and they keep crashing. =/ I guess I'll download an older version and see if it works...

EDIT2: This is what the jellyfish is supposed to look like:

jellyfish_1.png

Oh yeah, I forgot to mention that I still do have the old windows source here. It works fine, but only without the modifications so far (so you'll need an NVIDIA card; any modern one will do). There's lots of other irrelevant stuff in there (i.e. post processing, fog, some really lame underwater environment rendering code, etc.), but the relevant stuff is very easy to get to.

http://code.shogun3d.net/projects/Jellyfish.rar

Maybe that might help.

Shogun.

One problem I see in your GLSL loader is your use of glUniform calls; glUniform calls operate on the current program, but yet you don't have a glUseProgram call anywhere.

In other words, GLSL has no equivalent to glProgramEnvParameterARB; every uniform is effectively "local", so you definitely need to pop a glUseProgram call before you can start setting uniforms.

In more recent versions of OpenGL (or if you're willing to use GL_EXT_direct_state_access) you have a glProgramUniform call which allows setting uniforms without a glUseProgram call.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

Actually, I do have a glUseProgram call! wink.png It's in Jellyfish::create(), called right after the display list is being set up. But you're right about having to call glUseProgram before setting the uniforms, and whatnot.

I'll have to look into those extensions you mentioned there. That could come in handy.

On the positive side, I set glUseProgram before setting up the constants, and now I'm getting some really funky results. Not the results I want, but it's better than nothing! Right now, it looks like a game running on broken drivers, or a broken emulator.

[attachment=18829:Screen Shot 2013-11-19 at 2.49.48 PM.png]

I am very greatful for your help so far! Maybe I will get this working today! happy.png

Shogun

An update on this:

Good news: I fixed it with the help of Apple's OpenGL Shader Builder. Where has this thing been all my life?? Me and this tool are going to have a nice, LONG and intimate relationship together! happy.png

Bad news: I only managed to get the ARB version working (turns out that I had to remove the comment at the top, and it the inverse transpose of the modelview was already provided). The GLSL version is completely broken on the tool, and the actual program.

New shader (ARB):


!!ARBvp1.0

# Ported from !!VP1.0 to !!ARBvp1.0 
# by blueshogun96

ATTRIB iPos = vertex.position;
ATTRIB iCol = vertex.color;
ATTRIB iTex = vertex.texcoord;
ATTRIB iNor = vertex.normal;

PARAM mvp[4] = {state.matrix.mvp};
PARAM itmv[4] = {state.matrix.modelview.invtrans};

#PARAM c0 = program.local[0];
#PARAM c1 = program.local[1];
#PARAM c2 = program.local[2];
#PARAM c3 = program.local[3];
#PARAM c4 = program.local[4];
#PARAM c5 = program.local[5];
#PARAM c6 = program.local[6];

PARAM c8 = program.local[8];
PARAM c9 = program.local[9];
PARAM c10 = program.local[10];
PARAM c11 = program.local[11];
PARAM c12 = program.local[12];
PARAM c13 = program.local[13];
PARAM c14 = program.local[14];
PARAM c15 = program.local[15];
PARAM c16 = program.local[16];
PARAM c17 = program.local[17];

OUTPUT oPos = result.position;
OUTPUT oCol = result.color;
OUTPUT oTex = result.texcoord;
#OUTPUT oNor = result.normal;

TEMP R0;
TEMP R1;
TEMP R2;
TEMP R3;
TEMP R4;
TEMP R5;
TEMP R6;

# R0 = (y coordinate of vertex * k) + (w * t)
MAD R0.x, iPos.y, c16.x, c16.z;

# R1 = cos(R0), R0 = any floating point number
MUL R0.x, c11.w, R0.x; 
EXP R0.y, R0.x; 
SLT R2.x, R0.y, c11; 
SGE R2.yz, R0.y, c11; 
DP3 R2.y, R2, c14.zwzw;   
ADD R5.xyz, -R0.y, c10; 
MUL R5, R5, R5;
MAD R0, c12.xyxy, R5, c12.zwzw; 
MAD R0, R0, R5, c13.xyxy;      
MAD R0, R0, R5, c13.zwzw;      
MAD R0, R0, R5, c14.xyxy;      
MAD R0, R0, R5, c14.zwzw;      
DP3 R1.x, R0, -R2; 

# R2 = R1 * c[10] + 1.0
MAD R2.x, R1.x, c10.w, c10.z;

# R3 = perturbed vertex
# R4 = perturbed normal
MOV R2.yw, c16;
MUL R3, iPos, R2.xyxw;
MUL R4, iNor, R2.yxyw;

# Transform vertices into clip space via modelview-projection matrix
DP4 oPos.x, R3, mvp[0];
DP4 oPos.y, R3, mvp[1];
DP4 oPos.z, R3, mvp[2];
DP4 oPos.w, R3, mvp[3];

# Transform normals via inverse transpose modelview matrix & normalize
# R5 = transformed normal
DP3 R5.x, R4, itmv[0];
DP3 R5.y, R4, itmv[1];
DP3 R5.z, R4, itmv[2];
DP3 R5.w, R5, R5;
RSQ R5.w, R5.w;
MUL R5, R5, R5.w;

# Get unit length eye vector to vertex....this is how we texture map
# R6 = vector
ADD R6, c8, iPos;
DP3 R6.w, R6, R6;
RSQ R6.w, R6.w;
MUL R6, R6, R6.w;

# Multiply by 0.5 for texture wrapping
MUL R6, R6, c10.y;
	    
# Texture coord is dot product of normal and vector from eye to vertex
DP3 oTex.x, R5, R6;

# Pass color through
MOV oCol, iCol;

END 

[attachment=18845:Screen Shot 2013-11-20 at 4.30.15 PM.png]

It looks even better with post processing effects such as glow, bloom, and a little bit of blur though; the final product is going to look phenomenal, I guarantee it!

There doesn't appear to be an easy way to convert assembly shaders to GLSL though. I found this interesting article though, but that's not of much help to me, IMO.

Shogun

EDIT: There's 3 instructions (maybe 4) that I'm not sure that I've converted properly.

1. SLT R2.x, R0.y, c11;
2. SGE R2.yz, R0.y, c11;

3. ADD R5.xyz, -R0.y, c10;

4. MOV R2.yw, c16;

This is where I'm kinda confused. I understand what these instructions do, but I do not understand how the swizzling effects the output of each operation.

1 & 2: I understand that x and then yz are the values being written to, but is the value in R0.y being compared to c11.y or the first component, c11.x? Or all of them?

3. This is legal in GL_NV/ARB_vertex_program, but not in GLSL, so I don't understand how ARB handles this either. Is R0.y being subtracted from each component in c10, and input into R5.xyz?

4. This also confuses me. So, am I moving c16.xy only into R2.yw? Or am I moving c16.yw only?

Right now, I'm going through the ARB_vertex_program documentation trying to get a better understanding of how the swizzling works because I think I've been misunderstanding it the whole time. Any ideas?

Okay, sorry for bumping this old thread, but I think I *almost* got it. Sadly, at one point, I was even closer, and I didn't realize what I had changed, and ended up losing that progress. sad.png

But this is it now. I'm beginning to wonder if it has anything to do with the w component. I thought converting an assembly shader would be straight forward, but I guess it's not.

[attachment=19294:Screen Shot 2014-01-05 at 1.23.26 AM.png]

[attachment=19295:Screen Shot 2014-01-05 at 1.24.48 AM.png]

The reason I'm so dedicated to this is because I need to get this done by January 18th.

I changed much of my original GLSL converted shader, and it should be much cleaner now. It's probably some little detail I'm overlooking. Everything looks warped, as if the perspective is wrong...


/* Vertex attributes */
attribute vec4 v_pos;
attribute vec4 v_normal;
varying float v_tex;

/* Texture sampler */
uniform sampler1D texture;

/* General purpose vectors */
vec4 R0, R1, R2, R3, R4, R5, R6;

/* Constants */
uniform vec4 c8;
uniform vec4 c9;
uniform vec4 c10;
uniform vec4 c11;
uniform vec4 c12;
uniform vec4 c13;
uniform vec4 c14;
uniform vec4 c15;
uniform vec4 c16;
uniform vec4 c17;

/* Matrices */
mat4 mvp = gl_ModelViewProjectionMatrix;
mat3 itmm = gl_NormalMatrix;


void main()
{
    R0 = R1 = R2 = R3 = R4 = R5 = R6 = vec4( 0, 0, 0, 1 );
    R0.x = ( v_pos.y * c16.x ) + c16.z;

    R0.x *= c11.w;
    R0.y = exp( R0.x );
    R2.x = ( R0.y < c11.y ) ? 1.0 : 0.0;
    R2.y = ( R0.y >= c11.y ) ? 1.0 : 0.0;
    R2.z = ( R0.y >= c11.y ) ? 1.0 : 0.0;
    R2.y = dot( R2, c14.zwzw );
    R5 = -R0.y + c10;
    R5 = R5 * R5;
    R0 = ( c12.xyxy * R5 ) + c12.zwzw;
    R0 = ( R0 * R5 ) + c13.xyxy;
    R0 = ( R0 * R5 ) + c13.zwzw;
    R0 = ( R0 * R5 ) + c14.xyxy;
    R0 = ( R0 * R5 ) + c14.zwzw;
    R1.x = dot( R0.xyz, -R2.xyz );
    //R1.x = cos(R0.x);
    
    R2.x = ( R1.x * c10.w ) + c10.z;
    
    R2.yw = c16.yw;
    R3 = v_pos * R2.xyxw;
    R4.xyz = v_normal.xyz * R2.yxy;
    
    gl_Position = R3 * mvp;
    
    R5.xyz = R4.xyz * itmm;
    R5.xyz = normalize(R5.xyz);
    R6.xyz = normalize( c8.xyz + v_pos.xyz );
    R6 = R6 * c10.y;
    
    v_tex = dot( R5.xyz, R6.xyz );
    
    gl_FrontColor = gl_Color;
}

Lastly, I read an article about someone who write an ARB_vertex_program -> GLSL converter for Mesa. I've seen many cross compilers to convert various shader languages, but none for my case so far. I'm also at the point where I'm considering paying someone $100 USD to do it due to the sheer frustration I've been having for many months.

Any more ideas? Thanks.

Shogun.

This topic is closed to new replies.

Advertisement