簡體   English   中英

現代opengl紋理金字塔

[英]modern opengl textured pyramid

我認為在繪制紋理金字塔時遺漏了一些東西。 我無法讓它正常工作,因為我不擅長 3d 現代 OpenGL。 不知何故,我設法畫了一個立方體,但金字塔似乎出錯了。 我希望四個邊有 0.5(上)、0.0(左)、1,0(右)。 底部為 0,1(左上角)、1,1(右上角)、0,0(左下角)和 1,0(右下角)。 我需要這些坐標,因為我只用我的坐標得到不完整的圖像。 任何幫助,將不勝感激。

這是我的代碼,其中包含我之前創建並運行良好的立方體的頂點。

/*Header Inclusions*/
#include <iostream>
#include <GL/Glew.h>
#include <GL/freeglut.h>

// GLM Math inclusions
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include<glm/gtc/type_ptr.hpp>

//include soil
#include "SOIL2/SOIL2.h"

using namespace std; // Uses the standard namespace

#define WINDOW_TITLE "Modern OpenGL" // Macro for window title

//Vertex and fragment shader
#ifndef GLSL
#define GLSL(Version, source) "#version " #Version "\n" #source
#endif


// Variables for window width and height
GLint ShaderProgram, WindowWidth = 800, WindowHeight = 600;
GLuint VBO, VAO, texture;
GLfloat degrees = glm::radians(-45.0f);


/* User-defined Function prototypes to:*/
void UResizeWindow(int,int);
void URenderGraphics(void);
void UCreateShader(void);
void UCreateBuffers(void);
void UGenerateTexture(void);

/*Vertex shader source code*/
const GLchar * vertexShaderSource = GLSL(330,
layout(location = 0) in vec3 position;
layout(location = 2) in vec2 textureCoordinate;

out vec2 mobileTextureCoordinate; //declare a vec 4 variable

//Global variables for the transform matrices
    uniform mat4 model;
    uniform mat4 view;
    uniform mat4 projection;

     void main(){
            gl_Position = projection * view * model * vec4(position, 1.0f);//transform vertices
                mobileTextureCoordinate = vec2(textureCoordinate.x, 1.0f - textureCoordinate.y);
            }
    );

/*Fragment shader program source code*/
const GLchar * fragmentShaderSource = GLSL(330,
in vec2 mobileTextureCoordinate;

out vec4 gpuTexture;//out vertex_Color;

uniform sampler2D uTexture;

    void main(){

      gpuTexture = texture(uTexture, mobileTextureCoordinate);

    }
);

//main program
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(WindowWidth, WindowHeight);
glutCreateWindow(WINDOW_TITLE);

glutReshapeFunc(UResizeWindow);


glewExperimental = GL_TRUE;
    if (glewInit()!= GLEW_OK)
    {
    std::cout << "Failed to initialize GLEW" << std::endl;
    return -1;
    }

    UCreateShader();

    UCreateBuffers();

    UGenerateTexture();

    // Use the Shader program
        glUseProgram(ShaderProgram);


        glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set background color

        glutDisplayFunc(URenderGraphics);

        glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_CONTINUE_EXECUTION);

        glutMainLoop();

        // Destroys Buffer objects once used
        glDeleteVertexArrays(1, &VAO);
        glDeleteBuffers(1, &VBO);

        return 0;

    }

    /* Resizes the window*/
    void UResizeWindow(int w, int h)
    {
    WindowWidth = w;
        WindowHeight = h;
        glViewport(0, 0, WindowWidth, WindowHeight);
     }


     /* Renders graphics */
    void URenderGraphics(void)
    {

        glEnable(GL_DEPTH_TEST); // Enable z-depth

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clears the screen

        glBindVertexArray(VAO); // Activate the Vertex Array Object before rendering and transforming them

        // Transforms the object
        glm::mat4 model;
        model = glm::translate(model, glm::vec3(0.0, 0.0f, 0.0f)); // Place the object at the center of the 7i,p9rA
        model = glm::rotate(model, degrees, glm::vec3(0.0, 1.0f, 0.0f)); // Rotate the object 45 degrees on the XYZ
        model = glm::scale(model, glm::vec3(2.0f, 2.0f, 2.0f)); // Increase the object size by a scale of 2

        // Transforms the camera
        glm::mat4 view;
        view = glm::translate(view, glm::vec3(0.0f,0.0f,-5.0f)); //Moves the world 0.5 units on X and -5 units in Z

        // Creates a perspective projection
        glm::mat4 projection;
        projection = glm::perspective(45.0f, (GLfloat)WindowWidth / (GLfloat)WindowHeight, 0.1f, 100.0f);

        // Retrieves and passes transform matrices to the Shader program
        GLint modelLoc = glGetUniformLocation(ShaderProgram, "model");
        GLint viewLoc = glGetUniformLocation(ShaderProgram, "view");
        GLint projLoc = glGetUniformLocation(ShaderProgram, "projection");

        glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
        glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
        glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(projection));

        glutPostRedisplay();

        glBindTexture(GL_TEXTURE_2D, texture);

    // Draws the triangles
        glDrawArrays(GL_TRIANGLES,0, 36);

        glBindVertexArray(0); // Deactivate the Vertex Array Object

            glutSwapBuffers(); // Flips the the back buffer with the front buffer every frame. Similar to GL FLush

         }

         /*Creates the Shader program*/
         void UCreateShader()
         {

            // Vertex shader
            GLint vertexShader = glCreateShader(GL_VERTEX_SHADER); // Creates the Vertex Shader
            glShaderSource(vertexShader, 1, &vertexShaderSource, NULL); // Attaches the Vertex Shader to the source code
            glCompileShader(vertexShader); // Compiles the Vertex Shader

            // Fragment Shader
            GLint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); // Creates the Fragment Shader
            glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);// Attaches the Fragment Shader to the source code
            glCompileShader(fragmentShader); // Compiles the Fragment Shader

            // Shader program
            ShaderProgram = glCreateProgram(); // Creates the Shader program and returns an id
            glAttachShader(ShaderProgram, vertexShader); // Attach Vertex Shader to the Shader program
            glAttachShader(ShaderProgram, fragmentShader);; // Attach Fragment Shader to the Shader program
            glLinkProgram(ShaderProgram); //Link Vertex and Fragment shader, to Shader program

            // Delete the Vertex and Fragment shaders once linked
            glDeleteShader(vertexShader);
            glDeleteShader(fragmentShader);

         }


        /*creates the buffer and array object*/
         void UCreateBuffers()
         {

             //position and color data
             GLfloat vertices[] = {

                     -0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
                      0.5f, -0.5f, -0.5f, 1.0f, 0.0f,
                      0.5f,  0.5f, -0.5f, 1.0f, 1.0f,
                      0.5f,  0.5f, -0.5f, 1.0f, 1.0f,
                     -0.5f,  0.5f, -0.5f, 0.0f, 1.0f,
                     -0.5f, -0.5f, -0.5f, 0.0f, 0.0f,

                     -0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
                      0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
                      0.5f,  0.5f, 0.5f, 1.0f, 1.0f,
                      0.5f,  0.5f, 0.5f, 1.0f, 1.0f,
                     -0.5f,  0.5f, 0.5f, 0.0f, 1.0f,
                     -0.5f, -0.5f, 0.5f, 0.0f, 0.0f,


                      -0.5f, 0.5f,  0.5f, 1.0f, 0.0f,
                      -0.5f, 0.5f,  -0.5f, 1.0f, 1.0f,
                      -0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
                      -0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
                      -0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
                      -0.5f,  0.5f, 0.5f, 1.0f, 0.0f,

                      0.5f,  0.5f, 0.5f, 1.0f, 0.0f,
                      0.5f,  0.5f, -0.5f, 1.0f, 1.0f,
                      0.5f,  -0.5f, -0.5f, 0.0f, 1.0f,
                      0.5f,  -0.5f, -0.5f, 0.0f, 1.0f,
                      0.5f,  -0.5f, 0.5f, 0.0f, 0.0f,
                      0.5f,  0.5f, 0.5f, 1.0f, 0.0f,

                     -0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
                      0.5f, -0.5f, -0.5f, 1.0f, 1.0f,
                      0.5f,  -0.5f, 0.5f, 1.0f, 0.0f,
                      0.5f,  -0.5f, 0.5f, 1.0f, 0.0f,
                     -0.5f,  -0.5f, 0.5f, 0.0f, 0.0f,
                     -0.5f, -0.5f, -0.5f, 0.0f, 1.0f,

                     -0.5f,  0.5f, -0.5f, 0.0f, 1.0f,
                      0.5f,  0.5f, -0.5f, 1.0f, 1.0f,
                      0.5f,  0.5f,  0.5f, 1.0f, 0.0f,
                      0.5f,  0.5f,  0.5f, 1.0f, 0.0f,
                     -0.5f,  0.5f,  0.5f, 0.0f, 0.0f,
                     -0.5f,  0.5f, -0.5f, 0.0f, 1.0f


             };

             //Generate buffer id,
                glGenVertexArrays(1, &VAO);
                glGenBuffers(1,&VBO);


             // Activate the Vertex Array Object before binding and setting any VB0s and Vertex Attribute Pointers.
                glBindVertexArray(VAO);

              // Activate the VBO
             glBindBuffer(GL_ARRAY_BUFFER, VBO);
             glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); //Copy vertices to VBO

             // Set attribute pointer 0 to hold Position data
             glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)0);
             glEnableVertexAttribArray(0); // Enables vertex attribute

             // Set attribute pointer 2 to hold Color data
              glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
              glEnableVertexAttribArray(2); // Enables vertex attribute

             glBindVertexArray(0); // Deactivates the VAC, which is good practice

             }
         /*Generate and load the texture*/
         void UGenerateTexture(){

         glGenTextures(1, &texture);
         glBindTexture(GL_TEXTURE_2D, texture);

         int width,height;

         unsigned char* image = SOIL_load_image("snhu.jpg", &width, &height, 0, SOIL_LOAD_RGB);

         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
         glGenerateMipmap(GL_TEXTURE_2D);
         SOIL_free_image_data(image);
         glBindTexture(GL_TEXTURE_2D, 0);


         }

我建議為屬性元組定義一個結構類型:

struct TAttributeTuple
{
    glm::vec3 v;
    glm::vec2 uv;
};

定義金字塔的 5 個角點:
(你對這些點的解釋有點不清楚,所以我不確定這些點是否符合你的規范。可能你已經修改了它們。)

glm::vec3 top(  0.5f, 0.5f, 0.5f );
glm::vec3 p01( -0.1f, 0.0f, 1.0f );
glm::vec3 p11(  1.1f, 0.0f, 1.0f );
glm::vec3 p00(  0.0f, 0.0f, 0.0f );
glm::vec3 p10(  1.0f, 0.0f, 0.0f );

設置頂點屬性數組:

TAttributeTuple vertices[]
{
    { p00, glm::vec2(0.0f, 0.0f) },
    { p10, glm::vec2(1.0f, 0.0f) },
    { p11, glm::vec2(1.0f, 1.0f) },
    { p00, glm::vec2(0.0f, 0.0f) },
    { p11, glm::vec2(1.0f, 1.0f) },
    { p01, glm::vec2(0.0f, 1.0f) },

    { p00, glm::vec2(0.0f, 0.0f) },
    { p10, glm::vec2(1.0f, 0.0f) },
    { top, glm::vec2(0.5f, 1.0f) },

    { p10, glm::vec2(0.0f, 0.0f) },
    { p11, glm::vec2(1.0f, 0.0f) },
    { top, glm::vec2(0.5f, 1.0f) },

    { p11, glm::vec2(0.0f, 0.0f) },
    { p01, glm::vec2(1.0f, 0.0f) },
    { top, glm::vec2(0.5f, 1.0f) },

    { p01, glm::vec2(0.0f, 0.0f) },
    { p00, glm::vec2(1.0f, 0.0f) },
    { top, glm::vec2(0.5f, 1.0f) }
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM