opengl纹理不工作,但我写的一切权利[关闭]

1l5u6lss  于 2023-02-12  发布在  其他
关注(0)|答案(1)|浏览(125)

编辑问题以包含desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将有助于其他人回答问题。
21小时前关门了。
Improve this question
我正在尝试实现一个简单的纹理在一个正方形,但它不工作,我不知道是什么错了
这是我代码
我做了纹理,然后插入图像,当绘制正方形时,它是白色的,没有绘制纹理
图书馆

#include <chrono>
#include <cstdint>
#include <iostream>
#include <windows.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <fstream>
#include <vector>
#include <map>
#include "shapes.h"
#include <string>
#include <math.h>
#include <iomanip>

这是为了以后的视图更改

vect position= {-13.7234, -11.4469,  17.6397},up,cen;
float angle1=0.8,angle2=-0.8 ;
vect getVector(float x,float y,int b=0)
{
    if(b==1)
    {
        x-=3.141592653589/2;
        return vect{cos(x),sin(x),0};
    }
    if(b==2)
        y+=3.141592653589/2;
    return vect{cos(x)*cos(y),sin(x)*cos(y),sin(y)};
}

这里我做了纹理

char tex[256][256][3];
unsigned int texture=1;

void maketex()
{
    for(int i=0; i<256; i++)
    {
        for(int j=0; j<256; j++)
        {
            if((i/16+j/16)%2==0)
            {
                tex[i][j][0]=0x1f;
                tex[i][j][1] = 0x8f;
                tex[i][j][2] = 0x1f;
            }
            else
            {
                tex[i][j][0]=0xaa;
                tex[i][j][1] = 0xaa;
                tex[i][j][2] = 0xaa;
            }
        }
    }
    glGenTextures(1,&texture);

    glTexImage2D(GL_TEXTURE_2D, 0, 3, 256, 256, 0,
                 GL_RGB, GL_UNSIGNED_BYTE, tex);
}

初始化gl

void initGL()
{
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set background color to black and opaque
    glClearDepth(1.0f);                   // Set background depth to farthest
    glEnable(GL_DEPTH_TEST);   // Enable depth testing for z-culling
    glDepthFunc(GL_LEQUAL);    // Set the type of depth-test
    glShadeModel(GL_SMOOTH);   // Enable smooth shading
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);  // Nice perspective corrections
}

所述显示功能

void display2()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear color and depth buffers
    glMatrixMode(GL_MODELVIEW);     // To operate on model-view matrix
    cen=getVector(angle1,angle2,0).add(position);
    up=getVector(angle1,angle2,1);
    glLoadIdentity();
    gluLookAt(position.a,position.b,position.c,cen.a,cen.b,cen.c,0,0,1);
    glEnable(GL_TEXTURE_2D);
    glBegin(GL_QUADS);
    glBindTexture(GL_TEXTURE_2D,texture);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
    glTexCoord2f(0,0);
    glVertex3f(0,0,0);
    glTexCoord2f(1,0);
    glVertex3f(5,0,0);
    glTexCoord2f(1,1);
    glVertex3f(5,5,0);
    glTexCoord2f(0,1);
    glVertex3f(0,5,0);
    glEnd();
    glDisable(GL_TEXTURE_2D);
    glFlush();
    glutSwapBuffers();
}

调整窗口的形状

void reshape(GLsizei width, GLsizei height)    // GLsizei for non-negative integer
{
    // Compute aspect ratio of the new window
    if (height == 0)
        height = 1;                // To prevent divide by 0
    GLfloat aspect = (GLfloat)width / (GLfloat)height;

    // Set the viewport to cover the new window
    glViewport(0, 0, width, height);

    // Set the aspect ratio of the clipping volume to match the viewport
    glMatrixMode(GL_PROJECTION);  // To operate on the Projection matrix
    glLoadIdentity();             // Reset
    // Enable perspective projection with fovy, aspect, zNear and zFar
    gluPerspective(70.0f, aspect, 0.5f, 120.0f);
}

主函数

int main(int argc, char** argv)
{
    glutInit(&argc, argv);            // Initialize GLUT
    glutInitDisplayMode(GLUT_DOUBLE); // Enable double buffered mode
    glutInitWindowSize(640, 480);   // Set the window's initial width & height
    glutInitWindowPosition(50, 50); // Position the window's initial top-left corner
    glutCreateWindow("title");          // Create window with the given title
    glutDisplayFunc(display2);       // Register callback handler for window re-paint event
    glutReshapeFunc(reshape);
    glLineWidth(3.0);
    glMatrixMode(GL_MODELVIEW);
    initGL();
    glutMainLoop();
    return 0;
}
ttisahbt

ttisahbt1#

我忘记使用maktex()函数
现在它工作正常
顺便说一句我也不得不把

glBindTexture(GL_TEXTURE_2D,texture);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);

glbegin()glend()之间

相关问题