source: nanovis/tags/1.1.4/Shader.cpp @ 4937

Last change on this file since 4937 was 4904, checked in by ldelgass, 9 years ago

Merge serveral changes from trunk. Does not include threading, world space
changes, etc.

  • Property svn:eol-style set to native
File size: 3.1 KB
Line 
1/* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/*
3 * Copyright (c) 2004-2013  HUBzero Foundation, LLC
4 *
5 */
6#include <stdio.h>
7
8#include <GL/glew.h>
9#include <Cg/cg.h>
10#include <Cg/cgGL.h>
11
12#include <util/FilePath.h>
13
14#include "Shader.h"
15#include "Trace.h"
16
17using namespace nv;
18using namespace nv::util;
19
20CGprofile Shader::_defaultVertexProfile = CG_PROFILE_VP40;
21CGprofile Shader::_defaultFragmentProfile = CG_PROFILE_FP40;
22CGcontext Shader::_cgContext = NULL;
23
24void Shader::init()
25{
26    _cgContext = cgCreateContext();
27}
28
29void Shader::exit()
30{
31    setErrorCallback(NULL);
32    printErrorInfo();
33    if (_cgContext != NULL) {
34        TRACE("Before DestroyContext");
35        cgDestroyContext(_cgContext);
36        TRACE("After DestroyContext");
37        _cgContext = NULL;
38    }
39}
40
41bool Shader::printErrorInfo()
42{
43    CGerror lastError = cgGetError();
44
45    if (lastError) {
46        TRACE("Cg Error: %s", cgGetErrorString(lastError));
47        if (getCgContext())
48            TRACE("%s", cgGetLastListing(getCgContext()));
49        return false;
50    }
51    return true;
52}
53
54CGprogram
55Shader::loadCgSourceProgram(CGcontext context, const char *fileName,
56                            CGprofile profile, const char *entryPoint)
57{
58    std::string path = FilePath::getInstance()->getPath(fileName);
59    if (path.empty()) {
60        ERROR("can't find program \"%s\"", fileName);
61    }
62    TRACE("cg program compiling: %s", path.c_str());
63    CGprogram program;
64    program = cgCreateProgramFromFile(context, CG_SOURCE, path.c_str(), profile,
65                                      entryPoint, NULL);
66    cgGLLoadProgram(program);
67    CGerror LastError = cgGetError();
68    if (LastError) {
69        ERROR("Error message: %s", cgGetLastListing(context));
70    }
71    TRACE("successfully compiled program: %s", path.c_str());
72    return program;
73}
74
75Shader::Shader():
76    _vertexProfile(_defaultVertexProfile),
77    _fragmentProfile(_defaultFragmentProfile),
78    _cgVP(NULL),
79    _cgFP(NULL)
80{
81}
82
83Shader::~Shader()
84{
85    TRACE("Enter");
86    if (_cgContext == NULL) {
87        TRACE("Lost Cg context: vp: %s, fp: %s", _vpFile.c_str(), _fpFile.c_str());
88    } else {
89        resetPrograms();
90    }
91}
92
93void Shader::loadVertexProgram(const char *fileName)
94{
95    if (_cgVP != NULL) {
96        cgDestroyProgram(_cgVP);
97    }
98    _cgVP = loadCgSourceProgram(_cgContext, fileName,
99                                _vertexProfile, "main");
100    _vpFile = fileName;
101}
102
103void Shader::loadFragmentProgram(const char *fileName)
104{
105    if (_cgFP != NULL) {
106        cgDestroyProgram(_cgFP);
107    }
108    _cgFP = loadCgSourceProgram(_cgContext, fileName,
109                                _fragmentProfile, "main");
110    _fpFile = fileName;
111}
112
113void Shader::resetPrograms()
114{
115    if (_cgVP != NULL) {
116        TRACE("Destroying vertex program: %s", _vpFile.c_str());
117        cgDestroyProgram(_cgVP);
118    }
119
120    if (_cgFP != NULL) {
121        TRACE("Destroying fragment program: %s", _fpFile.c_str());
122        cgDestroyProgram(_cgFP);
123    }
124}
125
126void Shader::setErrorCallback(ShaderCallbackFunction callback)
127{
128    TRACE("Shader setting error callback to: %p", callback);
129    cgSetErrorCallback(callback);
130}
Note: See TracBrowser for help on using the repository browser.