Hi, this code gives me blackscreen, what is wrong? i want to use pallet to choose color from instead of an iter /100 or etc!
updated code:
#version 330 core
out vec4 pixelColor;
in vec2 coord;
uniform sampler1D tex;
void main()
{
float real = coord.x;
float imag = coord.y;
float Creal = real;
float Cimag = imag;
int MaxIterations = 1000;
float r2 = 0.0;
float iter;
for (iter = 0.0; iter < MaxIterations && r2 < 4.0; ++iter)
{
float tempreal = real;
real = (tempreal * tempreal) - (imag * imag) + Creal;
imag = 2.0 * tempreal * imag + Cimag;
r2 = (real * real) + (imag * imag);
}
vec4 color;
highp int iterint = int(iter);
color = texture1D(tex, (iter == MaxIterations ? 0.0 : iter) / 100.0);
pixelColor = vec4(color.xyz, 1.0);
}
but i get blackscreen
main.cpp:
// GLEW
#define GLEW_STATIC
#include <GL/glew.h>
// GLFW
#include <GLFW/glfw3.h>
// GL includes
#include "Shader.h"
// GLM Mathemtics
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
// Other Libs
#include <SOIL/SOIL.h>
// Function prototypes
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);
// The MAIN function, from here we start our application and run our Program/Game loop
int main()
{
// Init GLFW
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", nullptr, nullptr); // Windowed
glfwMakeContextCurrent(window);
// Set the required callback functions
glfwSetKeyCallback(window, key_callback);
// Initialize GLEW to setup the OpenGL Function pointers
glewExperimental = GL_TRUE;
glewInit();
// Define the viewport dimensions
glViewport(0, 0, 800, 600);
// Build and compile our shader program
Shader shader("vertex.vs", "fragment.frag");
// Set up our vertex data (and buffer(s)) and attribute pointers
//GLfloat vertices[] = {
// // First triangle
// 1.0f, 1.0f, // Top Right
// 1.0f, -1.0f, // Bottom Right
// -1.0f, 1.0f, // Top Left
// // Second triangle
// 1.0f, -1.0f, // Bottom Right
// -1.0f, -1.0f, // Bottom Left
// -1.0f, 1.0f // Top Left
//};
GLfloat vertices[] = {
1.0f, 1.0f, // Top Right
1.0f, -1.0f, // Bottom Right
-1.0f, -1.0f, // Bottom Left
-1.0f, 1.0f // Top Left
};
GLuint indices[] = { // Note that we start from 0!
0, 1, 3, // First Triangle
1, 2, 3 // Second Triangle
};
GLuint VBO, VAO, EBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
// Bind our Vertex Array Object first, then bind and set our buffers and pointers.
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0);
glEnableVertexAttribArray(0);
glBindVertexArray(0); // Unbind VAO (it's always a good thing to unbind any buffer to prevent strange bugs), remember: do NOT unbind the EBO, keep it bound to this VAO
// Uncommenting this call will result in wireframe polygons.
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_1D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
int width, height;
unsigned char* image = SOIL_load_image("pal.ppm",
&width, &height, 0, SOIL_LOAD_RGB);
glTexImage1D(GL_TEXTURE_1D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
glGenerateMipmap(GL_TEXTURE_1D);
SOIL_free_image_data(image);
glBindTexture(GL_TEXTURE_2D, 0);
// Game loop
while(!glfwWindowShouldClose(window))
{
// Check and call events
glfwPollEvents();
// Render
// Clear the colorbuffer
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// Draw our first triangle
shader.Use();
glBindTexture(GL_TEXTURE_1D, texture);
glBindVertexArray(VAO);
//glDrawArrays(GL_TRIANGLES, 0, 6);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
// Swap the buffers
glfwSwapBuffers(window);
}
glfwTerminate();
return 0;
}
// Is called whenever a key is pressed/released via GLFW
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
{
cout << key << endl;
if(key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
}
bump does anybody know whats wrong with this? i only have problem with getting the 1d texture . other than that code works fine with iter / 100.0
glTexImage1D() doesn’t have a [var]height[/var] parameter. In fact, it only takes 8 parameters, but you’ve given 9. The compiler should have complained about that.
There’s not much point in generating a mipmap if you’re using GL_NEAREST.
omg thanks ! how can i be so stupid + the bitmap was there from previous app