I am able to create a window and clear it to the desired color. But not able to draw a square in the lower left hand corner.
#include <iostream>
#define GLEW_STATIC
#include <GL/glew.h>
#include <GLFW/glfw3.h>
const GLint WIDTH = 720;
const GLint HEIGHT = 480;
int main()
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // Attempts to set to opengl 3.3
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);
GLFWwindow *window = glfwCreateWindow(WIDTH, HEIGHT, "Learn OpenGL", nullptr, nullptr);
int screenWidth, screenHeight;
glfwGetFramebufferSize(window, &screenWidth, &screenHeight);
if (nullptr == window)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return EXIT_FAILURE;
}
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;
if (GLEW_OK != glewInit())
{
std::cout << "Failed to initialise GLEW" << std::endl;
return EXIT_FAILURE;
}
glViewport(0, 0, screenWidth, screenHeight);
while (!glfwWindowShouldClose(window))
{
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0f, 0.0f, 1.0f);
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
glBegin(GL_POLYGON);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.5, 0.0, 0.0);
glVertex3f(0.5, 0.5, 0.0);
glVertex3f(0.0, 0.5, 0.0);
glEnd();
glFlush();
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return EXIT_SUCCESS;
}
Deprecated fixed-function functionality (glBegin() et al & the matrix stacks) doesn't work in a Core context (GLFW_OPENGL_CORE_PROFILE).
Switch to a Compatibility context:
#include <iostream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
const GLint WIDTH = 720;
const GLint HEIGHT = 480;
int main()
{
glfwInit();
glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);
GLFWwindow *window = glfwCreateWindow(WIDTH, HEIGHT, "Learn OpenGL", nullptr, nullptr);
int screenWidth, screenHeight;
glfwGetFramebufferSize(window, &screenWidth, &screenHeight);
if (nullptr == window)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return EXIT_FAILURE;
}
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;
if (GLEW_OK != glewInit())
{
std::cout << "Failed to initialise GLEW" << std::endl;
return EXIT_FAILURE;
}
glViewport(0, 0, screenWidth, screenHeight);
while (!glfwWindowShouldClose(window))
{
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glColor3f(0.0f, 0.0f, 1.0f);
glBegin(GL_POLYGON);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.5, 0.0, 0.0);
glVertex3f(0.5, 0.5, 0.0);
glVertex3f(0.0, 0.5, 0.0);
glEnd();
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return EXIT_SUCCESS;
}
Or supply some shaders & use a VAO & VBO to upload your geometry.
Related
I am trying to get started with openGL and GLFW in c++. My expectation is that the following code will simply render a square on the screen, but I just get a black screen.
The actual setup is working though, because if I add glClearColor(0.1f, 0.4f, 0.8f, 1.0f);, I do indeed get a blue screen. But what is wrong with my use of GL_QUADS?
#include <iostream>
// GLEW
#define GLEW_STATIC
#include <GL/glew.h>
//GLFW
#include <GLFW/glfw3.h>
const GLint WIDTH = 800, HEIGHT = 600;
int main() {
glfwInit();
glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint( GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint( GLFW_RESIZABLE, GL_TRUE);
GLFWwindow *window = glfwCreateWindow(WIDTH, HEIGHT, "Isosurface Stuffing", nullptr, nullptr);
int screenWidth, screenHeight;
glfwGetFramebufferSize(window, &screenWidth, &screenHeight);
if (window == nullptr) {
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;
if (GLEW_OK != glewInit()) {
std::cout << "Failed to create GLEW" << std::endl;
return -1;
}
while (!glfwWindowShouldClose(window)) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glViewport(0, 0, screenWidth, screenHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0f, screenWidth, 0.0f, screenHeight, 0.0f, 1.0f);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
glfwPollEvents();
glColor3f(0.5f, 0.5f, 0.5f);
glBegin(GL_QUADS);
glVertex2f(100, 100);
glVertex2f(200, 100);
glVertex2f(200, 200);
glVertex2f(100, 200);
glEnd();
glfwSwapBuffers(window);
}
glfwTerminate();
return 0;
}
You cannot use the legacy fixed-function pipeline instructions (glBegin, glEnd, glVertex2f, glMatrixMode, glLoadIdentity, glOrtho) in a Core profile OpenGL context (GLFW_OPENGL_CORE_PROFILE). You have to use Compatibility profile OpenGL Context:
glfwWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);
If your system does not allow you to create a compatibility profile context (e.g. Mac), you must use either an OpenGL 2.0 context or a modern OpenGL with Shaders and Vertex Array Objects. A nice tutorial for that is LearnOpenGL
I wrote the following code to display two images on two different monitors using GLFW. When I try running OPENGL_CORE_PROFILE it screws up and my image doesn't appear.
Instead the screen just stays black. But when I don't use core profile the image shows up on the screen just fine. I need to use core profile so I can use OpenGL 3.3 and 330 shaders.
Is my current method I'm using to texture a quad is wrong what would be the best approach to display and image using OpenGL 3.3?
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <iostream>
static GLuint tex_lite;
static GLuint tex_dark;
static GLuint tex_front;
static GLuint tex_back;
float ratio;
int width, height;
char *textFileRead(char *fn) {
FILE *fp;
char *content = NULL;
int count=0;
if (fn != NULL) {
fp = fopen(fn,"rt");
if (fp != NULL) {
fseek(fp, 0, SEEK_END);
count = ftell(fp);
rewind(fp);
if (count > 0) {
content = (char *)malloc(sizeof(char) * (count+1));
count = fread(content,sizeof(char),count,fp);
content[count] = '\0';
}
fclose(fp);
}
}
return content;
}
void printLog(GLuint obj)
{
int infologLength = 0;
int maxLength;
if(glIsShader(obj))
glGetShaderiv(obj,GL_INFO_LOG_LENGTH,&maxLength);
else
glGetProgramiv(obj,GL_INFO_LOG_LENGTH,&maxLength);
char infoLog[maxLength];
if (glIsShader(obj))
glGetShaderInfoLog(obj, maxLength, &infologLength, infoLog);
else
glGetProgramInfoLog(obj, maxLength, &infologLength, infoLog);
if (infologLength > 0)
printf("%s\n",infoLog);
}
void image2texture(cv::Mat Image,GLuint &texName, int i)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
glEnable(GL_DEPTH_TEST);
glActiveTexture(GL_TEXTURE0+i);
glGenTextures(1, &texName);
glBindTexture(GL_TEXTURE_2D, texName);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, 3, Image.cols, Image.rows, 0, GL_BGR,
GL_UNSIGNED_BYTE, Image.data);
}
static void error_callback(int error, const char* description)
{
fputs(description, stderr);
}
GLuint linkShader(char* vert, char* frag)
{
GLuint v,f,p;
char *vs = NULL,*fs = NULL;
v = glCreateShader(GL_VERTEX_SHADER);
f = glCreateShader(GL_FRAGMENT_SHADER);
vs = textFileRead(vert);
fs = textFileRead(frag);
const char * ff = fs;
const char * vv = vs;
glShaderSource(v, 1, &vv,NULL);
glShaderSource(f, 1, &ff,NULL);
free(vs);free(fs);
glCompileShader(v);
glCompileShader(f);
p = glCreateProgram();
glAttachShader(p,f);
glAttachShader(p,v);
glLinkProgram(p);
return p;
}
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
}
void checkWindow(GLFWwindow* window)
{
if (!window)
{
glfwTerminate();
exit(EXIT_FAILURE);
}
}
int main(void)
{
GLFWwindow* window_back;
GLFWwindow* window_front;
glfwSetErrorCallback(error_callback);
if ( !glfwInit() )
{
exit(EXIT_FAILURE);
}
cv::Mat image_lite = cv::imread("lena.tiff");
cv::Mat image_dark = cv::imread("lena.tiff");
cv::flip(image_lite, image_lite, 0);
cv::flip(image_dark, image_dark, 0);
int count;
GLFWmonitor** monitors = glfwGetMonitors(&count);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
if (count >= 2)
{
window_front = glfwCreateWindow(1680,1050,"Front Screen",monitors[0],NULL);
window_back = glfwCreateWindow(1280,800,"Back Screen",monitors[1],NULL);
glewInit();
}
else
{
exit(EXIT_FAILURE);
}
checkWindow(window_front);
checkWindow(window_back);
glfwMakeContextCurrent(window_front);
glfwSetKeyCallback(window_front, key_callback);
glewExperimental = GL_TRUE;
GLenum err = glewInit();
if(err!=GLEW_OK)
{
std::cout<<"glewInit failed, aborting."<<std::endl;
}
std::cout << glGetString(GL_RENDERER) << std::endl;
std::cout << glGetString(GL_VENDOR) << std::endl;
std::cout << glGetString(GL_VERSION) << std::endl;
std::cout << glGetString(GL_SHADING_LANGUAGE_VERSION) << std::endl;
while (!glfwWindowShouldClose(window_front) && !glfwWindowShouldClose(window_back))
{
glfwMakeContextCurrent(window_front);
glfwSetKeyCallback(window_front, key_callback);
glfwGetFramebufferSize(window_front, &width, &height);
ratio = width / (float) height;
glViewport(0,0,width,height);
image2texture(image_lite, tex_lite,0);
// GLuint prg = linkShader("toon.vert","toon.frag");
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-ratio,ratio,-1.f,1.f,-1.f,1.f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glActiveTexture(GL_TEXTURE0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glBindTexture(GL_TEXTURE_2D, tex_lite);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex3f(-1.0,-1.0,0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(-1.0,1.0,0.0);
glTexCoord2f(1.0, 1.0); glVertex3f(1.0,1.0,0.0);
glTexCoord2f(1.0, 0.0); glVertex3f(1.0,-1.0,0.0);
glEnd();
glFlush();
glfwSwapBuffers(window_front);
glfwPollEvents();
glfwMakeContextCurrent(window_back);
glfwSetKeyCallback(window_back, key_callback);
glfwGetFramebufferSize(window_back, &width, &height);
ratio = width / (float) height;
glViewport(0,0,width,height);
image2texture(image_dark, tex_dark,1);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-ratio,ratio,-1.f,1.f,-1.f,1.f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glActiveTexture(GL_TEXTURE0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glBindTexture(GL_TEXTURE_2D, tex_lite);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex3f(-1.0,-1.0,0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(-1.0,1.0,0.0);
glTexCoord2f(1.0, 1.0); glVertex3f(1.0,1.0,0.0);
glTexCoord2f(1.0, 0.0); glVertex3f(1.0,-1.0,0.0);
glEnd();
glFlush();
glfwSwapBuffers(window_back);
glfwPollEvents();
}
glfwMakeContextCurrent(window_front);
glfwDestroyWindow(window_front);
glfwMakeContextCurrent(window_back);
glfwDestroyWindow(window_back);
glfwTerminate();
exit(EXIT_SUCCESS);
}
Your code is totally invalid in a core profile. In core profiles, all the old functionality which has been declared "deprecated" many years ago is actrually removed. GL_QUADS primitive types are not supported, immediate mode rendering with glBegin()/glEnd() is not supported, glEnable(GL_TEXTURE_2D); is not supported, builtin vertex attributes like the texcoord and vertex position are not supported and GL_TEXTURE_ENV_MODE is not supported, rendering without shaders ("fixed-function pipeline") in not supported. The same goes for glMatrixMode(), glLoadIdentity, glOrtho() and all other matrix and matrix stack related functions. The internal format "3" is also not supported for glTexImage2D(), you should use GL_RGB (which you should have used before, too).
When you want to use a modern core profile, you have to use the programmable pipeline with shaders and without all the builtin attributes. You have vo define your own generic attributes instead. You have to use VBOs for your geometry and VAOs for the vertex pointers instead of immediate mode. You also have to replace quad primitves by triangle-based primitives. You will have to use your own matrix functions, or use some library for that.
You might want to have a look at tutorials like arcsynthesis or open.gl which exclusively deal with modern OpenGL.
Why in this program four points drawn by glVertex3f rendered in pointed positions, but instead of four array points rendered only one in the center of the window?
#include <GL/glew.h>
#include <GL/glut.h>
#include <iostream>
#include <vector>
using namespace std;
struct point3 {
GLfloat x, y, z;
};
vector<point3> Vertices(4);
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
GLuint abuffer;
glGenVertexArrays(1, &abuffer);
glBindVertexArray(abuffer);
GLuint buffer;
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
Vertices = {{0.3f, 0.3f, 0.5f}, {0.6f, 0.6f, 0.5f}, {0.6f, 0.3f, 0.5f}, {0.3f, 0.6f, 0.5f}};
glBufferData(GL_ARRAY_BUFFER, Vertices.size() * sizeof(Vertices[0]), &Vertices[0], GL_STATIC_DRAW);
glVertexPointer(3, GL_FLOAT, 0, &Vertices[0]);
glDrawArrays(GL_POINTS, 0, Vertices.size());
glFlush();
glBegin(GL_POINTS);
glVertex3f(0.25f, 0.25f, 0.5f);
glVertex3f(0.25f, 0.75f, 0.5f);
glVertex3f(0.75f, 0.75f, 0.5f);
glVertex3f(0.75f, 0.25f, 0.5f);
glEnd();
glFlush();
}
void init (void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
glutCreateWindow("simple OpenGL example");
glutDisplayFunc(display);
GLenum err = glewInit();
if (GLEW_OK != err) {
cerr << "Error: " << glewGetErrorString(err) << endl;
}
cout << "Status: Using GLEW" << glewGetString(GLEW_VERSION) << endl;
init();
glutMainLoop();
}
You haven't enabled the attribute to actually use the array. You do that by:
(OpenGL 2) glEnableClientState(GL_VERTEX_ARRAY) (or GL_COLOR_ARRAY, etc)
(OpenGL 3) glEnableVertexArray(attributeId)
Without that, what you point to with glVertexPointer (GL2) or glVertexAttribPointer (GL3) won't be actually used.
I am trying to load and draw a 2d texture using OpenGL with GLFW and SOIL. I have this code, but I only get one solid color (which seems to come from the texture).
I have tested whether the .png loads with an example that came with SOIL, and it worked fine so there has to be some issue in my code.
This is my code:
#include <cstdio>
#include "GL/glfw.h"
#include "SOIL.h"
// function declarations
void drawscene();
void idlefunc();
void updatedisplay();
// global data
GLuint texture; // our example texture
int main(int argc, char **argv) {
if (!glfwInit()) {
fprintf(stderr, "Failed to initialize GLFW\n");
return 1;
}
if (!glfwOpenWindow(640, 480, 0, 0, 0, 0, 16, 0, GLFW_WINDOW)) {
fprintf(stderr, "Failed to open GLFW window\n");
return 1;
}
// enable vsync (if available)
glfwSwapInterval(1);
// load textures
texture = SOIL_load_OGL_texture(
"tex.png",
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_POWER_OF_TWO | SOIL_FLAG_MIPMAPS | SOIL_FLAG_DDS_LOAD_DIRECT
);
// check for an error during the texture loading
if (!texture) {
printf("SOIL loading error: '%s'\n", SOIL_last_result());
}
while (glfwGetWindowParam(GLFW_OPENED)) {
idlefunc();
}
// if we get here something went wrong
return 0;
}
// this function gets called every frame
void idlefunc() {
updatedisplay();
drawscene();
}
// set up te display
void updatedisplay() {
int screen_width, screen_height;
glfwGetWindowSize(&screen_width, &screen_height);
if (screen_height <= 0) screen_height = 1;
if (screen_width <= 0) screen_width = 1;
glViewport(0, 0, screen_width, screen_height);
glClearColor(0.02f, 0.02f, 0.02f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glDisable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, screen_width, screen_height, 0.0, 0.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// displacement trick for exact pixelization
glTranslatef(0.375f, 0.375f, 0.0f);
}
// draw the scene in this function
void drawscene() {
glBindTexture(GL_TEXTURE_2D, texture);
glPushMatrix();
glTranslatef(10.0f, 10.0f, 0);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex2f(0.0f, 0.0f);
glTexCoord2f(0.0f, 128.0f);
glVertex2f(0.0f, 128.0f);
glTexCoord2f(128.0f, 128.0f);
glVertex2f(128.0f, 128.0f);
glTexCoord2f(128.0f, 0.0f);
glVertex2f(128.0f, 0.0f);
glEnd();
glPopMatrix();
glfwSwapBuffers();
}
Found the issue (thanks to user786653). No matter the vertex coords, the tex coords are between 0.0 and 1.0. This is the fixed code:
// draw the scene in this function
void drawscene() {
glBindTexture(GL_TEXTURE_2D, texture);
glPushMatrix();
glTranslatef(10.0f, 10.0f, 0);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex2f(0.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex2f(0.0f, 128.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex2f(128.0f, 128.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex2f(128.0f, 0.0f);
glEnd();
glPopMatrix();
glfwSwapBuffers();
}
here is the output: http://i43.tinypic.com/9a5zyx.png
if things were working the way i wanted, the colors in the left square would match the colors in the right square. thanks for any help regarding the subject
#include <gl/glfw.h>
const char* title="test";
GLuint img;
unsigned int w=64,h=64;
int screenwidth,screenheight;
void enable2d()
{
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glViewport(0,0,screenwidth,screenheight);
glOrtho(0,screenwidth,screenheight,0,-1,1);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glPushAttrib(GL_DEPTH_BUFFER_BIT|GL_LIGHTING_BIT);
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
}
void drawmytex()
{
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,img);
glBegin(GL_QUADS);
glTexCoord2i(0,0);
glVertex2i(0,0);
glTexCoord2i(1,0);
glVertex2i(w,0);
glTexCoord2i(1,1);
glVertex2i(w,h);
glTexCoord2i(0,1);
glVertex2i(0,h);
glEnd();
glDisable(GL_TEXTURE_2D);
}
void drawquad(int x,int y)
{
glBegin(GL_QUADS);
glColor3f(0.0f,1.0f,0.0f);
glVertex2i(x,y);
glColor3f(1.0f,0.0f,1.0f);
glVertex2i(x+w,y);
glColor3f(0.0f,1.0f,1.0f);
glVertex2i(x+w,y+h);
glColor3f(0.0f,0.0f,1.0f);
glVertex2i(x,y+h);
glEnd();
}
void texcopy()
{
if (!glIsTexture(img))
glDeleteTextures(1,&img);
glGenTextures(1,&img);
glBindTexture(GL_TEXTURE_2D,img);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,w,h,0,GL_RGBA,GL_UNSIGNED_BYTE,0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,w,h,0,-1,1);
glViewport(0,0,w,h);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
drawquad(0,0);
glBindTexture(GL_TEXTURE_2D,img);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
//glCopyTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,0,0,w,h,0);
glCopyTexSubImage2D(GL_TEXTURE_2D,0,0,0,0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,screenwidth,screenheight,0,-1,1);
glViewport(0,0,screenwidth,screenheight);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main()
{
int running;
glfwInit();
running=glfwOpenWindow(640,480,0,0,0,0,0,0,GLFW_WINDOW);
if (!running)
{
glfwTerminate();
return 0;
}
glfwSetWindowTitle(title);
glfwEnable(GLFW_STICKY_KEYS);
glfwGetWindowSize(&screenwidth,&screenheight);
enable2d();
texcopy();
do
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
drawquad(64,0);
drawmytex();
glfwSwapBuffers();
running=!glfwGetKey(GLFW_KEY_ESC)&&glfwGetWindowParam(GLFW_OPENED);
GLenum error=glGetError();
if (error!=GL_NO_ERROR)running=error;
glfwSleep(.017);
}
while (running==1);
glDeleteTextures(1,&img);
glfwTerminate();
return running;
}
Try adding 'glColor3f(1,1,1);' in your 'drawmytex' function. I suspect that your texture is modulated (multiplied) with the current color, if so, the problem is not the texture copy but the way you display it.