游戏开发论坛

 找回密码
 立即注册
搜索
查看: 2586|回复: 4

glConvolutionFilter2D

[复制链接]

1

主题

1

帖子

0

积分

新手上路

Rank: 1

积分
0
发表于 2010-8-9 16:35:00 | 显示全部楼层 |阅读模式
最近刚学opengl,但好象glConvolutionFilter2D这个函数在Visual C++ 6.0下不能正确调用,哪位高手可以指点下啊?!!!!!
#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>

extern GLubyte*  readImage(const char*, GLsizei*, GLsizei*);

GLubyte  *pixels;
GLsizei   width, height;

GLfloat  horizontal[3][3] = {
    { 0, -1, 0 },
    { 0,  1, 0 },
    { 0,  0, 0 }
};

GLfloat  vertical[3][3] = {
    {  0, 0, 0 },
    { -1, 1, 0 },
    {  0, 0, 0 }
};

GLfloat  laplacian[3][3] = {
    { -0.125, -0.125, -0.125 },
    { -0.125,  1.0  , -0.125 },
    { -0.125, -0.125, -0.125 },
};


void init(void)
{
   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
   glClearColor(0.0, 0.0, 0.0, 0.0);

   printf("Using the horizontal filter\n");
   glConvolutionFilter2D(GL_CONVOLUTION_2D, GL_LUMINANCE,
                         3, 3, GL_LUMINANCE, GL_FLOAT, horizontal);
   glEnable(GL_CONVOLUTION_2D);
}

void display(void)
{
   glClear(GL_COLOR_BUFFER_BIT);
   glRasterPos2i( 1, 1);
   glDrawPixels(width, height, GL_RGB, GL_UNSIGNED_BYTE, pixels);
   glFlush();
}

void reshape(int w, int h)
{
   glViewport(0, 0, (GLsizei) w, (GLsizei) h);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   glOrtho(0, w, 0, h, -1.0, 1.0);
   glMatrixMode(GL_MODELVIEW);
}

void keyboard(unsigned char key, int x, int y)
{
   switch (key) {
       case 'h' :
           printf("Using a horizontal filter\n");
           glConvolutionFilter2D(GL_CONVOLUTION_2D, GL_LUMINANCE, 3, 3,
                                  GL_LUMINANCE, GL_FLOAT, horizontal);
           break;

       case 'v' :
           printf("Using the vertical filter\n");
           glConvolutionFilter2D(GL_CONVOLUTION_2D, GL_LUMINANCE, 3, 3,
                                  GL_LUMINANCE, GL_FLOAT, vertical);
           break;

       case 'l' :
           printf("Using the laplacian filter\n");
           glConvolutionFilter2D(GL_CONVOLUTION_2D, GL_LUMINANCE, 3, 3,
                                  GL_LUMINANCE, GL_FLOAT, laplacian);
           break;

       case 27:
         exit(0);
   }
   glutPostRedisplay();
}

/*  Main Loop
*  Open window with initial window size, title bar,
*  RGBA display mode, and handle input events.
*/
int main(int argc, char** argv)
{
   pixels = readImage("Data/leeds.bin", &width, &height);

   glutInit(&argc, argv);
   glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
   glutInitWindowSize(width, height);
   glutInitWindowPosition(100, 100);
   glutCreateWindow(argv[0]);
   init();
   glutReshapeFunc(reshape);
   glutKeyboardFunc(keyboard);
   glutDisplayFunc(display);
   glutMainLoop();
   return 0;
}

Compiling...
convolution.c
e:\test\openglbk\convolution.c(82) : warning C4013: 'glConvolutionFilter2D' undefined; assuming extern returning int
e:\test\openglbk\convolution.c(82) : error C2065: 'GL_CONVOLUTION_2D' : undeclared identifier
Error executing cl.exe.

0

主题

3

帖子

0

积分

新手上路

Rank: 1

积分
0
发表于 2011-7-19 13:10:00 | 显示全部楼层

Re:glConvolutionFilter2D

加入这句试试
#include <gl/glew.h>
然后再把
glew32.lib
加入到工程中,我的编译倒是通过了,但是运行还有问题,
这个例子挺恶心的。

0

主题

3

帖子

0

积分

新手上路

Rank: 1

积分
0
发表于 2011-7-19 17:00:00 | 显示全部楼层

Re:glConvolutionFilter2D

#include <windows.h>
#include <GL/glew.h>
#include <GL/glut.h>

#include <cstdio>
#include <cstdlib>
#include <iostream>

using namespace std;

GLubyte  *pixels;

GLsizei   width, height;


GLubyte * readImage( const char* filename, GLsizei* width, GLsizei *height )
{
    int       n;
    GLubyte *  pixels;
    FILE * infile = fopen( filename, "rb" );

    if ( !infile )
        {
                fprintf( stderr, "Unable to open file '%s'\n", filename );
                return NULL;
    }
    fread( width, sizeof( GLsizei ), 1, infile );
        cout<<"width is "<<width<<endl;

    fread( height, sizeof( GLsizei ), 1, infile );
        cout<<"height is "<<height<<endl;
    n = 3 * (*width) * (*height);
    pixels = (GLubyte *) malloc( n * sizeof( GLubyte ));
    if ( !pixels )
        {
                fprintf( stderr, "Unable to malloc() bytes for pixels\n" );
                return NULL;
    }
    fread( pixels, sizeof( GLubyte ), n, infile );
   
    fclose( infile );

    return pixels;
}
GLfloat  horizontal[3][3] =
{
    { 0, -1, 0 },
    { 0,  1, 0 },
    { 0,  0, 0 }
};

GLfloat  vertical[3][3] =
{
    {  0, 0, 0 },
    { -1, 1, 0 },
    {  0, 0, 0 }
};

GLfloat  laplacian[3][3] = {
    { -0.125, -0.125, -0.125 },
    { -0.125,  1.0  , -0.125 },
    { -0.125, -0.125, -0.125 },
};


void init(void)
{
   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
   glClearColor(0.0, 0.0, 0.0, 0.0);

   printf("Using the horizontal filter\n");
   glConvolutionFilter2D(GL_CONVOLUTION_2D, GL_LUMINANCE,
                        3, 3, GL_LUMINANCE, GL_FLOAT, horizontal);
   glEnable(GL_CONVOLUTION_2D);
}

void display(void)
{
   glClear(GL_COLOR_BUFFER_BIT);
   glRasterPos2i( 1, 1);
   glDrawPixels(width, height, GL_RGB, GL_UNSIGNED_BYTE, pixels);
   glFlush();
}

void reshape(int w, int h)
{
   glViewport(0, 0, (GLsizei) w, (GLsizei) h);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   glOrtho(0, w, 0, h, -1.0, 1.0);
   glMatrixMode(GL_MODELVIEW);
}

void keyboard(unsigned char key, int x, int y)
{
   switch (key)
   {
       case 'h' :
           printf("Using a horizontal filter\n");
           glConvolutionFilter2D(GL_CONVOLUTION_2D, GL_LUMINANCE, 3, 3,
                                  GL_LUMINANCE, GL_FLOAT, horizontal);
           break;

       case 'v' :
           printf("Using the vertical filter\n");
           glConvolutionFilter2D(GL_CONVOLUTION_2D, GL_LUMINANCE, 3, 3,
                                  GL_LUMINANCE, GL_FLOAT, vertical);
           break;

       case 'l' :
           printf("Using the laplacian filter\n");
           glConvolutionFilter2D(GL_CONVOLUTION_2D, GL_LUMINANCE, 3, 3,
                                  GL_LUMINANCE, GL_FLOAT, laplacian);
           break;

       case 27:
         exit(0);
   }
   glutPostRedisplay();
}

/*  Main Loop
*  Open window with initial window size, title bar,
*  RGBA display mode, and handle input events.
*/
int main(int argc, char** argv)
{
   pixels = readImage("leeds(1).bin", &width, &height);
   cout<<"Width = "<<width <<" Height = "<<height<<endl;
   glutInit(&argc, argv);
   glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
   glutInitWindowSize(width, height);
   glutInitWindowPosition(100, 100);
   glutCreateWindow("Demo of convolution");
   //初始化OpenGL扩展。
   glewInit();
   init();
   glutReshapeFunc(reshape);
   glutKeyboardFunc(keyboard);
   glutDisplayFunc(display);
   glutMainLoop();
   return 0;
}


0

主题

3

帖子

0

积分

新手上路

Rank: 1

积分
0
发表于 2011-7-19 17:03:00 | 显示全部楼层

Re:glConvolutionFilter2D

还有,原始的leeds.bin是不能用的,因为它用了大端编码,我们PC机上的编码通常是小端编码。用一个程序把它转换一下就行了。程序的下载地址:
http://d.download.csdn.net/down/1953680/cq317a

2万

主题

2万

帖子

6万

积分

论坛元老

Rank: 8Rank: 8

积分
66489
QQ
发表于 2011-7-21 00:15:00 | 显示全部楼层

Re:glConvolutionFilter2D

这个API不是什么显卡都可以用的
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-6-8 12:12

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表