2d Vertex Shader with Cg

Discussion about everything. New games, 3d math, development tips...
Post Reply
jacky
Posts: 13
Joined: Wed Oct 04, 2006 7:08 pm

2d Vertex Shader with Cg

Post by jacky »

Hello Everyone

Im hope my question is right here, cause it's not really related to Irrlicht ;)
I want to write a simple OpenGL test application with Nvidia's Cg-Shader.
But the point is: i want to use 2D Graphics, so i set up OpenGL to act like a 2d-engine using:

Code: Select all

glOrtho(0, 1000, 0, 1000, 0, 1);
I already successfully tested Pixel/Fragment - Shaders, but now i want to use a Vertex-Shader (for 2d of course!):

Code: Select all

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

VS_OUTPUT main(float2 position : POSITION)
{	
  VS_OUTPUT OUT;

  OUT.position = float4(position, 0, 1);
  // if i put this one:
  // OUT.position = float4(position, 0, 1000);
  // than i can see my quad kind of far away from screen (smaller than the orginal one)
  OUT.color = float3(0,1,0);

  return OUT;	
}
My Problem is like i already commented in the code, that if i use

Code: Select all

OUT.position = float4(position, 0, 1);
than my Quad is not visible, but if i use

Code: Select all

OUT.position = float4(position, 0, 1000);
than im seeing it a lot smaller in the right upper part of the screen.
(it sould be in full size in the middle of the screen!)

Here is my OpenGL Code:

Code: Select all

// main.cpp

#include <string>
#include <iostream>

#include <GL/glut.h>
#include <GL/glaux.h>

#pragma comment(lib, "glaux.lib")

#include <Cg/cg.h>
#include <Cg/cgGL.h>

#pragma comment(lib, "cg.lib")
#pragma comment(lib, "cgGL.lib")

static CGcontext   myCgContext;
static CGprofile   myCgVertexProfile;
static CGprogram   myCgVertexProgram;

static GLuint texture;


void checkForCgError(const char *situation);
void display(void);
void keyboard(unsigned char c, int x, int y);
const GLuint loadTexture(const wchar_t* filename);

int main(int argc, char* argv[])
{
	// init window using glut
	glutInitWindowSize(400, 400);
	glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
	glutInit(&argc, argv);

	glutCreateWindow("blub");
	glutDisplayFunc(display);
	glutKeyboardFunc(keyboard);

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

	glEnable(GL_TEXTURE_2D);

	glDisable(GL_DEPTH_TEST);
	glDisable(GL_CULL_FACE);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(0, 1000, 0, 1000, 0, 1);

	// load shader using nvidia cg
	myCgContext = cgCreateContext();
	checkForCgError("creating context");

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

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

	glutMainLoop();
	return 0;
}

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

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

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

	glColor3f(0.0f, 1.0f, 0.0f);

	glBegin(GL_QUADS);

	glVertex2i(250, 750);

	glVertex2i(250, 250);

	glVertex2i(750, 250);

	glVertex2i(750, 750);

	glEnd();

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

	glutSwapBuffers();
}

void keyboard(unsigned char c, int x, int y)
{
	switch (c)
	{
	case 27:
		cgDestroyProgram(myCgVertexProgram);
		cgDestroyContext(myCgContext);
		exit(0);
		break;
	}
}

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

	if (error != CG_NO_ERROR)
	{
		printf("%s: %s: %s\n",
			"blub", situation, string);
		if (error == CG_COMPILER_ERROR) {
			printf("%s\n", cgGetLastListing(myCgContext));
		}
		exit(1);
	}
}
I want the Shader to NOT modifiy the object's Z Axis.
I want it to only modify X and Y Axis!

Is something like that possible? o.Ó
I hope so, so if someone knows a solution please tell me!

Thats what i get without any Shader:
Image

Here is what i get with a value of 1:
Image

And here is what i get with a value of 1000:
Image
Post Reply