• Advertisement
  • Popular Tags

  • Popular Now

  • Advertisement
  • Similar Content

    • By tj8146
      I am using immediate mode for OpenGL and I am creating a 2D top down car game. I am trying to configure my game loop in order to get my car-like physics working on a square shape. I have working code but it is not doing as I want it to. I am not sure as to whether it is my game loop that is incorrect or my code for the square is incorrect, or maybe both! Could someone help because I have been trying to work this out for over a day now
      I have attached my .cpp file if you wish to run it for yourself.. 
      WinMain code:
      /******************* WIN32 FUNCTIONS ***************************/ int WINAPI WinMain( HINSTANCE hInstance, // Instance HINSTANCE hPrevInstance, // Previous Instance LPSTR lpCmdLine, // Command Line Parameters int nCmdShow) // Window Show State { MSG msg; // Windows Message Structure bool done=false; // Bool Variable To Exit Loop Car car; car.x = 220; car.y = 140; car.dx = 0; car.dy = 0; car.ang = 0; AllocConsole(); FILE *stream; freopen_s(&stream, "CONOUT$", "w", stdout); // Create Our OpenGL Window if (!CreateGLWindow("OpenGL Win32 Example",screenWidth,screenHeight)) { return 0; // Quit If Window Was Not Created } while(!done) // Loop That Runs While done=FALSE { if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) // Is There A Message Waiting? { if (msg.message==WM_QUIT) // Have We Received A Quit Message? { done=true; // If So done=TRUE break; } else // If Not, Deal With Window Messages { TranslateMessage(&msg); // Translate The Message DispatchMessage(&msg); // Dispatch The Message } } else // If There Are No Messages { if(keys[VK_ESCAPE]) done = true; void processKeys(Car& car); //process keyboard while (game_is_running) { loops = 0; while (GetTickCount() > next_game_tick && loops < MAX_FRAMESKIP) { update(car); // update variables next_game_tick += SKIP_TICKS; loops++; } display(car); // Draw The Scene SwapBuffers(hDC); // Swap Buffers (Double Buffering) } } } // Shutdown KillGLWindow(); // Kill The Window return (int)(msg.wParam); // Exit The Program } //WIN32 Processes function - useful for responding to user inputs or other events. LRESULT CALLBACK WndProc( HWND hWnd, // Handle For This Window UINT uMsg, // Message For This Window WPARAM wParam, // Additional Message Information LPARAM lParam) // Additional Message Information { switch (uMsg) // Check For Windows Messages { case WM_CLOSE: // Did We Receive A Close Message? { PostQuitMessage(0); // Send A Quit Message return 0; // Jump Back } break; case WM_SIZE: // Resize The OpenGL Window { reshape(LOWORD(lParam),HIWORD(lParam)); // LoWord=Width, HiWord=Height return 0; // Jump Back } break; case WM_LBUTTONDOWN: { mouse_x = LOWORD(lParam); mouse_y = screenHeight - HIWORD(lParam); LeftPressed = true; } break; case WM_LBUTTONUP: { LeftPressed = false; } break; case WM_MOUSEMOVE: { mouse_x = LOWORD(lParam); mouse_y = screenHeight - HIWORD(lParam); } break; case WM_KEYDOWN: // Is A Key Being Held Down? { keys[wParam] = true; // If So, Mark It As TRUE return 0; // Jump Back } break; case WM_KEYUP: // Has A Key Been Released? { keys[wParam] = false; // If So, Mark It As FALSE return 0; // Jump Back } break; } // Pass All Unhandled Messages To DefWindowProc return DefWindowProc(hWnd,uMsg,wParam,lParam); }  
      C++ and OpenGL code:
      int mouse_x=0, mouse_y=0; bool LeftPressed = false; int screenWidth=1080, screenHeight=960; bool keys[256]; float radiansFromDegrees(float deg) { return deg * (M_PI / 180.0f); } float degreesFromRadians(float rad) { return rad / (M_PI / 180.0f); } bool game_is_running = true; const int TICKS_PER_SECOND = 50; const int SKIP_TICKS = 1000 / TICKS_PER_SECOND; const int MAX_FRAMESKIP = 10; DWORD next_game_tick = GetTickCount(); int loops; typedef struct { float x, y; float dx, dy; float ang; }Car; //OPENGL FUNCTION PROTOTYPES void display(const Car& car); //called in winmain to draw everything to the screen void reshape(int width, int height); //called when the window is resized void init(); //called in winmain when the program starts. void processKeys(Car& car); //called in winmain to process keyboard input void update(Car& car); //called in winmain to update variables /************* START OF OPENGL FUNCTIONS ****************/ void display(const Car& car) { const float w = 50.0f; const float h = 50.0f; glClear(GL_COLOR_BUFFER_BIT); glLoadIdentity(); glTranslatef(100, 100, 0); glBegin(GL_POLYGON); glVertex2f(car.x, car.y); glVertex2f(car.x + w, car.y); glVertex2f(car.x + w, car.y + h); glVertex2f(car.x, car.y + h); glEnd(); glFlush(); } void reshape(int width, int height) // Resize the OpenGL window { screenWidth = width; screenHeight = height; // to ensure the mouse coordinates match // we will use these values to set the coordinate system glViewport(0, 0, width, height); // Reset the current viewport glMatrixMode(GL_PROJECTION); // select the projection matrix stack glLoadIdentity(); // reset the top of the projection matrix to an identity matrix gluOrtho2D(0, screenWidth, 0, screenHeight); // set the coordinate system for the window glMatrixMode(GL_MODELVIEW); // Select the modelview matrix stack glLoadIdentity(); // Reset the top of the modelview matrix to an identity matrix } void init() { glClearColor(1.0, 1.0, 0.0, 0.0); //sets the clear colour to yellow //glClear(GL_COLOR_BUFFER_BIT) in the display function //will clear the buffer to this colour. } void processKeys(Car& car) { if (keys[VK_UP]) { float cdx = sinf(radiansFromDegrees(car.ang)); float cdy = -cosf(radiansFromDegrees(car.ang)); car.dx += cdx; car.dy += cdy; } if (keys[VK_DOWN]) { float cdx = sinf(radiansFromDegrees(car.ang)); float cdy = -cosf(radiansFromDegrees(car.ang)); car.dx += -cdx; car.dy += -cdy; } if (keys[VK_LEFT]) { car.ang -= 2; } if (keys[VK_RIGHT]) { car.ang += 2; } } void update(Car& car) { car.x += car.dx*next_game_tick; }  
      game.cpp
    • By tj8146
      I am using immediate mode for OpenGL and I am creating a 2D top down car game. I am trying to configure my game loop in order to get my car-like physics working on a square shape. I have working code but it is not doing as I want it to. I am not sure as to whether it is my game loop that is incorrect or my code for the square is incorrect, or maybe both! Could someone help because I have been trying to work this out for over a day now
      I have attached my .cpp file if you wish to run it for yourself.. 
       
      This is my C++ and OpenGL code:
      int mouse_x=0, mouse_y=0; bool LeftPressed = false; int screenWidth=1080, screenHeight=960; bool keys[256]; float radiansFromDegrees(float deg) { return deg * (M_PI / 180.0f); } float degreesFromRadians(float rad) { return rad / (M_PI / 180.0f); } bool game_is_running = true; const int TICKS_PER_SECOND = 50; const int SKIP_TICKS = 1000 / TICKS_PER_SECOND; const int MAX_FRAMESKIP = 10; DWORD next_game_tick = GetTickCount(); int loops; typedef struct { float x, y; float dx, dy; float ang; }Car; //OPENGL FUNCTION PROTOTYPES void display(const Car& car); //called in winmain to draw everything to the screen void reshape(int width, int height); //called when the window is resized void init(); //called in winmain when the program starts. void processKeys(Car& car); //called in winmain to process keyboard input void update(Car& car); //called in winmain to update variables /************* START OF OPENGL FUNCTIONS ****************/ void display(const Car& car) { const float w = 50.0f; const float h = 50.0f; glClear(GL_COLOR_BUFFER_BIT); glLoadIdentity(); glTranslatef(100, 100, 0); glBegin(GL_POLYGON); glVertex2f(car.x, car.y); glVertex2f(car.x + w, car.y); glVertex2f(car.x + w, car.y + h); glVertex2f(car.x, car.y + h); glEnd(); glFlush(); } void reshape(int width, int height) // Resize the OpenGL window { screenWidth = width; screenHeight = height; // to ensure the mouse coordinates match // we will use these values to set the coordinate system glViewport(0, 0, width, height); // Reset the current viewport glMatrixMode(GL_PROJECTION); // select the projection matrix stack glLoadIdentity(); // reset the top of the projection matrix to an identity matrix gluOrtho2D(0, screenWidth, 0, screenHeight); // set the coordinate system for the window glMatrixMode(GL_MODELVIEW); // Select the modelview matrix stack glLoadIdentity(); // Reset the top of the modelview matrix to an identity matrix } void init() { glClearColor(1.0, 1.0, 0.0, 0.0); //sets the clear colour to yellow //glClear(GL_COLOR_BUFFER_BIT) in the display function //will clear the buffer to this colour. } void processKeys(Car& car) { if (keys[VK_UP]) { float cdx = sinf(radiansFromDegrees(car.ang)); float cdy = -cosf(radiansFromDegrees(car.ang)); car.dx += cdx; car.dy += cdy; } if (keys[VK_DOWN]) { float cdx = sinf(radiansFromDegrees(car.ang)); float cdy = -cosf(radiansFromDegrees(car.ang)); car.dx += -cdx; car.dy += -cdy; } if (keys[VK_LEFT]) { car.ang -= 2; } if (keys[VK_RIGHT]) { car.ang += 2; } } void update(Car& car) { car.x += car.dx*next_game_tick; } My WinMain code:
      /******************* WIN32 FUNCTIONS ***************************/ int WINAPI WinMain( HINSTANCE hInstance, // Instance HINSTANCE hPrevInstance, // Previous Instance LPSTR lpCmdLine, // Command Line Parameters int nCmdShow) // Window Show State { MSG msg; // Windows Message Structure bool done=false; // Bool Variable To Exit Loop Car car; car.x = 220; car.y = 140; car.dx = 0; car.dy = 0; car.ang = 0; AllocConsole(); FILE *stream; freopen_s(&stream, "CONOUT$", "w", stdout); // Create Our OpenGL Window if (!CreateGLWindow("OpenGL Win32 Example",screenWidth,screenHeight)) { return 0; // Quit If Window Was Not Created } while(!done) // Loop That Runs While done=FALSE { if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) // Is There A Message Waiting? { if (msg.message==WM_QUIT) // Have We Received A Quit Message? { done=true; // If So done=TRUE break; } else // If Not, Deal With Window Messages { TranslateMessage(&msg); // Translate The Message DispatchMessage(&msg); // Dispatch The Message } } else // If There Are No Messages { if(keys[VK_ESCAPE]) done = true; void processKeys(Car& car); //process keyboard while (game_is_running) { loops = 0; while (GetTickCount() > next_game_tick && loops < MAX_FRAMESKIP) { update(car); // update variables next_game_tick += SKIP_TICKS; loops++; } display(car); // Draw The Scene SwapBuffers(hDC); // Swap Buffers (Double Buffering) } } } // Shutdown KillGLWindow(); // Kill The Window return (int)(msg.wParam); // Exit The Program } //WIN32 Processes function - useful for responding to user inputs or other events. LRESULT CALLBACK WndProc( HWND hWnd, // Handle For This Window UINT uMsg, // Message For This Window WPARAM wParam, // Additional Message Information LPARAM lParam) // Additional Message Information { switch (uMsg) // Check For Windows Messages { case WM_CLOSE: // Did We Receive A Close Message? { PostQuitMessage(0); // Send A Quit Message return 0; // Jump Back } break; case WM_SIZE: // Resize The OpenGL Window { reshape(LOWORD(lParam),HIWORD(lParam)); // LoWord=Width, HiWord=Height return 0; // Jump Back } break; case WM_LBUTTONDOWN: { mouse_x = LOWORD(lParam); mouse_y = screenHeight - HIWORD(lParam); LeftPressed = true; } break; case WM_LBUTTONUP: { LeftPressed = false; } break; case WM_MOUSEMOVE: { mouse_x = LOWORD(lParam); mouse_y = screenHeight - HIWORD(lParam); } break; case WM_KEYDOWN: // Is A Key Being Held Down? { keys[wParam] = true; // If So, Mark It As TRUE return 0; // Jump Back } break; case WM_KEYUP: // Has A Key Been Released? { keys[wParam] = false; // If So, Mark It As FALSE return 0; // Jump Back } break; } // Pass All Unhandled Messages To DefWindowProc return DefWindowProc(hWnd,uMsg,wParam,lParam); }  
      game.cpp
    • By lxjk
      Hi guys,
      There are many ways to do light culling in tile-based shading. I've been playing with this idea for a while, and just want to throw it out there.
      Because tile frustums are general small compared to light radius, I tried using cone test to reduce false positives introduced by commonly used sphere-frustum test.
      On top of that, I use distance to camera rather than depth for near/far test (aka. sliced by spheres).
      This method can be naturally extended to clustered light culling as well.
      The following image shows the general ideas

       
      Performance-wise I get around 15% improvement over sphere-frustum test. You can also see how a single light performs as the following: from left to right (1) standard rendering of a point light; then tiles passed the test of (2) sphere-frustum test; (3) cone test; (4) spherical-sliced cone test
       

       
      I put the details in my blog post (https://lxjk.github.io/2018/03/25/Improve-Tile-based-Light-Culling-with-Spherical-sliced-Cone.html), GLSL source code included!
       
      Eric
    • By Fadey Duh
      Good evening everyone!

      I was wondering if there is something equivalent of  GL_NV_blend_equation_advanced for AMD?
      Basically I'm trying to find more compatible version of it.

      Thank you!
    • By Jens Eckervogt
      Hello guys, 
       
      Please tell me! 
      How do I know? Why does wavefront not show for me?
      I already checked I have non errors yet.
      using OpenTK; using System.Collections.Generic; using System.IO; using System.Text; namespace Tutorial_08.net.sourceskyboxer { public class WaveFrontLoader { private static List<Vector3> inPositions; private static List<Vector2> inTexcoords; private static List<Vector3> inNormals; private static List<float> positions; private static List<float> texcoords; private static List<int> indices; public static RawModel LoadObjModel(string filename, Loader loader) { inPositions = new List<Vector3>(); inTexcoords = new List<Vector2>(); inNormals = new List<Vector3>(); positions = new List<float>(); texcoords = new List<float>(); indices = new List<int>(); int nextIdx = 0; using (var reader = new StreamReader(File.Open("Contents/" + filename + ".obj", FileMode.Open), Encoding.UTF8)) { string line = reader.ReadLine(); int i = reader.Read(); while (true) { string[] currentLine = line.Split(); if (currentLine[0] == "v") { Vector3 pos = new Vector3(float.Parse(currentLine[1]), float.Parse(currentLine[2]), float.Parse(currentLine[3])); inPositions.Add(pos); if (currentLine[1] == "t") { Vector2 tex = new Vector2(float.Parse(currentLine[1]), float.Parse(currentLine[2])); inTexcoords.Add(tex); } if (currentLine[1] == "n") { Vector3 nom = new Vector3(float.Parse(currentLine[1]), float.Parse(currentLine[2]), float.Parse(currentLine[3])); inNormals.Add(nom); } } if (currentLine[0] == "f") { Vector3 pos = inPositions[0]; positions.Add(pos.X); positions.Add(pos.Y); positions.Add(pos.Z); Vector2 tc = inTexcoords[0]; texcoords.Add(tc.X); texcoords.Add(tc.Y); indices.Add(nextIdx); ++nextIdx; } reader.Close(); return loader.loadToVAO(positions.ToArray(), texcoords.ToArray(), indices.ToArray()); } } } } } And It have tried other method but it can't show for me.  I am mad now. Because any OpenTK developers won't help me.
      Please help me how do I fix.

      And my download (mega.nz) should it is original but I tried no success...
      - Add blend source and png file here I have tried tried,.....  
       
      PS: Why is our community not active? I wait very longer. Stop to lie me!
      Thanks !
  • Advertisement
  • Advertisement
Sign in to follow this  

OpenGL Textures not working (CgFX + OpenGL): draws black silhoette (works ok in FXComposer)

This topic is 3589 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all, My first CgFX application is giving me trouble. I've managed to get some very simple shaders working but am having a lot of trouble passing textures to it. I'm trying to make use of existing libs (namely NVidia's nv::Image for loading the image data from file). The shader works fine in FX Composer (2.5b). The code is clean enough and should be logging all errors (using 'cgGetLastErrorString()' and 'glGetError()' etc). Shading appears is ok when I don't use the texture. But as soon as the texture is used, the CgFX shader compiles without warning and runs without error... but the test sphere I'm trying to draw is a black circle (which my arcball manipulator spins without any trouble). Can anyone suggest why when I use the following code all I see is a black circle? I've been chipping away at this for days (i hate to admit though it's becoming weeks...). can someone please put me out of my misery... even if it looks ok... any opinions/suggestions much a appreciated.
// simpleTextured.cgfx

// This is C2E1v_green from "The Cg Tutorial" (Addison-Wesley, ISBN
// 0321194969) by Randima Fernando and Mark J. Kilgard.  See page 38.

float4x4 modelViewProj : WorldViewProjection < string UIWidget="None"; >; 

float3 globalAmbient : Ambient    = { 0.1, 0.1, 0.1 };
float3 lightColor : Specular <
    string UIName =  "Lamp 0";
    string Object = "Pointlight0";
    string UIWidget = "Color";
> = {1.0f,1.0f,1.0f};
float3 lightPosition : Position <
    string Object = "PointLight0";
    string UIName =  "Lamp 0 Position";
    string Space = "World";
> = {0.81,-3.65,5};
float3 eyePosition   : Position   = { 0, 0, 13 };
float3 Ke : Emissive < string UIWidget = "Color"; > = {0.0, 0.0, 0.0};
float3 Ka : Ambient  = {0.0, 0.0, 0.0};
float3 Kd : Diffuse  = {0.5, 0.0, 0.0};
float3 Ks < string UIWidget = "Color"; > = {0.7, 0.6, 0.6};
float  shininess <
    string UIWidget = "slider";
    float UIMin = 0.0;
    float UIMax = 100.0;
    float UIStep = 1.0;
    string UIName =  "Specular";
> = 32.0;


//////// COLOR & TEXTURE /////////////////////

texture ColorTexture  <
    string ResourceName = "default_color.dds";
    string UIName =  "Diffuse Texture";
    string ResourceType = "2D";
>;

sampler2D ColorSampler = sampler_state {
    Texture = <ColorTexture>;
    MinFilter = LinearMipMapLinear;
    MagFilter = Linear;
    WrapS = Repeat;
    WrapT = Repeat;
};  
  
// This is C5E2v_fragmentLighting from "The Cg Tutorial" (Addison-Wesley, ISBN
// 0321194969) by Randima Fernando and Mark J. Kilgard.  See page 124.

void main(float4 position : POSITION,
             float3 normal   : NORMAL,
             float2 uv : TEXCOORD0,
             out float4 oPosition : POSITION,
             out float2 oUV : TEXCOORD0,
             out float3 objectPos : TEXCOORD1,
             out float3 oNormal   : TEXCOORD2)
{
	oPosition = mul(modelViewProj, position);
	objectPos = position.xyz;
	oNormal = normal;
    oUV = uv;
}

// This is C5E3f_basicLight from "The Cg Tutorial" (Addison-Wesley, ISBN
// 0321194969) by Randima Fernando and Mark J. Kilgard.  See page 125.

void psLight(float4 position  : TEXCOORD0,
             float2 uv : TEXCOORD1,
             float3 normal    : TEXCOORD2,
             out float4 color     : COLOR)
{
  float3 P = position.xyz;
  float3 N = normalize(normal);

  // Compute emissive term
  float3 emissive = Ke;

  // Compute ambient term
  float3 ambient = Ka * globalAmbient;

  // Compute the diffuse term
  float3 L = normalize(lightPosition - P);
  //float diffuseLight = max(dot(L, N), 0);
  float3 diffuseLight = tex2D(ColorSampler, uv) + max(dot(L, N), 0);
  float3 diffuse = Kd * lightColor * diffuseLight;

  // Compute the specular term
  float3 V = normalize(eyePosition - P);
  float3 H = normalize(L + V);
  float specularLight = pow(max(dot(H, N), 0), shininess);
  if (diffuseLight <= 0) specularLight = 0;
  float3 specular = Ks * lightColor * specularLight;

  color.xyz = emissive + ambient + diffuse + specular;
  color.w = 1;
}

technique NewTechnique <
	string Script = "Pass=p0;";
> {
    pass p0 <
	string Script = "Draw=geometry;";
    > {
        VertexProgram = compile vp40 main();
		DepthTestEnable = true;
		DepthMask = true;
		CullFaceEnable = false;
		BlendEnable = false;
		DepthFunc = LEqual;
        FragmentProgram = compile fp40 psLight();
    }
}






//  Initialize() CPP

    cgContext = cgCreateContext();
    LOG_CG("cgCreateContext", cgContext);
    cgVertexProfile = cgGLGetLatestProfile(CG_GL_VERTEX);
    cgFragmentProfile = cgGLGetLatestProfile(CG_GL_FRAGMENT);
    cgGLSetOptimalOptions(cgVertexProfile);
    cgGLSetOptimalOptions(cgFragmentProfile);
    LOG_CG("cgGLSetOptimalOptions", cgContext);
    LOG("Cg Vertex Profile:   " << cgGetProfileString(cgVertexProfile));
    LOG("Cg Fragment Profile: " << cgGetProfileString(cgFragmentProfile));

    PathResolver pathResolver("");
    //const std::string cgFXFile = "phong.cgfx";
    //const std::string cgFXFile = "green.cg";
    //const std::string cgFXFile = "vertlight.cgfx";
    const std::string cgFXFile = "blinn.cgfx";
    std::string resolvedCgFXFile = "";
    if (pathResolver.getFilePath(cgFXFile, resolvedCgFXFile))
    {
        cgProgram = cgCreateProgramFromFile(cgContext, CG_SOURCE, resolvedCgFXFile.c_str(), cgVertexProfile, "main", 0);
        LOG_CG("cgCreateProgramFromFile(" << resolvedCgFXFile << ")", cgContext);
    }
    else
        LOG_ERROR("File not found: " << cgFXFile);

    const std::string modelFile = "cow.obj";
    std::string resolvedModelFile = "";
    if (pathResolver.getFilePath(modelFile, resolvedModelFile))
        model = Utils::LoadModel(resolvedModelFile.c_str(), &modelBBMin, &modelBBMax);
    else
        LOG_ERROR("File not found: " << modelFile);

    cgGLLoadProgram(cgProgram);
    LOG_CG("cgGLLoadProgram", cgContext);

    if(cgProgram == 0)
    {
        LOG_ERROR("Invalid Cg program. This program will now exit...");
        exit(1);
    }

    cgGLRegisterStates(cgContext); 
    LOG_CG("cgGLRegisterStates", cgContext);
    cgGLSetManageTextureParameters(cgContext, CG_TRUE); 
    LOG_CG("cgGLSetManageTextureParameters", cgContext);
    cgEffect = cgCreateEffectFromFile(cgContext, resolvedCgFXFile.c_str(), NULL); 
    LOG_CG("cgCreateEffectFromFile(" << resolvedCgFXFile << ")", cgContext);

    cgTechnique = cgGetFirstTechnique(cgEffect);
    while (cgTechnique && cgValidateTechnique(cgTechnique) == CG_FALSE) 
    {
        LOG_ERROR("Technique '" << cgGetTechniqueName(cgTechnique) << "' did not validate");
        cgTechnique = cgGetNextTechnique(cgTechnique);
    }
    
    LOG_CG("CG Techniques initialise", cgContext);

    if (cgTechnique) {
        LOG("Using Cg technique '" << cgGetTechniqueName(cgTechnique) << "' from '" << cgFXFile << "'.");
    } else {
        LOG_ERROR("Valid Cg Technique not found");
    }

    // Load the texture (if needed)
    CGparameter param = cgGetNamedParameter(cgProgram, "ColorSampler");
    LOG_CG("cgGetNamedParameter", cgContext);
    if (param != 0) {
        cgGLEnableTextureParameter(param);

        image = new nv::Image();
        const std::string texFile = "Default_color.dds";
        std::string resolvedTexFile = "";
        if (!(pathResolver.getFilePath(texFile, resolvedTexFile) && 
              image->loadImageFromFile(resolvedTexFile.c_str())))
            LOG_ERROR("File not found: " << texFile);

        LOG("Texture loaded (" << resolvedTexFile << ")");

        GLuint texName;    
        glGenTextures(1, &texName);
        glBindTexture(GL_TEXTURE_2D, texName);
	    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
	    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	    glTexParameteri( GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_FALSE);
        glTexImage2D( GL_TEXTURE_2D, 0, image->getInternalFormat(), image->getWidth(), image->getHeight(), 0, image->getFormat(), image->getType(), image->getLevel(0));
        LOG_OGL("glTexImage2D()");

        cgGLSetTextureParameter(param, texName);
        LOG_CG("cgGLSetTextureParameter(param, texName)", cgContext);
    }
    else
        LOG("WARNING: No texture parameter found. Expecting 'sampler2D ColorSampler'.");

    cgGLEnableProfile(cgVertexProfile);
    cgGLEnableProfile(cgFragmentProfile);






//  Display() in CPP
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    manipulator.applyTransform();

    CGparameter param = 0;
    param = cgGetEffectParameterBySemantic(cgEffect, "WorldViewProjection");
    if (param != 0)
        cgGLSetStateMatrixParameter(param, CG_GL_MODELVIEW_MATRIX, CG_GL_MATRIX_IDENTITY);
    LOG_CG("cgGLSetStateMatrixParameter(WorldViewProjection)", cgContext);

    cgGLBindProgram(cgProgram);
    CGpass pass = cgGetFirstPass(cgTechnique);
    while (pass) {
        cgSetPassState(pass);

        //DrawModel(model, modelBBMin, modelBBMax);
        glutSolidSphere(0.5, 8, 8);
        cgResetPassState(pass);
        pass = cgGetNextPass(pass);
    }
    glutSwapBuffers();






[Edited by - axon on June 16, 2008 12:40:29 AM]

Share this post


Link to post
Share on other sites
Advertisement
Well like you may have gathered from my other thread you replied to, my understanding of Cg is not great, but theres something there I have never seen before: you don't pass the sampler2D into the pixel shader, you access it globally. I didn't know this was possible! Maybe you could try:

void psLight(float4 position : TEXCOORD0,
float2 uv : TEXCOORD1,
float3 normal : TEXCOORD2,
uniform sampler2D : colorSampler,
out float4 color : COLOR)
{
...
float3 diffuseLight = tex2D(colorSampler, uv) + max(dot(L, N), 0);
...
}

technique NewTechnique <
string Script = "Pass=p0;";
> {
pass p0 <
string Script = "Draw=geometry;";
> {
VertexProgram = compile vp40 main();
DepthTestEnable = true;
DepthMask = true;
CullFaceEnable = false;
BlendEnable = false;
DepthFunc = LEqual;
FragmentProgram = compile fp40 psLight(ColorSampler);
}
}

Share this post


Link to post
Share on other sites
Quote:
Original post by bluntman
Well like you may have gathered from my other thread you replied to, my understanding of Cg is not great, but theres something there I have never seen before: you don't pass the sampler2D into the pixel shader, you access it globally. I didn't know this was possible! Maybe you could try:
*** Source Snippet Removed ***

Thanks for the suggestion. Unfortunately what you've noticed is one of the differences between Cg and CgFX. In Cg you pass samplers and other variables to the shader methods as uniforms. In CgFX you can (should?/must?) declare them as globals. (I just had a look at a few FXComposer shaders and they're all handling the sampler in the same way as the above code.)

Any other suggestions?
Maybe my OpenGL code is wrong? (I worked from the latest OpenGL Bible (6th ed) but I guess I could still have missed something???

Share this post


Link to post
Share on other sites
Well I am using CgFx in my current project and I pass everything as uniforms from the globals. It makes more sense that way, what if your vertex and fragment shaders are in different source files? I don't think CgFx resolves like that. I have never used FxComposer, but all the CgFx examples I have seen, including those in the Cg 2.0 users manual show all variables passed as uniforms. e.g. page 123 from the CgUsersManual.pdf that comes with the Cg2.0 SDK.

Share this post


Link to post
Share on other sites
Just gave it a try using the following shader but I get the same result.

/*********************************************************************NVMH3****
$Revision$

Copyright NVIDIA Corporation 2007
TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, THIS SOFTWARE IS PROVIDED
*AS IS* AND NVIDIA AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES, EITHER EXPRESS
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL NVIDIA OR ITS SUPPLIERS
BE LIABLE FOR ANY SPECIAL, INCIDENTAL, INDIRECT, OR CONSEQUENTIAL DAMAGES
WHATSOEVER (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS,
BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR ANY OTHER PECUNIARY
LOSS) ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.


To learn more about shading, shaders, and to bounce ideas off other shader
authors and users, visit the NVIDIA Shader Library Forums at:

http://developer.nvidia.com/forums/

******************************************************************************/


// #define FLIP_TEXTURE_Y

float Script : STANDARDSGLOBAL <
string UIWidget = "none";
string ScriptClass = "object";
string ScriptOrder = "standard";
string ScriptOutput = "color";
string Script = "Technique=Main;";
> = 0.8;

//// UN-TWEAKABLES - AUTOMATICALLY-TRACKED TRANSFORMS ////////////////

float4x4 WorldITXf : WorldInverseTranspose < string UIWidget="None"; >;
float4x4 WvpXf : WorldViewProjection < string UIWidget="None"; >;
float4x4 WorldXf : World < string UIWidget="None"; >;
float4x4 ViewIXf : ViewInverse < string UIWidget="None"; >;

//// TWEAKABLE PARAMETERS ////////////////////

/// Point Lamp 0 ////////////
float3 Lamp0Pos : Position <
string Object = "PointLight0";
string UIName = "Lamp 0 Position";
string Space = "World";
> = {-0.5f,2.0f,1.25f};
float3 Lamp0Color : Specular <
string UIName = "Lamp 0";
string Object = "Pointlight0";
string UIWidget = "Color";
> = {1.0f,1.0f,1.0f};

// Ambient Light
float3 AmbiColor : Ambient <
string UIName = "Ambient Light";
string UIWidget = "Color";
> = {0.07f,0.07f,0.07f};

float Ks <
string UIWidget = "slider";
float UIMin = 0.0;
float UIMax = 1.0;
float UIStep = 0.05;
string UIName = "Specular";
> = 0.4;

float Eccentricity <
string UIWidget = "slider";
float UIMin = 0.0;
float UIMax = 1.0;
float UIStep = 0.0001;
string UIName = "Highlight Eccentricity";
> = 0.3;


//////// COLOR & TEXTURE /////////////////////

texture ColorTexture <
string ResourceName = "default_color.dds";
string UIName = "Diffuse Texture";
string ResourceType = "2D";
>;

sampler2D ColorSampler = sampler_state {
Texture = <ColorTexture>;
MinFilter = LinearMipMapLinear;
MagFilter = Linear;
WrapS = Repeat;
WrapT = Repeat;
};

//
// #define this macro to permit the import and use of shared shadow
// maps created by COLLADA-FX. Make sure that the macro is defined
// and the code recompile *before* executing "Convert to Collada-FX"!
//
// #define USE_SHARED_SHADOW

#ifdef USE_SHARED_SHADOW
#include "include/shadowMap.cgh"
DECLARE_SHADOW_XFORMS("SpotLight0",LampViewXf,LampProjXf,ShadowViewProjXf)
DECLARE_SHADOW_BIAS
DECLARE_SHADOW_MAPS(ColorShadTarget,ColorShadSampler,DepthShadTarget,DepthShadSampler)

float ShadDens <
string UIWidget = "slider";
float UIMin = 0.0;
float UIMax = 1.0;
float UIStep = 0.01;
string UIName = "Shadow Density";
> = 0.7;
#endif /* USE_SHARED_SHADOW */


//////// CONNECTOR DATA STRUCTURES ///////////

/* data from application vertex buffer */
struct appdata {
float3 Position : POSITION;
float4 UV : TEXCOORD0;
float4 Normal : NORMAL;
float4 Tangent : TANGENT0;
float4 Binormal : BINORMAL0;
};

/* data passed from vertex shader to pixel shader */
struct vertexOutput {
float4 HPosition : POSITION;
float2 UV : TEXCOORD0;
// The following values are passed in "World" coordinates since
// it tends to be the most flexible and easy for handling
// reflections, sky lighting, and other "global" effects.
float3 LightVec : TEXCOORD1;
float3 WorldNormal : TEXCOORD2;
float3 WorldTangent : TEXCOORD3;
float3 WorldBinormal : TEXCOORD4;
float3 WorldView : TEXCOORD5;
#ifdef USE_SHARED_SHADOW
// This optional value expresses the current location in "light"
// coordinates for use with shadow mapping.
float4 LProj : LPROJ_COORD;
#endif /* USE_SHARED_SHADOW */
};

///////// VERTEX SHADING /////////////////////

/*********** Generic Vertex Shader ******/

vertexOutput main(appdata IN) {
vertexOutput OUT = (vertexOutput)0;
OUT.WorldNormal = mul(WorldITXf,IN.Normal).xyz;
OUT.WorldTangent = mul(WorldITXf,IN.Tangent).xyz;
OUT.WorldBinormal = mul(WorldITXf,IN.Binormal).xyz;
float4 Po = float4(IN.Position.xyz,1);
float3 Pw = mul(WorldXf,Po).xyz;
OUT.LightVec = (Lamp0Pos - Pw);
#ifdef FLIP_TEXTURE_Y
OUT.UV = float2(IN.UV.x,(1.0-IN.UV.y));
#else /* !FLIP_TEXTURE_Y */
OUT.UV = IN.UV.xy;
#endif /* !FLIP_TEXTURE_Y */
#ifdef USE_SHARED_SHADOW
float4 Pl = mul(ShadowViewProjXf,Pw); // "P" in light coords
float4x4 BiasXf = make_bias_mat(ShadBias);
OUT.LProj = mul(BiasXf,Pl); // bias to make texcoord
#endif /* USE_SHARED_SHADOW */
OUT.WorldView = normalize(float3(ViewIXf[0].w,ViewIXf[1].w,ViewIXf[2].w) - Pw);
OUT.HPosition = mul(WvpXf,Po);
return OUT;
}

///////// PIXEL SHADING //////////////////////

// Utility function for blinn shading

void blinn_shading(vertexOutput IN,
float3 LightColor,
float3 Nn,
float3 Ln,
float3 Vn,
uniform sampler2D colorSampler,
out float3 DiffuseContrib,
out float3 SpecularContrib)
{
float3 Hn = normalize(Vn + Ln);
float hdn = dot(Hn,Nn);
float3 R = reflect(-Ln,Nn);
float rdv = dot(R,Vn);
rdv = max(rdv,0.001);
float ldn=dot(Ln,Nn);
ldn = max(ldn,0.0);
float ndv = dot(Nn,Vn);
float hdv = dot(Hn,Vn);
float eSq = Eccentricity*Eccentricity;
float distrib = eSq / (rdv * rdv * (eSq - 1.0) + 1.0);
distrib = distrib * distrib;
float Gb = 2.0 * hdn * ndv / hdv;
float Gc = 2.0 * hdn * ldn / hdv;
float Ga = min(1.0,min(Gb,Gc));
float fresnelHack = 1.0 - pow(ndv,5.0);
hdn = distrib * Ga * fresnelHack / ndv;
DiffuseContrib = ldn * LightColor;
SpecularContrib = hdn * Ks * LightColor;
}

float4 std_PS(vertexOutput IN,
uniform sampler2D colorSampler) : COLOR {
float3 diffContrib;
float3 specContrib;
float3 Ln = normalize(IN.LightVec);
float3 Vn = normalize(IN.WorldView);
float3 Nn = normalize(IN.WorldNormal);
blinn_shading(IN,Lamp0Color,Nn,Ln,Vn,colorSampler,diffContrib,specContrib);
float3 diffuseColor = tex2D(ColorSampler,IN.UV).rgb;
#ifdef USE_SHARED_SHADOW
float shadowed = tex2Dproj(DepthShadSampler,IN.LProj).x;
float faded = 1.0-(ShadDens*(1.0-shadowed));
diffContrib *= faded;
specContrib *= shadowed;
#endif /* USE_SHARED_SHADOW */
float3 result = specContrib+(diffuseColor*(diffContrib+AmbiColor));
// return as float4
return float4(result,1);
}

///// TECHNIQUES /////////////////////////////

technique Main <
string Script = "Pass=p0;";
> {
pass p0 <
string Script = "Draw=geometry;";
> {
VertexProgram = compile vp40 main();
DepthTestEnable = true;
DepthMask = true;
CullFaceEnable = false;
BlendEnable = false;
DepthFunc = LEqual;
FragmentProgram = compile fp40 std_PS(ColorSampler);
}
}

/////////////////////////////////////// eof //



I also tried setting the texParam explicitely per-frame (in Display()) but alas... no joy :(

Share this post


Link to post
Share on other sites
You say when you try and render with the texture the sphere appears as a black circle? Do you mean completely black, from all angles, i.e. no specular or ambient? If it was just the texture that was not being set correctly then I would still expect to see specular.
Are you sure glutSphere function generates UVs? Maybe you need to enable automatic texture coord generation?

Share this post


Link to post
Share on other sites
You're right, the specular term should be there but it isn't. The sphere is absolute black when viewed from every direction. Well spotted :)

So maybe it isn't the ColourSampler (or possibly even nothing to do with the texture)?

Looks like it could be the transforms after all.

Share this post


Link to post
Share on other sites
What happens when you keep everything the same but remove the texture diffuse component from the final calculation?
like:
float3 diffuse = Kd * lightColor;
instead of:
float3 diffuse = Kd * lightColor * diffuseLight;
The only way I can think of that the a problem with the texture could cause the final colour to always be completely black is if there is a NaN value getting in there somewhere, but afaik if the texture is not set then it will return zeros not NaNs.

Share this post


Link to post
Share on other sites
Hi Axon, I saw your post on the NVIDIA forums.

I'm having similar issues, with probably the world's simplest CgFX shader. At first I thought it was a problem using Cg with Qt, but turns out it isn't.

Try using this as a test:


float4 Diffuse : COLOR
<
string UIWidget = "Color";
string UIName = "Diffuse";
> = {0.8f, 0.8f, 0.8f, 1.0f};

sampler2D DiffuseSampler = sampler_state
{
MinFilter = LinearMipMapLinear;
};

float4x4 WorldViewProj : WORLDVIEWPROJECTION;

struct VInput
{
float4 Position : POSITION;
float4 Colour : COLOR0;
float2 UVCoord : TEXCOORD0;
};

struct VOutput
{
float4 Position : POSITION;
float4 Colour : COLOR0;
float2 UVCoord : TEXCOORD0;
};


VOutput VShader(VInput IN)
{
//Create output object
VOutput OUT;

//Calculate output position
OUT.Position = mul(WorldViewProj, IN.Position);

OUT.UVCoord = IN.UVCoord;
OUT.Colour = Diffuse;

return OUT;
}

float4 PShader(VOutput IN) : COLOR
{
return tex2D(DiffuseSampler, IN.UVCoord);;
}

technique Main
{
pass p0
{
VertexProgram = compile vp40 VShader();

DepthTestEnable = true;
DepthMask = true;
CullFaceEnable = true;
BlendEnable = false;
DepthFunc = LEqual;

FragmentProgram = compile fp40 PShader();
}
}



Let's see if we can sort this out, there must be a common problem with our code, see mine here for reference: http://www.gamedev.net/community/forums/topic.asp?topic_id=497585

Share this post


Link to post
Share on other sites
Hi deadstar.

Just read your thread. Maaaaan. I feel your pain!
We're in the same boat... let's make it float.

I've had this problem for, I hate to admit it, possibly over 2 months.

I'm supposed to be good at this gear. I am good at this gear. I've read a lot of documentation including the thin PDF examples (which do all kinds of non-conventional stuff w.r.t geometry and textures) combed the PDFs. The SDK samples (both Cg/Cg-toolkit SDK and NV OpenGL SDK) and re-created some of their demos.

So where are we up to?:
1. Transforms and verts are ok. (Since your shader in the prev post uses only WorldViewProj and renders black geometry ok).
2. Shader is ok. Works in FXComposer (after you add the Texture param so that FXComposer can hook in (that could be a clue... but none of the demos seem to need to touch the Texture param)).
3. UVs are ok. (Since tex2D(sampler, float2(0.5,0.5)) returns green (center pix color of texture) in FXComposer but returns black in my app.)

I'm tempted to grab their CgFX bumdemo and start to piecewise convert it.
Step 1: Convert it to use own geom and test
Step 2: Convert it to use own tex and test
Step 3: Convert it to use own CgFX and test

Theoretically that can't go wrong... it's be great to think the solution is closer than that (since i've already done similar approaches before).

Or is it something curlier like we're linking against beta dlls at run-time???

I'll def keep this post updated... glad not to be alone on this one. I have tried for so long to figure it out methodically (as you obviously have) and yet here we are.

EDIT:
@bluntman: I rekon deadstar's shader distills the problem down. tex2D(sampler, uv) returns zeros (black) when it should look up the texture. I've also tested tex2D(sampler, float2(0.5,0.5)) and in FXComposer it returns green (from center of texture) and in my app it returns black.

[Edited by - axon on June 17, 2008 10:22:35 PM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement