void piglit_init(int argc, char **argv) { bool piglit_pass = true; GLuint vs, fs, prog; GLint loc; float vf[] = { 1.0, 2.0, 3.0 }; double vd[] = { 1.0, 2.0, 3.0, 4.0, 5.0}; piglit_require_extension("GL_ARB_gpu_shader_fp64"); vs = piglit_compile_shader_text(GL_VERTEX_SHADER, vs_text); fs = piglit_compile_shader_text(GL_FRAGMENT_SHADER, fs_text); prog = piglit_link_simple_program(vs, fs); glUseProgram(prog); // Setting different type should fail loc = glGetUniformLocation(prog, "d"); glUniform1i(loc, 3); piglit_pass = piglit_pass && piglit_check_gl_error(GL_INVALID_OPERATION); glUniform1f(loc, 3.0); piglit_pass = piglit_pass && piglit_check_gl_error(GL_INVALID_OPERATION); glUniform1d(loc, 3.0); piglit_pass = piglit_pass && piglit_check_gl_error(GL_NO_ERROR); loc = glGetUniformLocation(prog, "v"); glUniform3fv(loc, 1, vf); piglit_pass = piglit_pass && piglit_check_gl_error(GL_INVALID_OPERATION); glUniform3d(loc, vd[0], vd[1], vd[2]); piglit_pass = piglit_pass && piglit_check_gl_error(GL_NO_ERROR); // Setting different size should fail loc = glGetUniformLocation(prog, "v"); glUniform2d(loc, vd[0], vd[1]); piglit_pass = piglit_pass && piglit_check_gl_error(GL_INVALID_OPERATION); glUniform4d(loc, vd[0], vd[1], vd[2], vd[3]); piglit_pass = piglit_pass && piglit_check_gl_error(GL_INVALID_OPERATION); glUniform3d(loc, vd[0], vd[1], vd[2]); piglit_pass = piglit_pass && piglit_check_gl_error(GL_NO_ERROR); // Special case for booleans loc = glGetUniformLocation(prog, "b"); glUniform1d(loc, 1.0); piglit_pass = piglit_pass && piglit_check_gl_error(GL_INVALID_OPERATION); glUniform1f(loc, 1.0); piglit_pass = piglit_pass && piglit_check_gl_error(GL_NO_ERROR); piglit_report_result(piglit_pass ? PIGLIT_PASS : PIGLIT_FAIL); }
void Mandlebrot::render(const int iter, const float aspect_ratio, const double cx, const double cy, const double cur_scale) { glUseProgram(programID); glUniform1i(u_iter, iter); glUniform1f(u_asp, aspect_ratio); #if VERSION == 330 glUniform2f(u_center, float(cx), float(cy)); glUniform1f(u_scale, float(cur_scale)); #else glUniform2d(u_center, cx, cy); glUniform1d(u_scale, cur_scale); #endif glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_1D, TextureID); glUniform1i(TextureLoc, 0); glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &Projection[0][0]); glBindVertexArray(VertexArrayID); glEnableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, (void*)0); glEnableVertexAttribArray(1); glBindBuffer(GL_ARRAY_BUFFER, uvbuffer); glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (void*)0); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glDisableVertexAttribArray(0); glDisableVertexAttribArray(1); }
int main(int argc, char *argv[]) { if(!glfwInit()) { std::cerr << "Failed to init GLFW" << std::endl; return 1; } atexit(glfwTerminate); glfwSetErrorCallback(error_callback); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); window = glfwCreateWindow(w, h, "Mandelbrot", NULL, NULL); if(!window) { std::cerr << "Failed to create window" << std::endl; return 1; } glfwSetKeyCallback(window, key_callback); glfwSetCursorPosCallback(window, cursor_callback); glfwSetMouseButtonCallback(window, mouse_button_callback); glfwSetScrollCallback(window, scroll_callback); glfwSetInputMode(window, GLFW_CURSOR_NORMAL, GLFW_STICKY_KEYS); glfwMakeContextCurrent(window); glewExperimental = GL_TRUE; glewInit(); std::cout << "Renderer: " << glGetString(GL_RENDERER) << std::endl; std::cout << "OpenGL Version: " << glGetString(GL_VERSION) << std::endl; GLuint prog; compile_shader(prog); last_mtime = get_mtime("shader.glsl"); float points[] = { -1.0f, 1.0f, 0.0f, -1.0f, -1.0f, 0.0f, 1.0f, -1.0f, 0.0f, -1.0f, 1.0f, 0.0f, 1.0f, -1.0f, 0.0f, 1.0f, 1.0f, 0.0f, }; GLuint vbo = 0; glGenBuffers (1, &vbo); glBindBuffer (GL_ARRAY_BUFFER, vbo); glBufferData (GL_ARRAY_BUFFER, 2 * 9 * sizeof (float), points, GL_STATIC_DRAW); GLuint vao = 0; glGenVertexArrays (1, &vao); glBindVertexArray (vao); glEnableVertexAttribArray (0); glBindBuffer (GL_ARRAY_BUFFER, vbo); glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, 0, NULL); glUseProgram (prog); last_time = glfwGetTime(); glBindVertexArray (vao); while(!glfwWindowShouldClose(window)) { time_t new_time = get_mtime("shader.glsl"); if(new_time != last_mtime) { glDeleteProgram(prog); compile_shader(prog); glUseProgram(prog); last_mtime = new_time; std::cout << "Reloaded shader: " << last_mtime << std::endl; } glfwGetWindowSize(window, &w, &h); glUniform2d(glGetUniformLocation(prog, "screen_size"), (double)w, (double)h); glUniform1d(glGetUniformLocation(prog, "screen_ratio"), (double)w / (double)h); glUniform2d(glGetUniformLocation(prog, "center"), cx, cy); glUniform1d(glGetUniformLocation(prog, "zoom"), zoom); glUniform1i(glGetUniformLocation(prog, "itr"), itr); glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glDrawArrays (GL_TRIANGLES, 0, 6); glfwSwapBuffers(window); glfwPollEvents(); ticks++; current_time = glfwGetTime(); if(current_time - last_time > 1.0) { fps = ticks; update_window_title(); last_time = glfwGetTime(); ticks = 0; } } glfwDestroyWindow(window); }
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_GL40_nglUniform2d(JNIEnv *__env, jclass clazz, jint location, jdouble x, jdouble y, jlong __functionAddress) { glUniform2dPROC glUniform2d = (glUniform2dPROC)(intptr_t)__functionAddress; UNUSED_PARAMS(__env, clazz) glUniform2d(location, x, y); }
void GLWidget::paintGL() { if (data.currentFile != data.nextFile) { if (data.nextFile == -1) loadShaders(data.filePath); else loadShaders(fileList[data.nextFile]); data.currentFile = data.nextFile; data.zoom = 0; data.moveX = 0; data.moveY = 0; data.pause = false; updateTitle(); } double posX = (data.moveX + 1.) * float(DIM / 2); double posY = (data.moveY - 1.) * float(DIM / 2) * -1.; glUniform1ui(data.loc.dim, DIM); glUniform1f(data.loc.zoom, data.zoom); glUniform2d(data.loc.position, posX, posY); glVertexAttribPointer(data.loc.vertex, 2, GL_FLOAT, GL_FALSE, 0, data.vertex.constData()); if (data.loc.animation != -1 && !data.pause) { glUniform1f((float)data.loc.animation, (double)QTime::currentTime().msecsSinceStartOfDay() / 1000.); update(); } render(); if (!data.saving) return; data.saving = false; QImage *img = saveDialog->img; QImage imgRen(data.save.blockSize, QImage::Format_RGB888); if (img == 0) { saveDialog->failed(tr("img allocation failed")); return; } if (img->isNull()) { saveDialog->failed(tr("img is null")); return; } if (imgRen.isNull()) { saveDialog->failed(tr("imgRen is null")); return; } QOpenGLFramebufferObject fbo(data.save.blockSize); fbo.bind(); glViewport(0, 0, data.save.blockSize.width(), data.save.blockSize.height()); QPoint pos; int w = data.save.blockSize.width() * data.save.blockCount.width(); int h = data.save.blockSize.height() * data.save.blockCount.height(); float asp = (float)h / (float)w; render: // Select region data.projection.setToIdentity(); float left = float(2 * pos.x() - data.save.blockCount.width()) / data.save.blockCount.width(); float top = float(2 * pos.y() - data.save.blockCount.height()) / data.save.blockCount.height(); data.projection.ortho(left, left + 2. / data.save.blockCount.width(), asp * top, asp * (top + 2. / data.save.blockCount.height()), -1., 1.); render(); QPoint imgPos(pos.x() * data.save.blockSize.width(), pos.y() * data.save.blockSize.height()); glReadPixels(0, 0, imgRen.width(), imgRen.height(), GL_RGB, GL_UNSIGNED_BYTE, imgRen.bits()); unsigned char *imgPtr = img->scanLine(img->height() - imgPos.y() - 1) + imgPos.x() * 3; for (int i = 0; i < imgRen.height(); i++) { memcpy(imgPtr, imgRen.constScanLine(i), imgRen.bytesPerLine()); imgPtr -= img->bytesPerLine(); } if (pos.x() != data.save.blockCount.width() - 1) pos.setX(pos.x() + 1); else if (pos.y() != data.save.blockCount.height() - 1) { pos.setX(0); pos.setY(pos.y() + 1); } else { fbo.release(); resizeGL(width(), height()); saveDialog->done(); return; } goto render; }