Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.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 为什么我的openGL纹理只覆盖了我的四边形的一半?来源包括_C_Opengl_Textures_Glut - Fatal编程技术网

C 为什么我的openGL纹理只覆盖了我的四边形的一半?来源包括

C 为什么我的openGL纹理只覆盖了我的四边形的一半?来源包括,c,opengl,textures,glut,C,Opengl,Textures,Glut,这是我的密码。我试图画一个简单的四边形,并在它的两侧放置一个棋盘格图案。我想允许用户用鼠标围绕这一块旋转。除了纹理之外,一切都很好——它是倾斜的,只覆盖了四分之一的区域。有人能看出我做错了什么显而易见的事情吗?谢谢 #include "glut-3.7.6-bin\glut.h" // Constants for rotating camera #define CAMERA_RELEASE 0 #define CAMERA_ROTATE 1 #define CAMERA_ZOOM 2 //

这是我的密码。我试图画一个简单的四边形,并在它的两侧放置一个棋盘格图案。我想允许用户用鼠标围绕这一块旋转。除了纹理之外,一切都很好——它是倾斜的,只覆盖了四分之一的区域。有人能看出我做错了什么显而易见的事情吗?谢谢

#include "glut-3.7.6-bin\glut.h"

// Constants for rotating camera
#define CAMERA_RELEASE 0
#define CAMERA_ROTATE 1
#define CAMERA_ZOOM 2

// Current camera control setting
int cameraSetting;

// Current viewing angle and scale of the scene
float viewAngleX, viewAngleY, scaleFactor = 1.0;

// Click coordinates
int clickX, clickY;

// Screen size
const int screenWidth = 600;
const int screenHeight = 600;

// Texture data
GLuint texture;

////////////////////////////////////////////////////////////////
//      Function Prototypes
////////////////////////////////////////////////////////////////

GLuint loadTexture(const char * filename);

////////////////////////////////////////////////////////////////
//      Callback and Initialization Functions
////////////////////////////////////////////////////////////////

void callbackMouse(int button, int state, int x, int y)
{
    if (state == GLUT_DOWN)
    {
        // Store clicked coordinates
        clickX = x;
        clickY = y;

        // Set camera mode to rotate
        if (button == GLUT_LEFT_BUTTON)
        {
            cameraSetting = CAMERA_ROTATE;
        } 
        // Set camera mode to zoom
        else if (button == GLUT_RIGHT_BUTTON) 
        {
            cameraSetting = CAMERA_ZOOM;
        }
    }
    // Ignore camera commands if no button is clicked
    else if (state == GLUT_UP)
    {
        cameraSetting = CAMERA_RELEASE;
    }
}

void callbackKeyboard(unsigned char key, int x, int y)
{
    if (key == 'q') {
        exit(0);
    }

}

void callbackMotion(int x, int y)
{
    if (cameraSetting == CAMERA_ROTATE)
    {
        // Camera rotate setting - adjust the viewing angle by the direction of motion
        viewAngleX += (x - clickX) / 5.0;
        viewAngleX = viewAngleX > 180 ? (viewAngleX - 360) : (viewAngleX < - 180 ? (viewAngleX + 360) : viewAngleX);
        clickX = x;

        viewAngleY += (y - clickY) / 5.0;
        viewAngleY = viewAngleY > 180 ? (viewAngleY - 360) : (viewAngleY < - 180 ? (viewAngleY + 360) : viewAngleY);
        clickY = y;
    }
    else if (cameraSetting == CAMERA_ZOOM)
    {
        // Polygonal scale to simulate camera zoom
        float currentScaleFactor = scaleFactor;

        scaleFactor *= (1+ (y - clickY) / 60.0);

        scaleFactor = scaleFactor < 0 ? currentScaleFactor : scaleFactor;

        clickY = y;
    }

    glutPostRedisplay();
}

void callbackDisplay()
{
    // Clear the screen
    glEnable(GL_DEPTH_TEST);
    glClearColor(0, 0, 0, 0);
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    // Set world window
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60, 1, 1, 100);

    // Setup 3D environment
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    gluLookAt(0,0,5,
            0,0,0,
            0,1,0);

    // Rotate and scale 3D environment to current user settings
    glRotatef(viewAngleX, 0, 1, 0);
    glRotatef(viewAngleY, 1, 0, 0);
    glScalef(scaleFactor, scaleFactor, scaleFactor);

    glBegin(GL_QUADS);
        glTexCoord2d(0.0, 0.0); glVertex3f(-30, -5, -30);
        glTexCoord2d(1.0, 0.0); glVertex3f(30, -5, -30);
        glTexCoord2d(1.0, 1.0); glVertex3f(30, -5, 30);
        glTexCoord2d(0.0, 1.0); glVertex3f(-30, -5, 30);
    glEnd();

    // Swap frame buffers
    glutSwapBuffers();
}

void windowInitialization() {

    // Enable textures by default
    glEnable(GL_TEXTURE_2D);

    // Load texture
    texture = loadTexture("checkerboard.raw");

    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

}

////////////////////////////////////////////////////////////////
//      Main
////////////////////////////////////////////////////////////////

int main(int argc, char** argv)
{   
    // GLUT Initialization
    glutInit(&argc, argv); 
    glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH); 
    glutInitWindowSize(screenWidth,screenHeight); 

    // Create main window
    glutCreateWindow("Test"); 
    windowInitialization();

    // Register callback functions
    glutDisplayFunc(callbackDisplay); 
    glutMouseFunc(callbackMouse); 
    glutMotionFunc(callbackMotion);
    glutKeyboardFunc(callbackKeyboard); 

    // Enter event processing loop
    glutMainLoop(); 

}

////////////////////////////////////////////////////////////////
//      Prototyped Functions
////////////////////////////////////////////////////////////////

GLuint loadTexture(const char * filename)
{
    GLuint texture;
    int width, height;
    BYTE * data;
    FILE * file;

    // open texture data
    file = fopen( filename, "rb" );
    if ( file == NULL ) return 0;

    // allocate buffer
    width = 256;
    height = 256;
    data = (BYTE *) malloc( width * height * 3 );

    // read texture data
    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 );

    // texture wraps over at the edges
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, 1 );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, 1);

    // build texture
    gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height,
                       GL_RGB, GL_UNSIGNED_BYTE, data );

    // free buffer
    free( data );

    return texture;
}
#包括“glut-3.7.6-bin\glut.h”
//旋转相机常数
#定义相机版本0
#定义相机旋转1
#定义相机缩放2
//当前摄像机控制设置
内景摄像;
//场景的当前视角和比例
浮动viewAngleX、viewAngleY、scaleFactor=1.0;
//单击坐标
int clickX,clickY;
//屏幕大小
常数int屏幕宽度=600;
常数int屏幕高度=600;
//纹理数据
胶合结构;
////////////////////////////////////////////////////////////////
//功能原型
////////////////////////////////////////////////////////////////
GLuint loadTexture(常量字符*文件名);
////////////////////////////////////////////////////////////////
//回调函数和初始化函数
////////////////////////////////////////////////////////////////
无效回调鼠标(int按钮、int状态、int x、int y)
{
如果(状态==GLUT\U DOWN)
{
//存储单击的坐标
点击x=x;
点击y=y;
//将相机模式设置为旋转
如果(按钮==GLUT\u左按钮)
{
摄像机设置=摄像机旋转;
} 
//将相机模式设置为缩放
else if(按钮==GLUT\u右按钮)
{
cameraSetting=摄影机\缩放;
}
}
//如果未单击任何按钮,则忽略相机命令
else if(state==GLUT\u UP)
{
摄像机设置=摄像机释放;
}
}
无效回调键盘(无符号字符键,整数x,整数y)
{
如果(键=='q'){
出口(0);
}
}
无效回调运动(整数x,整数y)
{
如果(摄影机设置==摄影机旋转)
{
//相机旋转设置-根据运动方向调整视角
viewAngleX+=(x-点击x)/5.0;
viewAngleX=viewAngleX>180?(viewAngleX-360):(viewAngleX<-180?(viewAngleX+360):viewAngleX);
点击x=x;
viewAngleY+=(y-点击)/5.0;
viewAngleY=viewAngleY>180?(viewAngleY-360):(viewAngleY<-180?(viewAngleY+360):viewAngleY);
点击y=y;
}
else if(摄影机设置==摄影机\u缩放)
{
//用于模拟摄影机缩放的多边形比例
float currentScaleFactor=scaleFactor;
scaleFactor*=(1+(y-clickY)/60.0);
scaleFactor=scaleFactor<0?当前scaleFactor:scaleFactor;
点击y=y;
}
再发现();
}
void callbackDisplay()
{
//清除屏幕
glEnable(GLU深度试验);
glClearColor(0,0,0,0);
glClear(GL_颜色_缓冲_位| GL_深度_缓冲_位);
//设置世界窗口
glMatrixMode(GL_投影);
glLoadIdentity();
(60,1,1100);
//设置三维环境
glMatrixMode(GLU模型视图);
glLoadIdentity();
gluLookAt(0,0,5,
0,0,0,
0,1,0);
//旋转三维环境并将其缩放到当前用户设置
glRotatef(视角x,0,1,0);
glRotatef(viewAngleY,1,0,0);
glScalef(scaleFactor,scaleFactor,scaleFactor);
glBegin(GL_QUADS);
glTexCoord2d(0.0,0.0);glVertex3f(-30,-5,-30);
glTexCoord2d(1.0,0.0);glVertex3f(30,-5,-30);
glTexCoord2d(1.0,1.0);glVertex3f(30,-5,30);
glTexCoord2d(0.0,1.0);glVertex3f(-30,-5,30);
格伦德();
//交换帧缓冲区
glutSwapBuffers();
}
void windowInitialization(){
//默认情况下启用纹理
glEnable(GL_纹理_2D);
//加载纹理
纹理=loadTexture(“checkboard.raw”);
glHint(GL\u透视图\u校正\u提示,GL\u最佳);
}
////////////////////////////////////////////////////////////////
//主要
////////////////////////////////////////////////////////////////
int main(int argc,字符**argv)
{   
//GLUT初始化
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_深度);
GLUTINITWindowsSize(屏幕宽度、屏幕高度);
//创建主窗口
测试窗口(“测试”);
windowInitialization();
//寄存器回调函数
glutDisplayFunc(回调显示);
glutMouseFunc(callbackMouse);
glutMotionFunc(回调运动);
glutKeyboardFunc(回拨键盘);
//进入事件处理循环
glutMainLoop();
}
////////////////////////////////////////////////////////////////
//原型函数
////////////////////////////////////////////////////////////////
GLuint loadTexture(常量字符*文件名)
{
胶合结构;
int宽度、高度;
字节*数据;
文件*文件;
//打开纹理数据
file=fopen(文件名为“rb”);
如果(file==NULL)返回0;
//分配缓冲区
宽度=256;
高度=256;
数据=(字节*)malloc(宽*高*3);
//读取纹理数据
fread(数据,宽度*高度*3,1,文件);
fclose(文件);
glGenTextures(1,&纹理);
glBindTexture(GL_TEXTURE_2D,纹理);
glTexEnvf(GL_纹理_环境,GL_纹理_环境模式,GL_调制);
glTexParameterf(GL_纹理2D、GL_纹理MIN过滤器、GL_线性MIPMAP最近);
glTexParameterf(GL_TEXTURE_2D、GL_TEXTURE_MAG_FILTER、GL_LINEAR);
//纹理在边缘包裹
glTexParameterf(GL_纹理2D,GL_纹理包裹S,1);
glTexParameterf(GL_纹理2D,GL_纹理包裹T,1);
//构建纹理
gluBuild2DMipmaps(GL_纹理_2D,3,宽度,高度,
GL_RGB,GL_无符号字节,数据);
//自由缓冲区
免费(数据);
返回纹理;
}

你的代码对我来说很好;我怀疑问题出在用于纹理的源图像中。复核
# Convert an image into raw RGB:
convert checkerboard.png checkerboard.rgb
# Convert raw RGB back to an image (assuming we know the size and color depth):
convert -size 256x256 -depth 8 checkerboard.rgb checkerboard.png