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

Last change on this file since 3502 was 3502, checked in by ldelgass, 11 years ago

Add basic VTK structured points reader to nanovis, update copyright dates.

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