Reputation: 71
I am trying to show a texture here on opengl 2.1 using glsl 120 in linux.But it shows a error.my main.cpp
#include "GL/glew.h"
#include "GL/glu.h"
#include "GLFW/glfw3.h"
#include "stb_image/stb_image.h"
#include <iostream>
unsigned char* texture;
unsigned int render_ID;
const char* vertexcode = {
"#version 120\n"
"\n"
"attribute vec3 coord;\n"
"attribute vec2 texCoord;\n"
"\n"
"varying vec2 UV;\n"
"\n"
"void main(){\n"
"gl_Position = vec4(coord.x, coord.y, coord.z, 1.0);\n"
"UV = texCoord;\n"
"}\n"
};
const char* fragmentcode = {
"#version 120\n"
"uniform sampler2D tex;\n"
"varying vec2 UV;\n"
"void main(){\n"
"gl_FragColor.rgb = texture2D(tex, UV).rgb;\n"
"gl_FragColor.a = 1.0;\n"
"}"
};
float quadCoords[] = {
-0.5f, -0.5f,
0.5f, -0.5f,
0.5f, 0.5f,
0.5f, 0.5f,
-0.5f, 0.5f,
-0.5f, -0.5f,};
const float texCoords[] = {
0.0f, 0.0f,
1.0f, 0.0f,
1.0f, 1.0f,
1.0f, 1.0f,
0.0f, 1.0f,
0.0f, 0.0f};
unsigned int Texture()
{
texture = stbi_load("cubes.png",0,0,0,4);
glActiveTexture(GL_TEXTURE0);
glGenTextures(1,&render_ID);
glBindTexture(GL_TEXTURE_2D,render_ID);
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0,0, 0, GL_RGBA, GL_UNSIGNED_BYTE,texture);
}
int main()
{
glfwInit();
GLFWwindow* window = glfwCreateWindow(900,600,"ada",NULL,NULL);
if (window == NULL)
{
std::cout << "no glfw" << std::endl;
glfwTerminate();
}
glfwMakeContextCurrent(window);
unsigned int buffer;
unsigned int texture_buffer;
unsigned int shader = glCreateProgram();
unsigned int vs = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vs,1,&vertexcode,nullptr);
glCompileShader(vs);
unsigned int fs = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fs,1,&fragmentcode,nullptr);
glCompileShader(fs);
glAttachShader(shader,vs);
glAttachShader(shader,fs);
glLinkProgram(shader);
glGenBuffers(1,&buffer);
glBindBuffer(GL_ARRAY_BUFFER,buffer);
glBufferData(GL_ARRAY_BUFFER,sizeof(quadCoords) * sizeof(float),quadCoords,GL_STATIC_DRAW);
glGenBuffers(1,&texture_buffer);
glBindBuffer(GL_ARRAY_BUFFER,texture_buffer);
glBufferData(GL_ARRAY_BUFFER,sizeof(texCoords) * sizeof(float),texCoords,GL_STATIC_DRAW);
glUseProgram(shader);
Texture();
while (!glfwWindowShouldClose(window))
{
glClear(GL_COLOR_BUFFER_BIT);
glfwPollEvents();
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER,buffer);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3* sizeof(GLfloat), nullptr);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER,texture_buffer);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE,2 * sizeof(GLfloat), nullptr);
glDrawArrays(GL_TRIANGLES,0,6);
glfwSwapBuffers(window);
}
glfwTerminate();
return 0;
}
that stb_image.h header is just a library which lets us read data from the picture:
but rather than that the window opens and then just instantly closes:
fish: Job 1, './raw' terminated by signal SIGSEGV (Address boundary error)
i am also using fish shell.My compiler settings:
g++ main.cpp stb_image/stb_image.cpp -o raw -lGL -lX11 -lglfw -lGLU -lGLEW
Upvotes: 1
Views: 138
Reputation: 211166
stbi_load
returns the size of the texture in the output parameters(because of this, these arguments are pointers). You need to get the size and use it with glTexImage2D
:
int cx, cy, ch;
texture = stbi_load("cubes.png", &cx, &cy, &ch, 4);
// [...]
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, cx, cy, 0, GL_RGBA, GL_UNSIGNED_BYTE,texture);
Upvotes: 1