gtk draw "expose-event" and redraw - gtk

I want to use expose-event to draw something then update or redraw. That's to say, there are a drawing area and a button in window. When clicking button, the drawing area will be redrawn accordingly. My problems are
In the following code, if I changed gtk_container_add (GTK_CONTAINER (box), canvas); to gtk_box_pack_start(GTK_BOX(box), canvas, FALSE, FALSE, 0);, nothing is drawn. Usually we use gtk_box_pack_start to add something into box. Why doesn't it work this time?
The function build_ACC_axis refreshed(deleted what has drawn) drawing area and prepared for new draw. But it didn't delete what has drawn. Why? How to refresh the drawing area?
If the source file is test.c, then compilation is
gcc -o test test.c `pkg-config --cflags --libs gtk+-2.0`
The code is below:
#include <gtk/gtk.h>
#include <glib.h>
static void draw (GdkDrawable *d, GdkGC *gc)
{
/* Draw with GDK */
gdk_draw_line (d, gc, 0, 0, 50, 50);
gdk_draw_line (d, gc, 50, 50, 50, 150);
gdk_draw_line (d, gc, 50, 150, 0, 200);
gdk_draw_line (d, gc, 200, 0, 150, 50);
gdk_draw_line (d, gc, 150, 50, 150, 150);
gdk_draw_line (d, gc, 150, 150, 200, 200);
gdk_draw_line (d, gc, 50, 50, 150, 50);
gdk_draw_line (d, gc, 50, 150, 150, 150);
}
static gboolean expose_cb (GtkWidget *canvas, GdkEventExpose *event, gpointer user_data)
{
GdkGC *gc;
gc = gdk_gc_new (canvas->window);
draw (canvas->window, gc);
g_object_unref (gc);
return FALSE;
}
void build_ACC_axis (GtkWidget *button, GtkWidget *widget)
{
GdkRegion *region;
GtkWidget *canvas = g_object_get_data(G_OBJECT(widget), "plat_GA_canvas");
region = gdk_drawable_get_visible_region(canvas->window);
gdk_window_invalidate_region(canvas->window, region, TRUE);
gtk_widget_queue_draw(canvas);
/* gdk_window_process_updates(canvas->window, TRUE); */
gdk_region_destroy (region);
}
int main (int argc, char **argv)
{
GtkWidget *window;
GtkWidget *canvas, *box, *button;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_widget_set_size_request(window, 500, 300);
g_signal_connect (G_OBJECT (window), "destroy", G_CALLBACK (gtk_main_quit), NULL);
box = gtk_vbox_new(FALSE, 0);
gtk_container_add (GTK_CONTAINER (window), box);
canvas = gtk_drawing_area_new ();
g_object_set_data(G_OBJECT(window), "plat_GA_canvas", canvas);
/* gtk_box_pack_start(GTK_BOX(box), canvas, FALSE, FALSE, 0); */
gtk_container_add (GTK_CONTAINER (box), canvas);
g_signal_connect (G_OBJECT (canvas), "expose-event", G_CALLBACK (expose_cb), NULL);
button = gtk_button_new_with_label ("ok");
gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 0);
/* gtk_container_add (GTK_CONTAINER (box), button); */
gtk_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(build_ACC_axis), window);
gtk_widget_show_all (window);
gtk_main ();
}
I would like to answer my second question. After clicking button, it would send expose-event signal and run expose_cb. Since there is no change in draw function, we can't see the refreshing. Following is the updated code that can show refreshing. The point is to add variable factor. But for the first question, I still didn't know why.
#include <gtk/gtk.h>
#include <glib.h>
int factor;
static void draw (GdkDrawable *d, GdkGC *gc, double fac)
{
/* Draw with GDK */
gdk_draw_line (d, gc, 0, 0, 50, 50 * fac);
gdk_draw_line (d, gc, 50, 50, 50, 150);
gdk_draw_line (d, gc, 50, 150, 0, 200);
gdk_draw_line (d, gc, 200, 0, 150, 50);
gdk_draw_line (d, gc, 150, 50, 150, 150);
gdk_draw_line (d, gc, 150, 150, 200, 200);
gdk_draw_line (d, gc, 50, 50, 150, 50);
gdk_draw_line (d, gc, 50, 150, 150, 150);
}
static gboolean expose_cb (GtkWidget *canvas, GdkEventExpose *event, gpointer user_data)
{
GdkGC *gc;
gc = gdk_gc_new (canvas->window);
draw (canvas->window, gc, factor);
g_object_unref (gc);
return FALSE;
}
void build_ACC_axis (GtkWidget *button, GtkWidget *widget)
{
GdkRegion *region;
GtkWidget *canvas = g_object_get_data(G_OBJECT(widget), "plat_GA_canvas");
region = gdk_drawable_get_visible_region(canvas->window);
gdk_window_invalidate_region(canvas->window, region, TRUE);
/* gtk_widget_queue_draw(canvas); */
gdk_window_process_updates(canvas->window, TRUE);
gdk_region_destroy (region);
factor++;
}
int main (int argc, char **argv)
{
GtkWidget *window;
GtkWidget *canvas, *box, *button;
gtk_init (&argc, &argv);
factor = 1;
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_widget_set_size_request(window, 500, 300);
g_signal_connect (G_OBJECT (window), "destroy", G_CALLBACK (gtk_main_quit), NULL);
box = gtk_vbox_new(FALSE, 0);
gtk_container_add (GTK_CONTAINER (window), box);
canvas = gtk_drawing_area_new ();
g_object_set_data(G_OBJECT(window), "plat_GA_canvas", canvas);
/* gtk_box_pack_start(GTK_BOX(box), canvas, FALSE, FALSE, 0); */
gtk_container_add (GTK_CONTAINER (box), canvas);
g_signal_connect (G_OBJECT (canvas), "expose-event", G_CALLBACK (expose_cb), NULL);
button = gtk_button_new_with_label ("ok");
gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 0);
/* gtk_container_add (GTK_CONTAINER (box), button); */
gtk_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(build_ACC_axis), window);
gtk_widget_show_all (window);
gtk_main ();
}

Related

GTK align is not working for the widget inside layout

I was trying to align the buttons okay and cancel buttons to the end of the GTK layout on a adjustable window, but the widget is not aligning to the layout corners. Instead, both are sticks into the mentioned layout positions by gtk_layout_put in the below code.
#include <gtk/gtk.h>
int main(int argc, char *argv[]) {
GtkWidget *window;
GtkWidget *button1;
GtkWidget *button2;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size(GTK_WINDOW(window), 600, 400);
gtk_container_set_border_width(GTK_CONTAINER(window), 15);
GtkWidget *layout = gtk_layout_new (NULL, NULL);
gtk_container_add (GTK_CONTAINER (window), layout);
gtk_layout_set_size(GTK_LAYOUT(layout), 400, 400);
gtk_widget_show (layout);
button1 = gtk_button_new_with_label("Okay");
gtk_layout_put (GTK_LAYOUT (layout), button1, 350, 250);
gtk_widget_set_halign (button1, GTK_ALIGN_END);
gtk_widget_set_valign (button1, GTK_ALIGN_END);
button2 = gtk_button_new_with_label("Cancel");
gtk_layout_put (GTK_LAYOUT (layout), button2, 450, 250);
gtk_widget_set_halign (button2, GTK_ALIGN_END);
gtk_widget_set_valign (button2, GTK_ALIGN_END);
g_signal_connect(G_OBJECT(window), "destroy",
G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all(window);
gtk_main();
return 0;
}

Cairo - overlapping lines - "deadzone" with no draw

I have multiple lines in Cairo. I want to have a "deadzone" around the line, so if I render multiple overlapping lines, they are not overlapping.
Something line on image below.
Here is a sample program that draws the later lines first in white with a larger line width and then again in black with the actual line width:
#include <cairo.h>
int main()
{
cairo_surface_t *s = cairo_image_surface_create(CAIRO_FORMAT_RGB24, 200, 200);
cairo_t *cr = cairo_create(s);
cairo_set_source_rgb(cr, 1, 1, 1);
cairo_paint(cr);
cairo_move_to(cr, 50, 20);
cairo_line_to(cr, 110, 160);
cairo_set_line_width(cr, 4);
cairo_set_source_rgb(cr, 0, 0, 0);
cairo_stroke(cr);
cairo_move_to(cr, 200, 0);
cairo_line_to(cr, 60, 160);
cairo_set_line_width(cr, 15);
cairo_set_source_rgb(cr, 1, 1, 1);
cairo_stroke_preserve(cr);
cairo_set_line_width(cr, 4);
cairo_set_source_rgb(cr, 0, 0, 0);
cairo_stroke(cr);
cairo_move_to(cr, 10, 100);
cairo_line_to(cr, 220, 50);
cairo_set_line_width(cr, 15);
cairo_set_source_rgb(cr, 1, 1, 1);
cairo_stroke_preserve(cr);
cairo_set_line_width(cr, 4);
cairo_set_source_rgb(cr, 0, 0, 0);
cairo_stroke(cr);
cairo_surface_write_to_png(s, "out.png");
cairo_destroy(cr);
cairo_surface_destroy(s);
}
Output:
Okay, now to the background. Option 1 is to draw the lines with a transparent background and then draw the background below that via operator DEST_OVER:
#include <cairo.h>
int main()
{
/* I changed this to ARGB32 */
cairo_surface_t *s = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 200, 200);
cairo_t *cr = cairo_create(s);
cairo_pattern_t *p = cairo_pattern_create_linear(0, 0, 200, 200);
/* Draw the lines */
cairo_move_to(cr, 50, 20);
cairo_line_to(cr, 110, 160);
cairo_set_line_width(cr, 4);
cairo_set_source_rgb(cr, 0, 0, 0);
cairo_stroke(cr);
cairo_move_to(cr, 200, 0);
cairo_line_to(cr, 60, 160);
cairo_set_line_width(cr, 15);
/* I changed all "drawing white" to "drawing transparency" */
cairo_save(cr);
cairo_set_source_rgba(cr, 0, 0, 0, 0);
cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
cairo_stroke_preserve(cr);
cairo_restore(cr);
cairo_set_line_width(cr, 4);
cairo_set_source_rgb(cr, 0, 0, 0);
cairo_stroke(cr);
cairo_move_to(cr, 10, 100);
cairo_line_to(cr, 220, 50);
cairo_set_line_width(cr, 15);
cairo_save(cr);
cairo_set_source_rgba(cr, 0, 0, 0, 0);
cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
cairo_stroke_preserve(cr);
cairo_restore(cr);
cairo_set_line_width(cr, 4);
cairo_set_source_rgb(cr, 0, 0, 0);
cairo_stroke(cr);
/* Draw a background below what we drew so far */
cairo_pattern_add_color_stop_rgb(p, 0, 1, 0, 0);
cairo_pattern_add_color_stop_rgb(p, 1, 0, 0, 1);
cairo_set_source(cr, p);
/* Actually, I mean DEST_OVER instead of ATOP (which I wrote in my comment) */
cairo_set_operator(cr, CAIRO_OPERATOR_DEST_OVER);
cairo_paint(cr);
cairo_surface_write_to_png(s, "out.png");
cairo_pattern_destroy(p);
cairo_destroy(cr);
cairo_surface_destroy(s);
}
Output:
Option 2 is to use a temporary surface. The lines are drawn with a transparent background to this. Afterwards, this is drawn ontop of the existing background:
#include <cairo.h>
int main()
{
cairo_surface_t *s = cairo_image_surface_create(CAIRO_FORMAT_RGB24, 200, 200);
cairo_t *cr = cairo_create(s);
cairo_pattern_t *p = cairo_pattern_create_linear(0, 0, 200, 200);
/* Draw a background */
cairo_pattern_add_color_stop_rgb(p, 0, 1, 0, 0);
cairo_pattern_add_color_stop_rgb(p, 1, 0, 0, 1);
cairo_set_source(cr, p);
cairo_paint(cr);
cairo_pattern_destroy(p);
/* Draw the lines */
/* but draw them to a temporary surface */
cairo_push_group_with_content(cr, CAIRO_CONTENT_COLOR_ALPHA);
cairo_move_to(cr, 50, 20);
cairo_line_to(cr, 110, 160);
cairo_set_line_width(cr, 4);
cairo_set_source_rgb(cr, 0, 0, 0);
cairo_stroke(cr);
cairo_move_to(cr, 200, 0);
cairo_line_to(cr, 60, 160);
cairo_set_line_width(cr, 15);
/* I changed all "drawing white" to "drawing transparency" */
cairo_save(cr);
cairo_set_source_rgba(cr, 0, 0, 0, 0);
cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
cairo_stroke_preserve(cr);
cairo_restore(cr);
cairo_set_line_width(cr, 4);
cairo_set_source_rgb(cr, 0, 0, 0);
cairo_stroke(cr);
cairo_move_to(cr, 10, 100);
cairo_line_to(cr, 220, 50);
cairo_set_line_width(cr, 15);
cairo_save(cr);
cairo_set_source_rgba(cr, 0, 0, 0, 0);
cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
cairo_stroke_preserve(cr);
cairo_restore(cr);
cairo_set_line_width(cr, 4);
cairo_set_source_rgb(cr, 0, 0, 0);
cairo_stroke(cr);
/* Now draw the temporary surface to the target surface */
cairo_pop_group_to_source(cr);
cairo_paint(cr);
cairo_surface_write_to_png(s, "out.png");
cairo_destroy(cr);
cairo_surface_destroy(s);
}
Output:

Problem with opengles to show an image

I use xcode Opengl App template to create a sample.
I am new to opengles, and having to try re-write the 'render' method in ES1Renderer.m
I try create a texture and show it on the screen, but nothing showed.
Someone can help me ? I have no idea how to fix it:
- (void)render
{
int imageW = 16;
int imageH = 16;
GLubyte *textureData = (GLubyte *) malloc(imageW * imageH << 2);
for (int i = 0; i < imageW * imageH << 2; i++) {
textureData[i]= 0xff & i;
}
GLuint textureId;
glGenTextures(1, &textureId);
glBindTexture(GL_TEXTURE_2D, textureId);
// when texture area is small, bilinear filter the closest mipmap
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR );
// when texture area is large, bilinear filter the original
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
// the texture wraps over at the edges (repeat)
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, imageW, imageH, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData);
GLenum err = glGetError();
if (err != GL_NO_ERROR)
NSLog(#"Error uploading texture. glError: 0x%04X", err);
free(textureData);
float x = 10.0f;
float y = 10.0f;
float z = 0.0f;
float scaleX = 1.0f;
float scaleY = 1.0f;
float scaleZ = 1.0f;
int w = imageW /2;
int h = imageH /2;
const GLfloat squareVertices[] = {
-w, -h,
w, -h,
-w, h,
w, h,
};
const GLfloat textureCoords[] = {
0, 0,
1, 0,
0, 1,
1, 1,
};
glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glBindTexture(GL_TEXTURE_2D, textureId);
glVertexPointer(2, GL_FLOAT, 0, squareVertices);
glTexCoordPointer(2, GL_FLOAT, 0, textureCoords);
glPushMatrix();
glTranslatef(x, y, z);
glScalef(scaleX, scaleY, scaleZ);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glPopMatrix();
glDisable(GL_TEXTURE_2D);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
NSLog(#"-->");
glDeleteTextures(1, &textureId);
// This application only creates a single color renderbuffer which is already bound at this point.
// This call is redundant, but needed if dealing with multiple renderbuffers.
glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER_OES];
}
Sadly the OpenGL template provided by Xcode has changed at some point — the current code (as of Xcode 3.2.5, creating an iOS Application with the 'OpenGL ES Application' template) no longer supplies a separate ES1Renderer.m and ES2Renderer.m, preferring to provide a single, simplified EAGLView.m and to perform runtime tests within GLTestViewController.m. With that in mind, I modified GLTestViewController.m's awakeFromNib no longer to attempt to get an ES 2 context:
- (void)awakeFromNib
{
EAGLContext *aContext = nil;//[[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
if (!aContext)
{
aContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];
}
if (!aContext)
NSLog(#"Failed to create ES context");
else if (![EAGLContext setCurrentContext:aContext])
NSLog(#"Failed to set ES context current");
self.context = aContext;
[aContext release];
[(EAGLView *)self.view setContext:context];
[(EAGLView *)self.view setFramebuffer];
if ([context API] == kEAGLRenderingAPIOpenGLES2)
[self loadShaders];
animating = FALSE;
animationFrameInterval = 1;
self.displayLink = nil;
}
And copied and pasted relevant portions of your code into drawFrame:
- (void)drawFrame
{
[(EAGLView *)self.view setFramebuffer];
// Replace the implementation of this method to do your own custom drawing.
static const GLfloat squareVertices[] = {
-0.5f, -0.33f,
0.5f, -0.33f,
-0.5f, 0.33f,
0.5f, 0.33f,
};
const GLfloat textureCoords[] = {
0, 0,
1, 0,
0, 1,
1, 1,
};
static float transY = 0.0f;
glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
int imageW = 16;
int imageH = 16;
GLubyte *textureData = (GLubyte *) malloc(imageW * imageH << 2);
for (int i = 0; i < imageW * imageH << 2; i++) {
textureData[i]= 0xff & i;
}
GLuint textureId;
glGenTextures(1, &textureId);
glBindTexture(GL_TEXTURE_2D, textureId);
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, imageW, imageH, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData);
free(textureData);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f, (GLfloat)(sinf(transY)/2.0f), 0.0f);
transY += 0.075f;
glEnable(GL_TEXTURE_2D);
glVertexPointer(2, GL_FLOAT, 0, squareVertices);
glEnableClientState(GL_VERTEX_ARRAY);
glTexCoordPointer(2, GL_FLOAT, 0, textureCoords);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glDeleteTextures(1, &textureId);
[(EAGLView *)self.view presentFramebuffer];
}
The result works entirely as you seem to intend. At a guess, is it possible either that:
you're setting something other than the identity as your projection matrix, causing your geometry to be clipped because it is placed at z = 0?
you've neglected properly to abandon an attempt at ES 2 rendering, causing unexpected results because tasks like textured rendering aren't hardwired in with ES 2 in the same way that they are with ES1?

How can I draw a series of lines using OpenGL ES on iPhone OS?

I have tried as follows (got through Google search)-
static const GLfloat vertices[] =
{
0.0, 0.0,
250.0, 280.0,
250.0, 280.0,
250.0, 500.0,
250.0, 500.0,
350.0, 500.0,
350.0, 500.0,
250.0, 280.0
};
static const GLubyte squareColors[] = {
255, 255, 255, 255,
255, 255, 255, 255
};
glDisable(GL_TEXTURE_2D);
BOOL colorArrayEnabled = glIsEnabled(GL_COLOR_ARRAY);
if (!colorArrayEnabled) {
glEnableClientState(GL_COLOR_ARRAY);
}
BOOL vertexArrayEnabled = glIsEnabled(GL_VERTEX_ARRAY);
if (!vertexArrayEnabled) {
glEnableClientState(GL_VERTEX_ARRAY);
}
glLineWidth(16.0f);
glColorPointer(4, GL_UNSIGNED_BYTE, 0, squareColors);
glVertexPointer(2, GL_FLOAT, sizeof(GLfloat)*2, vertices);
glDrawArrays(GL_LINES, 0, 2);
if (!vertexArrayEnabled) {
glDisableClientState(GL_VERTEX_ARRAY);
}
if (!colorArrayEnabled) {
glDisableClientState(GL_COLOR_ARRAY);
}
glEnable(GL_TEXTURE_2D);
FYI: I am a starter in iPhone programming.
Draw your lines in Blender and export Wavefront OBJ file. Use that file with OpenGLOBJLoader class. After that you can edit vertices in OBJ file if you want.

OpenGL ES; rendering texture created from CGBitmapContext

I am executing the following, which I have derived from a few different tutorials (Just a single render pass, initialisation code not shown but works fine for untextured primitives):
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrthof(0, xSize, 0, ySize, -1.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
glEnable(GL_TEXTURE_2D);
glBlendFunc(GL_ONE, GL_SRC_COLOR);
GLuint texture[1];
glGenTextures(1, &texture[0]);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
int width = 50;
int height = 50;
void* textureData = malloc(width * height * 4);
CGColorSpaceRef cSp = CGColorSpaceCreateDeviceRGB();
CGContextRef ct = CGBitmapContextCreate(textureData, width, height, 8, width*4, cSp, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGContextSetRGBFillColor(ct, 0, 1, 0, 1);
CGContextFillRect(ct, CGRectMake(0, 0, 50, 50));
CGContextRelease(ct);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData);
float verts[] = {
0.0f, 0.0f, 0.0f,
50.0f, 0.0f, 0.0f,
0.0f, 50.0f, 0.0f,
50.0f, 50.0f, 0.0f
};
float texCords[] = {
0.0f, 0.0f,
1.0f, 0.0f,
0.0f, 1.0f,
1.0f, 1.0f
};
glVertexPointer(3, GL_FLOAT, 0, verts);
glTexCoordPointer(2, GL_FLOAT, 0, texCords);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glDisable(GL_TEXTURE_2D);
The result is a white square. Not the green one as intended. Can anyone spot the error(s) in my code which result in its' failure to render?
I hope to get this working then move it on to text rendering.
The problem is that width and height are not powers of two. There are two solutions:
Use the texture rectangle extension. Set the texture target to GL_TEXTURE_RECTANGLE_ARB instead of GL_TEXTURE_2D. You will have to enable this extension before using it. Note that rectangle textures do not support mipmaps.
Use powers of two for texture dimensions.