source: trunk/packages/vizservers/nanovis/NvShader.cpp @ 3423

Last change on this file since 3423 was 2972, checked in by ldelgass, 12 years ago

Remove vrutil library. Was only used for FilePath? that already exists in
R2 library (may need some fixing for windows portability though). Make
R2FilePath::getPath return a std::string so users can allocate a std::string
on the stack and not worry about deleting the string buffer returned. Also,
use std::string in R2Fonts to avoid leaking font names. Remove R2string as
it is now replaced by std::strings.

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