Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c++;:如何在opengl中使用sdl将位图加载到多维数据集中?_C++_Opengl_Sdl_Sdl Opengl - Fatal编程技术网

C++ c++;:如何在opengl中使用sdl将位图加载到多维数据集中?

C++ c++;:如何在opengl中使用sdl将位图加载到多维数据集中?,c++,opengl,sdl,sdl-opengl,C++,Opengl,Sdl,Sdl Opengl,我正在学习OpenGL和SDL,到目前为止,我已经能够正确地绘制和旋转3d多边形(对我来说是的!) 如何使用SDL加载图像文件,并将该纹理加载到使用OpenGL在屏幕上绘制的3d形状上 谢谢 更新 我尝试了下面的代码来绘制一个立方体,旋转它并添加一个位图,但是我没有看到位图可见。有什么想法吗 #ifdef WIN32 #define WIN32_LEAN_AND_MEAN #include <windows.h> #endif #if defined(__APPLE__) &am

我正在学习OpenGL和SDL,到目前为止,我已经能够正确地绘制和旋转3d多边形(对我来说是的!)

如何使用SDL加载图像文件,并将该纹理加载到使用OpenGL在屏幕上绘制的3d形状上

谢谢

更新 我尝试了下面的代码来绘制一个立方体,旋转它并添加一个位图,但是我没有看到位图可见。有什么想法吗

#ifdef WIN32

#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#endif
#if defined(__APPLE__) && defined(__MACH__)
#include <OpenGL/gl.h>  // Header File For The OpenGL32 Library    
#include <OpenGL/glu.h> // Header File For The GLu32 Library
#else
#include <GL/gl.h>  // Header File For The OpenGL32 Library
#include <GL/glu.h> // Header File For The GLu32 Library
#endif
#include "SDL.h"
#include <stdio.h>
#include <unistd.h>

SDL_Surface *screen=NULL;

GLfloat     rtri;                       // Angle For The Triangle ( NEW )
GLfloat     rquad;                      // Angle For The Quad     ( NEW )

GLuint texture;         // This is a handle to our texture object
SDL_Surface *surface;   // This surface will tell us the details of the image
GLenum texture_format;
GLint  nOfColors;


bool InitGL(int Width, int Height)          // We call this right after our OpenGL window is created.
{
glViewport(0, 0, Width, Height);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);       // This Will Clear The Background Color To Black
glClearDepth(1.0);              // Enables Clearing Of The Depth Buffer
glDepthFunc(GL_LESS);               // The Type Of Depth Test To Do
glEnable(GL_DEPTH_TEST);            // Enables Depth Testing
glShadeModel(GL_SMOOTH);            // Enables Smooth Color Shading

glMatrixMode(GL_PROJECTION);
glLoadIdentity();               // Reset The Projection Matrix

gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,100.0f);   // Calculate The Aspect Ratio Of The Window

glMatrixMode(GL_MODELVIEW);

if ( (surface = SDL_LoadBMP("icon.bmp")) ) { 
    
    // Check that the image's width is a power of 2
    if ( (surface->w & (surface->w - 1)) != 0 ) {
        printf("error: image.bmp's width is not a power of 2\n");
        return false;
    }
    
    // Also check if the height is a power of 2
    if ( (surface->h & (surface->h - 1)) != 0 ) {
        printf("error: image.bmp's height is not a power of 2\n");
        return false;
    }
    
    // get the number of channels in the SDL surface
    nOfColors = surface->format->BytesPerPixel;
    if (nOfColors == 4)     // contains an alpha channel
    {
        if (surface->format->Rmask == 0x000000ff)
            texture_format = GL_RGBA;
        else
            texture_format = GL_BGRA;
    } else if (nOfColors == 3)     // no alpha channel
    {
        if (surface->format->Rmask == 0x000000ff)
            texture_format = GL_RGB;
        else
            texture_format = GL_BGR;
    } else {
        printf("error: the image is not truecolor..  this will probably break\n");
        return false;
        // this error should not go unhandled
    }
    
    // Have OpenGL generate a texture object handle for us
    glGenTextures( 1, &texture );
    
    // Bind the texture object
    glBindTexture( GL_TEXTURE_2D, texture );
    
    // Set the texture's stretching properties
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
    
    // Edit the texture object's image data using the information SDL_Surface gives us
    glTexImage2D( GL_TEXTURE_2D, 0, nOfColors, surface->w, surface->h, 0,
                 texture_format, GL_UNSIGNED_BYTE, surface->pixels     );
} 
else {
    printf("SDL could not load image.bmp: %s\n", SDL_GetError());
    SDL_Quit();
    return false;
}    
return true;
}

/* The main drawing function. */
int DrawGLScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);     // Clear The Screen And The Depth Buffer
glLoadIdentity();               // Reset The View

glTranslatef(-1.5f,0.0f,-6.0f);     // Move Left 1.5 Units And Into The Screen 6.0
glRotatef(rtri,0.0f,1.0f,0.0f);             // Rotate The Triangle On The Y axis ( NEW )    

glBegin(GL_QUADS);
// Front Face
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);  // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);  // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);  // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);  // Top Left Of The Texture and Quad
// Back Face
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);  // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);  // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);  // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);  // Bottom Left Of The Texture and Quad
// Top Face
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);  // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f,  1.0f,  1.0f);  // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f,  1.0f,  1.0f);  // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);  // Top Right Of The Texture and Quad
// Bottom Face
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);  // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);  // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);  // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);  // Bottom Right Of The Texture and Quad
// Right face
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);  // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);  // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);  // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);  // Bottom Left Of The Texture and Quad
// Left Face
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);  // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);  // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);  // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);  // Top Left Of The Texture and Quad
glEnd();


rtri+=0.02f;                        // Increase The Rotation Variable For The Triangle ( NEW )
rquad-=0.015f;                      // Decrease The Rotation Variable For The Quad     ( NEW )


// swap buffers to display, since we're double buffered.
SDL_GL_SwapBuffers();
return true;
}


int main(int argc,char* argv[])
{


int done;
/*variable to hold the file name of the image to be loaded
 *In real world error handling code would precede this
 */

/* Initialize SDL for video output */
if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
    fprintf(stderr, "Unable to initialize SDL: %s\n", SDL_GetError());
    exit(1);
}

atexit(SDL_Quit);

/* Create a 640x480 OpenGL screen */
if ( SDL_SetVideoMode(640, 480, 0, SDL_OPENGL) == NULL ) {
    fprintf(stderr, "Unable to create OpenGL screen: %s\n", SDL_GetError());
    SDL_Quit();
    exit(2);
}
SDL_WM_SetCaption("another example",NULL);  
InitGL(640,480);
done=0;
while (! done) {
    DrawGLScene();
    SDL_Event event;
    while ( SDL_PollEvent(&event) ) {
        if ( event.type == SDL_QUIT ) {
            done = 1;
        }
        if ( event.type == SDL_KEYDOWN ) {
            if ( event.key.keysym.sym == SDLK_ESCAPE ) {
                done = 1;
            }
        }
    }         
}
return 1;
}
#ifdef WIN32
#定义WIN32_精益_和_平均值
#包括
#恩迪夫
#如果已定义(uuu苹果公司)和已定义(uuu马赫)
#包含//OpenGL32库的头文件
#包含//GLu32库的头文件
#否则
#包含//OpenGL32库的头文件
#包含//GLu32库的头文件
#恩迪夫
#包括“SDL.h”
#包括
#包括
SDL_表面*屏幕=空;
GLRTRI;//三角形的角度(新)
GLfloat rquad;//四边形的角度(新)
胶合纹理;//这是纹理对象的句柄
SDL_表面*表面;//这个曲面将告诉我们图像的细节
骨盂纹理图;
闪烁的彩色;
bool InitGL(int-Width,int-Height)//我们在创建OpenGL窗口后立即调用它。
{
glViewport(0,0,宽度,高度);
glClearColor(0.0f,0.0f,0.0f,0.0f);//这将把背景色清除为黑色
glClearDepth(1.0);//启用深度缓冲区的清除
glDepthFunc(GL_LESS);//要执行的深度测试类型
glEnable(GL_DEPTH_TEST);//启用深度测试
glShadeModel(GL_SMOOTH);//启用平滑颜色着色
glMatrixMode(GL_投影);
glLoadIdentity();//重置投影矩阵
gluPerspective(45.0f,(GLfloat)宽度/(GLfloat)高度,0.1f,100.0f);//计算窗口的纵横比
glMatrixMode(GLU模型视图);
如果((surface=SDL_LoadBMP(“icon.bmp”)){
//检查图像的宽度是否为2的幂
如果((曲面->w&(曲面->w-1))!=0){
printf(“错误:image.bmp的宽度不是2\n的幂”);
返回false;
}
//同时检查高度是否为2的幂
如果((曲面->h&(曲面->h-1))!=0){
printf(“错误:image.bmp的高度不是2\n的幂”);
返回false;
}
//获取SDL曲面中的通道数
nOfColors=表面->格式->字节/像素;
if(nOfColors==4)//包含alpha通道
{
如果(曲面->格式->Rmask==0x000000ff)
纹理_格式=GL_RGBA;
其他的
纹理\格式=GL \ U BGRA;
}else if(nOfColors==3)//无alpha通道
{
如果(曲面->格式->Rmask==0x000000ff)
纹理_格式=GL_RGB;
其他的
纹理\u格式=GL\u BGR;
}否则{
printf(“错误:图像不是truecolor..这可能会中断\n”);
返回false;
//此错误不应未经处理
}
//让OpenGL为我们生成一个纹理对象句柄
glGenTextures(1,&纹理);
//绑定纹理对象
glBindTexture(GL_TEXTURE_2D,纹理);
//设置纹理的拉伸属性
glTexParameteri(GL_纹理2D、GL_纹理最小过滤器、GL_线性);
glTexParameteri(GL_纹理2D、GL_纹理MAG_过滤器、GL_线性);
//使用SDL_Surface提供的信息编辑纹理对象的图像数据
glTexImage2D(GL_纹理_2D,0,无颜色,表面->w,表面->h,0,
纹理格式,GL\u无符号字节,表面->像素);
} 
否则{
printf(“SDL无法加载image.bmp:%s\n”,SDL_GetError());
SDL_退出();
返回false;
}    
返回true;
}
/*主要绘图功能*/
int DrawGLScene()
{
glClear(GL_颜色_缓冲区| GL_深度_缓冲区|位);//清除屏幕和深度缓冲区
glLoadIdentity();//重置视图
glTranslatef(-1.5f,0.0f,-6.0f);//向左移动1.5个单位,进入屏幕6.0
glRotatef(rtri,0.0f,1.0f,0.0f);//在Y轴上旋转三角形(新)
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);//纹理和四边形的左上角
//背面
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);//纹理和四边形的左上角
glTexCoord2f(0.0f,0.0f);glVertex3f(1.0f,-1.0f,-1.0f);//纹理和四边形的左下角
//顶面
glTexCoord2f(0.0f,1.0f);glVertex3f(-1.0f,1.0f,-1.0f);//纹理和四边形的左上角
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(1.0f,1.0f);glVertex3f(-1.0f,-1.0f,-1.0f);//纹理和四边形的右上角
glTexCoord2f(0.0f,1.0f);glVertex3f(1.0f,-1.0f,-1.0f);//纹理和四边形的左上角
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,0.0f);glVertex3f(1.0f,-1.0f,-1.0f);//纹理和四边形的右下角
glTexCoord2f(1.0f,1.0f);glVertex3f(1.0f,1.0f,-1.0f);//纹理的右上角
ilInit();
iluInit();
GLuint  texture;


ILuint texid;
ilGenImages(1, &texid);
ilBindImage(texid);
ilLoadImage("tex.png");
iluFlipImage();
ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE);

glGenTextures(1,&texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);

gluBuild2DMipmaps(GL_TEXTURE_2D, 4, ilGetInteger(IL_IMAGE_WIDTH), ilGetInteger(IL_IMAGE_HEIGHT), ilGetInteger(IL_IMAGE_FORMAT),
                  GL_UNSIGNED_BYTE, ilGetData());


ilDeleteImage(texid);