source: nanovis/branches/1.1/Shader.cpp @ 4893

Last change on this file since 4893 was 4889, checked in by ldelgass, 9 years ago

Merge r3611:3618 from trunk

  • Property svn:eol-style set to native
File size: 3.3 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::initCg(CGprofile defaultVertexProfile,
25                    CGprofile defaultFragmentProfile)
26{
27    _defaultVertexProfile = defaultVertexProfile;
28    _defaultFragmentProfile = defaultFragmentProfile;
29    _cgContext = cgCreateContext();
30}
31
32void Shader::exitCg()
33{
34    setErrorCallback(NULL);
35    printErrorInfo();
36    if (_cgContext != NULL) {
37        TRACE("Before DestroyContext");
38        cgDestroyContext(_cgContext);
39        TRACE("After DestroyContext");
40        _cgContext = NULL;
41    }
42}
43
44bool Shader::printErrorInfo()
45{
46    CGerror lastError = cgGetError();
47
48    if (lastError) {
49        TRACE("Cg Error: %s", cgGetErrorString(lastError));
50        if (getCgContext())
51            TRACE("%s", cgGetLastListing(getCgContext()));
52        return false;
53    }
54    return true;
55}
56
57CGprogram
58Shader::loadCgSourceProgram(CGcontext context, const char *fileName,
59                            CGprofile profile, const char *entryPoint)
60{
61    std::string path = FilePath::getInstance()->getPath(fileName);
62    if (path.empty()) {
63        ERROR("can't find program \"%s\"", fileName);
64    }
65    TRACE("cg program compiling: %s", path.c_str());
66    CGprogram program;
67    program = cgCreateProgramFromFile(context, CG_SOURCE, path.c_str(), profile,
68                                      entryPoint, NULL);
69    cgGLLoadProgram(program);
70    CGerror LastError = cgGetError();
71    if (LastError) {
72        ERROR("Error message: %s", cgGetLastListing(context));
73    }
74    TRACE("successfully compiled program: %s", path.c_str());
75    return program;
76}
77
78Shader::Shader():
79    _vertexProfile(_defaultVertexProfile),
80    _fragmentProfile(_defaultFragmentProfile),
81    _cgVP(NULL),
82    _cgFP(NULL)
83{
84}
85
86Shader::~Shader()
87{
88    TRACE("Enter");
89    if (_cgContext == NULL) {
90        TRACE("Lost Cg context: vp: %s, fp: %s", _vpFile.c_str(), _fpFile.c_str());
91    } else {
92        resetPrograms();
93    }
94}
95
96void Shader::loadVertexProgram(const char *fileName, const char *entryPoint)
97{
98    if (_cgVP != NULL) {
99        cgDestroyProgram(_cgVP);
100    }
101    _cgVP = loadCgSourceProgram(_cgContext, fileName,
102                                _vertexProfile, entryPoint);
103    _vpFile = fileName;
104}
105
106void Shader::loadFragmentProgram(const char *fileName, const char *entryPoint)
107{
108    if (_cgFP != NULL) {
109        cgDestroyProgram(_cgFP);
110    }
111    _cgFP = loadCgSourceProgram(_cgContext, fileName,
112                                _fragmentProfile, entryPoint);
113    _fpFile = fileName;
114}
115
116void Shader::resetPrograms()
117{
118    if (_cgVP != NULL) {
119        TRACE("Destroying vertex program: %s", _vpFile.c_str());
120        cgDestroyProgram(_cgVP);
121    }
122
123    if (_cgFP != NULL) {
124        TRACE("Destroying fragment program: %s", _fpFile.c_str());
125        cgDestroyProgram(_cgFP);
126    }
127}
128
129void Shader::setErrorCallback(CgCallbackFunction callback)
130{
131    TRACE("Shader setting error callback to: %p", callback);
132    cgSetErrorCallback(callback);
133}
Note: See TracBrowser for help on using the repository browser.