Jump to content
  • Advertisement
Sign in to follow this  
Hobart21

Beginning CG Question

This topic is 3087 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

I've installed the CG developer's toolkit and all of the example code compiles and runs well. However, I'm attempting to understand how one of the pixel shades works, and I'm not getting the results I would expect. Specifically, the 02_vertex_and_fragment_program has this fragment shader: struct C2E2f_Output { float4 color : COLOR; }; C2E2f_Output C2E2f_passthru(float4 color : COLOR) { C2E2f_Output OUT; OUT.color = color; return OUT; } but if I alter the OUT.color line to: OUT.color = (1.0f, 0.0f, 0.0f, 0.9f); the resulting fragments are not red as I would expect, but grayscaled using the alpha-value (0 is black, 1 white). It is odd because the original code produces color fragments, not black and white. Am I missing something conceptually? Thanks in advance!

Share this post


Link to post
Share on other sites
Advertisement
Here is all of the code from the example, btw.


02_vertex_and_fragment_program.c


/* 02_vertex_and_fragment_program.c - OpenGL-based example using a Cg
vertex and a Cg fragment programs from Chapter 2 of "The Cg Tutorial"
(Addison-Wesley, ISBN 0321194969). */


/* Requires the OpenGL Utility Toolkit (GLUT) and Cg runtime (version
1.0 or higher). */


#include <stdio.h> /* for printf and NULL */
#include <stdlib.h> /* for exit */
#include <math.h> /* for sin and cos */
#if __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include <Cg/cg.h> /* Can't include this? Is Cg Toolkit installed! */
#include <Cg/cgGL.h>

static CGcontext myCgContext;
static CGprofile myCgVertexProfile,
myCgFragmentProfile;
static CGprogram myCgVertexProgram,
myCgFragmentProgram;

static const char *myProgramName = "02_vertex_and_fragment_program",
*myVertexProgramFileName = "C2E1v_green.cg",
/* Page 38 */ *myVertexProgramName = "C2E1v_green",
*myFragmentProgramFileName = "C2E2f_passthru.cg",
/* Page 53 */ *myFragmentProgramName = "C2E2f_passthru";

static void checkForCgError(const char *situation)
{
CGerror error;
const char *string = cgGetLastErrorString(&error);

if (error != CG_NO_ERROR) {
printf("%s: %s: %s\n",
myProgramName, situation, string);
if (error == CG_COMPILER_ERROR) {
printf("%s\n", cgGetLastListing(myCgContext));
}
exit(1);
}
}

/* Forward declared GLUT callbacks registered by main. */
static void display(void);
static void keyboard(unsigned char c, int x, int y);

int main(int argc, char **argv)
{
glutInitWindowSize(400, 400);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInit(&argc, argv);

glutCreateWindow(myProgramName);
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);

glClearColor(0.1, 0.3, 0.6, 0.0); /* Blue background */

myCgContext = cgCreateContext();
checkForCgError("creating context");
cgGLSetDebugMode(CG_FALSE);
cgSetParameterSettingMode(myCgContext, CG_DEFERRED_PARAMETER_SETTING);

myCgVertexProfile = cgGLGetLatestProfile(CG_GL_VERTEX);
cgGLSetOptimalOptions(myCgVertexProfile);
checkForCgError("selecting vertex profile");

myCgVertexProgram =
cgCreateProgramFromFile(
myCgContext, /* Cg runtime context */
CG_SOURCE, /* Program in human-readable form */
myVertexProgramFileName, /* Name of file containing program */
myCgVertexProfile, /* Profile: OpenGL ARB vertex program */
myVertexProgramName, /* Entry function name */
NULL); /* No extra compiler options */
checkForCgError("creating vertex program from file");
cgGLLoadProgram(myCgVertexProgram);
checkForCgError("loading vertex program");

myCgFragmentProfile = cgGLGetLatestProfile(CG_GL_FRAGMENT);
cgGLSetOptimalOptions(myCgFragmentProfile);
checkForCgError("selecting fragment profile");

myCgFragmentProgram =
cgCreateProgramFromFile(
myCgContext, /* Cg runtime context */
CG_SOURCE, /* Program in human-readable form */
myFragmentProgramFileName, /* Name of file containing program */
myCgFragmentProfile, /* Profile: OpenGL ARB vertex program */
myFragmentProgramName, /* Entry function name */
NULL); /* No extra compiler options */
checkForCgError("creating fragment program from file");
cgGLLoadProgram(myCgFragmentProgram);
checkForCgError("loading fragment program");

glutMainLoop();
return 0;
}

static void drawStar(float x, float y,
int starPoints, float R, float r)
{
int i;
double piOverStarPoints = 3.14159 / starPoints,
angle = 0.0;

glBegin(GL_TRIANGLE_FAN);
glVertex2f(x, y); /* Center of star */
/* Emit exterior vertices for star's points. */
for (i=0; i<starPoints; i++) {
glVertex2f(x + R*cos(angle), y + R*sin(angle));
angle += piOverStarPoints;
glVertex2f(x + r*cos(angle), y + r*sin(angle));
angle += piOverStarPoints;
}
/* End by repeating first exterior vertex of star. */
angle = 0;
glVertex2f(x + R*cos(angle), y + R*sin(angle));
glEnd();
}

static void drawStars(void)
{
/* star outer inner */
/* x y Points radius radius */
/* ===== ===== ====== ====== ====== */
drawStar(-0.1, 0, 5, 0.5, 0.2);
drawStar(-0.84, 0.1, 5, 0.3, 0.12);
drawStar( 0.92, -0.5, 5, 0.25, 0.11);
drawStar( 0.3, 0.97, 5, 0.3, 0.1);
drawStar( 0.94, 0.3, 5, 0.5, 0.2);
drawStar(-0.97, -0.8, 5, 0.6, 0.2);
}

static void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

cgGLBindProgram(myCgVertexProgram);
checkForCgError("binding vertex program");

cgGLEnableProfile(myCgVertexProfile);
checkForCgError("enabling vertex profile");

cgGLBindProgram(myCgFragmentProgram);
checkForCgError("binding fragment program");

cgGLEnableProfile(myCgFragmentProfile);
checkForCgError("enabling fragment profile");

drawStars();

cgGLDisableProfile(myCgVertexProfile);
checkForCgError("disabling vertex profile");

cgGLDisableProfile(myCgFragmentProfile);
checkForCgError("disabling fragment profile");

glutSwapBuffers();
}

static void keyboard(unsigned char c, int x, int y)
{
switch (c) {
case 27: /* Esc key */
/* Demonstrate proper deallocation of Cg runtime data structures.
Not strictly necessary if we are simply going to exit. */

cgDestroyProgram(myCgVertexProgram);
cgDestroyProgram(myCgFragmentProgram);
cgDestroyContext(myCgContext);
exit(0);
break;
}
}



C2E1v_green.cg

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

struct C2E1v_Output {
float4 position : POSITION;
float3 color : COLOR;
};

C2E1v_Output C2E1v_green(float2 position : POSITION)
{
C2E1v_Output OUT;

OUT.position = float4(position,0,1);
OUT.color = float3(0,1,0);

return OUT;
}



C2E2f_passthru.cg

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

struct C2E2f_Output {
float4 color : COLOR;
};

C2E2f_Output C2E2f_passthru(float4 color : COLOR)
{
C2E2f_Output OUT;
OUT.color = color;//changing to (1,0,0,1) does not make red fragments??
return OUT;
}



Thanks!

Share this post


Link to post
Share on other sites
Found it!

http://developer.nvidia.com/forums/index.php?showtopic=742&hl=passthru


OUT.color = float4(1.0f, 1.0f, 0.0f, 0.0f);//correctly makes yellow

Seems so obvious now.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!