[OPENGL-CG] Fehler beim Erstellen eines CGprofiles ??

Für Fragen zu Grafik APIs wie DirectX und OpenGL sowie Shaderprogrammierung.
Antworten
Tactive
Beiträge: 61
Registriert: 21.07.2004, 15:10
Kontaktdaten:

[OPENGL-CG] Fehler beim Erstellen eines CGprofiles ??

Beitrag von Tactive »

Hallo ,

habe meinen OpenGL Test-Quellcode ein wenig weiter ausgebaut und wollte nun die ersten rudimentären Shader einbauen. Dafür habe ich mir das neueste CG Toolkit von NVIDIA heruntergeladen und installiert. Die Erstellung des CG Contextes (cgCreateContext) klappt auch noch, aber beim Erstellen der Profile für die Fragment und Vertex Shader erhalte ich die Fehlermeldung das das entsprechende Profil nicht unterstützt wird. Dabei halte ich mich an die Dokumentation von NVIDIA und die Iniaitialisierung des Profile führe ich folgendermaßen durch:

Code: Alles auswählen

CGprofile cgVertexProfile = cgGLGetLatestProfile(CG_GL_VERTEX);
cgGLSetOptimalOptions(cgVertexProfile);
cgCheckForError(cgContext, "Create profile for vertex shader");

//= Ergebnis: CG ERROR: "The profile is not supported"
Es ist auch egal ob ich ein Profil für einen Vertex- oder einen Fragment-Shader erzeugen will. In beiden Fällen wird die Variable mit CG_UNKNOWN_PROFILE gefüllt?

Hier noch der gesamte Quellcode meines OpenGL Testprogramms, falls es eventuelle woanders spukt und jemand eine Erklärung darin finden kann:

Code: Alles auswählen

#include <stdio.h>
#include <Windows.h>
#include <SDL.h>

#define GL3_PROTOTYPES 1
#include <GL/glew.h>
#include <GL/wglew.h>
#include <Cg/cg.h>
#include <Cg/cgGL.h>
#include <boost/format.hpp>
#include <sstream>

#define PROGRAM_NAME "OpenGL32Test"

void SDLDie(const char* message)
{
	::MessageBox(NULL, SDL_GetError(), message, MB_OK);
	SDL_Quit();
	exit(1);
}

void cgCheckForError(CGcontext context, const char* situation)
{
	CGerror error;
	const char* str = cgGetLastErrorString(&error);
	
	if (CG_NO_ERROR != error)
	{
		std::ostringstream oss;
		oss << boost::format("%1%: %2%: %3%") % PROGRAM_NAME % situation % str << std::endl << std::endl;

		if (CG_COMPILER_ERROR == error && context)
		{
			oss << cgGetLastListing(context) << std::endl;
		}
		MessageBox(NULL, oss.str().c_str(), situation, MB_OK);
		SDL_Quit();
		exit(1);
	}
}

void ShowGLColor(SDL_WindowID window, GLclampf r, GLclampf g, GLclampf b, GLclampf a)
{
    glClearColor(r, g, b, a);
    glClear(GL_COLOR_BUFFER_BIT);
    /* Swap our back buffer to the front */
    SDL_GL_SwapWindow(window);
    /* Wait 2 seconds */
    SDL_Delay(2000);
}

// Erstellt die VBOs (Vertex Buffer Objects) und füllt Sie mit Daten.
GLuint* SetData()
{
	float* verts = new float[9];
	float* cols  = new float[9];

	verts[0] = 0.0f; verts[1] = 0.8f; verts[2] =-1.0f;
	verts[3] =-0.8f; verts[4] =-0.8f; verts[5] =-1.0f;
	verts[6] = 0.8f; verts[7] =-0.8f; verts[8]= -1.0f;
 
	cols[0] = 1.0f; cols[1] = 0.0f; cols[2] = 0.0f;
	cols[3] = 0.0f; cols[4] = 1.0f; cols[5] = 0.0f;
	cols[6] = 0.0f; cols[7] = 0.0f; cols[8] = 1.0f;

	GLuint* vboId = new GLuint[2];
	glGenBuffers(2, &vboId[0]);

	glBindBuffer(GL_ARRAY_BUFFER, vboId[0]);
	glBufferData(GL_ARRAY_BUFFER, (9 * sizeof(GLfloat)), verts, GL_STATIC_DRAW);
	glVertexAttribPointer((GLuint)0, 3, GL_FLOAT, GL_FALSE, 0, 0); 
	glEnableVertexAttribArray(0);

	glBindBuffer(GL_ARRAY_BUFFER, vboId[1]);
	glBufferData(GL_ARRAY_BUFFER, (9 * sizeof(GLfloat)), cols, GL_STATIC_DRAW);
	glVertexAttribPointer((GLuint)1, 3, GL_FLOAT, GL_FALSE, 0, 0);
	glEnableVertexAttribArray(1);

	delete [] verts;
	delete [] cols;

	return vboId;
}

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	SDL_WindowID mainWindow;
	SDL_GLContext glContext;

	// Initialize video system of the SDL library
	if (SDL_Init(SDL_INIT_VIDEO) < 0)
	{
		SDLDie("Unable to initialize SDL");
	}

	// Request an OpenGL 3.2 context.
	SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
	SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);

	// Turn ob double buffering with a 24bit Z buffer.
	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
	SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);

	mainWindow = SDL_CreateWindow(PROGRAM_NAME, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 512, 512, (SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN));
	if (!mainWindow)
	{
		SDLDie("Unable to create window");
	}

	// Create the OpenGL Context an attach it to the window
	glContext = SDL_GL_CreateContext(mainWindow);

	// Initialize GLEW
	if (GLEW_OK != glewInit())
	{
		SDLDie("Unable to initialize glew");
	}

	// Create the vertex data
	GLuint* vboIds = SetData();

	// Create CG context
	CGcontext cgContext = cgCreateContext();
	cgCheckForError(cgContext, "Creating CG context");

	// We need to create CG profiles for each shader-type we'll use
	CGprofile cgVertexProfile = cgGLGetLatestProfile(CG_GL_VERTEX);
	cgGLSetOptimalOptions(cgVertexProfile);
	cgCheckForError(cgContext, "Create profile for vertex shader");
		
	CGprofile cgFragmentProfile = cgGLGetLatestProfile(CG_GL_FRAGMENT);
	cgGLSetOptimalOptions(cgFragmentProfile);
	cgCheckForError(cgContext, "Create profile for fragment shader");

	CGprogram cgVertexProgram = NULL;
	CGprogram cgFragmentProgram = NULL;

	// Synchronize the buffer swap 
	SDL_GL_SetSwapInterval(1);

	// Löschen des Backbuffers
	glClearColor(0, 0, 0, 0);
	glClear(GL_COLOR_BUFFER_BIT);

	// Zeichnen der Vertex-Daten als Dreiecke
	glDrawArrays(GL_TRIANGLES, 0, 3);
	glFlush();
	
	// Backbuffer zur Ansicht bringen
	SDL_GL_SwapWindow(mainWindow);

	// 2 Sekunden warten
	SDL_Delay(2000);

	// VBOs freigeben ( Ordnung muss sein! )
	glDeleteBuffers(2, vboIds);
	delete [] vboIds;

	// Delete the OpenGL context, destroy the window and shutdown SDL.
	SDL_GL_DeleteContext(glContext);
	SDL_DestroyWindow(mainWindow);
	SDL_Quit();

	return 0;
}
Hinweise zur Identifizierung oder Überführung des Fehlers bitte nicht an der nächsten Polizeistation abgeben sondern einfach hier reinschreiben.
Vielen Dank im Voraus.
Tactive
Beiträge: 61
Registriert: 21.07.2004, 15:10
Kontaktdaten:

Re: [OPENGL-CG] Fehler beim Erstellen eines CGprofiles ??

Beitrag von Tactive »

Hmm, es liegt wohl irgendwie an der Erstellung des OpenGL - Fensters durch SDL, denn wenn ich es per GLUT probiere dann klappt die Erstellung der Profile.
Daher habe ich mir jetzt folgende Methode gebastelt, welche mir die Profile erstellt:

Code: Alles auswählen

CGprofile* cgGetOptimalProfiles(CGcontext context, size_t count, CGGLenum* types)
{
	if (count > 0 && types)
	{
		glutInitWindowSize(100, 100);
		glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
		glutCreateWindow("DummyWindow");
		
		CGprofile* profiles = new CGprofile[count];

		for (size_t i = 0; i < count; i++)
		{
			profiles[i] = cgGLGetLatestProfile(types[i]);
			cgGLSetOptimalOptions(profiles[i]);
			cgCheckForError(context, "Creating shader profile");
		}

		return profiles;
	}
	return NULL;
}
In der Main-Methode kann ich nun die Profile folgendermaßen erstellen lassen:

Code: Alles auswählen

CGGLenum* profTypes = new CGGLenum[2];
profTypes[0] = CG_GL_VERTEX;
profTypes[1] = CG_GL_FRAGMENT;

CGprofile* profiles = cgGetOptimalProfiles(cgContext, 2, profTypes);
CGprofile cgVertexProfile   = profiles[0];
CGprofile cgFragmentProfile = profiles[1];

delete [] profTypes;
delete [] profiles;
Das ist zwar ein wenig getrickst, aber es funktioniert - erstmal. Sollte jeman eine bessere Idee haben, ich bin für jeden Hinweis
zu haben.
Benutzeravatar
kimmi
Moderator
Beiträge: 1405
Registriert: 26.02.2009, 09:42
Echter Name: Kim Kulling
Wohnort: Luebeck
Kontaktdaten:

Re: [OPENGL-CG] Fehler beim Erstellen eines CGprofiles ??

Beitrag von kimmi »

Bist du bei der SDL auf der Latest / Greatest Version?

Gruß Kimmi
Benutzeravatar
Aramis
Moderator
Beiträge: 1458
Registriert: 25.02.2009, 19:50
Echter Name: Alexander Gessler
Wohnort: 2016
Kontaktdaten:

Re: [OPENGL-CG] Fehler beim Erstellen eines CGprofiles ??

Beitrag von Aramis »

Das ist zwar ein wenig getrickst
Mach dir keine Sorgen, ohne eine gehoerige Bereitschaft zum Tricksen kommst du mit CG sowieso nicht weit :-)
Tactive
Beiträge: 61
Registriert: 21.07.2004, 15:10
Kontaktdaten:

Re: [OPENGL-CG] Fehler beim Erstellen eines CGprofiles ??

Beitrag von Tactive »

kimmi hat geschrieben:Bist du bei der SDL auf der Latest / Greatest Version?

Gruß Kimmi
Ja, habe mir die aktuellste Version 1.3.xx installiert und verwende diese. Ist aber keine Version aus dem Mercurial-Bereich sondern eine Zusammenstellung der aktuellen Quellen.
Aramis hat geschrieben:Mach dir keine Sorgen, ohne eine gehoerige Bereitschaft zum Tricksen kommst du mit CG sowieso nicht weit
Oh oh, das macht mir ja Mut. Naja, wenn es zu wild wird schmeiss ich Cg aus dem Projekt raus und mach es "zu Fuss" per GLSL. Wollte nur Cg
verwenden, weil ich gelesen habe das es nah an HLSL sei - und da ich ja noch mit XNA am basteln dachte ich mir warum zweimal lernen.
Antworten