Jump to content
  • Advertisement
Sign in to follow this  
Gumgo

OpenGL Garbled textures with Cg

This topic is 3119 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 been having this issue for a while. At first I thought it was my graphics card, but it's also happening on my new computer which has a Radeon HD 5830 graphics card. What's happening is this. When I try to draw something with a Cg shader, the texture coordinates flicker and change randomly on the object. I've never seen this happen in any Cg apps except for my own, so I'm probably doing something wrong. Here is a video of what is happening. It happens with this simple program:
#include <windows.h>
#include <string>
#include <gl\GLee.h>
#include <gl\gl.h>
#include <gl\glu.h>
#include <cg\cg.h>
#include <cg\cggl.h>

#pragma comment( lib, "GLee.lib" ) // library for GL easy extensions
#pragma comment( lib, "opengl32.lib" ) // library for OpenGL
#pragma comment( lib, "glu32.lib" ) // library for OpenGL Utility Library
#pragma comment( lib, "cg.lib" ) // library for Cg functions
#pragma comment( lib, "cggl.lib" ) // library for Cg with OpenGL functions
#pragma comment( lib, "winmm.lib" ) // library for timing functions

class Window {
	HGLRC hRC;
	HDC hDC;
	HWND hWnd;
	HINSTANCE hInstance;

	bool quit;

	int width;
	int height;
	int numbofbits;
	std::string title;

public:
	friend LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );

	Window() {
		hRC = NULL;
		hDC = NULL;
		hWnd = NULL;
		quit = false;
		width = 0;
		height = 0;
		numbofbits = 0;
		title = "";
	}

	~Window() {
		killWindow();
	}

	bool makeWindow( const std::string & text, int w, int h, int bits ) {
		UINT PixelFormat;
		WNDCLASS wc;
		DWORD dwExStyle, dwStyle;

		width = w;
		height = h;
		numbofbits = bits;
		title = text;

		RECT WindowRect;
		WindowRect.left = (long)0;
		WindowRect.right = (long)w;
		WindowRect.top = (long)0;
		WindowRect.bottom = (long)h;

		hInstance = GetModuleHandle( NULL );
		wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
		wc.lpfnWndProc = (WNDPROC)WndProc;
		wc.cbClsExtra = 0;
		wc.cbWndExtra = 0;
		wc.hInstance = hInstance;
		wc.hIcon = LoadIcon( NULL, IDI_WINLOGO );
		wc.hCursor = LoadCursor( NULL, IDC_ARROW );
		wc.hbrBackground = NULL;
		wc.lpszMenuName = NULL;
		wc.lpszClassName = "OpenGL";

		if (!RegisterClass( &wc ))
			return false;

		dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
		dwStyle = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;

		AdjustWindowRectEx( &WindowRect, dwStyle, false, dwExStyle );

		if (!(hWnd = CreateWindowEx( dwExStyle,
									 "OpenGL",
									 title.c_str(),
									 WS_CLIPSIBLINGS | WS_CLIPCHILDREN | dwStyle,
									 0, // x
									 0, // y
									 WindowRect.right - WindowRect.left,
									 WindowRect.bottom - WindowRect.top,
									 NULL,
									 NULL,
									 hInstance,
									 NULL ))) {
			killWindow();
			return false;
		}

		RECT WinPos;
		ULONG NewX, NewY, NewW, NewH, ScreenWidth, ScreenHeight;
		GetWindowRect( hWnd, &WinPos );
		ScreenWidth = GetSystemMetrics( SM_CXSCREEN );
		ScreenHeight = GetSystemMetrics( SM_CYSCREEN );
		NewX = ScreenWidth/2 - (WinPos.right - WinPos.left)/2;
		NewY = ScreenHeight/2 - (WinPos.bottom - WinPos.top)/2;
		NewW = WinPos.right - WinPos.left;
		NewH = WinPos.bottom - WinPos.top;
		MoveWindow( hWnd, NewX, NewY, NewW, NewH, true );

		static PIXELFORMATDESCRIPTOR pfd = {
			sizeof( PIXELFORMATDESCRIPTOR ),
			1,
			PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
			PFD_TYPE_RGBA,
			bits,
			0, 0, 0, 0, 0, 0,
			0,
			0,
			0,
			0, 0, 0, 0,
			16,
			0,
			0,
			PFD_MAIN_PLANE,
			0,
			0, 0, 0
		};

		if (!(hDC = GetDC( hWnd ))) {
			killWindow();
			return false;
		}

		if (!(PixelFormat = ChoosePixelFormat( hDC, &pfd ))) {
			killWindow();
			return false;
		}

		if (!SetPixelFormat( hDC, PixelFormat, &pfd )) {
			killWindow();
			return false;
		}

		if (!(hRC = wglCreateContext( hDC ))) {
			killWindow();
			return false;
		}

		if (!wglMakeCurrent( hDC, hRC )) {
			killWindow();
			return false;
		}

		SetWindowLongPtr( hWnd, GWLP_USERDATA, (long)this );
		ShowWindow( hWnd, SW_SHOW );
		SetForegroundWindow( hWnd );
		SetFocus( hWnd );

		glViewport( 0, 0, w, h );
		return true;
	}

	void killWindow() {
		if (hRC) {
			wglMakeCurrent( NULL, NULL );
			wglDeleteContext( hRC );
			hRC = NULL;
		}

		DestroyWindow( hWnd );
		hWnd = NULL;

		UnregisterClass( "OpenGL", hInstance );
		hInstance = NULL;
	}

	bool getQuit() {
		return quit;
	}

	void update() {
		MSG msg;
		while (PeekMessage( &msg, NULL, 0, 0, PM_REMOVE )) {
			if (msg.message == WM_QUIT)
				quit = true;
			else {
				TranslateMessage( &msg );
				DispatchMessage( &msg );
			}
		}
	}

	void updateDisplay() {
		SwapBuffers( hDC );
	}
};

LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) {
	Window * win = (Window*)GetWindowLongPtr( hWnd, GWLP_USERDATA );
	switch ( uMsg ) {
	case WM_SYSCOMMAND:
		switch (wParam) {
		case SC_SCREENSAVE:
		case SC_MONITORPOWER:
			return 0;
		}
		break;
	case WM_CLOSE:
		PostQuitMessage( 0 );
		return 0;
	case WM_SIZE:
		return 0;
	}
	return DefWindowProc( hWnd, uMsg, wParam, lParam );
}

int main( int argc, char ** argv ) {
	Window * win = new Window();
	win->makeWindow( "test", 640, 480, 16 );

	CGcontext cgContext;
	CGprofile cgVertexProfile;
	CGprogram program;
	CGparameter position;
	CGparameter texCoord;
	CGparameter color;
	CGparameter modelViewMatrix;

	cgContext = cgCreateContext(); // create a cg context
	cgVertexProfile = cgGLGetLatestProfile( CG_GL_VERTEX ); // get the vertex profile
	cgGLSetOptimalOptions( cgVertexProfile );

	program = cgCreateProgramFromFile( cgContext, CG_SOURCE, "./Mesh.cg", cgVertexProfile, "main", NULL );
	cgGLLoadProgram( program );
	position        = cgGetNamedParameter( program, "vtxIn.position" );
	texCoord        = cgGetNamedParameter( program, "vtxIn.texCoord" );
	color           = cgGetNamedParameter( program, "vtxIn.color" );
	modelViewMatrix = cgGetNamedParameter( program, "modelViewMatrix" );
	cgGLBindProgram( program );

	int tex[32][32]; // generate a simple gradient-like texture
	for (int x = 0; x < 32; ++x) {
		for (int y = 0; y < 32; ++y)
			tex[x][y] = ((x*8) << 16) + ((y*8) << 8);
	}

	UINT texID;
	glGenTextures( 1, &texID );
	glBindTexture( GL_TEXTURE_2D, texID );

	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
	glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 32, 32, 0, GL_RGBA, GL_UNSIGNED_BYTE, tex );

	while (!win->getQuit()) {
		win->update();

		glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
		glMatrixMode( GL_PROJECTION );
		glLoadIdentity();
		gluPerspective( 45, 4.0/3.0, 1, 300 );
		glMatrixMode( GL_MODELVIEW );
		glLoadIdentity();

		gluLookAt( 40, 40, 40, 0, 0, 0, 0, 0, 1 );

		//cgGLEnableProfile( cgVertexProfile );
		cgGLBindProgram( program );
		glEnable( GL_TEXTURE_2D );
		glBindTexture( GL_TEXTURE_2D, texID );
		glColor4f( 1, 1, 1, 1 );

		glPushMatrix();
		cgGLSetStateMatrixParameter( modelViewMatrix, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY );
		glPopMatrix();

		glBegin( GL_QUADS );
		glTexCoord2f( 0,0 );
		glVertex3f( -10,10,0 );
		glTexCoord2f( 1,0 );
		glVertex3f( 10,10,0 );
		glTexCoord2f( 1,1 );
		glVertex3f( 10,-10,0 );
		glTexCoord2f( 0,1 );
		glVertex3f( -10,-10,0 );
		glEnd();

		win->updateDisplay();
		Sleep( 20 );
	}

	delete win;
	return 0;
}

And the shader:
struct input { // vertex model
	float3 position	: POSITION;
	float3 normal	: NORMAL;
	float2 texCoord	: TEXCOORD0;
	float4 color	: COLOR0;
};

struct output {
	float4 position	: POSITION;
	float2 texCoord	: TEXCOORD0;
	float4 color	: COLOR0;
};

void main( input vtxIn,							// vertex in
		   out output vtxOut,					// vertex out
		   uniform float4x4 modelViewMatrix) {	// modelview matrix
	vtxOut.position = mul( modelViewMatrix, float4( vtxIn.position, 1.0f ) );
	vtxOut.texCoord = vtxIn.texCoord;
	vtxOut.color = vtxIn.color;
}



I'm using the Cg libs/headers from the latest Cg toolkit download. I've been trying to solve this for WEEKS! Am I using the wrong version of Cg? Forgetting to initialize something??? Any ideas help! [Edited by - Gumgo on March 6, 2010 3:36:22 AM]

Share this post


Link to post
Share on other sites
Advertisement
Just curious... what are you trying to accomplish by drawing the same quad 30 times?


for (UINT i = 0; i < 30; ++i) {
glTexCoord2f( 0,0 );
glVertex3f( -10,10,0 );
glTexCoord2f( 1,0 );
glVertex3f( 10,10,0 );
glTexCoord2f( 1,1 );
glVertex3f( 10,-10,0 );
glTexCoord2f( 0,1 );
glVertex3f( -10,-10,0 );
}



Kind of looks like a z fighting thing, maybe your quads are fighting over each other.

Can you post an image of what you expect your shape to look like? I'm not totally clear from your function, and it might give more clue as to what's happening.

Share this post


Link to post
Share on other sites
Oh whoops, that loop shouldn't be there. But anyway, the same thing occurs without the loop. Here is a picture of how it should look. Also, it seems to "freeze" on a frame until something changes on the screen (eg. I drag a window, or hover over the close button), and then starts flickering. That's why I was moving the mouse around to make the glitch occur.

Could someone run that code and see if the same thing happens? Or post the bare minimum code for getting texture coordinates working in Cg, and maybe I'll see something different between that and what I've written. Thanks.

[Edited by - Gumgo on March 6, 2010 12:33:04 PM]

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!