Texture Access inside Fragment Shaders
Hi All,
With reference to the forum topic
http://www.gamedev.net/community/forums/topic.asp?topic_id=272311
I followed the steps that was suggested to create a handle to a texture and sample it inside a fragment shader using sampler2D semantic. Unfortunately my program doesn't work. I created a chessboard texture and tried to attach it to a quad. But all I get is a black quad (as if all the texture values are '0', while it is not). It works fine with regular OGL code, so I know that the texture creation is working properly.
Here's my code:
OpenGL : http://students.uta.edu/sa/sab8695/files/chessCG.cpp
Vertex Shader : http://students.uta.edu/sa/sab8695/files/pixelVP.cg
Fragment Shader : http://students.uta.edu/sa/sab8695/files/pixelFP.cg
It would be great if someone can tell me on whats wrong with the code.
Thanks,
Sriram
put the RELIVENT code in [ source ][ /source ] tags (without the spaces) and you might get a better responce (i for one cba to copy your links, make a new tab and open the page....)
Alright, here's the code :
OpenGL:
pixelVP.cg :
pixelFP.cg :
Thanks,
Sriram
OpenGL:
#include <windows.h>#include <gl\glew.h>//#include <gl\gl.h>//#include <gl\glu.h>#include <gl\glaux.h>#include <cg\cg.h>#include <cg\cggl.h>#include "NeHeGL.h"#pragma comment(lib, "opengl32.lib")#pragma comment(lib, "glu32.lib")#pragma comment(lib, "cg.lib")#pragma comment(lib, "cggl.lib")PFNGLACTIVETEXTUREARBPROC glActiveTextureARB;GL_Window* g_window;Keys* g_keys;CGcontext cgContext;CGprogram cgProgramV, cgProgramF;CGprofile cgVertexProfile, cgFragmentProfile;CGparameter color, modelViewMatrix, tex;GLuint texture[1];static GLubyte img[64][64][4];int LoadGLTextures() { int Status = TRUE; glGenTextures(1, &texture[0]); glBindTexture(GL_TEXTURE_2D, texture[0]); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 64, 64, 0, GL_RGBA, GL_UNSIGNED_BYTE, img); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); return Status;}BOOL Initialize (GL_Window* window, Keys* keys){ g_window = window; g_keys = keys; // Start Of User Initialization glClearColor (0.0f, 0.0f, 0.0f, 0.5f); glClearDepth (1.0f); glDepthFunc (GL_LEQUAL); glEnable (GL_DEPTH_TEST); glEnable (GL_TEXTURE_2D); glShadeModel (GL_SMOOTH); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); glActiveTextureARB = (PFNGLACTIVETEXTUREARBPROC)wglGetProcAddress("glActiveTextureARB"); /***************************************************************************** SETUP CG *****************************************************************************/ // Setup Cg cgContext = cgCreateContext(); // Validate Our Context Generation Was Successful if (cgContext == NULL) { MessageBox(NULL, "Failed To Create Cg Context", "Context Error", MB_OK); return FALSE; // We Cannot Continue } cgVertexProfile = cgGLGetLatestProfile(CG_GL_VERTEX); cgFragmentProfile = cgGLGetLatestProfile(CG_GL_FRAGMENT); // Validate Our Profile Determination Was Successful if (cgVertexProfile == CG_PROFILE_UNKNOWN || cgFragmentProfile == CG_PROFILE_UNKNOWN) { MessageBox(NULL, "Invalid profile type", "Profile Error", MB_OK); return FALSE; } cgGLSetOptimalOptions(cgVertexProfile); cgGLSetOptimalOptions(cgFragmentProfile); // Load And Compile The Vertex Shader From File cgProgramV = cgCreateProgramFromFile(cgContext, CG_SOURCE, "CG/pixelVP.cg", cgVertexProfile, "main", 0); cgProgramF = cgCreateProgramFromFile(cgContext, CG_SOURCE, "CG/pixelFP.cg", cgFragmentProfile, "main", 0); if (cgProgramV == NULL || cgProgramF == NULL) { CGerror Error = cgGetError(); MessageBox(NULL, cgGetErrorString(Error), "Program Create Error", MB_OK); return FALSE; } cgGLLoadProgram(cgProgramV); cgGLLoadProgram(cgProgramF); color = cgGetNamedParameter(cgProgramV, "IN.color"); modelViewMatrix = cgGetNamedParameter(cgProgramV, "ModelViewProj"); tex = cgGetNamedParameter(cgProgramF, "decal"); /***************************************************************************/ int i, j, c; for (i=0;i<64;i++) { for (j=0;j<64;j++) { c = (((i&0x8)==0)^((j&0x8)==0))*255; img[j][0] = (GLubyte)c; img[j][1] = (GLubyte)c; img[j][2] = (GLubyte)c; img[j][3] = 255; } } if(!LoadGLTextures()) return(false); return TRUE;}void Deinitialize (void) { cgDestroyContext(cgContext);}void Update (float milliseconds){ if (g_keys->keyDown [VK_ESCAPE]) TerminateApplication (g_window); if (g_keys->keyDown [VK_F1]) ToggleFullscreen (g_window);}void Draw(void) // Here's Where We Do All The Drawing{ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer glLoadIdentity(); // Reset The View glTranslatef(0.0f,0.0f,-5.0f); gluLookAt(0.0f,0.0f,10.0f,0.0f,0.0f,0.0f,0,1,0); glClearColor (0.5f, 0.5f, 0.5f,1.0f); glActiveTextureARB(texture[0]); cgGLSetStateMatrixParameter(modelViewMatrix, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY); cgGLEnableProfile(cgVertexProfile); cgGLEnableProfile(cgFragmentProfile); cgGLBindProgram(cgProgramV); cgGLBindProgram(cgProgramF); cgGLSetTextureParameter(tex,texture[0]); cgGLEnableTextureParameter(tex); glColor3f(1.0f, 1.0f, 1.0f); glBegin(GL_QUADS); /*glTexCoord2f(0.0f, 0.0f);*/ glVertex3f(-1.0f, -1.0f, 1.0f); /*glTexCoord2f(1.0f, 0.0f);*/ glVertex3f( 1.0f, -1.0f, 1.0f); /*glTexCoord2f(1.0f, 1.0f);*/ glVertex3f( 1.0f, 1.0f, 1.0f); /*glTexCoord2f(0.0f, 1.0f);*/ glVertex3f(-1.0f, 1.0f, 1.0f); glEnd(); cgGLDisableTextureParameter(tex); cgGLDisableProfile(cgVertexProfile); cgGLDisableProfile(cgFragmentProfile); glFlush();}
pixelVP.cg :
struct appdata{ float4 pos : POSITION; float4 col : COLOR; float2 texCoord : TEXCOORD0;};struct c3e4v_Output{ float4 position : POSITION; float4 color : COLOR; float2 texCoord : TEXCOORD0;};c3e4v_Output main(appdata IN, uniform float4x4 ModelViewProj){ c3e4v_Output OUT; OUT.color = IN.col; OUT.position = mul(ModelViewProj, IN.pos); OUT.texCoord = IN.texCoord; return OUT;}
pixelFP.cg :
struct pixelOutput{ float4 color : COLOR;};pixelOutput main(float4 color : COLOR, float2 texCoord : TEXCOORD0, uniform sampler2D decal){ pixelOutput OUT; //OUT.color.xyz = color.xyz; OUT.color = tex2D(decal, texCoord); return OUT;}
Thanks,
Sriram
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement