问题描述
我正在尝试将图像应用于三角形;但是,结果显示三角形显示为白色。我想知道我的代码的颜色是否有可能覆盖纹理。
/*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,uTexture;
GLfloat degrees = glm::radians(-45.0f);
struct TAttributeTuple
{
glm::vec3 v;
glm::vec2 uv;
};
/* 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 ="#version 400 core\n"
// Vertex data from Vertex Attrib Pointer 0
"layout(location = 0) in vec3 position;"
// Color data from Vertex Attrib Pointer 1
"layout(location = 1) in vec3 color;"
"layout(location = 2 in vec2 vertex_texcoord;"
//variable to transfer color data to the fragment shader
"out vec3 mobileColor;"
"out vec3 modelPosition"
"out vec2 vs_texcoord;"
//Global variables for the transform matrices
// "uniform mat4 model;"
//
// "uniform mat4 view;"
//
// "uniform mat4 projection;"
"void main()\n"
"{\n"
// transforms vertices to clip coordinates
"modelPosition = position;"
"vs_texcoord = vec2(vertex_texcoord.x,vertex_texcoord.y * 1.0f;"
// references incoming color data
"mobileColor = color;"
"gl_Position = vec4(position,1.0f);"
"}\n";
/* Fragment Shader Source Code*/
const GLchar * fragmentShaderSource ="#version 400 core\n"
"in vec3 mobileColor;" // Variable to hold incoming color data from vertex shader
"in vec2 vs_texcoord;"
"out vec4 gpuColor;" // Variable to pass color data to the GPU
"uniform sampler2D uTexture"
"void main()\n"
"{\n"
"gpuColor = texture(uTexture,vs_texcoord) * vec4(mobileColor,1.0);"
"}\n";
/* Fragment Shader Source Code */
//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(1.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,WindowWidth,WindowHeight);
}
/* Renders graphics */
void URenderGraphics(void)
{
glEnable(GL_TEXTURE_2D);
glDisable(GL_BLEND);
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)); // Place the object at the center of the 7i,p9rA
model = glm::rotate(model,degrees,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,-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,glm::value_ptr(view));
glUniformMatrix4fv(projLoc,glm::value_ptr(projection));
glutPostRedisplay();
glUniform1i(glGetUniformLocation(ShaderProgram,"uTexture"),0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D,uTexture);
// Draws the triangles
glDrawArrays(GL_TRIANGLES,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,&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,&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()
{
glm::vec3 top( 0.5f,0.5f,0.5f );
glm::vec3 p01( -0.1f,1.0f );
glm::vec3 p11( 1.1f,1.0f );
glm::vec3 p00( 0.0f,0.0f );
glm::vec3 p10( 1.0f,0.0f );
//position and color data
TAttributeTuple vertices[]
{
{ p00,glm::vec2(0.0f,0.0f) },{ p10,glm::vec2(1.0f,{ p11,1.0f) },{ p00,{ p01,{ top,glm::vec2(0.5f,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);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0);
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,5 * sizeof(GLfloat),(GLvoid*)0);
glEnableVertexAttribArray(0); // Enables vertex attribute
// Set attribute pointer 2 to hold Color data
glVertexAttribPointer(2,2,(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,&uTexture);
glBindTexture(GL_TEXTURE_2D,uTexture);
int width = 0,height = 0 ;
unsigned char* image = SOIL_load_image("./image/bricks.png",&width,&height,NULL,SOIL_LOAD_RGB);
if(image)
{
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D,GL_RGB,width,height,GL_UNSIGNED_BYTE,image);
glGenerateMipmap(GL_TEXTURE_2D);
}
else{
std::cout << "FAIL TO LOAD IMAGE" << "\n";
}
glActiveTexture(0);
SOIL_free_image_data(image);
glBindTexture(GL_TEXTURE_2D,0);
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)