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

Last change on this file since 2884 was 2877, checked in by ldelgass, 12 years ago

Some minor refactoring, also add some more fine grained config.h defines
(e.g. replace NV40 define with feature defines). Add tests for some required
OpenGL extensions (should always check for extensions or base version before
calling entry points from the extension). Also, clamp diffuse and specular
values on input and warn when they are out of range.

  • Property svn:eol-style set to native
File size: 2.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
13CGcontext NvShader::_cgContext = NULL;
14
15void NvShader::initCg()
16{
17    _cgContext = cgCreateContext();
18}
19
20void NvShader::exitCg()
21{
22    setErrorCallback(NULL);
23    printErrorInfo();
24    if (_cgContext != NULL) {
25        cgDestroyContext(_cgContext);
26        _cgContext = NULL;
27    }
28}
29
30bool NvShader::printErrorInfo()
31{
32    CGerror lastError = cgGetError();
33
34    if (lastError) {
35        TRACE("Cg Error: %s\n", cgGetErrorString(lastError));
36        if (getCgContext())
37            TRACE("%s\n", cgGetLastListing(getCgContext()));
38        return false;
39    }
40    return true;
41}
42
43CGprogram
44LoadCgSourceProgram(CGcontext context, const char *fileName, CGprofile profile,
45                    const char *entryPoint)
46{
47    const char *path = R2FilePath::getInstance()->getPath(fileName);
48    if (path == NULL) {
49        ERROR("can't find program \"%s\"\n", fileName);
50    }
51    TRACE("cg program compiling: %s\n", path);
52    CGprogram program;
53    program = cgCreateProgramFromFile(context, CG_SOURCE, path, profile,
54                                      entryPoint, NULL);
55    cgGLLoadProgram(program);
56    CGerror LastError = cgGetError();
57    if (LastError) {
58        ERROR("Error message: %s\n", cgGetLastListing(context));
59    }
60    TRACE("successfully compiled program: %s\n", path);
61    delete [] path;
62    return program;
63}
64
65NvShader::NvShader():
66    _cgVP(NULL),
67    _cgFP(NULL)
68{
69}
70
71NvShader::~NvShader()
72{
73    resetPrograms();
74}
75
76void NvShader::loadVertexProgram(const char *fileName, const char *entryPoint)
77{
78    resetPrograms();
79
80    _cgVP = LoadCgSourceProgram(_cgContext, fileName, CG_PROFILE_VP40, entryPoint);
81}
82
83void NvShader::loadFragmentProgram(const char *fileName, const char *entryPoint)
84{
85    _cgFP = LoadCgSourceProgram(_cgContext, fileName, CG_PROFILE_FP40, entryPoint);
86}
87
88void NvShader::resetPrograms()
89{
90    if (_cgVP != NULL) {
91        cgDestroyProgram(_cgVP);
92    }
93
94    if (_cgFP != NULL) {
95        cgDestroyProgram(_cgFP);
96    }
97}
98
99void NvShader::setErrorCallback(NvCgCallbackFunction callback)
100{
101    TRACE("NvShader setting error callback to: %p\n", callback);
102    cgSetErrorCallback(callback);
103}
Note: See TracBrowser for help on using the repository browser.