I've gotten all the really basic stuff to work but I can't figure out how to load a texture into my shader. I'm going to post the code and I hope one of you can tell me what I'm doing wrong. Also if you see anything else in the code that you think needs to be fixed please tell me. Here is the code:
Shader:
struct VS_INPUT
{
float4 position : POSITION;
float4 color : COLOR0;
};
struct VS_OUTPUT
{
float4 position : POSITION;
float4 color : COLOR0;
};
VS_OUTPUT VS(VS_INPUT IN, uniform float4x4 ModelViewProj)
{
VS_OUTPUT OUT;
OUT.position = mul(ModelViewProj, IN.position);
OUT.color = IN.color;
return OUT;
}
float4 PS(VS_OUTPUT IN, uniform sampler2D Tex0) : COLOR
{
return tex2D( Tex0, IN.position.xy );
}
Probably this code is the problem:
GLuint LoadTextureRAW( const char * filename, int wrap )
{
GLuint texture;
int width, height;
BYTE * data;
FILE * file;
file = fopen( filename, "rb" );
if ( file == NULL ) return 0;
width = 256;
height = 256;
data = new BYTE[width * height * 3];
fread( data, width * height * 3, 1, file );
fclose( file );
glGenTextures( 1, &texture );
glBindTexture( GL_TEXTURE_2D, texture );
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,wrap ? GL_REPEAT : GL_CLAMP );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,wrap ? GL_REPEAT : GL_CLAMP );
gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height,GL_RGB, GL_UNSIGNED_BYTE, data );
delete [] data;
return texture;
}
void render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(0.0f, 25.0f, -45.0f, 0.0f, 0.0f, 0.0f, 0, 1, 0);
cgGLSetStateMatrixParameter(modelViewMatrix, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY);
cgGLSetTextureParameter( Tex0, texture[0] );
cgGLEnableProfile(cgVertexProfile);
cgGLBindProgram(cg_vs);
cgGLEnableProfile(cgPixelProfile);
cgGLBindProgram(cg_ps);
cgGLEnableTextureParameter( Tex0 );
glEnableClientState( GL_VERTEX_ARRAY );
glBindBuffer( GL_ARRAY_BUFFER, vertex_buffer );
glVertexPointer( 3, GL_FLOAT, 0, 0);
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, index_buffer );
glDrawElements(GL_TRIANGLES,3,GL_UNSIGNED_SHORT,0);
glDisableClientState( GL_VERTEX_ARRAY );
cgGLDisableTextureParameter( Tex0 );
cgGLDisableProfile(cgPixelProfile);
cgGLDisableProfile(cgVertexProfile);
SwapBuffers(hDC);
}
Full Application:
#include <windows.h>
#include <stdio.h>
#include <gl\glew.h>
#include <gl\gl.h>
#include <gl\glu.h>
#include <cg\cg.h>
#include <cg\cgGL.h>
HWND hWnd=NULL;
HGLRC hRC=NULL;
HDC hDC=NULL;
bool keys[256];
CGcontext cgContext;
CGprogram cg_vs, cg_ps;
CGprofile cgVertexProfile, cgPixelProfile;
CGparameter position, color, modelViewMatrix;
CGparameter Tex0;
GLuint vertex_buffer, index_buffer;
GLuint texture[1];
class CVert
{
public:
float x;
float y;
float z;
};
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
GLvoid GLResize(GLsizei width, GLsizei height)
{
if(height==0)
height=1;
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
GLuint LoadTextureRAW( const char * filename, int wrap )
{
GLuint texture;
int width, height;
BYTE * data;
FILE * file;
file = fopen( filename, "rb" );
if ( file == NULL ) return 0;
width = 256;
height = 256;
data = new BYTE[width * height * 3];
fread( data, width * height * 3, 1, file );
fclose( file );
glGenTextures( 1, &texture );
glBindTexture( GL_TEXTURE_2D, texture );
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,wrap ? GL_REPEAT : GL_CLAMP );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,wrap ? GL_REPEAT : GL_CLAMP );
gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height,GL_RGB, GL_UNSIGNED_BYTE, data );
delete [] data;
return texture;
}
void load_mesh()
{
CVert* vertices = new CVert[3];
vertices[0].x=0;
vertices[0].y=0;
vertices[0].z=0;
vertices[1].x=10;
vertices[1].y=0;
vertices[1].z=0;
vertices[2].x=0;
vertices[2].y=10;
vertices[2].z=0;
int* indices = new int[3];
indices[0]=2;
indices[1]=1;
indices[2]=0;
glGenBuffers( 1, &vertex_buffer );
glBindBuffer( GL_ARRAY_BUFFER, vertex_buffer );
glBufferData( GL_ARRAY_BUFFER, 9*sizeof(float), vertices, GL_STATIC_DRAW );
glGenBuffers( 1, &index_buffer );
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, index_buffer );
glBufferData( GL_ELEMENT_ARRAY_BUFFER, 3*sizeof(int), indices, GL_STATIC_DRAW );
delete [] vertices;
delete [] indices;
}
void render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(0.0f, 25.0f, -45.0f, 0.0f, 0.0f, 0.0f, 0, 1, 0);
cgGLSetStateMatrixParameter(modelViewMatrix, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY);
cgGLSetTextureParameter( Tex0, texture[0] );
cgGLEnableProfile(cgVertexProfile);
cgGLBindProgram(cg_vs);
cgGLEnableProfile(cgPixelProfile);
cgGLBindProgram(cg_ps);
cgGLEnableTextureParameter( Tex0 );
glEnableClientState( GL_VERTEX_ARRAY );
glBindBuffer( GL_ARRAY_BUFFER, vertex_buffer );
glVertexPointer( 3, GL_FLOAT, 0, 0);
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, index_buffer );
glDrawElements(GL_TRIANGLES,3,GL_UNSIGNED_SHORT,0);
glDisableClientState( GL_VERTEX_ARRAY );
cgGLDisableTextureParameter( Tex0 );
cgGLDisableProfile(cgPixelProfile);
cgGLDisableProfile(cgVertexProfile);
SwapBuffers(hDC);
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_CLOSE:
{
PostQuitMessage(0);
return 0;
}
case WM_KEYDOWN:
{
keys[wParam] = TRUE;
return 0;
}
case WM_KEYUP:
{
keys[wParam] = FALSE;
return 0;
}
case WM_CREATE:
{
GLResize(LOWORD(lParam),HIWORD(lParam));
return 0;
}
case WM_SIZE:
{
GLResize(LOWORD(lParam),HIWORD(lParam));
return 0;
}
}
return DefWindowProc(hWnd,uMsg,wParam,lParam);
}
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
{
RECT WindowRect;
WindowRect.left=0;
WindowRect.right=640;
WindowRect.top=0;
WindowRect.bottom=480;
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wc.lpfnWndProc = (WNDPROC) WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = 0;
wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = 0;
wc.lpszMenuName = 0;
wc.lpszClassName = "Debug";
RegisterClass(&wc);
hWnd = CreateWindowEx( WS_EX_APPWINDOW | WS_EX_WINDOWEDGE, "Debug",
"Debug" ,WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
WindowRect.left, WindowRect.top, WindowRect.right-WindowRect.left,
WindowRect.bottom-WindowRect.top, 0, 0, 0, 0);
ShowWindow(hWnd,SW_SHOW);
UnregisterClass( "Debug", wc.hInstance );
static PIXELFORMATDESCRIPTOR pfd={ sizeof(PIXELFORMATDESCRIPTOR), 1,
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL |PFD_DOUBLEBUFFER,PFD_TYPE_RGBA, 16,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, PFD_MAIN_PLANE, 0, 0, 0, 0 };
hDC=GetDC(hWnd);
SetPixelFormat(hDC,ChoosePixelFormat(hDC,&pfd),&pfd);
hRC = wglCreateContext(hDC);
wglMakeCurrent(hDC,hRC);
glShadeModel(GL_SMOOTH);
glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
cgContext = cgCreateContext();
cgVertexProfile = cgGLGetLatestProfile(CG_GL_VERTEX);
cgPixelProfile = cgGLGetLatestProfile(CG_GL_FRAGMENT);
cg_vs = cgCreateProgramFromFile(cgContext, CG_SOURCE, "Data/Shaders/default.cg", cgVertexProfile, "VS", 0);
cgGLLoadProgram(cg_vs);
cg_ps = cgCreateProgramFromFile(cgContext, CG_SOURCE, "Data/Shaders/default.cg", cgPixelProfile, "PS", 0);
cgGLLoadProgram(cg_ps);
position = cgGetNamedParameter(cg_vs, "IN.position");
color = cgGetNamedParameter(cg_vs, "IN.color");
modelViewMatrix = cgGetNamedParameter(cg_vs, "ModelViewProj");
Tex0 = cgGetNamedParameter(cg_ps, "Tex0");
GLResize(WindowRect.right-WindowRect.left, WindowRect.bottom-WindowRect.top);
glewInit();
load_mesh();
texture[0]=LoadTextureRAW( "u.raw", 0 );
MSG msg;
ZeroMemory( &msg, sizeof( msg ) );
while( msg.message!=WM_QUIT )
{
if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else
render();
}
wglMakeCurrent(NULL,NULL);
wglDeleteContext(hRC);
hRC=NULL;
return (msg.wParam);
}
[Edited by - EmptyVoid on August 3, 2008 11:36:51 AM]
This is your life, and it's ending one minute at a time. - Fight club